const app = getApp()
Page({
  data: {
    spinShow: false,
    buyModel:false,
    code_url:'',
    info:{},
    system: "",
    groups: [],
    pushPaper: [],
    timeLimitPaper: [],
    userInfo:{},
    from:0,
    size: 10,
    type:'岗前培训',
    courseList: []
  },
  onLoad: function(){
    let that = this
    wx.getSystemInfo({
      success: function(res) {
        that.setData({system: res.system})
      }
    })
  },
  gotoCourse( e ){
    let courseId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/study/course/index?courseId=${courseId}`,
    })
  },
  onShow: function () {
    app.getUserInfo( userInfo =>{
      this.setData({userInfo})
      this.indexLoad()
    })
  },
  onPullDownRefresh() {
    if (!this.loading) {
      this.indexLoad( this.stopPullDownRefresh )
    }
  },
  stopPullDownRefresh(){
    wx.stopPullDownRefresh()
  },
  indexLoad: function( cb ) {
    let param ={ 
      from: this.data.from,
      size: this.data.size,
      type:this.data.type,
    }
    app.formPost('Study.GetWxCourseList', param).then(res => {
      if (res.code === 200) {
        this.setData({
          from: res.data.from,
          courseList: res.data.list,
        });
       cb&&cb() 
      }
    })
  },
  gotoExam:function( e ){
    let groupId = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `/pages/exam/do/index?id=${groupId}`
    })
  },
  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 )
        }
      }
    })
  }

})