//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    spinShow: false,
    buyModel:false,
    code_url:'',
    system: "",
    userInfo:{},
    info:{},
    onPlay: false,
    item:{},
    courseId:0,
    mediaList: []
  },
  onLoad: function( options ){
    let courseId = +options.courseId||35;
    this.setData({courseId})
    app.getSystemInfo( system=>{
      this.setData({system})
    })
    // 
    app.getUserInfo( userInfo =>{
      this.setData({userInfo})
      this.loadData( courseId )
    })
  },
  loadData: function( courseId ) {
    let param ={ courseId}
    app.formPost('Study.GetWxCourseInfo', param).then(res => {
      if( res.code == 200){
        let {info, list} = res.data
        wx.setNavigationBarTitle({
          title: info.name
        })
        this.setData({info, mediaList: list})
      }
    })
  },
  onReady(res) {
    this.videoContext = wx.createVideoContext('myVideo')
  },
  gotoExam(){
    let courseId = this.data.courseId
    wx.navigateTo({
      url: `/pages/study/exam/index?courseId=${courseId}`,
    })
  },
  // 拖动快进
  timeUpdate( e ){
    let that = this;
    let item = this.data.item
    var position = item.position
    var currentTime = parseInt(e.detail.currentTime)
    if( currentTime - position < 1 || item.isFinish ){
      return
    }
    // 
    if( currentTime - position > 5 ){
      let videoContext = wx.createVideoContext('myVideo');
      videoContext.seek( position )
      wx.showToast({
        title: '未完整看完该视频，不能快进',
        icon: 'none',
        duration: 2000,
      })
      return;
    }else{
      item.position= currentTime
    }
    if( item.position - item.prev_tick > 4 ){
      item.prev_tick = item.position;
      // 前一秒完成
      if( item.prev_tick >= e.detail.duration ){
        item.isFinish = 1
      }
      this.doTick( item )
    }
    that.setData({item})
  },
  doTick( item ){
    let param = {
      id: item.id,
      isFinish: item.isFinish||0,
      position: item.position,
    }
    let mediaList = this.data.mediaList;
    app.formPost('study.tick', param).then( res => {
      if( res.code == 200){
        item.isFinish = res.data.isFinish
        if( !res.data.skip ){
          item.position = res.data.position;
        }
        mediaList[item.index] = item;
        this.setData( {item, mediaList} );
      }
    })
  },
  playMedia( e ){
    let index = e.currentTarget.dataset.index;
    let mediaList =  this.data.mediaList
    let item = mediaList[index];
    item.index = index;
    item.prev_tick = item.position
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 1000
   })
    if( item.url ){
      this.setData({onPlay: true, item})
      return;
    }
    app.formPost('study.GetMedia', {id:item.id}).then( res => {
      if( res.code == 200){
        if( !res.data ) return
        item.url = res.data
        mediaList[index] = item;
        this.setData({onPlay: true, item, mediaList})
      }
    });
  },
  doWxPay( data ){
    let that = this
    wx.requestPayment({
      timeStamp: data.timeStamp,
      nonceStr: data.nonceStr,
      package: data.package, 
      signType: data.signType,
      paySign: data.paySign,
      success: function (event) {
        wx.showModal({
          title: '支付成功',
          showCancel: false,
          content: '本次共支付费用￥'+(data.fee/100)+'元'+ `\n\n试卷更新可能延迟，请耐心等待`,
          success(res) {
            that.indexLoad()
          }
        })
      }
    })
  },
  startWxpay(e){
    let groupId = +e.currentTarget.dataset.id
    let system = this.data.system
    app.formPost('User.PayExam', {groupId, system}).then(res => {
      if (res.code === 200) {
        if( res.data.payType=="free" ){
          this.indexLoad()
        }else if (res.data.payType=="qrcode"){
          this.setData({info: res.data, buyModel:true})
        }else{
          this.doWxPay( res.data )
        }
      }
    })
  }
})