import {
  formatSeconds
} from '../../../utils/util.js'

let app = getApp()
Page({
  data: {
    list: [],
    redo: false,
    preList:["A","B","C", "D","E","F"],
    info:{},
    timer: null,
    doTime: 0,
    remainTime: 0,
    remainTimeStr: '',
    modalShow: false,
    result: {},
    startTime: "",
    timeOutShow: false
  },
  onLoad: function(options) {
    let groupId = +options.id||3
    let redo = !!options.redo;
    this.setData({redo})
    let method =  redo?'Exam.examreStart':'Exam.examStart';
    let _this = this
    app.formPost(method, {groupId}).then(res => {
      if (res.code ==200) {
        let {startTime, info, list} = res.data;
        let duration = list.length * 60;
        list = list.map(item=>{
          delete( item["select"])
          return item
        }).sort( (i,j) =>{
          return i.type > j.type?1:-1;
        })
        wx.setNavigationBarTitle({
          title: info.title+'-模拟考试'
        })

        _this.setData({
          list: list,
          info: info,
          startTime: startTime,
          remainTime: duration
        });
        _this.timeReduce()
      }
    })
  },
  timeReduce() {
    let _this = this
    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
    });
  },
  onUnload() {
    clearInterval(this.data.timer)
  },
  returnRecord() {
    if( this.data.redo){
      wx.reLaunch({
        url: '/pages/exam/error/index',
      });
    }else{
      wx.reLaunch({
        url: '/pages/exam/record/index',
      });
    }
  },
  timeOut() {
    clearInterval(this.data.timer)
    this.setData({
      timeOutShow: true
    });
  },
  radioChange( e ){
    let index = e.currentTarget.dataset.index;
    let list = this.data.list
    list[index].select = +e.detail.value
    console.log("select", index, list[index].select )
    this.setData( {list} );
  },
  checkboxChange( e ){
    let index = e.currentTarget.dataset.index;
    let list = this.data.list
    list[index].select = +e.detail.value.sort().join("")
    console.log("select", index, list[index].select)
    this.setData( {list} );
  },
  formSubmit: function(e) {
    let _this = this
    let force = this.data.remainTime < 3;
    let isFinish = true;
    wx.showLoading({
      title: '提交中',
      mask: true
    })
    let info = this.data.info;
    let param ={};
    param.groupId = info.groupId;
    param.paperId = info.paperId;
    param.answers = [];
    param.result  = [];
    param.correct = 0;

    param.counter = this.data.list.length
    param.groupName = info.title;
    param.useTime =  this.data.doTime
    param.duration = info.duration
    param.startTime = this.data.startTime;
    let errIds = [];
    for( let i=0; i< this.data.list.length; i++){
        let item = this.data.list[i];
        param.answers.push( item.answerId );
        param.result.push( item.select || 0);
        if( item.select == item.result){
          param.correct += 1
        }else{
          errIds.push( item.answerId)
        }
        if( !item.select ) isFinish = false
    }
    if( !isFinish && !force){
      app.message("还未完成", 'error')
      wx.hideLoading()
      return 
    }
    param.result = param.result.join(",")
    param.answers = param.answers.join(",")
    param.errors = errIds.join(",")
    param.isRedo = this.data.redo?1:0;
    app.formPost('Exam.ExamSubmit', param).then(res => {
      if (res.code === 200) {
        _this.setData({
          modalShow: true,
          result:param,
        });
        if (this.data.timer) {
          clearInterval(this.data.timer)
        }
      } else {
        app.message(res.msg, 'error')
      }
      wx.hideLoading()
    })
  }
})