import {formatSeconds} from '../../../utils/util.js'
let app = getApp()
Page({
  data: {
    preList:["A","B","C", "D", "E", "F"],
    userInfo: {},
    modalShow: false,
    timeOutShow: false,
    remainTime: 0,
    remainTimeStr: '',
    index:0,
    score:0,
    useTime:0,
    item: {index:0},
    info: {},
    list: [],
  },
  onLoad: function(options) {
    let courseId = +options.courseId||36
    app.getUserInfo( userInfo =>{
      this.setData({userInfo})
      this.loadData( courseId )
    })
  },
  loadData( courseId ){
    let info = wx.getStorageSync('@examinfo')||{};
    if( info && info.courseId == courseId){      
      let index = info.index||0;
      this.setData({info})
      this.loadQuestion( index )
      this.timeReduce()
      return;
    }
    app.formPost('Course.startExam', {courseId}).then(res => {
      if (res.code ==200) {
        this.setData({info:res.data })
        this.loadQuestion( 0 )
        this.timeReduce()
      }
    })
  },
  selectquestion( e ){
    let index = e.currentTarget.dataset.index
    this.loadQuestion( index )
  },
  checkAnswer( e ){
    let item = this.data.item;
    let examId = this.data.index.examId;
    if( !item.select ){
      util.message("还未作答", 'error')
      return;
    }
    let select = item.select;
    if( item.type == 3) select = +select.join("");
    let param = {id: item.id, examId, select}
    app.formPost('Course.SubmitAnswer', param).then(res => {
      this.nextAnswer( )
    })
  },
  radioChange( e ){
    let item = this.data.item
    item.select = +e.detail.value;
    console.log( "radioChange", item)
    this.setData({item})
    this.checkAnswer()
  },
  loadQuestion( index ) {
    let item = this.data.info.answers[index];
    item.index = index;
    if( !item.title ){
      app.formPost("course.loadAnswer", {id:item.id} ).then(res => {
        if( res.code != 200) return;
        Object.assign( item, res.data)
        this.saveItem( item ) 
      })
    }else{
      this.saveItem( item ) 
    }
  },
  saveItem( item ){
    let info = this.data.info;
    info.index = item.index;
    info.answers[ item.index] = item;
    wx.setStorageSync('@examinfo', info)
    console.log("item", item)
    this.setData({item, info})
  },
  checkboxChange( e ){
    let item = this.data.item
    item.select = parseInt(e.detail.value.sort().join(''))
    console.log( "radioChange", item)
    this.setData({item})
  },
  nextAnswer(  ){
    console.log( "next")
    this.loadQuestion( this.data.info.index+1)
  },
  returnRecord(){
    wx.navigateBack({
      delta: 1,
    })
  },
  dosubmit(e){
    let examId = this.data.info.examId
    app.formPost("course.FinishExam", {examId}).then(res => {
      if (res.code == 200) {
        let { score, useTime } = res.data;
        this.setData({score, useTime, modalShow:true})
        wx.setStorageSync('@examinfo', {})
        if( this.data.timer) clearInterval(this.data.timer)
      }
    });
  },
  timeOut() {
    clearInterval(this.data.timer)
    this.setData({
      timeOutShow: true
    });
  },

  timeReduce() {
    let _this = this
    let  remainTime = this.data.info.duration + this.data.info.startTime - parseInt(Date.now()/1000)
    this.setData({remainTime})
    let timer = setInterval(function() {
      let remainTime = _this.data.remainTime
      if (remainTime <= 0) {
        _this.timeOut()
      } else {
        _this.setData({
          remainTime: remainTime - 1,
          remainTimeStr: formatSeconds(remainTime),
          doTime: _this.data.doTime + 1
        });
      }
    }, 1000)
    _this.setData({
      timer: timer
    });
  }
})