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

  /**
   * 页面的初始数据
   */
  data: {
    questioncat: null,
    tm_current:null,
    tms: [],
    ydtms: [],
    ctms: [],
    tm_index: 0,
    answerP: false,
    page:1,
    domain:getApp().globalData.mediahost,
    moShi:'背题模式',
  },
  changeM: function () {
    var that = this
    if(that.data.moShi=='看题模式'){
      that.setData({
        moShi:'背题模式',
      })
      that.showOptions()
    }else{
      that.setData({
        moShi: '看题模式',
      })
    }
  },
  radioChange: function(e) {
    var that = this
    that.data.tm_current['user_answer'] = e.detail.value
    that.data.tms[that.data.tm_index] = that.data.tm_current
    that.showAnswer()
    if (that.data.moShi=='背题模式' && that.data.ydtms.indexOf(that.data.tm_current.id) == -1) {
      that.data.ydtms.push(that.data.tm_current.id)
    }
  },
  checkboxChange: function(e) {
    var that = this
    that.data.tm_current['user_answer'] = e.detail.value
    that.data.tms[that.data.tm_index] = that.data.tm_current
    if (that.data.moShi=='背题模式' && that.data.ydtms.indexOf(that.data.tm_current.id) == -1) {
      that.data.ydtms.push(that.data.tm_current.id)
    }
  },
  changeProcess: function(e){
    let data = {'questioncat':this.questioncat, 'ydtms':this.ydtms}

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    wx.hideShareMenu()
    var that = this
    that.data.questioncat = options.questioncat
    try {
      var value = wx.getStorageSync('cat' + that.data.questioncat.toString())
      if (value) {
        that.data.ydtms = value
      }
    } catch (e) {

    }
    try {
      var value = wx.getStorageSync('ctms')
      if (value) {
        that.data.ctms = value
      }
    } catch (e) {}
    that.getTms()
    try {
      const res = wx.getSystemInfoSync()
      that.setData({
        scrollHeight: res.windowHeight - 78
      })
    } catch (e) {}
  },
  getTms: function() {
    var that = this
    var query = {
      questioncat: that.data.questioncat,
      ydtms: wx.getStorageSync('cat' + that.data.questioncat.toString()),
      ydtms_:that.data.ydtms
    }
    api.request('/question/question/exercise/?limit=10&page='+that.data.page, 'POST', query).then(res => {
      if(res.code>=200){
        for (var i = 0; i < res.data.results.length; i++) {
          if(res.data.results[i].img){
            res.data.results[i].img = getApp().globalData.mediahost + res.data.results[i].img
          } 
        }

        that.data.tms = that.data.tms.concat(res.data.results)
        that.showTm(that.data.tm_index) //展示题目和答案
        that.setData({
          tmtotal: res.data.count,
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

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

  },

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

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    try {
      wx.setStorageSync('cat' + this.data.questioncat.toString(), this.data.ydtms)
    } catch (e) {}
    try {
      wx.setStorageSync('ctms', this.data.ctms)
    } catch (e) {}
  },

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

  },

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

  },

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

  },

  showTm: function(index) {
    var that = this
    if(index<that.data.tms.length){
      var tm_current = that.data.tms[index]
      that.setData({
        'tm_index': index,
        'tm_current': tm_current
      })
      that.showOptions()
      if (tm_current.user_answer) {
        that.showAnswer()
      }
      else{
        that.setData({
          isright:false
        })
      }
      if (that.data.moShi=='看题模式' && that.data.ydtms.indexOf(that.data.tm_current.id) == -1) {
        that.data.ydtms.push(that.data.tm_current.id)
      }
    }
    
  },
  panTi: function() {
    var that = this
    let tm_current = that.data.tm_current
    let isright = false
    if (tm_current.type == '多选') {
      if (tm_current.user_answer) {
        if (tm_current.user_answer.sort().toString() == tm_current.right.sort().toString()) {
          isright = true
        }
      }

    } else {
      isright = tm_current.right == tm_current.user_answer
    }
    if (isright == false && tm_current.user_answer != undefined) {
      tm_current.dtime = util.formatTime(new Date())
      that.data.ctms.unshift(tm_current)
      if (that.data.ctms.length > 100) {
        that.data.ctms.length = 100
      }
    }
    return isright
  },
  showAnswer: function() {
    let isright = this.panTi()
    this.setData({
      isright: isright,
      answerP: true,
      tm_current: this.data.tm_current
    })
  },
  next: function() {
    var that = this
    var tm_index = that.data.tm_index
    if (tm_index+1==that.data.tmtotal){
      wx.showToast({
        title: '没有更多',
        icon:'none'
      })
    }else{
      tm_index = tm_index + 1
      that.setData({
        tm_index: tm_index,
        answerP: false
      })
      if (tm_index + 1 > that.data.tms.length) {
        that.data.page = that.data.page + 1
        that.getTms()
      } else {
        that.showTm(tm_index)
      }
    }
    
    

  },
  previous: function() {
    var that = this
    var tm_index = that.data.tm_index - 1
    that.setData({
      answerP: false
    })
    that.showTm(tm_index)
  },
  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 = '<p>' + tm_current.options[key].toString() + '</p>'
      option.value = tm_current.options[key]
      option.checked = false
      option.right = false
      if(option.key == tm_current.right||tm_current.right.indexOf(option.key)!=-1){
        option.right = true
      }
      if (tm_current.user_answer) {
        if (key == tm_current.user_answer || tm_current.user_answer.indexOf(key) != -1) {
          option.checked = true
        }
      }
      options.push(option)
    }
    this.setData({
      options: options
    })
  },
  shoucang: function() {
    var that = this
    var tm_current = that.data.tm_current
    if (tm_current.is_collect) {
      api.request('/crm/consumer/collects/', 'DELETE', { question: tm_current.id }).then(res => {
        wx.showToast({
          title: '已取消!',
          icon: 'none'
        })
        tm_current.is_collect = false
        that.setData({
          tm_current: tm_current
        })
      })
      
    } else {
      api.request('/crm/consumer/collects/', 'POST', { question: tm_current.id }).then(res => {
        wx.showToast({
          title: '收藏成功!',
          icon: 'none'
        })
        tm_current.is_collect = true
        that.setData({
          tm_current: tm_current
        })
      })
      
    }
  }
})