import httpRequestApi from '../../../utils/APIClient';
import {
  formatDate
} from '../../../utils/util';
Page({
  data: {
    fullScreenBtn: false,
    playBtn: false,
    gesture: true,
    videoUrl: '',
    poster: '',
    works: [],
    total: '',
    title: '',
    id: '',
    iconImg: '',
    pageNo: 1,
    totalNo: 0,
    collectFlag: false,
  },
  onLoad: function (option) {
    console.log(option);
    wx.setNavigationBarTitle({
      title: option.title //页面标题为路由参数
    })
    this.setData({
      title: option.title,
      id: option.id
    })

    this.uid = wx.getStorageSync('uid');;

    httpRequestApi.getClassDetail(this.uid, option.id).success(res => {
      console.log(res);
      this.setData({
        title: res.data.data.title,
        videoUrl: res.data.data.playUrl,
        iconImg: res.data.data.iconImg
      })
      this.getReadInfo(1, 10);
      this.checkLike();
    })
  },
  // 检查是否收藏
  checkLike: function () {
    httpRequestApi.classIsLike(this.uid, {
      targetCode: this.data.id
    }).success(res => {
      this.setData({
        collectFlag: res.data.data
      })
    })
  },
  goToReading: function () {
    let id = this.data.id;
    let title = this.data.title;
    wx.navigateTo({
      url: `../../main/reading/reading?id=${id}`
    })
  },
  openShare: function (e) {
    // this.setData({
    //     shareFlag: !this.data.shareFlag
    // })
    this.shareDialog = this.selectComponent("#share-dialog");
    const data = {
      avatar: this.data.authorAvatar,
      author: this.data.author,
      iconImg: this.data.iconImg,
      title: this.data.title,
      path: `pages/social/works/works?id=${this.data.id}&title=${this.data.title}`
      // tip: this.data.tip,
    }
    this.shareDialog.share(data);
  },
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
        // 来自页面内转发按钮
        console.log(res.target)
    }
    return {
        title: this.data.title,
        path: `pages/main/class/class?id=${this.data.id}&title=${this.data.title}`,
        imageUrl: this.data.iconImg
    }
},
  goToWorks: function (e) {
    this.videoCtx = wx.createVideoContext('myVideo', this);
    this.videoCtx.stop();
    console.log(e);
    let readId = e.target.dataset.uid;
    let title = this.data.title;
    wx.navigateTo({
      url: `../../social/works/works?id=${readId}&tilte=${title}`
    })
  },
  collect: function (e) {
    // let uid =  wx.getStorageSync('uid');
    let data = {
      targetCode: this.data.id,
      title: this.data.title,
      iconImg: this.data.iconImg
    }
    httpRequestApi.collectClass(this.uid, data).success(res => {
      this.setData({
        collectFlag: !this.data.collectFlag
      })
    })
  },
  getReadInfo: function (pageNo, pageSize) {
    // const uid =  wx.getStorageSync('uid');
    const data = {
      lessonId: this.data.id,
      pageNo: this.data.pageNo,
      pageSize: 1
    };
    httpRequestApi.getClassRead(this.uid, data).success(res => {
      const readInfo = res.data.data.list;
      const readTemp = [];
      readInfo.forEach(item => {
        const temp = {};
        temp.nickName = item.user.wechatName;
        temp.time = formatDate(item.userRead.gmtModified, 3);
        temp.avatar = item.user.avatar;
        temp.likes = item.userRead.likeAmount ? item.userRead.likeAmount : 0;
        temp.uid = item.userRead.uid;
        temp.id = item.userRead.id;
        this.data.works.push(temp);
      });
      this.setData({
        totalNo: res.data.data.totalNo,
        total: res.data.data.totalSize,
        works: this.data.works
      })
    });
  },
  // 下拉加载
  onReachBottom: function (e) {
    console.log(this.data.totalNo);
    this.setData({
      pageNo: this.data.pageNo + 1
    })
    if (this.data.pageNo <= this.data.totalNo) {
      this.getReadInfo();

    }
  }
})