// pages/contest/answer/answer.js
import {
  answer
} from '../../../api/api.js'
let timer
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pagination: {
      page: 1,
      rows: 10
    },
    show: false,
    pattern: false,
    isupdate: false,
    paperId: '',
    paperScore: 0,
    questionWrong: '',
    questionList: [],
    questionTotal: [],
    chooseList: [],
    time: 0
  },

  attendContest () {
    answer.attendContest({reqdata:{}},r => {
      let {paperId,questions} = r.object
      this.setData({
        paperId,
        questionTotal: questions,
        questionList: questions.filter((item,index)=>index<10)
      })
    })
  },

  showToggle(e) {
    this.setData({
      show: !this.data.show
    })
  },

  patternToggle (e) {
    this.setData({
      pattern: !this.data.pattern
    })
    this.showToggle()
    this.bindNavTagger({currentTarget:{dataset:{index:1}}})
  },

  updateTagger (e) {
    this.setData({
      pattern: false,
      isupdate: true
    })
    this.bindNavTagger({currentTarget:{dataset:{index:1}}})
  },

  bindNavTagger(e) {
    let i = e.currentTarget.dataset.index
    this.setData({
      ['pagination.page']: i-0
    },()=>{
      if (wx.pageScrollTo) {
        wx.pageScrollTo({
          scrollTop: 0
        })
      } else {
        wx.showModal({
          title: '提示',
          content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
        })
      }
      this.setData({
        questionList: this.data.questionTotal.filter((item,index)=>index>=(i-1)*10&&index<i*10)
      })
    })
  },

  clickChoose (e) {
    let {questionindex,index} = e.currentTarget.dataset
    let {chooseList} = this.data
    chooseList[questionindex] = index
    this.setData({
      chooseList
    }, () => {
      console.log(chooseList);
    })
  },

  clickConfim (e) {
    let {paperId,chooseList,questionTotal,time} = this.data
    let paperScore = 0
    let questionWrong  = []
    questionTotal.forEach((item,index) => {
      (chooseList[index]||chooseList[index]==0)&&item.options[chooseList[index]].optionAnswer
      ? paperScore += 2
      : questionWrong.push({[item.id]: (chooseList[index]||chooseList[index]==0)?item.options[chooseList[index]].optionId:''})
    })
    answer.submitUserAnswer({reqdata:{
      paperId,
      userId: wx.getStorageSync('id'),
      questionWrong: JSON.stringify(questionWrong),
      paperScore,
      paperDuration:time
    }},r => {
      wx.navigateTo({
        url: `/pages/contest/score/score?rank=${r.object}&score=${paperScore}`,
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.attendContest()
    timer = setInterval(() => {
      this.setData({
        time: ++this.data.time
      })
    }, 1000)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

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

  },

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

  },

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

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

  },

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

  },

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

  }
})