//index.js

const util = require("../../utils/util")

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

Page({
  data: {
    spinShow: false,
    buyModel:false,
    code_url:'',
    info:{},
    system: "",
    groups: [],
    pushPaper: [],
    timeLimitPaper: [],
    userInfo:{},
    taskList: [],
    departments:[]
  },
  onLoad: function(){
    let that = this
    wx.getSystemInfo({
      success: function(res) {
        that.setData({system: res.system})
      }
    })
    app.getDepartments(  departments=>{
      this.setData({departments})
    })
  },
  onShow: function(){
    app.getUserInfo( userInfo =>{
      this.setData({userInfo})
      // 已经实名认证
      if( userInfo.identify ){
        this.indexLoad()
      }
    })
  },
  onPullDownRefresh() {
    if (!this.loading) {
      this.indexLoad( this.stopPullDownRefresh )
    }
  },
  stopPullDownRefresh(){
    wx.stopPullDownRefresh()
  },
  indexLoad: function( cb ) {
    let _this = this
    app.formPost('Exam.index', {}).then(res => {
      if (res.code === 200) {
        _this.setData({
          groups: res.data.groups
        });
       cb&&cb() 
      }
    })
  },
  downloadFile: function(){
    let url = this.data.info.code_url
    util.downloadFile( url, res=>{
      this.setData({buyModel:false, code_url:''})
    } )
  },
  gotoHistory: function(){
    wx.navigateTo({
      url: '/pages/exam/record/index',
    })
  },
  gotoExam:function( e ){
    let groupId = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `/pages/exam/do/index?id=${groupId}`
    })
  },
  gotoCollection(){
    wx.navigateTo({
      url: '/pages/exam/collection/index',
    })
  },
  onShareAppMessage: function(){

  },
  gotoError: function(){
    wx.navigateTo({
      url: '/pages/exam/error/index',
    })
  }
})