const app = getApp();
import HTTP from "../../../requestFn/Api"
Page({
  data: {
    garden_id: null,
    article_list: [],
    page: 1,
    page_size: 10,
    hasMore: true,
    comment_page: 1,
    class_list: [],
    comment_list: [],
    class: {},
    files: [],
    inputValue: '',
    tabbar: {},
    article_height: 50,
    oldHeight: 50
  },
  onLoad() {
    const {unreadCountNeig} = app.globalData;
    this.setData({unreadCountNeig});
    getApp().watch('unreadCountNeig',this.watchBack);
    app.editTabbar();
  },
  watchBack(name, value) {
    let data = {};
    data[name] = value;
    this.setData(data);
  },
  onShow() {
    const {
      garden_id
    } = app.globalData;
    if (garden_id) {
      this.setData({
        garden_id
      })
    }
    this.init();
  },
  init() {
    this.setData({
      page: 1,
      hasMore: true
    })
    this.getArticle(true);
    this.getNeighbor_list();
  },
  getArticle(flag = false) {
    const that = this;
    let {
      page,
      page_size,
      article_list,
      garden_id
    } = that.data;
    HTTP.NeighborArticle({
      garden_id,
      page,
      page_size,
      is_me: false
    }).then(res => {
      that.setData({
        article_list: flag ? res.list : [...article_list, ...res.list],
        page: ++page,
        hasMore: res.list.length === page_size
      })
    })
  },
  onReachBottom() {
    if (!this.data.hasMore) {
      wx.showToast({
        title: '没有更多数据了',
        icon: 'none'
      })
      return console.log('没有更多数据了');
    }
    this.getArticle()
  },
  deleteArticle(e) {
    const that = this;
    const {
      id
    } = e.currentTarget.dataset;
    const {
      garden_id
    } = that.data;
    console.log(id);
    wx.showModal({
      title: "提示",
      content: "是否删除该文章",
      success: function (res) {
        if (res.confirm) {
          HTTP.DeleteArticle({
            method: 'delete',
            garden_id,
            id
          }).then(res => {
            if (res.code == 0) {
              setTimeout(() => {
                wx.showToast({
                  title: '删除成功',
                  icon: "success"
                })
              }, 500)
              that.init()
            }
          })
        }
      }
    })
  },
  like(e) {
    const that = this;
    const {
      garden_id,
      article_list
    } = that.data;
    const {
      like,
      like_id,
      like_type,
      article_id
    } = e.currentTarget.dataset;
    HTTP.Like({
      like: !like,
      like_id,
      like_type: like_type - 0,
      garden_id
    }).then(res => {
      if (res.code == 0) {
        if (like_type == 1) {
          article_list.map(item => {
            if (item.id == like_id) {
              item.like = !item.like;
              if (item.like) {
                item.likes++
              } else {
                item.likes--
              }
            }
          })
          that.setData({
            article_list
          })
        } else {
          article_list.map(item => {
            if (item.id == article_id) {
              item.comment_list.map(key => {
                if (key.id == like_id) {
                  key.like = !key.like
                  if (key.like) {
                    key.likes++
                  } else {
                    key.likes--
                  }
                }
              })
            }
          })
          that.setData({
            article_list
          })
          // that.get_Neighbor_comment(article_id, like_id)
        }
      }
    })
  },
  getNeighbor_list() {
    const {
      garden_id
    } = this.data;
    HTTP.Neighbor_class({
      garden_id
    }).then(res => {
      this.setData({
        class_list: res.list
      })
    })
  },
  getNeighbor_cpmment(e) {
    const that = this;
    const {
      article_id
    } = e.currentTarget.dataset
    that.get_Neighbor_comment(article_id)
  },
  get_Neighbor_comment(article_id, pid = '', flag = false) {
    console.log(article_id, pid, flag);
    const that = this;
    const {
      garden_id,
      comment_page
    } = this.data;
    HTTP.NeighborComment({
      comment_page,
      page_size: 10,
      garden_id,
      article_id,
      pid
    }).then(res => {
      const commentTag = [];
      const arr = that.data.article_list;
      arr.map(item => {
        if (item.id == article_id) {
          if (item.comment_list && flag) {
            item.comment_list = that.removeDuplicates(item.comment_list.concat(res.list));
            for (let i = 0; i < item.comment_list.length; i++) {
              commentTag.push(false)
            }
            item.commentTag = commentTag
          } else {
            item.comment_list = res.list
            for (let i = 0; i < res.list.length; i++) {
              commentTag.push(false)
            }
            item.commentTag = commentTag
          }
        }
      })
      console.log(arr);
      that.setData({
        article_list: arr
      })
      console.log(that.data.article_list);
    })
  },
  previewImage(e) {
    const {
      id
    } = e.currentTarget;
    const files = this.data.article_list.filter(item => item.id == id)
    console.log(files);
    this.setData({
      files: files[0].pics
    })
    wx.previewImage({
      current: this.data.files[e.currentTarget.dataset.index],
      urls: this.data.files,
    });
  },
  addComment(e) {
    const that = this;
    const {
      garden_id,
      inputValue
    } = that.data;
    const {
      pid,
      article_id
    } = e.currentTarget.dataset;
    HTTP.NeighborComment({
      method: 'post',
      content: inputValue,
      pid,
      garden_id,
      pics: []
    }).then(res => {
      if (res.code == 0) {
        wx.showToast({
          title: '评论成功',
          icon: "success",
          success() {
            that.get_Neighbor_comment(article_id)
          }
        })
      }
    })
  },
  showComment(e) {
    const {
      index,
      id,
      article_id
    } = e.currentTarget.dataset;
    const {
      article_list
    } = this.data;
    article_list.map(item => {
      if (item.id == article_id) {
        item.commentTag[index] = true;
      }
    })
    this.setData({
      article_list
    })
  },
  hiddenComment(e) {
    const {
      index,
      article_id
    } = e.currentTarget.dataset;
    const {
      article_list
    } = this.data;
    console.log(article_list);
    article_list.map(item => {
      if (item.id == article_id) {
        item.commentTag[index] = false
      }
    })
    this.setData({
      article_list
    })
  },
  deleteComment(e) {
    console.log(e);
    const that = this;
    wx.showModal({
      title: "提示",
      content: "是否删除该条评论",
      success: function (res) {
        if (res.confirm) {
          const {
            garden_id
          } = that.data;
          const {
            id,
            article_id
          } = e.currentTarget.dataset
          HTTP.DeleteComment({
            garden_id,
            id
          }).then(res => {
            if (res.code == 0) {
              that.get_Neighbor_comment(article_id)
            }
          })
        }
      }
    })
  },
  goAddArticle() {
    wx.navigateTo({
      url: '/page/neighbor/pages/addArticle/addArticle',
    })
  },
  getComment_son(e) {
    const {
      article_id,
      pid
    } = e.currentTarget.dataset
    this.get_Neighbor_comment(article_id, pid, true)
  },
  removeDuplicates(arr) {
    let newObj = {};
    arr = arr.reduce((preVal, curVal) => {
      newObj[curVal.id] ? '' : newObj[curVal.id] = preVal.push(curVal);
      return preVal
    }, [])
    return arr
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    app.onRefresh();
    this.init();
  },
  getCommentValue(e) {
    this.setData({
      inputValue: e.detail.value
    })
  },
  goDetail(e) {
    const {
      item
    } = e.currentTarget.dataset;
    let data = item;
    console.log(data);
    wx.navigateTo({
      url: `../../neighbor/pages/Detail/Detail`,
      success: function (res) {
        res.eventChannel.emit('acceptDataFromOpenerPage', data)
      }
    })
  }
})