// pages/form/index.js
var api = require('../../utils/api.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    data: {
      formContainerClass: 'mode-1-container',
      mode: 1
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.request({
      url: api.getFormSetUrl,
      method: 'GET',
      success: res => {
        if(res.data.status == 'success') {
          var formContainerClass = res.data.data.mode == 1 ? 'mode-1-container' : 'mode-2-container'
          that.setData({
            data: res.data.data,
            formContainerClass: formContainerClass,
            mode: res.data.data.mode
          })
          if(res.data.data.top_title) {
            wx.setNavigationBarTitle({
              title: res.data.data.top_title
            })
          }
          
        }
      }
    })
  },

  formSubmit: function(e) {
    var that = this,
        data = that.data.data,
        value = e.detail.value;
    
    if(data.money && data.money != '0') {
      value.type = 'pay';
    } else {
      value.type = 'form';
    }
    var id = wx.getStorageSync('we_chat_user_id')
    if(data.text_1_need == 1 && !value.text_1) {
      wx.showToast({
        icon: 'none',
        title: data.text_1 + '必填',
      })
      return false;
    }
    if (data.text_2_need == 1 && !value.text_2) {
      wx.showToast({
        icon: 'none',
        title: data.text_2 + '必填',
      })
      return false;
    }
    if (data.text_3_need == 1 && !value.text_3) {
      wx.showToast({
        icon: 'none',
        title: data.text_3 + '必填',
      })
      return false;
    }
    if (data.text_4_need == 1 && !value.text_4) {
      wx.showToast({
        icon: 'none',
        title: data.text_4 + '必填',
      })
      return false;
    }
    if (data.multi_text_need == 1 && !value.multi_text) {
      wx.showToast({
        icon: 'none',
        title: data.multi_text + '必填',
      })
      return false;
    }
    if (data.radio_need == 1 && !value.radio) {
      wx.showToast({
        icon: 'none',
        title: data.radio + '必填',
      })
      return false;
    }
    if (data.checkbox_need == 1 && !value.checkbox) {
      wx.showToast({
        icon: 'none',
        title: data.checkbox + '必填',
      })
      return false;
    }
    wx.request({
      url: api.submitFormUrl,
      method: 'POST',
      data: {
        data: value,
        id: id,
        mode: this.data.mode
      },
      success: res => {
        if (res.data.status == 'success') {
          if(value.type == 'pay') {
            data = JSON.parse(res.data.data)
            wx.requestPayment({
              timeStamp: data.timeStamp,
              nonceStr: data.nonceStr,
              package: data.package,
              signType: data.signType,
              paySign: data.paySign,
            })
          } else {
            wx.showToast({
              icon: 'none',
              title: '提交成功',
            })
          }
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

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

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

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

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

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

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (t) {
    var a = this;
    return {
      path: "/pages/index/index",
      success: function (t) {
      },
      title:"钢琴时间打卡"
    };
  },
})