import httpRequestApi from '../../utils/APIClient';
import {
  formatDate
} from '../../utils/util';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    collection_data: [],
    line1: '您还没有收藏过哦',
    line2: '快去收藏自己喜欢的吧',
    videoList: [],
    commentShow: false,
    statusbarobj: {
      isshowbtn: false, //是否显示按钮
      title: "小学语文朗读配音", //标题
  },
  },

  toClass: function (e) {
    let targetCode = e.currentTarget.dataset.targetcode;
    let title = e.currentTarget.dataset.title;
    // wx.navigateTo({
    //   url: `../../main/class/class?id=${targetCode}&title=${title}`
    // });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (option) {
    console.log(option.title);
    if (option.title) {
      wx.setNavigationBarTitle({
        title: '我的收藏' //页面标题为路由参数
      });
      this.setData({
        title: option.title
      });
    }
    httpRequestApi.myFavorites().success(res => {
      this.formatWorksList(res.data.data.list)
    });
  },
  formatWorksList(list) {
    list.forEach(item => {
      const temp = {};
      temp.title = item.userRead.title;
      temp.summary = item.userRead.summary;
      temp.img = item.userRead.iconImg;
      temp.plays = item.userRead.playAmount ? item.userRead.playAmount : 0;
      temp.likes = item.userRead.likeAmount ? item.userRead.likeAmount : 0;
      temp.commentAmount = item.userRead.commentAmount ? item.userRead.commentAmount : 0;
      temp.classId = item.userRead.exampleId ? item.userRead.exampleId : 1605097720036046;
      temp.time = formatDate(item.userRead.gmtCreated, 3);
      temp.avatar = item.user.avatar;
      temp.profession = item.user.profession;
      temp.uid = item.user.uid;
      temp.url = item.userRead.videoPath ? item.userRead.videoPath : item.userRead.originVideo;
      temp.id = item.userRead.id;
      temp.type = item.userRead.type;
      temp.nickName = item.user.wechatName;
      temp.isLike = item.isLike;
      temp.isFans = true;
      temp.isFavorite = true;
      temp.coverImg = item.userRead.coverImg;
      temp.grade=item.userRead.grade;
      temp.videoShow = false;
      this.data.videoList.push(temp);
    });
    this.setData({
      videoList: this.data.videoList
    }, () => {
      console.log(this.data.videoList)
    })

  },
  goToReading: function (e) {
    console.log('去朗读', e)
    const id = e.detail.activeId ? e.detail.activeId : e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `../../pages/reading/reading?id=${id}`
    });
  },
  // 评论区点击
  commentTap: function (e) {
    console.log('点击评论区', e)
    if (e.target.dataset.type === 'blank') {
      this.setData({
        commentShow: false
      })
    }
  },
  // 打开评论
  openComment: function (e) {
    // 
    console.log('id', e.detail.activeId)
    this.setData({
      commentShow: !this.data.commentShow,
      commentId: e.detail.activeId,
      // commentList: []
    });
    // this.getReply(e.detail.activeId);
  },

  // 发布回复
  sendReply: function (e) {
    console.log('triger', e.detail.content);
    let data = {
      columnId: this.data.commentId,
      colunmNames: 'what',
      detailDesc: e.detail.content,
      // productId: this.data.productId
    }
    httpRequestApi.postReply(this.uid, data).success(res => {
      console.log(res)
      this.setData({
        pageNo: 1,
        commentList: []
      }, () => {
        this.getReply(this.data.commentId);
      })
    });
  },
  commentTouchMove: function (e) {
    return;
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },


  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  collectTap: function(e){
    console.log('点击收藏',e)
    const index = e.detail.index;
    let str = `videoList[${index}].isFavorite`
    this.setData({
     [str]: e.detail.isCollect
    })
  },
  likeTap:function(e){
    console.log('点赞',e)
    const index = e.detail.index;
    let likeStr = `videoList[${index}].isLike`;
    let likeNumStr = `videoList[${index}].likes`;
   this.setData({
     [likeStr]: true,
     [likeNumStr]: this.data.videoList[index].likes + 1
   })
  },
  openShare: function (e) {
    console.log('用户点击分享按钮', e)
    this.setData({
      shareTitle: e.detail.currentTarget.dataset.title,
      shareId: e.detail.currentTarget.dataset.id,
      shareImg: e.detail.currentTarget.dataset.img
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    console.log('用户点分享',res)
    if (res.from === 'button') {
      return {
        title: '请欣赏我的课文朗读作品,点赞+评论。',
        path: `/pages/index/index?readId=${this.data.shareId}`,
        imageUrl: '../../static/index/share_icon.png'

      }
    } else {
      return {
        title: '课文朗读,从未如此有趣。',
        path: '/pages/index/index',
      }
    }
  }
})