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

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

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

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

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

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

  },

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

  },

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

  },

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

  },

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

  },
  //获取数据
  getName() {
    let urlStr = app.getNetAddresss("plugin.integral.Frontend.Controllers.page.get-name");
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        let resData = res.data;
        if (resData.result === 1) {
          this.setData({
            name: resData.data
          });

        } else {
          wx.showToast({
            title: resData.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) => {
        let resData = res.data;
        if (resData.result === 1) {
          this.setData({
            integral: resData.data.member_integral,
            cash: ""
          });

        } else {
          wx.showToast({
            title: resData.msg,
            icon: "none"
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  sureCash() {
    if(!this.data.isclick){
      wx.showToast({
        title: '请勿重复点击',
        icon: 'none'
      });
      return;			
    }
    this.data.isclick = false;
    if (!this.data.cash) {
      wx.showToast({
        title: '请输入提现数额',
        icon: 'none'
      });
      this.data.isclick = true;
      return;
    }
    if (Number(this.data.cash) > 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.Withdraw");
    urlStr += '&change_value=' + this.data.cash;
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        console.log(res, '数据');
        if (res.data.result === 1) {
          this.getData();
          wx.navigateTo({
            url: '/packageA/member/withdrawal/withdrawal',
          });
        } else {
          this.data.isclick = true;
          wx.showToast({
            title: res.data.msg,
            icon: "none"
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  // 输入框改变的时候触发
  onChange(val) {
    this.setData({
      cash: val.detail
    });
  },
});
