import { CalcMethod } from '../../utils/comm.js'
const app = getApp(),
  { hostname, business_id } = app.globalData,
  Calc = new CalcMethod()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    focus:true,
    inputValue:"",
    g: false
  },
  bindKeyInput: function (e) {
    let inputValue= e.detail.value;
    //   console.log(e.detail.value);
      this.setData({
          inputValue
      })
      if (inputValue){
        this.setData({
          g: true
        })
      }else{
        this.setData({
          g: false
        })
      }
  },
  geiqian:function(){
         let geiqian = this.data.inputValue,
             that = this,
             openid = wx.getStorageSync("openId"),
             user_id = wx.getStorageSync("users_id");
         console.log(geiqian);
         if (Number(geiqian)<=0){
           wx.showToast({
             title: '付款金额错误',
             icon: 'none',
             duration: 2000
           })
           return;
         }
         var business_id = app.globalData.business_id;
      //  生成订单
         wx.request({
           url: hostname + 'Kaiguan/payMoney',
             data: {
                 business_id,
                 openid,
                 user_id,
                 money: geiqian
             },
             header: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;' },
             method: 'POST',
             dataType: 'JSON',
             success: function (res) {
                console.log(JSON.parse(res.data))
                var resData = JSON.parse(res.data)
               // 得到支付需要的字段发起支付
               wx.requestPayment({
                 timeStamp: resData.timeStamp,
                 nonceStr: resData.nonceStr,
                 package: resData.package,
                 signType: resData.signType,
                 paySign: resData.paySign,
                 success(res) {
                   console.log(res)
                   wx.showLoading({
                     title: '支付完成',
                   })
                   setTimeout(() => {
                     wx.hideLoading();
                     wx.switchTab({
                       url: '../order/order',
                     })
                   }, 1500)
                 }
               })
             }
         })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    that.setData({
      more_info: wx.getStorageSync('more_info')
    })
    
  },

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

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

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

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

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

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

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