// pages/withdrawal/withdrawal.js
import {
  getMoneyAccountDetail,
} from '../../../../services/index'
let setInter = false;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    money: '',
    active_index: '1'
  },
  // 清除输入框内容
  clear() {
    this.setData({
      money: ''
    })
  },
  // 输入可提现最大值
  all() {
    this.setData({
      money: this.data.can_draw.toFixed(2)
    })
  },
  // 展示按钮背景
  show(e) {
    clearTimeout(setInter);
    setInter = setTimeout(_ => {
      this.setData({
        money: e.detail.value
      })
    }, 300)
  },
  // 改变金额格式
  changeMoney(e) {
    const _num = Number(e.detail.value);
    this.setData({
      money: (_num - _num % 10).toFixed(2)
    })
  },
  // 更改提现方式
  choose(e) {
    const index = e.currentTarget.dataset.index;
    this.setData({
      active_index: index
    })
  },
  // 提现
  withdrawal() {
    const money = Number(this.data.money);
    if (money) {
      wx.navigateTo({
        url: '/pages/withdrawal-result/withdrawal-result',
      })
    }

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

  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    getMoneyAccountDetail({
      user_id: wx.getStorageSync('user_id'),
      user_token: wx.getStorageSync('user_token'),
      member_id: wx.getStorageSync('member_id'),
    }).then(res => {
      if (res.errcode === 0) {
        const moneyBalance = res.data.MpMemberAccountAPI.moneyBalance;
        const can_draw = moneyBalance - moneyBalance % 10;
        this.setData({
          moneyBalance: moneyBalance.toFixed(2),
          can_draw,
        })
      }
    })
  },

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

  },

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

  },

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

  },

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

  },
})