import Vue from 'vue'
import lib from 'lib'

let uid = lib.getParam("uid"),token = lib.getParam("token")

export default {
    data() {
        return {
            winHeight : 0,
            money : 0,
            zfbName : "",
            zfbAccount : "",
            phone : "",
            uid : lib.getParam("uid"),
            token : lib.getParam("token"),
            applyId : lib.getParam("applyId")
        };
    },
    mounted () {
        lib.setTitle("申请提现");

        this.winHeight = $(window).height()
        this.getApplyWithdraw()
    },
    methods: {
        removeError: function(){
            this.isMoneyError = false,this.isNameError = false,this.isAccountError = false,this.isPhoneError = false
        },
        checkForm : function(){
            let moneyReg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/
            let phoneReg = /^[0-9]{11}$/

            if(!moneyReg.test(this.money)) {
                lib.showTip("提现金额格式不正确")
                return
            }
            if(!this.zfbName) {
                lib.showTip("请输入真实姓名")
                return
            }
            if(!this.zfbAccount) {
                lib.showTip("请输入支付宝账号")
                return
            }
            if(!phoneReg.test(this.phone)) {
                lib.showTip("联系电话格式不正确")
                return
            }

            this.updateApplyWithdraw()
        },
        //获取提现信息
        getApplyWithdraw : function(){
            let self = this
            let url = `${lib.apiUrl}/share/getApplyWithdraw.do`
            let param = {
                channel : "LuciferChannel",
                ver : 1,
                os : 1,
                uid : uid || 1,
                token : token || "lucifer_test_token",
                applyId : self.applyId
            }

            $.ajax({
                type: "get",
                url: url,
                data: param,
                dataType: "jsonp",

                success: function(ret) {
                    if (ret.code==0) {
                        let data = ret.data

                        self.money = data.money/100
                        self.phone = data.phone
                        self.zfbAccount = data.zfbAccount
                        self.zfbName = data.zfbName
                    } else {
                        lib.showTip(ret.msg)
                    }
                }
            });
        },
        //更新提现信息
        updateApplyWithdraw : function(){
            let self = this
            let url = `${lib.apiUrl}/share/updateApplyWithdraw.do`
            let param = {
                channel : "LuciferChannel",
                ver : 1,
                os : 1,
                applyId : self.applyId,
                uid : uid || 1,
                token : token || "lucifer_test_token",
                zfbName : self.zfbName,
                zfbAccount : self.zfbAccount,
                phone : self.phone
            }

            $.ajax({
                type: "get",
                url: url,
                data: param,
                dataType: "jsonp",

                success: function(ret) {
                    if (ret.code==0) {
                        lib.showTip("提交成功")
                        setTimeout(function() {
                            location.replace(`/?page=answer-cashrecord&uid=${uid}&token=${token}`)
                        }, 1000);
                    } else {
                        lib.showTip(ret.msg)
                    }
                }
            });
        }
    }
};