// pages/balance_recharge/balance_recharge.js
const INFO_URL = "plugin.auction.api.prepayment.withdraw.page";
const LOVE_INDEX_URL = "plugin.auction.api.member-prepayment.recharge";
var yz_pay = require("../../../../mycomponent/yz_pay/yz_pay");
var app = getApp();
Page({
  behaviors: [yz_pay],
  /**
	 * 页面的初始数据
	 */
  data: {
    credit2: 0,
    buttons: [],
    typename: '',
    recharge: '',
    ordersn: '',
    money: '',
    // 选择的充值方式
    pay_type: '',
    balanceLang: '余额'
  },

  /**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function(options) {
    // this.getData(); // 初始化参数
  },

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

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

  /**
	 * 用户点击右上角分享
	 */
  onShareAppMessage: function() {},
  // 初始化参数
  getData(){
    var that = this;
    let urlStr = app.getNetAddresss(INFO_URL);
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            value:res.data.balance
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1000
          });
        }
      },
      fail: function (res) {
        console.log(res);
        wx.showToast({
          title: res.msg,
          icon: "none",
          duration: 1000
        });
      }
    });
  },
  getTitle(){
    var that = this;
    let urlStr = app.getNetAddresss(LOVE_INDEX_URL);
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            buttons:res.data.buttons
          });
          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)
              });
            }
          }
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1000
          });
        }
      },
      fail: function (res) {
        console.log(res);
        wx.showToast({
          title: res.msg,
          icon: "none",
          duration: 1000
        });
      }
    });
  },

  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_way: type
    });
    if (this.data.money <= 0) {
      wx.showToast({
        icon: 'none',
        title: '金额不可低于0元',
        duration: 1500
      });
      return;
    }
    let urlStr = app.getNetAddresss('plugin.auction.api.member-prepayment.check-recharge');
    urlStr += '&client_type=2';
    urlStr += '&app_type=wechat';
    urlStr += '&pay_way=' + this.data.pay_way;
    urlStr += '&money=' + this.data.money;
    app._getNetWork({
      url: urlStr,
      success: (resdata)=> {
        var res = resdata.data;
        if (res.result == 1) {
          this.rechargePay(type, res, res.data.ordersn);
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: (res)=> {
        console.log(res);
      }
    });
  },
});
