import {
    getSaleData,
    cashOut
} from '~/api/sale'
import event from '~/mixins/event'
Page({
    behaviors: [event],
    data: {
        allIncome: {},
        money: '',
        errMsg: '',
        activationModal: false,
    },
    onShow() {
        this.getIncome()
    },
    async getIncome() {
        let allIncome = await getSaleData()
        this.setData({
            allIncome
        })
    },
    async withdrawalFun() {
        let money = this.data.money
        if (isNaN(money)) {
            this.setData({
                errMsg: '金额不合法'
            })
            return
        }
        // 判断金额是否超过两位小数
        var decimalCount = (money.split('.')[1] || '').length;
        if (decimalCount > 2) {
            this.setData({
                errMsg: '最多两位小数'
            })
            return
        }
        if (money <= 0) {
            this.setData({
                errMsg: '请输入要提现的金额'
            })
            return
        }
        if (money > this.data.allIncome.withdraw / 100) {
            this.setData({
                errMsg: '可提现金额不足'
            })
            return
        }
        if (money > 500 || money < 50) {
            this.setData({
                errMsg: '提现金额需≥50元，单笔最高可提500元'
            })
            return
        }
        let res = await cashOut({
            amount: money * 100
        })
        this.setData({
            activationModal: true,
        })
        this.getIncome()
    },
    clearMoney() {
        this.setData({
            money: "",
            errMsg: ""
        });
    },
    setMoney({
        detail
    }) {
        this.setData({
            money: detail.value
        });
    },
    jump({
        currentTarget
    }) {
        wx.navigateTo({
            url: `/salesperson/pages/${currentTarget.dataset.url}/index`,
        })
    },
    closeModal() {
        this.setData({
            activationModal: false
        })
    },
})