// pages/cashier_desk/cashier_desk.js
let app = getApp();
Page({

  /**
	 * 页面的初始数据
	 */
  data: {
    storeName: {
      thumb: "",
      name: ""
    },
    payMuch: "0"
  },

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

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },
  //处理按键
  _handleKeyPress(e) {
    let num = e.currentTarget.dataset.num;
    //不同按键处理逻辑
    // -1 代表无效按键，直接返回
    if (num == -1) return false;
    switch (String(num)) {
    //小数点
    case ".":
      this._handleDecimalPoint();
      break;
      //删除键
    case "D":
      this._handleDeleteKey();
      break;
      //确认键
    case "S":
      this._handleConfirmKey();
      break;
    default:
      this._handleNumberKey(num);
      break;
    }
  },
  //处理小数点函数
  _handleDecimalPoint() {
    //如果包含小数点，直接返回
    if (this.data.payMuch.indexOf(".") > -1) {
      return false;
    }
    //如果小数点是第一位，补0
    if (!this.data.payMuch.length) {
      this.setData({
        payMuch: "0."
      });
    } else {
      //如果不是，添加一个小数点
      this.setData({
        payMuch: this.data.payMuch + "."
      });
    }
  },
  //处理删除键
  _handleDeleteKey() {
    let S = this.data.payMuch;
    //如果没有输入，直接返回
    if (S.length <= 1) {
      this.setData({
        payMuch: "0"
      });
      return;
    }
    //否则删除最后一个
    this.setData({
      payMuch: S.substring(0, S.length - 1)
    });
  },
  _handleConfirmKey() {
    let S = this.data.payMuch;
    //未输入
    if (!S.length || Number(S) === 0) {
      wx.showToast({
        icon: 'none',
        title: "您目前未输入!",
        duration: 1500
      });
      return false;
    }
    //将 8. 这种转换成 8.00
    if (S.indexOf(".") > -1 && S.indexOf(".") === S.length - 1) {
      S = Number(S.substring(0, S.length - 1)).toFixed(2);
    }
    //保留两位
    S = Number(S).toFixed(2);
    this.setData({
      payMuch: S
    });
    this.confirmOrder();
  },
  confirmOrder() {
    var val = this.data.payMuch;
    if (val && val > 0) {
      this.getData(true, val);
      setTimeout(function() {
        wx.hideLoading();
      }, 3000);
    } else {
      wx.showToast({
        icon: 'none',
        title: "请输入正确的付款金额!",
        duration: 1500
      });
    }
  },
  //处理数字
  _handleNumberKey(num) {
    let S = this.data.payMuch;
    //如果有小数点且小数点位数不小于2
    if (S.indexOf(".") > -1 && S.substring(S.indexOf(".") + 1).length < 2) {
      this.setData({
        payMuch: S + num
      });
    }
    //没有小数点
    if (!(S.indexOf(".") > -1)) {
      //如果第一位是0，只能输入小数点
      if (num == 0 && S.length == 0) {
        this.setData({
          payMuch: "0."
        });
      } else {
        if (S.length && Number(S.charAt(0)) === 0) {
          this.setData({
            payMuch: num
          });
          return;
        }
        this.setData({
          payMuch: S + num
        });
      }
    }
  },
  getData(bolshow, money) {
    let Json = {
      bolshow: bolshow,
      money: money,
      moneyBol:true
    };
    
    let _wx_token = '';
    wx.showLoading({
      title: '正在发起付款金额',
    });
    try {
      var value = wx.getStorageSync('wx_token');
      if (value) {
        _wx_token = value;
      }
    } catch (e) {
      console.log(e);
    }
    wx.request({
      url: app.getFrogproNetAddresss('plugin.store-cashier.frontend.app.cashier.statistic.index'),
      header: {
        'Content-Type': 'application/json',
        'Accept': 'application/json',
        'Cookie': 'PHPSESSID=' + _wx_token
      },
      method: "get",
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          wxfaceapp.postMsg({
            targetAppid: app.data.accept_message_id,
            content: JSON.stringify(Json),
            success(res) {},
            fail(res) {
              wx.showToast({
                title: res.reply,
                icon: 'none',
                duration: 2000
              });
            }
          });
          wx.redirectTo({
            url: "/packageC/frogpro/waitingPayment/waitingPayment?money=" + money
          });
        } else if (res.result == 0 && res.msg === '请登录') {
          wx.redirectTo({
            url: '/packageC/frogpro/backScreenlogin/backScreenlogin'
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 1500
          });
        }
        wx.hideLoading();
      },
      fail: function(res) {
        wx.hideLoading();
        wx.showToast({
          title: res,
          icon: 'none',
          duration: 1500
        });
      }
    });
  }
});
