let util = require("../../../../utils/util.js")
Page({
  data: {
    alertStatus: false,//弹窗
    answerStatus: false,//答题状态
    info: {
      choiceOptions: [],
      rank:0,
      choiceQuestionNum: 0,//题目总数
      completeNum: 0,//答题人数
      question: '',//问题
      answer: "",//答案
      myAnswer: '',//选中
      radio: 0,//题目类型 0单选 1多选
      contentType: "",//内容类型
      id: '',//id
      explain: '',//答案解答
    },
    courseId: 0,//课程id
    rank: 0,//题目号
    playerId:0,//参赛者id
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({ rank: options.rank, courseId: options.courseId, playerId: options.playerId})
    this.questionDetail();
  },
  close() {
    this.setData({ answerStatus: true })
    this.initAnswer();
  },
  changeQuestion(e){
    let rank = e.currentTarget.dataset.rank;
    if (rank < 1 || rank > this.data.info.choiceQuestionNum) return false;
    this.setData({ rank });
    this.questionDetail();
  },
  complete(){
    wx.navigateBack({
      delta: 1
    })
  },
  // 确认答案
  submitAnswer() {
    let that = this; 
    util.ajax({
      func:'v2/course/question/confirm',
      data: { id: that.data.info.id, answer: that.data.info.myAnswer, playerId:that.data.playerId },
      method:'POST',
      contentType:'application/x-www-form-urlencoded'
    },function(res){
      if(res.code == 0){ 
        let alert = {};
        if (res.data.result == 1){//正确
          if (res.data.status==1){//有效
            alert= {icon:'answer-ok',title:'恭喜您,答对了',score:res.data.score}
          }else{
            alert = { icon: 'time-out', title: '恭喜您,答对了', content: res.data.message,status:1 }
          }
        }else{//错误
            alert={icon: 'answer-error',title:'很抱歉，答错了',score:res.data.score}
        }
        that.setData({
          alertStatus: true,
          alert: alert
        });
      }else
        util.showTips(res.reason);
    });
  },
  //显示图标
  initAnswer() {
    let items = this.data.info.choiceOptions, answer = this.data.info.answer,myAnswer=this.data.info.myAnswer;
    items.forEach((item, index) => {
      if (answer.toString().indexOf(item.option) != -1) {//需要显示的ok
        items[index].icon = "icon-mini-ok"
      } else if (myAnswer.toString().indexOf(item.option) != -1) {
        items[index].icon = "icon-mini-error"
      } else {
        items[index].icon = "";
      }
    })
    this.setData({info:this.data.info})
  },
  // checked发生改变的时候
  setAnswerChecked(e){
    let data = e.detail.value;
    if (typeof (data) == 'string'){
      this.data.info.myAnswer = data;
    }else{
      this.data.info.myAnswer = data.join(",");
    }
    this.setData({ info: this.data.info });
    this.initAnswerChecked();
  },
  //初始化checked 
  initAnswerChecked() {
    let items = this.data.info.choiceOptions, answer = this.data.info.myAnswer;
    items.forEach((item, index) => {
      if (answer.indexOf(item.option) != -1) {
        items[index].checked = true;
      } else {
        items[index].checked = false;
      }
    });
    this.setData({ info: this.data.info });
  },
  // 获取题目详情
  questionDetail() {
    let that = this;
    util.ajax({
      func: "v2/course/question/detail",
      data: { courseId: that.data.courseId, playerId: that.data.playerId, rank: that.data.rank }
    }, function (res) {
      if (res.code == 0) {
        let data = res.data;
        if(data==null){
          wx.navigateBack({
            delta:1
          })
        }
        that.setData({ info: data, answerStatus: data.myAnswer==null?0: data.myAnswer.length });
        if (data.myAnswer!=null){
          if (data.myAnswer.length > 0){
            that.initAnswerChecked();
            that.initAnswer();
          }
        }
      } else {
        util.showTips(res.reason);
      }
    });
  }
})