var isSendRequest = false;
var messageInfo = "对不起，请稍后刷新重试!";
var withdrawMoneyCount = 0;
var userMoney = 0;
var maxWithdrawMoney = 0;
mui.ready(function () {
    applyBofore();
    mui('body').on('click', '#submit-apply-btn', function () {
        if (isSendRequest) {
            var username = $("#username").val();
            $.trim(username);
            var re1 = new RegExp("^([\u4E00-\uFA29]|[\uE7C7-\uE7F3]|[a-zA-Z0-9]){1,30}$");
            if (!re1.test(username)) {
                mui.alert("请输入中文名称~");
                return false
            }
            var userTel = $("#user-tel").val();
            if (!(/^1[3|4|5|7|8]\d{9}$/.test(userTel))) {
                mui.alert("手机号码有误，请重填");
                return false
            }
            var userCashNum = $("#user-cash-num").val();
            var r = /^[1-9]+\d*$/;
            if (!r.test(userCashNum)) {
                mui.alert("请输入合法的金额!");
                return false
            }
            if (parseInt(userCashNum) >= userMoney) {
                mui.alert("请输入提现数量不大于等于您的可用金额的值!");
                return false
            }
            var bankNo = $("#bank-name").val();
            if (!(/^(\d{16}|\d{19})$/.test(bankNo))) {
                mui.alert("请输入正确的银行卡号!");
                return false
            }
            var bankch = $("#bankch-name").val();
            if (!re1.test(bankch)) {
                mui.alert("请输入中文支行名称~");
                return false
            }
            isSendRequest = false;
            messageInfo = "正在请求提现中...";
            var paramArgs = {
                "username": username,
                "tel": userTel,
                "integralNum": userCashNum,
                "bankName": bankNo,
                "branch": bankch
            };
            mui.ajax(base_path + '/withdraw/applyCash', {
                data: paramArgs,
                dataType: 'json',
                async: false,
                type: 'POST',
                timeout: 150000,
                success: function (dt) {
                    if (dt.status) {
                        isSendRequest = true;
                        window.location.href = base_path + "/withdraw/success"
                    } else {
                        isSendRequest = true;
                        mui.alert(dt.message)
                    }
                },
                error: function (xhr, type, errorThrown) {
                    errorfn(xhr)
                }
            })
        } else {
            mui.alert(messageInfo)
        }
    });
    getCartNum();
    document.querySelector('.loading-bg').style.display = 'none'
});
var userInputMoney = 0;
function cashChange() {
    var cashNum = $("#user-cash-num").val();
    $.trim(cashNum);
    var r = /^[1-9]+\d*$/;
    if (!r.test(cashNum)) {
        $("#user-cash-num").val(userInputMoney);
        return false
    }
    if (parseInt(cashNum) >= userMoney) {
        $("#user-cash-num").val(userMoney);
        return false
    }
    userInputMoney = cashNum
};function applyBofore() {
    mui.ajax(base_path + '/withdraw/applyCashBefore', {
        dataType: 'json',
        type: 'POST',
        timeout: 15000,
        success: function (dt) {
            if (dt.status) {
                isSendRequest = true;
                var dataObj = dt.data;
                minWithdrawMoneyAmount = accDiv(dataObj.minWithdrawMoneyAmount, 100);
                userMoney = accDiv(dataObj.userMoney, 100);
                $("#show-title-span").html("<span class='cl-red'>温馨提示：</span>金额提现每次不能低于<span class='cl-red'>" + minWithdrawMoneyAmount + "</span>元！每月<span class='cl-red'>" + dataObj.withdrawMoneyCount + "</span>次提现机会。以每月15日为分界线，在15日之前申请提现的，将会于15-20日到账；若当月15日后申请提现的，将会于下月的15日-20日到账，如遇法定假期将顺延。");
                $("#user-info-span").html("<span class='cl-red'>温馨提示：</span>当前最大可提现金额<span class='cl-red'>" + userMoney + "</span>元");
                var Withdraw = dataObj.Withdraw;
                $("#username").val(Withdraw.withdrawalsUsername);
                $("#user-tel").val(Withdraw.withdrawalsTel);
                $("#bank-name").val(Withdraw.withdrawalsCardNo);
                $("#bankch-name").val(Withdraw.withdrawalsBankName);
            } else {
                messageInfo = dt.message;
                mui.alert(dt.message)
            }
        },
        error: function (xhr, type, errorThrown) {
            console.log(xhr)
        }
    })
};