let app = getApp()
Page({
  data: {
    preList:["A","B","C", "D", "E", "F"],
    from:0,
    size:10,
    item: {},
    next: false,
    list: [],
  },
  onLoad: function(options) {
    this.loadData(  true )
  },
  loadData( firstAnswer ){
    let {size,from} = this.data
    app.formPost('Exam.GetErrorAnswerList', {from,size}).then(res => {
      if (res.code ==200) {
        let list = res.data.list||[];
        list.reverse()
        if( firstAnswer){
          let item = list.pop() ||{};
          this.setData({list, item})
        }else{
          this.setData({list})
        }
      }
    })
  },
  checkAnswer( e ){
    let item = this.data.item;
    if( !item.select ){
      app.message("还未作答", 'error')
      return;
    }
    let param = {answerId: item.answerId}
    // 判断是否正确
    if( item.type == 2){
      item.correct = item.select.join("") == item.result;
    }else{
      item.correct = item.select == item.result;
    }
    param.correct = item.correct?1:0
    // 打开下一题
    app.formPost('Exam.EditErrorAnswer', param).then(res => {
      this.setData({item, next:true})
    }) 
  },
  radioChange( e ){
    let item = this.data.item
    item.select = +e.detail.value;
    this.setData({item})
  },
  checkboxChange( e ){
    let item = this.data.item
    item.select = e.detail.value.sort()
    this.setData( {item} );
  },
  nextAnswer( ){
    let {list} = this.data;
    let item = list.pop()||{};
    delete( item.select )
    this.setData({item, list, next:false})
    if( list.length < 1){
      this.loadData( )
    }
  }
})