// pages/balance_recharge/balance_recharge.js
var yz_pay = require("../../../mycomponent/yz_pay/yz_pay");
var app = getApp();
Page({
  behaviors: [yz_pay],
  /**
	 * 页面的初始数据
	 */
  data: {
    language: '',
    credit2: 0,
    buttons: [],
    typename: '',
    recharge: '',
    ordersn: '',
    money: '',
    activatDes: {},
    // 选择的充值方式
    pay_type: '',
    balanceLang: '余额',
    proportion_status:0,
    payMuch:'',
    info:[],
    popshow:false
  },

  /**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function(options) {
    if(options.store_id){
      this.setData({
        store_id:options.store_id
      });
    }
  },
  onShow:function(){
    this.getStoreBalance();
  },
  selectSpecs(e){
    let val = e.detail;
    for( let key in this.data.buttons){
      if(this.data.buttons[key].value == val){
        this.setData({
          radio:this.data.buttons[key].value,
          methodsArr : this.data.buttons[key]
        });
      }
    }
  },
  /**
	 * 生命周期函数--监听页面初次渲染完成
	 */
  onReady: function() {
    let language = wx.getStorageSync('langIndex');
    this.setData({ 'language': language.en});
  },

  /**
	 * 用户点击右上角分享
	 */
  onShareAppMessage: function() {},
  // 初始化参数
  getStoreBalance() {
    let that = this;
    let urlStr = app.getNetAddresss('plugin.store-cashier.frontend.store.balance.balance.index');
    app._getNetWork({
      url: urlStr,
      data:{
        store_id:this.data.store_id
      },
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            credit2: res.data.member_balance,
            show_activity:res.data.show_activity,
            buttons: res.data.pay_buttons,
            title: res.data.title,
            activatDes: res.data.activity,
            proportion_status:res.data.proportion_status,
            info:res.data,
            is_open_recharge_order:res.data.is_open_recharge_order == 0?false:true,
            store:res.data.store
          });
          for (let i = 0; i < that.data.buttons.length; i++) {
            if (that.data.buttons[i]) {
              that.setData({
                ['buttons[' + i + '].btclass']: that.btnclass(that.data.buttons[i].value)
              });
            }
          }
          for( let key in that.data.buttons){
            that.setData({
              radio:that.data.buttons[key].value,
              methodsArr : that.data.buttons[key]
            });
            return;
          }
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  moneyinp(e) {
    let val = e.detail.value;
    this.setData({
      money: val
    });
  },
  // 确认充值
  confirm(e) {
    console.log(this.data.methodsArr.value);
    let type = this.data.methodsArr.value;
    this.setData({
      money: this.data.payMuch,
      pay_type: type
    });
    let urlStr = app.getNetAddresss('plugin.store-cashier.frontend.store.balance.balance.recharge');
    urlStr += '&client_type=2';
    urlStr += '&app_type=wechat';
    urlStr += '&pay_type_id=' + this.data.pay_type;
    urlStr += '&recharge_money=' + this.data.money;
    urlStr += '&store_id=' + this.data.store_id;
    app._getNetWork({
      url: urlStr,
      success: (resdata)=> {
        var res = resdata.data;
        if (res.result == 1) {
          if (app._isTextEmpty(res.data.ordersn)) {
            wx.showToast({
              icon: 'none',
              title: '参数错误',
              duration: 1500
            });
            return;
          }
          this.setData({
            ordersn: res.data.ordersn
          });
          this.rechargePay(type, res, res.data.ordersn);
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail:(res)=> {
        console.log(res);
      }
    });
  },
  // 键盘
  //处理按键
  _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() {
    if(this.data.store){
      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,
      });
      if(this.data.is_open_recharge_order){
        this.setData({
          popshow:true
        });
      }else{
        this.confirmOrder();
      }
    }
  },
  confirmOrder() {
    var val = this.data.payMuch;
    if (val && val > 0) {
      this.setData({
        payPrice: val,
        confirmOrderStatus: true,
        isdisabled: true,
      });
      // this.preGoodsBuy();
      this.confirm();
    } else {
      wx.showToast({
        icon: "none",
        title: "请输入正确的付款金额!",
        duration: 1500,
      });
      this.setData({
        confirmOrderStatus: false,
        isdisabled: false,
      });
    }
  },
  //处理数字
  _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,
        });
      }
    }
  },
  showOffMethod(e){
    this.setData({
      popshow:e.detail.show1
    });
  }
});
