var purchaseInvoicePayment_form = {
    settings: {
        bank_autocomplete_url: null,
        slAccount_autocomplete_url: null,
        vatRate: 0,
        whTaxRate: 0
    },
    init: function() {
        purchaseInvoicePayment_form.bindAutocomplete();
        purchaseInvoicePayment_form.bindElements();
        purchaseInvoicePayment_form.computeAmounts();
    },
    bindAutocomplete: function() {
        global.autocomplete.bind(
            purchaseInvoicePayment_form.settings.bank_autocomplete_url,
            '#purchaseInvoicePayment_bank_description',
            '#purchaseInvoicePayment_bank'
        );
        jQ('.txt-dr-sl-desc').each(function(){
            const idx = jQ(this).data('idx');
            global.autocomplete.bind(
                purchaseInvoicePayment_form.settings.slAccount_autocomplete_url,
                'input[name="otherForm[drAccounts][' + idx + '][slDesc]"]',
                'input[name="otherForm[drAccounts][' + idx + '][slId]"]', {
                    onSelect: purchaseInvoicePayment_form.computeAmounts,
                    checkEmpty: purchaseInvoicePayment_form.computeAmounts
                }
            );
        });
    },
    bindElements: function() {
        jQ('#purchaseInvoicePayment_payment_method').on('change', function(){
            if('Check' === jQ(this).val()) {
                jQ('.check-type-fields').removeClass('hide');
            } else {
                jQ('.check-type-fields').addClass('hide');
            }
        });
        jQ('#href-add-dr-acct').unbind('click').bind('click', function(){
            const newIdx = jQ('.txt-dr-sl-desc').length,
                divRow = jQ('<div/>').attr({class: 'row max-width-initial'}),
                divCol1 = jQ('<div/>').attr({class: 'large-8 columns'}),
                divCol2 = jQ('<div/>').attr({class: 'large-4 columns'}),
                hdnId = jQ('<input/>').attr({type: 'hidden', name: 'otherForm[drAccounts][' + newIdx + '][slId]'}),
                txtDesc = jQ('<input/>').attr({type: 'text', name: 'otherForm[drAccounts][' + newIdx + '][slDesc]', class: 'txt-dr-sl-desc'}),
                txtAmt = jQ('<input/>').attr({type: 'text', name: 'otherForm[drAccounts][' + newIdx + '][amt]', class: 'txt-dr-amt', 'data-idx': newIdx});
            divCol1.appendTo(divRow);
            divCol2.appendTo(divRow);
            hdnId.appendTo(divCol1);
            txtDesc.appendTo(divCol1);
            txtAmt.appendTo(divCol2);
            jQ('#dr-acct-cont').append(divRow);
            global.autocomplete.bind(
                purchaseInvoicePayment_form.settings.slAccount_autocomplete_url,
                'input[name="otherForm[drAccounts][' + newIdx + '][slDesc]"]',
                'input[name="otherForm[drAccounts][' + newIdx + '][slId]"]', {
                    onSelect: purchaseInvoicePayment_form.computeAmounts,
                    checkEmpty: purchaseInvoicePayment_form.computeAmounts
                }
            );
            jQ('input[name="otherForm[drAccounts][' + newIdx + '][amt]"]').unbind('keyup.computeAmounts').bind('keyup.computeAmounts', purchaseInvoicePayment_form.computeAmounts);
        });
        jQ('#purchaseInvoicePayment_amount, .txt-dr-amt').unbind('keyup.computeAmounts').bind('keyup.computeAmounts', purchaseInvoicePayment_form.computeAmounts);
    },
    computeAmounts: function() {

        const vatRate = global.parseNum(purchaseInvoicePayment_form.settings.vatRate),
            whTaxRate = global.parseNum(purchaseInvoicePayment_form.settings.whTaxRate);

        if(!(vatRate > 0 && whTaxRate > 0)) {
            return;
        }

        let tDrAmt = 0;

        jQ('.txt-dr-amt').each(function(){
            const id = global.parseNum(jQ('input[name="otherForm[drAccounts][' + jQ(this).data('idx') + '][slId]"]').val()),
                amt = global.parseNum(jQ(this).val());
            if(id > 0 && amt > 0) {
                tDrAmt += amt;
            }
        });

        const whTaxAmt = (tDrAmt / (1 + (vatRate / 100))) * (whTaxRate / 100);

        jQ('#purchaseInvoicePayment_wh_tax_amount').val(global.numWithCommas(whTaxAmt.toFixed(2)));
    }
};