// pages/member/rankingListSecond/rankingListSecond.js
var app = getApp();
Page({

  /**
	 * 页面的初始数据
	 */
  data: {
    language: '',
    //华侨币账值
    recharge_value: undefined,
    //华侨币ID
    recharge_id: undefined,
    // 登陆会员可用爱心值
    usable: 0,
    // 转让手续费比率
    ratio: 0,
    // 实际到账数
    arrival_count: 0,
    coin_name: "" //华侨币自定义名称
  },

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

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

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },
  getUsable() {
    let urlStr = app.getNetAddresss("plugin.coin.Frontend.Controllers.page.index");
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        if (res.data.result === 1) {
          this.setData({
            usable: res.data.data.usable,
            coin_name: res.data.data.coin_name,
            ratio: res.data.data.transfer_proportion
          });
          wx.setNavigationBarTitle({
            title: res.data.data.coin_name ? (res.data.data.coin_name + '转账') : '转账'
          });
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none"
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },

  getBalance() {
    //recipient 被转让者ID
    //change_value 转让爱心值
    if (!this.data.recharge_value) {
      wx.showToast({
        title: '转账值不能为空',
        icon: 'none'
      });
      return;
    }
    let urlStr = app.getNetAddresss("plugin.coin.Frontend.Modules.Coin.Controllers.transfer.index");
    urlStr += "&recipient=" + this.data.recharge_id;
    urlStr += "&change_value=" + this.data.recharge_value;
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        if (res.data.result === 1) {
          wx.showToast({
            title: res.data.msg,
            icon: "none"
          });
          this.setData({
            recharge_value: '',
            recharge_search: ""
          });
          setTimeout(() => {
            this.getUsable();
            // 返回上一页
            wx.navigateBack({
              delta: 1
            });
          }, 1000);

        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none"
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  // id输入
  onChange(val) {
    var reg = /^[\+\-]?\d*?\.?\d*?$/;
    console.log(val.detail, '值');

    if (!reg.test(val.detail)) {
      wx.showToast({
        title: '只能输入数字',
        icon: 'none'
      });
      this.setData({
        recharge_id: ''
      });
      return;
    }
    this.setData({
      recharge_id: val.detail
    });
  },
  // 值输入的时候
  onChange2(val) {
    this.setData({
      recharge_value: val.detail
    });
    let num = this.data.recharge_value - this.data.recharge_value * (this.data.ratio / 100);
    num = num.toString();
    this.setData({
      arrival_count: num.replace(/([0-9]+.[0-9]{2})[0-9]*/, "$1")
    });
  }
});
