const app = getApp()
const util = require("../../../utils/util");
const api = require('../../../api/api.js');
const user = require('../../../utils/user.js');
import moment from 'moment'
var WxParse = require('../../../lib/wxParse/wxParse.js');



Page({
  data: {
    isFinished:false,
    id:'',
    courseid:'',
    courseHtml:'',
    courseInfo:{
      arrangement:{

      },
      course:{
        contentType:''
      },
      courseSkill:'',
      courseVideoList:[

      ],
      enrolment:{},
      relationCourse:[],

    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    if(options){
      this.setData({
        id:options.id,
        courseid:options.courseid
      });
    }
    this.getCourseDetail();
  },


 
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
 
 
  },
  videoBegin(e){
    console.log(e);
    // wx.showModal({
    //   title: 'start',
    //   content: JSON.stringify(e),
    //   showCancel: false,
    //   confirmText: '确定',
    //   success: function (res) {
       
    //   }
    // })
  },
  videoPlay(e){
   console.log(e);   
  },
  videoEnd(e){
    console.log(e);
    let videoid=e.currentTarget.dataset.videoid;
    // wx.showModal({
    //   title: 'end',
    //   content: JSON.stringify(e),
    //   showCancel: false,
    //   confirmText: '确定',
    //   success: function (res) {
       
    //   }
    // });
    let userInfo = wx.getStorageSync('userInfo');
    util.request(api.addStudyLog, {
      courseEmentId:this.data.id,
      courseId:this.data.courseid,
      videoOssId:videoid,
      workerNo: userInfo.workerNo,
      workerName:userInfo.workerName
    }, 'POST').then(res=> {
      if (res.errno === 0) {
         wx.showToast({
           title: '该视频已完成',
           icon:'none'
         });
         this.getCourseDetail();
      }
    });
    
  },
  getCourseDetail(){
    util.request(api.getCourseInfo, {
      enrolmentId:this.data.id,
      courseId:this.data.courseid
    }, 'GET').then(res=> {
      if (res.errno === 0) {
        this.setData({
          courseInfo:res.data,
          isFinished:res.data.enrolment.isCompleted==1?true:false
        });
        if(this.data.courseInfo.course.contentType==1){
          let detail=this.data.courseInfo.course.courseContent;
          WxParse.wxParse('courseHtml', 'html', detail, this);
        }
        let list=this.data.courseInfo.courseVideoList;
        if(list.length>0){
          list.forEach((item,index)=>{
            if(index==0){
              item.isTiled=true;
            }else{
              item.isTiled=false;
            }
          });
          this.setData({
            'courseInfo.courseVideoList':list
          });
        }
      }

    
    });
  },
  goCourseDetail(e){
   
    let courseid=e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/upgrade/courseDetail/courseDetail?id='+''+'&courseid='+courseid,
    });
  },
  toggleVideo(e){
    let index=e.currentTarget.dataset.index;
    let istiled=e.currentTarget.dataset.istiled;
    if(!istiled){
      let list=this.data.courseInfo.courseVideoList;
      list.forEach((item,index)=>{
       
          item.isTiled=false;
      });
      this.setData({
        'courseInfo.courseVideoList':list
      });
      let itemData = "courseInfo.courseVideoList[" + index + "].isTiled";
      this.setData({
        [itemData]:true
      });
    }
   
    
  },
 switchTab(e){
   let tab=e.currentTarget.dataset.tab;
   if(this.data.currentTab!=tab){
     this.setData({
      currentTab:tab
     });
   }
 },
 goHistory(){
  wx.pageScrollTo({
    selector: '.tab-sticky',
    duration: 300
  })
  this.setData({
    currentTab:'history'
   });
  
 },
 goLearn(){
   wx.navigateTo({
     url: '/pages/upgrade/courseDetail/courseDetail',
   })
 },
 submit(){
  util.request(api.complateCourse, {
    enrolmentId:this.data.id,
  }, 'GET').then(res=> {
    if (res.errno === 0) {
      this.setData({
        isFinished:true,
      })
    }else{
      wx.showToast({
        title: res.errmsg,
        icon:'none'
      })
    }
  });
  
 },
  onHide: function () {

  },

  onUnload: function () {

  },
  

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
   
  },
  
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  
})