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,
    goBackHome: false
  },
  onLoad: function (option) {
    console.log(option);
    if(option.scene){
      this.setData({
          goBackHome: true
      })
  }
    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
      },()=>{
        wx.setNavigationBarTitle({
          title: res.data.data.title //页面标题为路由参数
        })
      })
      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 classId = this.data.id;
    httpRequestApi.checkLesson(classId).success(res=>{
      const productId = res.data.data[0];
      httpRequestApi.areYouSuper(res.data.data).success(res => {
        if(res.data.success){
          wx.navigateTo({
            url: `../../main/reading/reading?id=${classId}`
          })
        } else {
          wx.showModal({
            title: '您未购买过本书,不能朗读',
            content: '超值团购进行中,快去看看',
            success (res) {
              if (res.confirm) {
                console.log('用户点击确定')
                wx.navigateTo({
                  url: `../../groupPage/grade-details/grade-details?productId=${productId}`
                })
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
        }
      })
    });
    
  },
  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) {
    console.log(this.data.works)
    // this.videoCtx = wx.createVideoContext('myVideo', this);
    if(this.videoCtx){
      this.videoCtx.stop(); 
    }
    console.log(e);
    let readId = e.target.dataset.id;
    let title = this.data.title;
    wx.navigateTo({
      url: `../../social/works/works?id=${readId}&title=${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: 10
    };
    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();

    }
  }
})