import HTTP from "../../../../requestFn/Api";
const app = getApp();
Page({

  data: {
    myArticle_list: [],
    page: 1,
    page_size: 10,
    has_more: true,
  },
  onLoad(options) {
    const {
      garden_id
    } = app.globalData;
    this.setData({
      garden_id,
    })
  },
  onShow() {
    this.init();
  },
  init() {
    this.setData({
      page: 1,
      has_more: true
    });
    this.getArticle(true);
  },
  getArticle(flag = false) {
    const that = this;
    let {
      page,
      page_size,
      myArticle_list,
      garden_id
    } = that.data;
    HTTP.NeighborArticle({
      garden_id,
      page,
      page_size,
      is_me: true
    }).then(res => {
      that.setData({
        myArticle_list: flag ? res.list : [...myArticle_list, ...res.list],
        page: ++page,
        has_more: res.list.length === page_size
      })
    })
  },
  onReachBottom() {
    if (!this.data.has_more) {
      wx.showToast({
        title: '没有更多数据了',
        icon: 'none'
      })
      return console.log('没有更多数据了');
    }
    this.getArticle()
  },
  onPullDownRefresh: function () {
    app.onRefresh();
    this.init();
  },
  like(e) {
    const that = this;
    const {
      garden_id,
      myArticle_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) {
          myArticle_list.map(item => {
            if (item.id == like_id) {
              item.like = !item.like;
              if (item.like) {
                item.likes++
              } else {
                item.likes--
              }
            }
          })
          that.setData({
            myArticle_list
          })
        } else {
          myArticle_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({
            myArticle_list
          })
        }
      }
    })
  },
  previewImage(e) {
    const {
      id
    } = e.currentTarget;
    const files = this.data.myArticle_list.filter(item => item.id == id)
    this.setData({
      files: files[0].pics
    })
    wx.previewImage({
      current: this.data.files[e.currentTarget.dataset.index],
      urls: this.data.files,
    });
  },
  deleteArticle(e) {
    const that = this;
    const {
      id
    } = e.currentTarget;
    const {
      garden_id
    } = that.data;
    wx.showModal({
      content: "是否删除该文章",
      success: function (res) {
        if (res.confirm) {
          HTTP.DeleteArticle({
            method: 'delete',
            garden_id,
            id
          }).then(res => {
            if (res.code == 0) {
              wx.showToast({
                title: '删除成功',
                icon: "success",
                success() {
                  setTimeout(() => {
                    that.init();
                  }, 500)
                }
              })
            }
          })
        }
      }
    })
  },
})