// packagecard/purse/takecash.js
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        selType: '1',
        accountInfo: {},
        takeValue: 0, //提现的金额
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getAccount()
    },
    //获取账户信息
    getAccount() {
        var _self = this
        wx.showLoading({
            title: '加载中...',
        })
        app.http.get(app.urls.getMyAccount, {
                header: {
                    token: app.globalData.token
                }
            })
            .then(res => {
                wx.hideLoading({})
                _self.setData({
                    accountInfo: res.data
                })
            })
            .catch(err => {
                console.log(err)
            })
    },
    //全部提现
    takeAll() {
        this.setData({
            takeValue: this.data.accountInfo.accountMoney / 100
        })
    },
    //监听输入框
    inputWatch(e) {
        this.setData({
            takeValue: e.detail.value
        })
    },
    //提交提现申请
    doCash(e) {
        //判断输入的值是否大于账户中的数量
        var _self = this
        if (_self.data.takeValue == 0) {
            wx.showToast({
                title: '请输入提现金额',
                icon: 'error'
            })
            return
        }
        if (_self.data.takeValue < 1) {
            wx.showToast({
                title: '最低提现1元',
                icon: 'error'
            })
            return
        }
        if (_self.data.takeValue > (_self.data.accountInfo.accountMoney / 100)) {
            wx.showToast({
                title: '超过零钱余额',
                icon: 'error'
            })
            return
        }
        //提现
        _self.toCash()
    },
    toCash() {
        var _self = this
        wx.showLoading({
            title: '申请中...',
        })
        app.http.post(app.urls.doTakeCash, {
                header: {
                    token: app.globalData.token
                },
                data: {
                    accountWithdrawMoney: _self.data.takeValue * 100
                }
            })
            .then(res => {

                wx.hideLoading({})
                wx.showToast({
                    title: '申请成功',
                    icon: 'success',
                    success(res) {
                        setTimeout(() => {
                            wx.navigateBack({})
                        }, 800)
                    }
                })
            })
            .catch(err => {
                console.log(err)
            })
    },
    //提现记录
    toRecord() {
        wx.navigateTo({
            url: '/packagecard/purse/recordlist',
        })
    }
})