import {
  getComment,
  postReply,
  ReplyComment,
  likeReply
} from '~/api/video'
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    // 是否在tabbar页面使用,是的话就给个padding
    tabBarPadding: {
      type: Boolean,
      value: false
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    show: false,
    commentId: '',
    totalSize: 0,
    list: [],
    detailDesc: '',
    postId: '',
    postIndex: '',
    ifGetFocus: false,
    replyType: 'works', // 回复类型,works是回复作品,comment是回复评论
    animation: {}
  },
  methods: {
    open(columnId) {
      // 背景遮罩层
      var animation = wx.createAnimation({
        duration: 300,
        timingFunction: "linear",
        delay: 0
      })
      animation.translateY(1000).step()
      this.setData({
        animationData: animation.export(),
        columnId,
        show: true,
      })
      setTimeout(() => {
        animation.translateY(0).step()
        this.setData({
          animationData: animation.export()
        })
      }, 100)
      this.getComment()
    },
    close() {
      this.setData({
        show: false,
        commentId: '',
        totalSize: 0,
        list: [],
        detailDesc: '',
        replyType: 'works',
        postId: null,
        postIndex: null,
        ifGetFocus: false,
      })
    },
    async getComment() {
      let params = {
        columnId: this.data.columnId,
        pageNo: 1,
        pageSize: 100
      }
      let {
        totalSize,
        list
      } = await getComment(params)
      this.setData({
        totalSize,
        list
      })
    },
    bindKeyInput(e) {
      this.setData({
        detailDesc: e.detail.value
      })
    },
    // 评论作品
    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.getComment()
    },
    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
      })
    }
  }
})