Page({

    /**
     * 页面的初始数据
     */
    data: {
        banks       : [],
        type        : 1,
        bankVal 	: 0,
        isDisabled	: false,
        balance     : 0,
        greater     : false,
        rate        : 0,
        min         : 0,
        name        : '',
        idcard      : '',
        identity    : {
            name		: '',
			id_card_no	: '' 
        },
        bankNo      : '',
        mobile      : '',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad() {
        wx.$api.withdraw.create().then(res => {
            let { bank, tax, min, balance, banks, id_card, has_bank} = res.data;
            let banksArr = [{ id: '', name: '请选择开户银行'}].concat(banks)
            this.setData({
                banks       : banksArr,
                balance     : balance,
                rate        : tax,
                min         : min,
                name        : id_card.name,
                idcard      : id_card.id_card_no,
                identity    : id_card,
                isDisabled	: has_bank
            })
            if(has_bank){
                let bankIndex = banksArr.findIndex(val => val.name === bank.bank_name)
                this.setData({
                    type	: bank.message_type,
                    bankNo  : bank.bank_no,
                    mobile  : bank.mobile,
                    bankVal : bankIndex >= 0 ? bankIndex: 0
                })
            }
        })
    },
    /**
     * 选择提现类型
     */
    onTypeTab(e){
        let { type } = e.currentTarget.dataset
        if(type == this.data.type) return
        if(this.data.isDisabled){
            wx.showToast({
                title: "银行卡信息已绑定，如需变更请联系系统管理员",
                icon : "none"
            })
            return
        }
        this.setData({
            bankNo  : '',
            mobile  : '',
            bankVal : 0
        })
        if(type == 1){
            this.setData({
                name    : this.data.identity.name
            })
        }else{
            this.setData({
                name    : '',
                idcard  : ''
            })
        }
        this.setData({
            type
        })
    },
    /**
     * 选择开户银行
     */
    bankChange(e){
        let { value } = e.detail;
        this.setData({
            bankVal: value
        })
    },
    /**
     * 全部提现
     */
    onAll(){
        this.setData({
            amount: this.data.balance
        })
    },
    /**
     * 输入提现金额
     */
    onInput(e){
        let { value } = e.detail;
        this.setData({
            greater: parseFloat(value) > parseFloat(this.data.balance)
        })
    },
    /**
     * 提现
     */
    onSubmit(e){
        if(this.data.bankVal == 0){
            wx.showToast({
                title: '请选择开户银行',
                icon : 'none'
            })
            return
        }
        wx.showLoading({
            title: '提交中...',
            mask : true
        })
        let valueObj = e.detail.value
        let data = {
            bank_name	: this.data.banks[this.data.bankVal].name,
            amount		: valueObj.amount,
            name  		: valueObj.name,
            mobileNo	: valueObj.mobile,
            bank_no		: valueObj.bankNo,
            id_card		: this.data.type == 1 ? this.data.identity.id_card_no : valueObj.idcard || '',
            message_type: this.data.type,
        }
        wx.$api.withdraw.withdraws(data).then(res => {
            wx.hideLoading()
            wx.showModal({
                title         : '提示',
                content       : res.data,
                showCancel	  : false,
                confirmColor  : '#da2b54',
                success       : ModalRes => {
                    if(ModalRes.confirm){
                        wx.navigateBack()
                    }
                }
            })
        }).catch(err => {})
    }
})