// pages/withdrawal_form/withdrawal_form.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        balance     : '',   //余额
        tax         : '',   //手续费
        amount      : '',   //提现金额
        disabled    : false //按钮状态
    },

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

    /**
     * 提现前置账户信息
     */
    withdrawsInfo() {
        wx.$api.user.withdraws().then(res=>{
            this.setData({
                balance: res.data.balance,
                tax    : res.data.tax
            })
           
        }).catch(err=>{})
    },

    /**
     * 获取提现金额
     */
    getAmount (val) {
        var money;
        if (/^(\d?)+(\.\d{0,1})?$/.test(val.detail.value)) { //正则验证，提现金额小数点后不能大于两位数字
            money = val.detail.value;
        } else {
            money = val.detail.value.substring(0, val.detail.value.length - 1);
        }
        this.setData({
            amount: money
        })
    },

    /**
     * 提现表单填写
     */
    formSubmit(e) {
        let newAmount = this.data.amount
        if(newAmount > 1) {
            wx.$api.user.withdrawsForm(newAmount, "mini").then(res=>{
                this.setData({
                    disabled: true
                })
    
                wx.showToast({
                    title: '提现申请提交成功',
                })
    
                setTimeout(()=>{
                    wx.redirectTo({
                        url: "/pages/withdrawal_record/withdrawal_record?status=''&idx=0"
                    })
                },2000)
               
            }).catch(err=>{})
        }else {
            wx.showToast({
                title: '金额不得低于2元',
                icon: 'none'
            })
        }
    }
})