import { updateProgress , getVideoProgress } from '../../../api/learining.js'

Page({
  data:{
    videoInfo:{},
    videoProgress: 0,
    openid: null,
    initialTime: 0,
    control:false,
    statusList:{
      0: '待学习',
    //   1: '学习中',
      2: '已学习',
    },
    classList:{
      0: 'no--study',
    //   1: 'is--study',
      2: 'is--finish',
    },
    detail: {}
  },
  onLoad(options){
    let data = JSON.parse(decodeURIComponent(options.data))
    data.status = data.status === 0 ? 0 : data.status
    const openid = wx.getStorageSync('openid')
    this.setData({
        detail:data,
        openid,
        control : data.status === 2 ? true : false,
    })
  },

  saveTime(e){
    let time = e.detail.currentTime
    this.setData({
        videoProgress:time
    })
  },
  getProgress(){
    const data = {
        videoId: this.data.detail.id,
        openid: this.data.openid,
      }
      getVideoProgress(data).then(res=>{
         this.setData({
            initialTime:res.data.videoProgress,
         })
      })
  },
  
  handleEnded(){
    if(this.data.control){
        return
    }
    const isNumber =  typeof(status) === 'number'
    const params = {
      status: 2,
      videoId: this.data.detail.id,
      openid: this.data.openid,
      videoProgress: isNumber ?  this.data.videoProgress : 0
    }
    updateProgress(params)
    .then(res=>{
        let initialTime = null
        if(res.code == 0){ 
            initialTime = res.data==null?0:res.data
        }
      this.setData({
        'detail.status': params.status,
        control : true
      })
    })
  }
})