const app = require('../../utils/util.js');
const tools = require('../../utils/tools.js');
Page({
  data: {
    index: 0, // 题目序列
    chooseValue: [], // 选择的答案序列
    questionList: [],
    totalScore: 0,
    id: '', //考试主键id(可取list接口中id)
    paperId: '', //试卷id(可取list接口中paperId)
    answersId: '', // 提交答案
    sessionKey: '',
  },
  /*
   * 单选事件
   */
  radioChange: function (e) {
    let json = {};
    json.currentAnswer = e.detail.value;
    json.id = e.currentTarget.dataset.idx;
    json.questType = e.currentTarget.dataset.questtypes;
    this.data.chooseValue[this.data.index] = json;
    this.data.questionList[this.data.index].checked = e.detail.value;

    this.setData({
      questionList: this.data.questionList
    })

    wx.setStorageSync('questionList' + wx.getStorageSync('answersId'), this.data.questionList);
    wx.setStorageSync('titleINDEX' + wx.getStorageSync('answersId'), this.data.index);
    wx.setStorageSync('chooseValue' + wx.getStorageSync('answersId'), this.data.chooseValue);

  },
  radioChange: function (e) {
    let json = {};
    json.currentAnswer = e.detail.value;
    json.id = e.currentTarget.dataset.idx;
    json.questType = e.currentTarget.dataset.questtypes;
    this.data.chooseValue[this.data.index] = json;
    const items = this.data.questionList[this.data.index].QuestChoice
    for (let i = 0, len = items.length; i < len; ++i) {
      items[i].checked = items[i].value === e.detail.value
    }
    this.setData({
      questionList: this.data.questionList
    })
    wx.setStorageSync('questionList' + wx.getStorageSync('answersId'), this.data.questionList);
    wx.setStorageSync('titleINDEX' + wx.getStorageSync('answersId'), this.data.index);
    wx.setStorageSync('chooseValue' + wx.getStorageSync('answersId'), this.data.chooseValue);
  },
  /*
   * 多选事件
   */
  // checkboxChange: function (e) {
  //   let json = {};
  //   let aa = e.detail.value.join("||")
  //   json.currentAnswer = aa;
  //   json.questType = e.currentTarget.dataset.questtypes;
  //   json.id = e.currentTarget.dataset.idx;
  //   this.data.chooseValue[this.data.index] = json;
  //   const items = this.data.questionList[this.data.index].QuestChoice
  //   const values = e.detail.value
  //   for (let i = 0, lenI = items.length; i < lenI; ++i) {
  //     items[i].checked = false
  //     for (let j = 0, lenJ = values.length; j < lenJ; ++j) {
  //       if (items[i].value === values[j]) {
  //         items[i].checked = true
  //         break
  //       }
  //     }
  //   }
  //   this.setData({
  //     questionList: this.data.questionList
  //   })
  //   wx.setStorageSync('questionList' + wx.getStorageSync('answersId'), this.data.questionList);
  //   wx.setStorageSync('titleINDEX' + wx.getStorageSync('answersId'), this.data.index);
  //   wx.setStorageSync('chooseValue' + wx.getStorageSync('answersId'), this.data.chooseValue);
  // },
  checkboxChange: function (e) {
    const questionList = wx.getStorageSync('questionList' + wx.getStorageSync('answersId'));
    console.log(questionList,'我是选项es');
    console.log(this.data.index)
    let data=questionList[this.data.index].QuestChoice;
    let json = {};
    let list=e.detail.value;
    let valueData=[];
    data.map(ite=>{
      list.map(items=>{
        if(ite.name==items)
        {
          valueData.push(ite.name)
        }
      })
    })
    let aa = valueData.join("||")
    json.currentAnswer = aa;
    json.questType = e.currentTarget.dataset.questtypes;
    json.id = e.currentTarget.dataset.idx;
    this.data.chooseValue[this.data.index] = json;
    const items = this.data.questionList[this.data.index].QuestChoice
    const values = e.detail.value
    for (let i = 0, lenI = items.length; i < lenI; ++i) {
      items[i].checked = false
      for (let j = 0, lenJ = values.length; j < lenJ; ++j) {
        if (items[i].value === values[j]) {
          items[i].checked = true
          break
        }
      }
    }
    this.setData({
      questionList: this.data.questionList
    })
    wx.setStorageSync('questionList' + wx.getStorageSync('answersId'), this.data.questionList);
    wx.setStorageSync('titleINDEX' + wx.getStorageSync('answersId'), this.data.index);
    wx.setStorageSync('chooseValue' + wx.getStorageSync('answersId'), this.data.chooseValue);
  },
  /*
   * 解答事件
   */
  bindTextAreaBlur(e) {
    let json = {};
    json.currentAnswer = e.detail.value;
    json.id = e.currentTarget.dataset.idx;
    json.questType = e.currentTarget.dataset.questtypes;
    this.data.chooseValue[this.data.index] = json;
    this.data.questionList[this.data.index].currentAnswer = e.detail.value;
    this.setData({
      questionList: this.data.questionList
    })
    wx.setStorageSync('questionList' + wx.getStorageSync('answersId'), this.data.questionList);
    wx.setStorageSync('titleINDEX' + wx.getStorageSync('answersId'), this.data.index);
    wx.setStorageSync('chooseValue' + wx.getStorageSync('answersId'), this.data.chooseValue);
  },
  /*
   * 下一题/提交 按钮
   */
  nextSubmit: function () {
    // console.log("0000")
    // 判断是不是最后一题
    if (this.data.index < this.data.questionList.length - 1) {
      console.log(this.data.chooseValue,"0000000000000000")
      // 渲染下一题
      this.setData({
        index: this.data.index + 1
      })
      wx.setStorageSync('titleINDEX' + wx.getStorageSync('answersId'), this.data.index);
    } else {
      console.log(this.data.chooseValue,"1111111111")
      if (wx.getStorageSync('chooseValue' + this.data.answersId)) {
        this.setData({
          chooseValue: wx.getStorageSync('chooseValue' + this.data.answersId)
        })
      }
      this.submitAll(this.data.chooseValue);
    }
  },
  upSubmit() {
    console.log(this.data.chooseValue,"我点击上一题了")
    this.setData({
      index: this.data.index - 1
    })
    wx.setStorageSync('titleINDEX' + wx.getStorageSync('answersId'), this.data.index);
  },
  async onLoad(options) {
    console.log("我是答题页","00000000000")
    const sessionKey = await tools.checkSessionAndLogin();
    wx.setStorageSync('answersId', options.answersId);
    this.setData({
      sessionKey: sessionKey,
      paperId: options.paperId,
      answersId: options.answersId,
      id: options.id,
    })
    if (wx.getStorageSync('questionList' + wx.getStorageSync('answersId'))) {
      const questionList = wx.getStorageSync('questionList' + wx.getStorageSync('answersId'));
      const titleINDEX = wx.getStorageSync('titleINDEX' + wx.getStorageSync('answersId'));
      const chooseValue = wx.getStorageSync('chooseValue' + wx.getStorageSync('answersId'));
      this.setData({
        questionList: questionList,
        answersId: wx.getStorageSync('answersId'),
        chooseValue: chooseValue,
        index: titleINDEX
      })
    } else {
      this.allShi(options.id, options.paperId, sessionKey);
    }
  },
  allShi(id, paperId, sessionKey) {
    wx.request({
      url: app.globalData.publicUrl + '/wx/paper/detail',
      method: "post",
      data: {
        sessionKey: sessionKey,
        id: id,
        paperId: paperId
      },
      success: (res) => {
        let jsonArr = JSON.parse(res.data.paperContent).map(item => {
          let json = {};
          json.QuestType = item.QuestType;
          json.id = item.Id;
          json.QuestAnswer = item.QuestAnswer;
          json.QuestStem = item.QuestStem;
          if (item.QuestChoice) {
            let arr = item.QuestChoice.split('||');
            json.QuestChoice = arr;
          }
          if (item.QuestType == '1') {
            let arr = item.QuestChoice.split('||');
            let arrJson = arr.map(item => {
              let json1 = {};
              json1.name = item;
              json1.value = item;
              json1.checked = false;
              return json1
            });
            json.QuestChoice = arrJson;
          }
          if (item.QuestType == '0') {
            let arr = item.QuestChoice.split('||');
            let arrJson = arr.map(item => {
              let json1 = {};
              json1.name = item;
              json1.value = item;
              json1.checked = false;
              return json1
            });
            json.QuestChoice = arrJson;
          }
          if (item.QuestType == '2') {
            let arr = item.QuestChoice.split('||');
            let arrJson = arr.map(item => {
              let json1 = {};
              json1.name = item;
              json1.value = item;
              json1.checked = false;
              return json1
            });
            json.QuestChoice = arrJson;
          }
          return json;
        })
        this.setData({
          questionList: jsonArr
        })
        wx.setStorageSync('titleINDEX' + wx.getStorageSync('answersId'), 0);
        wx.setStorageSync('chooseValue' + wx.getStorageSync('answersId'), []),
          wx.setStorageSync('questionList' + wx.getStorageSync('answersId'), this.data.questionList);
      },
      fail:()=>{
        wx.hideLoading();
      }
    })
  },
  submitAll(currentAnswerTemp) {
    console.log(currentAnswerTemp,"我是带过来的答案")
    let indexs = currentAnswerTemp.findIndex(item => {
      return !item
    })
    if (indexs == -1) {
      if (currentAnswerTemp.length == 0) {
        let arr = this.data.questionList.map(item => {
          let json = {};
          json.currentAnswer = '';
          json.questType = item.QuestType;
          json.id = item.id;
          return json
        });
        wx.request({
          url: app.globalData.publicUrl + '/wx/paper/commit',
          method: "post",
          data: {
            sessionKey: this.data.sessionKey,
            currentAnswerTemp: arr,
            answersId: wx.getStorageSync('answersId')
          },
          success: (res) => {
            wx.hideLoading();
            if (res.data.code == 0) {
              wx.showModal({
                showCancel: false,
                content: '恭喜您,提交答案成功，请耐心等待老师判卷',
                success() {
                  wx.navigateBack({
                    delta: 1
                  })
                }
              })
            } else {
              wx.showModal({
                showCancel: false,
                content: res.data.msg,
                success() {
                  wx.navigateBack({
                    delta: 1
                  })
                }
              })
            }
          }
        })
      } else {
        wx.request({
          url: app.globalData.publicUrl + '/wx/paper/commit',
          method: "post",
          data: {
            sessionKey: this.data.sessionKey,
            currentAnswerTemp: currentAnswerTemp,
            answersId: wx.getStorageSync('answersId')
          },
          success: (res) => {
            wx.hideLoading();
            if (res.data.code == 0) {
              wx.showModal({
                showCancel: false,
                content: '恭喜您,提交答案成功，请耐心等待老师判卷',
                success() {
                  wx.navigateBack({
                    delta: 1
                  })
                }
              })
            } else {
              wx.showModal({
                showCancel: false,
                content: res.data.msg,
                success() {
                  wx.navigateBack({
                    delta: 1
                  })
                }
              })
            }
          }
        })
      }
    }else {
      wx.hideLoading();
      wx.showModal({
        showCancel: false,
        content: '当前第' + Number(indexs+1) + '没答，请答题',
      })
    }   
    // wx.request({
    //   url: app.globalData.publicUrl + '/wx/paper/commit',
    //   method: "post",
    //   data: {
    //     sessionKey: this.data.sessionKey,
    //     currentAnswerTemp: currentAnswerTemp,
    //     answersId: wx.getStorageSync('answersId')
    //   },
    //   success: (res) => {
    //     if (res.data.code == 0) {
    //       wx.showModal({
    //         showCancel: false,
    //         content: '恭喜您,提交答案成功，请耐心等待老师判卷',
    //         success() {
    //           wx.navigateBack({
    //             delta: 1
    //           })
    //         }
    //       })
    //     }
    //      else {
    //       wx.showModal({
    //         showCancel: false,
    //         content: res.data.msg,
    //         success() {
    //           wx.navigateBack({
    //             delta: 1
    //           })
    //         }
    //       })
    //     }
    //   }
    // })
  }
})