// pages/test/detail.js
const api = require("../../utils/request.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tm_index:0,
    results:[],
    tm_current:null,
    domain:getApp().globalData.mediahost,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    var query = {'examtest':options.id}
    wx.showLoading({
      title: '加载中...',
      mask:true
    })
    api.request('/examtest/answerdetail/', 'GET', query).then(res => {
      wx.hideLoading()
      for (var i = 0; i < res.data.length; i++) {
        if(res.data[i].img){
          res.data[i].img = getApp().globalData.mediahost + res.data[i].img
        } 
      }
      that.data.results= res.data
      that.showTm(0)
      that.setData({
        tmtotal:res.data.length
      })
    }).catch(e=>{wx.hideLoading()})
    try {
      const res = wx.getSystemInfoSync()
      that.setData({
        scrollHeight: res.windowHeight - 78
      })
    } catch (e) {
    }
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },
  showTm: function (index) {
    var that = this
    if(index<that.data.results.length){
      var tm_current = that.data.results[index]
      that.setData({
        'tm_index': index,
        'tm_current': tm_current
      })
      that.showOptions()
    }
    
  },
  next: function () {
    var that = this
    var tm_index = that.data.tm_index + 1
    that.showTm(tm_index)

  },
  previous: function () {
    var that = this
    var tm_index = that.data.tm_index - 1
    that.showTm(tm_index)
  },
  showOptions: function () {
    var that = this
    let question_options = that.data.tm_current.question.options
    let options = []
    let user_answer =  that.data.tm_current.user_answer
    for (let key in question_options) {
      let option = {}
      option.key = key
      option.value = question_options[key]
      if (user_answer) {
        if (key == user_answer || user_answer.indexOf(key) != -1) {
          option.checked = true
        }
      } else {
        option.checked = false
      }
      options.push(option)
    }
    that.setData({
      options: options
    })
  },
})