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

    httpRequestApi.getClassDetail(uid, option.id).success(res => {
      console.log(res);
      this.setData({
        title: res.data.data.title,
        videoUrl: res.data.data.playUrl
      })
      this.getReadInfo(1,10);
    })
  },

  goToReading: function () {
    let id = this.data.id;
    let title = this.data.title;
    wx.navigateTo({
      url: `../../main/reading/reading?id=${id}&title=${this.data.title}&img=${this.data.img}&video=${this.data.videoUrl}`
    })
  },
  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 = {
      
    }
    httpRequestApi.collectBook(uid, data).success(res => {
      console.log(res);
    })
  },
  getReadInfo: function(pageNo,pageSize){
    const uid =  wx.getStorageSync('uid');
    const data = {
      lessonId: 1 ,
      pageNo: pageNo,
      pageSize: pageSize
    };
    httpRequestApi.getClassRead(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.gmtCreated, 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;
        readTemp.push(temp);
      });
      this.setData({
        total: res.data.data.totalSize,
        works: readTemp
      })
    });
  },
  // 下拉加载
  scrollUpdate:function(e){
    console.log(e)
  }
})