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

  /**
   * 页面的初始数据
   */
  data: {
    sctms: [],
    tm_index: 0,
    is_right: false,
    answerP: false,
    tmtotal: 0,
    domain:getApp().globalData.mediahost,
  },
  radioChange: function (e) {
    var that = this
    that.data.tm_current['user_answer'] = e.detail.value
    that.data.sctms[that.data.tm_index] = that.data.tm_current
    that.showAnswer()
  },
  checkboxChange: function (e) {
    var that = this
    that.data.tm_current['user_answer'] = e.detail.value
    that.data.sctms[that.data.tm_index] = that.data.tm_current
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    var that = this
    api.request('/crm/consumer/collects/', 'GET').then(res => {
      
      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.sctms = res.data
      that.setData({
        tmtotal:res.data.length
      })
      if (res.data.length) {
        that.showTm(that.data.tm_index)
      } else {
        wx.showModal({
          title: '提示',
          content: '收藏集无记录!',
          confirmText: '返回',
          cancelText: '前去练习',
          success: function (res) {
            if (res.confirm) {
              wx.navigateBack({
              })
            } else {
              wx.redirectTo({
                url: '/pages/lianxi/index',
              })
            }
          }
        })
      }
    })
    try {
      const res = wx.getSystemInfoSync()
      that.setData({
        scrollHeight: res.windowHeight - 70
      })
    } catch (e) {
    }

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

  },

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

  },

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

  },

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

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

  },

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

  },

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

  },

  showTm: function (index) {
    var that = this
    var tm_current = that.data.sctms[index]
    that.setData({
      tm_index: index,
      tmtotal: that.data.sctms.length,
      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
    })
  },
  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 () {
    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]
      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)
    }
    console.log(options)
    this.setData({
      options: options
    })
  },
  remove: function () {
    var that = this
    var index = that.data.tm_index
    api.request('/crm/consumer/collects/', 'DELETE', { question: that.data.tm_current.id }).then(res => {
      that.data.sctms.splice(index, 1)
      if (that.data.sctms.length > index) {
        that.showTm(index)
      } else if (that.data.sctms.length > 0 && index > 1) {
        that.showTm(index - 1)
      }
      else {
        wx.navigateBack({
        })
      }
    })
    
  }
})