// packageE/extension/dealer_award/peerAwardes_withdraw/peerAwardes_withdraw.js
var app = getApp();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    language: "",
    cash: "",
    withdraw_amount: "",
    withdraw_limit: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let language = wx.getStorageSync("langIndex");
    this.setData({ language: language.en });
    this.getData();
  },

  getData() {
    let that = this;
    let urlStr = app.getNetAddresss(
      "plugin.team-sideways.frontend.withdraw.index"
    );
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        if (res.data.result === 1) {
          that.setData({
            withdraw_amount: res.data.data.withdraw_amount || 0,
            withdraw_limit: res.data.data.withdraw_limit,
          });
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none",
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  cashBtn(e) {
    let val = e.detail.value;
    this.setData({
      cash: val,
    });
  },
  sureCash() {
    if (Number(this.data.withdraw_amount) < Number(this.data.withdraw_limit)) {
      wx.showToast({
        icon: "none",
        title: "提现金额不足",
        duration: 1000,
      });
      return;
    }
    if (Number(this.data.cash) > Number(this.data.withdraw_amount)) {
      wx.showToast({
        icon: "none",
        title: "提现金额超出",
        duration: 1000,
      });
      return;
    }
    if (
      this.data.withdraw_limit &&
      Number(this.data.cash) < Number(this.data.withdraw_limit)
    ) {
      wx.showToast({
        icon: "none",
        title: `提现金额不小于${this.data.withdraw_limit}`,
        duration: 1500,
      });
      return;
    }
    if (Number(this.data.cash) < 0) {
      wx.showToast({
        icon: "none",
        title: "输入的提现数额需大于0",
        duration: 1500,
      });
      return;
    }
    // var that = this;
    let urlStr = app.getNetAddresss(
      "plugin.team-sideways.frontend.withdraw.handle"
    );
    app._getNetWork({
      url: urlStr,
      data: {
        amount: Number(this.data.cash).toFixed(2),
      },
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          wx.showToast({
            icon: "none",
            title: "提现成功",
            duration: 1500,
          });
          wx.navigateBack({
            //返回
            delta: 1,
          });
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
    });
  },

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

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

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

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

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

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

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