import {
  getComment,
  postReply,
  ReplyComment,
  likeReply,
  getLikeNotes,
  getLikeNote
} from '~/api/video'
import reachBottom from '~/mixins/reachBottom'
let that = this
Component({
  behaviors: [reachBottom],
  properties: {
    // 是否在tabbar页面使用,是的话就给个padding
    tabBarPadding: {
      type: Boolean,
      value: false
    }
  },
  data: {
    show: false,
    quickShow: true,
    type: 'comment',
    commentId: '',
    totalSize: 0,
    firstData: {},
    list: [],
    detailDesc: '',
    postId: '',
    postIndex: '',
    ifGetFocus: false,
    replyType: 'works', // 回复类型,works是回复作品,comment是回复评论
    animation: {}
  },
  methods: {
    open(columnId, type = 'comment', onceId) {
      console.log(columnId, type, onceId);
      // 背景遮罩层
      var animation = wx.createAnimation({
        duration: 300,
        timingFunction: "linear",
        delay: 0
      })
      animation.translateY(1000).step()
      this.setData({
        animationData: animation.export(),
        columnId,
        type,
        show: true,
      })
      setTimeout(() => {
        animation.translateY(0).step()
        this.setData({
          animationData: animation.export()
        })
      }, 100)
      if (onceId) {
        this.topping(onceId)
      } else {
        this.resetData()
      }
    },
    changeType({
      currentTarget
    }) {
      let type = currentTarget.dataset.type
      this.setData({
        type
      })
      this.resetData()
      console.log(currentTarget.dataset);
    },
    close() {
      this.setData({
        show: false,
        quickShow: true,
        commentId: '',
        detailDesc: '',
        replyType: 'works',
        postId: null,
        postIndex: null,
        ifGetFocus: false,
      })
    },
    quickClose() {
      this.setData({
        quickShow: false
      })
    },
    loadMore() {
      console.log(this);
      if (this.data.type == 'like') {
        this.getData(this.getLikeNotes, {
          userReadId: this.data.columnId,
        })
      } else {
        this.getData(getComment, {
          columnId: this.data.columnId,
        })
      }
    },
    getLikeNotes(data) {
      console.log(that);
      return new Promise(async (reslove) => {
        let res = await getLikeNotes(data)
        if (this.data.firstData) {
          res.list = res.list.filter(item => {
            return item.id != this.data.firstData.id
          })
          res.list.unshift(this.data.firstData)
        }
        console.log(res);
        reslove(res)
      })
    },
    bindKeyInput(e) {
      this.setData({
        detailDesc: e.detail.value
      })
    },
    async topping(id) {
      let res = await getLikeNote(id)
      console.log(res);
      this.setData({
        firstData: res
      })
      this.loadMore()
    },
    async quickRemark({
      currentTarget
    }) {
      let data = {
        columnId: this.data.columnId,
        detailDesc: currentTarget.dataset.remark
      }
      await postReply(data)
      // 评论数+1
      this.triggerEvent('addCommentNum', this.data.columnId)
      this.resetData()
    },
    // 评论作品
    async sendReply() {
      if (!this.data.detailDesc.trim()) {
        return
      }
      if (this.data.replyType == 'works') {
        let data = {
          columnId: this.data.columnId,
          detailDesc: this.data.detailDesc,
        }
        await postReply(data)
        // 评论数+1
        this.triggerEvent('addCommentNum', this.data.columnId)
      } else {
        let data = {
          postsId: this.data.postId,
          content: this.data.detailDesc,
        }
        await ReplyComment(data)
      }
      this.setData({
        detailDesc: '',
        replyType: 'works'
      })
      this.resetData()
    },
    async ReplyComment({
      currentTarget
    }) {
      let postId = currentTarget.dataset.id
      let index = currentTarget.dataset.index
      this.setData({
        postId: postId,
        replyType: 'comment',
        ifGetFocus: true,
        postIndex: index
      })
    },
    cancelId() {
      this.setData({
        replyType: 'works',
        postId: null,
        postIndex: null,
        ifGetFocus: false,
      })
    },
    // 评论点赞
    async setLike({
      currentTarget
    }) {
      let postId = currentTarget.dataset.id
      let index = currentTarget.dataset.index
      let res = await likeReply(postId)
      const str = `list[${index}].likeCount`;
      const strImg = `list[${index}].isLike`;
      this.setData({
        [str]: res,
        [strImg]: true
      })
    },
    jumpUserInfo({
      currentTarget
    }) {
      wx.navigateTo({
        url: `/pages/personal/index?uid=${currentTarget.dataset.uid}&type=user`,
      })
    },
    onLongPress(e) {
      wx.showActionSheet({
        itemList: ['删除评论'],
        success(res) {
          console.log(res.tapIndex)
        },
      })
    },
  }
})