import HTTP from "../../../../requestFn/Api"
const app = getApp();
let time = null //用来存放手指触摸时间
let touchDot = null //用来存放手指触摸时的原点
Page({

  data: {
    detail: {},
    garden_id: app.globalData.garden_id,
    page: 1,
    comment_page: 1,
    page_size: 10,
    has_more: true,
    comment_has_more: true,
    article_comment_list: [],
    flag: true,
    inputValue: '',
    article_height: 75,
    bottom: 0,
    dialog4: false,
    comment_Arr: [],
    pid: '',
    commentPid: [],
  },

  onLoad(options) {
    const that = this;
    const safebottom = app.globalData.systemInfo.screenHeight - app.globalData.systemInfo.safeArea.bottom;
    console.log(app.globalData.systemInfo);
    const windowHeight = app.globalData.systemInfo.windowHeight
    that.setData({
      bottom: safebottom,
      safebottom,
      windowHeight
    })
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.on('acceptDataFromOpenerPage', function (data) {
      that.setData({
        detail: data,
        article_id: data.id,
        comment_count: data.comment_count
      })
      that.init();
    })
  },
  init() {
    this.setData({
      page: 1,
      has_more: true
    });
    this.getComment(false);
  },
  commentInit(pid) {
    this.setData({
      comment_page: 1,
      comment_has_more: true
    });
    this.getComment(false, pid);
  },
  onReachBottom() {
    if (!this.data.has_more) {
      wx.showToast({
        title: '没有更多数据了',
        icon: 'none'
      })
      return console.log('没有更多数据了');
    }
    this.getComment(true)
  },
  async getComment(flag, pid = '') {
    let {
      garden_id,
      page,
      page_size,
      article_comment_list,
      article_id,
      comment_Arr,
      comment_page,
    } = this.data;
    let {
      list,
      total
    } = await HTTP.NeighborComment(({
      garden_id,
      article_id,
      page: pid ? comment_page : page,
      page_size,
      pid: pid ? pid : ""
    }))
    if (pid) {
      this.setData({
        comment_Arr: flag ? [...comment_Arr, ...list] : list,
        comment_page: ++comment_page,
        comment_has_more: page_size === list.length,
        count: total
      })
    } else {
      this.setData({
        article_comment_list: flag ? [...article_comment_list, ...list] : list,
        page: ++page,
        has_more: page_size === list.length,
      })
    }
  },
  previewImage(e) {
    const {
      id
    } = e.currentTarget;
    const files = this.data.article_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,
    });
  },
  getValue(e) {
    const {
      flag,
      bottom,
      safebottom
    } = this.data;
    if (bottom !== safebottom) {
      this.setData({
        flag: !flag,
        bottom: safebottom
      })
    } else {
      this.setData({
        bottom: safebottom
      })
    }
  },
  bindlinechange(e) {
    const {
      article_height
    } = this.data;
    const {
      lineCount,
    } = e.detail;
    if (lineCount > 3) {
      this.setData({
        article_height: lineCount * 25
      })
    }
  },
  bindkeyboardheightchange(e) {
    const {
      height
    } = e.detail;
    this.setData({
      bottom: height + safebottom
    })
  },
  bindfocus(e) {
    this.setData({
      flag: false
    })
  },
  getArticleValue(e) {
    this.setData({
      inputValue: e.detail.value
    })
  },
  addArticle_comment(e) {
    const that = this;
    const {
      garden_id,
      inputValue,
      article_id,
      pid,
      safebottom
    } = that.data;
    HTTP.NeighborComment({
      method: 'post',
      content: inputValue,
      article_id: pid ? 0 : article_id,
      garden_id,
      pics: [],
      pid: pid ? pid : 0
    }).then(res => {
      if (res.code == 0) {
        wx.showToast({
          title: '评论成功',
          icon: "success",
          success() {
            if (pid) {
              that.commentInit(pid);
              that.setData({
                count: that.data.count + 1
              })
            }
            that.init();
            that.setData({
              inputValue: "",
              flag: true,
              bottom: safebottom,
            })
          }
        })
      }
    })
  },
  cancleReply() {
    this.setData({
      flag: true,
    })
  },
  deleteComment(e) {
    const that = this;
    wx.showModal({
      content: "是否删除该条评论",
      success: function (res) {
        if (res.confirm) {
          const {
            garden_id,
            pid,
            commentPid
          } = that.data;
          const {
            id,
          } = e.currentTarget.dataset
          HTTP.DeleteComment({
            garden_id,
            id
          }).then(res => {
            if (res.code == 0) {
              if (pid) {
                that.commentInit(commentPid[commentPid.length - 1])
              } else {
                that.init();
              }
            }
          })
        }
      }
    })
  },
  replyComment(e) {
    const {
      item,
      index
    } = e.currentTarget.dataset;
    const that = this;
    const {
      commentPid,
      safebottom,
      windowHeight
    } = this.data;
    this.setData({
      comment_list: item,
      index,
      dialog4: true,
      flag: true,
      bottom: safebottom,
      pid: item.id,
      comment_page: 1,
      commentPid: [...commentPid, item.id]
    });
    const query = wx.createSelectorQuery()
    query.select('.reply_aricle_title').boundingClientRect()
    query.select('.replyArticle').boundingClientRect()
    query.selectViewport().scrollOffset()
    query.exec(function (res) {
      let reduceHeight = res[0].height;
      let inputHeight = res[1].height;
      let scrollViewHeight = windowHeight - reduceHeight - 16 - 64 - 5 -inputHeight;
      that.setData({scrollViewHeight})
    })
    this.getComment(false, item.id)
  },
  close() {
    this.setData({
      dialog4: false,
      comment_has_more: true,
      comment_page: 1,
      pid: '',
      commentPid: []
    });
  },
  like(e) {
    const that = this;
    const {
      garden_id,
    } = that.data;
    const {
      like,
      like_id,
      like_type,
      name
    } = e.currentTarget.dataset;
    HTTP.Like({
      like: !like,
      like_id,
      like_type: like_type - 0,
      garden_id,
    }).then(res => {
      if (res.code == 0) {
        if (name == "commentArr") {
          const {
            comment_Arr
          } = that.data;
          that.replaceIcon('comment_Arr', comment_Arr, like_id)
        } else {
          const {
            article_comment_list
          } = that.data;
          that.replaceIcon('article_comment_list', article_comment_list, like_id)
        }
      }
    })
  },
  replaceIcon(way, name, like_id) {
    name.map(item => {
      if (item.id == like_id) {
        item.like = !item.like;
        if (item.like) {
          item.likes++
        } else {
          item.likes--
        }
      }
    })
    this.setData({
      [way]: name
    })
  },
  back() {
    const {
      commentPid
    } = this.data;
    commentPid.pop();
    this.setData({
      commentPid,
      comment_page: 1
    })
    this.getComment(false, commentPid[commentPid.length - 1])
  },
  lower(e) {
    if (!this.data.comment_has_more) {
      wx.showToast({
        title: '没有更多数据了',
        icon: 'none'
      })
      return console.log('没有更多数据了');
    }
    const {
      commentPid
    } = this.data;
    this.getComment(true, commentPid[commentPid.length - 1])
  },

})