import { baseUrl } from "../../../request";

// pages/personCenter/withdrawal/withdrawal.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    currentBalance: 0,  // 当前可提现余额
    withdrawalAccount: '',  // 提现账户信息
    withdrawnAmount: '',  // 用户输入的提现金额
  },

  // 获取当前账户信息
  getAccountInfo() {
    wx.request({
      url: baseUrl + '/withdrawalApply/query/condition', // 替换为你的后端接口
      method: 'POST',
      header: {
        Authorization: wx.getStorageSync('token'),
      },
      success: (res) => {
        console.log('当前账户是---->', res.data.data);
        if (res.data.code === 1) {
          let result = res.data.data;
          this.setData({
            currentBalance: result.currentBalance.toFixed(2),
            withdrawalAccount: result.bankCardNumber
          });
        } else {
          wx.showToast({
            title: '获取数据失败',
            icon: 'none'
          });
        }
      },
      fail: () => {
        wx.showToast({
          title: '请求失败',
          icon: 'none'
        });
      }
    });
  },

  // 输入框内容变化时更新数据
  onInput(e) {
    this.setData({
      withdrawnAmount: e.detail.value,  // 获取用户输入的金额
    });
  },

  // 点击“全部提现”，填充当前可提现余额到输入框
  onWithdrawAll() {
    this.setData({
      withdrawnAmount: this.data.currentBalance,  // 将当前余额填入提现金额输入框
    });
  },

  // 点击“立即提现”，发送请求
  onSubmitWithdraw() {
    const { withdrawnAmount, currentBalance } = this.data;

      // 校验提现金额是否大于当前可提现余额
      if (!withdrawnAmount) {
        wx.showToast({
          title: '请输入提现金额',
          icon: 'none'
        });
        return;
      }

    // 校验提现金额是否大于当前可提现余额
    if (parseFloat(withdrawnAmount) > parseFloat(currentBalance)) {
      wx.showToast({
        title: '提现金额不可大于余额',
        icon: 'none'
      });
      return;
    }

    // 校验提现金额是否小于1元
    if (parseFloat(withdrawnAmount) < 1) {
      wx.showToast({
        title: '最低提现金额为1元',
        icon: 'none'
      });
      return;
    }

    // 发送提现请求
    wx.request({
      url: baseUrl + '/withdrawalApply/add',  // 后端提现请求路径
      method: 'POST',
      header: {
        Authorization: wx.getStorageSync('token'),  // 请求头中加入token
      },
      data: {
        withdrawnAmount: withdrawnAmount  // 请求体携带提现金额
      },
      success: (res) => {
        if (res.data.code === 1) {
          wx.showToast({
            title: '提现成功，等待后台审核',
            icon: 'none',
            duration: 1000,
          });
          // 1秒后跳转到账单详情页面
          setTimeout(() => {
            wx.redirectTo({
              url: '/pages/personCenter/billingDetails/billingDetails',
            });
          }, 1000);  // 延时1秒跳转
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none'
          });
        }
      },
      fail: () => {
        wx.showToast({
          title: '请求失败，请稍后再试',
          icon: 'none'
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getAccountInfo();  // 加载页面时获取账户信息
  },

  /**
   * 跳转到添加提现账户页面
   */
  goToAddAccount() {
    wx.navigateTo({
      url: '/pages/personCenter/bindBankCard/bindBankCard',  // 自定义的添加提现账户页面
    });
  },

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

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

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

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

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

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

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