
const api = require("../../utils/request.js");
var util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    is_right: false,
    answerP: false,
    domain:getApp().globalData.mediahost,
  },
  radioChange: function (e) {
    var that = this
    that.data.tm_current['user_answer'] = e.detail.value
    that.showAnswer()
  },
  checkboxChange: function (e) {
    var that = this
    that.data.tm_current['user_answer'] = e.detail.value
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    wx.showLoading({
      title: '加载中',
      mask:true
    })
    api.request(`/question/question/${options.id}/`, 'GET').then(res => {
        if(res.data.img){
          res.data.img = getApp().globalData.mediahost + res.data.img
        }
      that.data.tm_current = res.data
      that.showTm()
      wx.hideLoading({
        complete: (res) => {},
      })
    })
    try {
      const res = wx.getSystemInfoSync()
      that.setData({
        scrollHeight: res.windowHeight - 90
      })
    } catch (e) {
    }

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

  },

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

  },

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

  },

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

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

  },

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

  },

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

  },

  showTm: function (index) {
    var that = this
    var tm_current = that.data.tm_current
    that.setData({
      tm_current: tm_current,
      answerP: false
    })
    that.showOptions()
    if (tm_current.user_answer) {
      that.showAnswer()
    }
  },
  panTi: function () {
    var that = this
    let tm_current = that.data.tm_current
    let is_right = false
    if (tm_current.type == '多选') {
      if (tm_current.user_answer) {
        if (tm_current.user_answer.sort().toString() == tm_current.right.sort().toString()) {
          is_right = true
        }
      }

    } else {
      is_right = tm_current.right == tm_current.user_answer
    }
    return is_right
  },
  showAnswer: function () {
    let is_right = this.panTi()
    this.setData({
      is_right: is_right,
      answerP: true,
      tm_current: this.data.tm_current
    })
  },

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