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

jQuery(function ($) {
    //默认搜索，对方户名
    getCustomer(ajaxGetCustomerUrl, statementId, customerNo, $('#remittance_user').html());

    //搜索
    $(document).on('click', '#secrch_submit', function () {
        var keywords = $('#keywords').val();
        var customer_category_id = $('#customer_category_id').val();
        var customer_district_id = $('#customer_district_id').val();
        getCustomer(ajaxGetCustomerUrl, statementId, customerNo, keywords, customer_category_id, customer_district_id);
    });

    $("#keywords").keydown(function (e) {
        if (e.keyCode == 13) {
            $('#secrch_submit').trigger('click');
        }
    });

    //重置
    $(document).on('click', '.reset_search_customer', function () {
        $('#customer-search-form').find(':input').not(':button,:submit,:reset,:hidden').val('').removeAttr('checked').removeAttr('selected');
        $('#secrch_submit').trigger('click');
    });

    //添加关联
    $(document).on('click', '.add_link', function () {
        var customerNo = $(this).data('customer_no');
        var iconType = 'succeed';
        if (!confirm('确定关联？')) {
            return false;
        }
        $.getJSON(ajaxAddLinkUrl, {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
            });
            $('#secrch_submit').trigger('click');
            if (json.status && json.url.length > 0) {
                location.href = json.url;
            }
        });
    });

    //删除关联
    $(document).on('click', '.delete_link', function () {
        var customerNo = $(this).data('customer_no');
        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
            });
            location.reload()
        });
    });

    //获取客户数据
    function getCustomer(url, statement_id, customer_no, name, cate_id, district_id) {
        $.getJSON(url, {statement_id: statement_id, customer_no: customer_no, keywords: name, customer_category_id: cate_id, customer_district_id: district_id}, function (json) {
            $('#customer-list').html(json);
        });
    }
});
