// 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: '余额'
  },

  /**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function(options) {
    console.log(options.balance);
    if(options.balance){
      this.setData({
        money:options.balance
      });
    }
    if(options.name == 'store'){
      console.log('门店独立余额充值');
    }else{
      this.getData(); // 初始化参数
    }
    try {
      // let value = wx.getStorageSync('balance');
      let value = wx.getStorageSync('yz_basic_info');
      value = value.balance || "余额";
      if (value) {
        this.setData({
          balanceLang: value
        });
      }
    } catch (e) {
      console.log(e);
    }
  },

  /**
	 * 生命周期函数--监听页面初次渲染完成
	 */
  onReady: function() {
    let language = wx.getStorageSync('langIndex');
    this.setData({ 'language': language.en});
  },

  /**
	 * 用户点击右上角分享
	 */
  onShareAppMessage: function() {},
  // 初始化参数
  getData() {
    let that = this;
    let urlStr = app.getNetAddresss('finance.balance.balance');
    app._getNetWork({
      url: urlStr,
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            credit2: res.data.credit2,
            buttons: res.data.buttons,
            typename: res.data.typename,
            recharge: res.data.recharge,
            activatDes: res.data.remark
          });
          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)
              });
              console.log(that.data.buttons);
            }
          }
        } 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) {
    let type = e.currentTarget.dataset.val;
    this.setData({
      money: parseFloat(this.data.money),
      pay_type: type
    });
    if (this.data.money <= 0) {
      wx.showToast({
        icon: 'none',
        title: '金额不可低于0',
        duration: 1500
      });
      return;
    }
    let urlStr = app.getNetAddresss('finance.balance.recharge');
    urlStr += '&client_type=2';
    urlStr += '&app_type=wechat';
    urlStr += '&pay_type=' + this.data.pay_type;
    urlStr += '&recharge_money=' + this.data.money;
    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, "balance");
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail:(res)=> {
        console.log(res);
      }
    });
  },
});
