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

// pages/personCenter/fundingDetails/fundingDetails.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentBalance: 0,  // 当前余额
    withdrawalAmount: 0,  // 提现中的余额
    withdrawnAmount: 0,  // 已提现的金额
    fundsChangeVOList: []  // 资金变动明细记录
  },

  // 跳转绑定银行卡页面
  gotoBindCard() {
    wx.navigateTo({
      url: `/pages/personCenter/bindBankCard/bindBankCard?isUpdate=${true}`,
    })
  },

  // 跳转去提现
  gotoFundingDetails() {
    wx.navigateTo({
      url: '/pages/personCenter/withdrawal/withdrawal',
    })
  },

  // 获取资金变动记录
  getFundChangeList() {
    wx.request({
      url: baseUrl + '/withdrawalApply/query/change',
      method: 'POST',
      header: {
        Authorization: wx.getStorageSync('token')
      },
      success: res => {
        console.log('后端返回---->',res);
        if (res.data.data === null) {
          wx.showModal({
            title: '账户为空',
            content: '请新建账户',
            complete: (res) => {
              if (res.cancel) {  // 点击取消时
                wx.navigateTo({
                  url: '/pages/personCenter/mine/mine',
                })
              }
          
              if (res.confirm) {  //  点击确定时
                wx.navigateTo({
                  url: '/pages/personCenter/bindBankCard/bindBankCard',
                })
              }
            }
          })
        }
        if (res.data.code === 1) {
          let result = res.data.data
          this.setData({
            currentBalance: result.currentBalance,
            withdrawalAmount: result.withdrawalAmount,
            withdrawnAmount: result.withdrawnAmount,
            fundsChangeVOList: result.fundsChangeVOList
          });
        }
      }
    })
  },

  gotoBillingDetails() {
    wx.navigateTo({
      url: '/pages/personCenter/billingDetails/billingDetails',
    })
  },

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

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

  },

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

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

  },

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

  },

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

  },

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

  },

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

  }
})