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

  /**
   * 页面的初始数据
   */
  data: {
    transferID: "",
    transferIntegral: "",
    integral: 0,
    name: "",
    isclick:true
  },

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

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

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

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

  },

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

  },

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

  },

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

  },

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

  },
  // 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({
      transferID: val.detail
    });
  },
  onChange1(val) {
    var reg = /^[\+\-]?\d*?\.?\d*?$/;
    console.log(val.detail, '值');

    if (!reg.test(val.detail)) {
      wx.showToast({
        title: '只能输入数字',
        icon: 'none'
      });
      this.setData({
        transferIntegral: ''
      });
      return;
    }
    this.setData({
      transferIntegral: 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")
    });
  },
  getName() {
    let urlStr = app.getNetAddresss("plugin.integral.Frontend.Controllers.page.get-name");
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        if (res.data.result === 1) {
          this.setData({
            name: res.data.data
          });
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none"
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getData() {
    let urlStr = app.getNetAddresss("plugin.integral.Frontend.Controllers.Page.index");
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        if (res.data.result === 1) {
          console.log(res.data.data.member_integral,'数据的强');
          
          this.setData({
            transferID: '',
            transferIntegral: '',
            integral: res.data.data.member_integral
          });
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none"
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  sureTransfer() {
    if(!this.data.isclick){
      wx.showToast({
        title: '请勿重复点击',
        icon: 'none'
      });
      return;			
    }
    this.data.isclick = false;
    if (!this.data.transferIntegral) {
      wx.showToast({
        title: "请输入转让数额",
        icon: 'none'
      });
      this.data.isclick = true;
      return;
    }
    if (Number(this.data.transferIntegral) > Number(this.data.integral)) {
      wx.showToast({
        title: '输入的提现数额超过已有的' + this.data.name,
        icon: 'none'
      });
      this.data.isclick = true;
      return;
    }
    let urlStr = app.getNetAddresss("plugin.integral.Frontend.Modules.Integral.Controllers.Transfer");
    urlStr += "&change_value=" + this.data.transferIntegral;
    urlStr += "&recipient_id=" + this.data.transferID;
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        if (res.data.result === 1) {
          this.getData();
          wx.showToast({
            title: '转让成功',
            icon: 'none'
          });
          setTimeout(() => {
            wx.navigateTo({
              url: '/packageB/member/Integral_love_detail/Integral_love_detail',
            });
          }, 1000);
        } else {
          this.data.isclick = true;
          wx.showToast({
            title: res.data.msg,
            icon: "none"
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  }
});