// packageE/video_question/answer/answer.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pass: false,
    question: [],
    answer: [],
    score: '',
    is_qualified: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.init();
    this.getData();
  },
  init() {
    this.setData({
      question: [],
      answer: [],
      score: [],
      is_qualified: "",
      pass: false
    });
  },
  again(){
    this.init();
    wx.pageScrollTo({
      duration: 1000,
      scrollTop:0
    });
    this.getData();
  },
  onClose() {
    this.setData({
      pass: false
    });
  },
  goApply() {
    wx.navigateTo({
      url: '/packageE/video_question/apply/apply',
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getData() {
    var that = this;
    let urlStr = app.getNetAddresss('plugin.agent-qa.api.answer-question.get-question');
    app._postNetWork({
      url: urlStr,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            question: res.data
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none'
          });
        }
      },
      fail: (res) => {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        });
      }
    });
  },
  tapAnswer(e) {
    console.log(e);
    let qid = e.currentTarget.dataset.qid;
    let aid = e.currentTarget.dataset.aid;
    var that = this;
    let is_asw = {};
    let arr = that.data.answer;
    is_asw.question_id = qid;
    is_asw.answer_id = aid;

    if (that.data.answer.length == 0) {
      arr[0] = is_asw;
    } else {
      let is_as = that.check(arr, qid);
      console.log(is_as);
      if (is_as || is_as == 0) {
        arr[is_as] = is_asw;
      } else {
        arr[arr.length] = is_asw;
      }
    }

    let qarr = that.data.question;
    for (let i = 0; i < qarr.length; i++) {
      if (qarr[i].id == qid) {
        for (let j = 0; j < qarr[i].option.length; j++) {
          if (qarr[i].option[j].id == aid) {
            console.log('111111111111111111');
            that.setData({
              [`question[${i}].option[${j}].crd`]: true
            });
          } else {
            that.setData({
              [`question[${i}].option[${j}].crd`]: false
            });
          }
        }
      }
    }
    this.setData({
      answer: arr,
      question: qarr
    });
  },
  check(arr, qid) {
    for (let i = 0; i < arr.length; i++) {
      if (arr[i].question_id == qid) {
        console.log('1q2', i);
        return i;
      }
    }
  },
  submit() {
    var that = this;
    let as = JSON.stringify(that.data.answer);
    let urlStr = app.getNetAddresss('plugin.agent-qa.api.answer-question.answer-question');
    app._postNetWork({
      url: urlStr,
      data: {
        answers: as
      },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            pass: true,
            is_qualified: res.data.is_qualified,
            score: res.data.score
          });
        }
      },
      fail: (res) => {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        });
      }
    });
  }
});