/**
 * 财务进账关联合同
 */

jQuery(function ($) {
    //正整型数据判断
    var intType = "^[0-9]*[1-9][0-9]*$";
    var floatType = "^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$";
    var intReg = new RegExp(intType);
    var floatReg = new RegExp(floatType);
    var unlockSubmit = 1;//避免重复提交，默认可以提交


    //添加关联合同金额
    $(document).on('click', '.add-link-contract', function () {
        var ch_contract = [];
        var total = 0;
        var length = $(".box-link:checked").length;
        var iconType = 'error';
        if (unlockSubmit == 0) {
            art.dialog({
                title: "提示",
                time: 1.5,
                icon: 'error',
                content: "请勿重复提交！"
            });
            return false;
        }
        if (length == 0) {
            art.dialog({
                title: "提示",
                time: 1.5,
                icon: 'error',
                content: "请至少选择一项"
            });
            return false;
        }
        $('input[name="link[]"]:checked').each(function () {
            var contract = $(this).val();
            var money = $("input[name='" + contract + "']").val();

            if (money.match(floatReg) !== null) {
                ch_contract.push(contract + "@" + $("input[name='" + contract + "']").val());
                total += parseFloat(money);
            }
        });
        if (remainingMoney <= 0) {
            art.dialog({
                title: "提示",
                time: 1.5,
                icon: 'error',
                content: "账单金额已全部分配！"
            });
            return false;
        }
        if (total > remainingMoney) {
            art.dialog({
                title: "金额不合法",
                time: 1.5,
                icon: 'error',
                content: "所选总金额" + total + "，大于未分配金额！"
            });
            return false;
        }
        if (total <= 0) {
            art.dialog({
                title: "金额不合法",
                time: 1.5,
                icon: 'error',
                content: "所选总金额" + total + "，请重新分配！"
            });
            return false;
        }
        ch_contract.join(',');
        if (!confirm('确定关联？')) {
            return false;
        }
        unlockSubmit = 0;
        $.getJSON(linkContractUrl, {statement_id: statementId, contract_money: ch_contract}, function (json) {
            unlockSubmit = 1;
            if (json.status) {
                iconType = 'succeed'
            }
            art.dialog({
                title: "提示",
                time: 1.5,
                icon: iconType,
                content: json.info
            });
            location.reload();
        });
    });


    /**
     *校正金额有效性
     */
    $(document).on('change', '.link-money', function () {
        var money = $(this).val();
        if (money.match(floatReg) == null) {//输入非数字，则重置为0
            $(this).val(0);
        }
    });

//删除关联
    $(document).on('click', '#delete_link_customer', function () {
        var iconType = 'succeed';
        if (!confirm('确定解除关联？解除关联的同时会删除原有金额分配，以及对应回款记录！')) {
            return false;
        }
        $.getJSON(ajaxDeleteLinkUrl, {statement_id: statementId, customer_no: customerNo}, function (json) {
            if (json.status == 0) {
                iconType = 'error';
            }
            art.dialog({
                title: "提示",
                time: 1.5,
                icon: iconType,
                content: json.info
            });
            if (json.status && json.url.length > 0) {
                location.href = json.url;
            }
        });
    });


});
