import {
  get, post,
} from '../../utils/http';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    current: 0,
    detail: {},
    id: '',
    fromList: "",
    visible: false,
    spinning: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    let current = 0,fromList = "";
    if(options.fromList == "1") {
      current = 2
      fromList = options.fromList 
    }
    this.setData({
      id: options.id,
      current,
      fromList,
      userInfo: wx.getStorageSync('userInfo')
    })
    this.getSkinInfo(options.id);
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },


  /**
   * 切换状态
   */
  onTabsChange(e) {
    let current = e.currentTarget.dataset.id
    if (current == this.data.current) {
      return;
    }
    this.setData({
      current,
      // page: 1,
    }, () => {
      // this.getCoupon()
    })
  },

  /**
   * 获取检测记录详情
   * /api/user/skin_log_info
  */
  getSkinInfo(id) {
    return new Promise((resolve,reject) => {
      get('api/user/skin_log_info',{
        id
      },(res) => {
        if(res.data) {
          res.data.question.forEach((item) => {
            item.status = 0
          })
          this.setData({
            spinning: false,
            detail: res.data
          })
        }
        resolve(res)
      })
    })
    
  },

  /**
   * 点击切换问诊卡问题
  */
  onChangeStatus(e) {
    let index = e.currentTarget.dataset.index
    let status = e.currentTarget.dataset.status
    this.setData({
      ['detail.question[' + index + '].status']: status
    })
  },

  /**
   * 问诊卡不提交
  */
  onCancelUse() {
    this.setData({
      visible: true
    })
  },

  /**
   * 问诊卡确认提交
  */
  onConfirmUse() {
    let detail = this.data.detail;
    let flag = true;
    let ids = [];
    let values = [];
    detail.question.forEach(item => {
      if(item.status === undefined || item.status == 'undefined') {
        console.log('空')
        flag = false
      }
      ids.push(item.id)
      values.push(item.status)
    });
    if(!flag) {
      wx.showToast({
        title: '请选择完所有选项',
        icon: 'none'
      })
      return;
    }
    post('api/user/save_investigation',{
      analysis_id: detail.analysis_id,
      ids: ids.toString(),
      values: values.toString()
    },(res) => {
      wx.showToast({
        title: '提交成功',
        icon: 'none'
      })
      this.setData({
        visible: true,
        spinning: true
      },() => {
        this.getSkinInfo(detail.analysis_id).then((res) => {
          // this.setData({
          //   current: 2
          // })
        })
      })
      console.log(res)
    })
  }
})