const transferInventory_form = {
    settings: {
        branch_autocomplete_url: null,
        location_autocomplete_url: null,
        mrAjaxUrl: null
    },
    init: function() {
        transferInventory_form.bindAutocomplete();
        transferInventory_form.initElements();
        transferInventory_form.initDataTable();
    },
    bindAutocomplete: function() {
        global.autocomplete.bind(transferInventory_form.settings.branch_autocomplete_url, '#transferInventory_branchFrom_description', '#transferInventory_branchFrom', {
            onSelect: function (d) {
                jQ("#transferInventory_locationFrom_description").removeClass('hide');
                jQ("#transferInventory_locationFrom_description");
                jQ("#transferInventory_locationFrom").val('');
            }
        });
        global.autocomplete.bind(transferInventory_form.settings.branch_autocomplete_url, '#transferInventory_branchTo_description', '#transferInventory_branchTo', {
            onSelect: function (d) {
                jQ('#transferInventory_locationTo_description').devbridgeAutocomplete('setOptions', {params: {branchId: btoa(d.id)}});
                jQ("#transferInventory_locationTo_description").removeClass('hide');
                jQ("#transferInventory_locationTo_description").val('');
                jQ("#transferInventory_locationTo").val('');
            }
        });
        // global.autocomplete.bind(transferInventory_form.settings.branch_autocomplete_url, '#transferInventory_branchFrom_description', '#transferInventory_branchFrom');
        // global.autocomplete.bind(transferInventory_form.settings.branch_autocomplete_url, '#transferInventory_branchTo_description', '#transferInventory_branchTo');
        global.autocomplete.bind(transferInventory_form.settings.location_autocomplete_url, '#transferInventory_locationFrom_description', '#transferInventory_locationFrom');
        global.autocomplete.bind(transferInventory_form.settings.location_autocomplete_url, '#transferInventory_locationTo_description', '#transferInventory_locationTo');
        jQ('#transferInventory_locationFrom_description').devbridgeAutocomplete('setOptions', {params: {branchId: btoa(jQ("#transferInventory_branchFrom").val())}});
        jQ('#transferInventory_locationTo_description').devbridgeAutocomplete('setOptions', {params: {branchId: btoa(jQ("#transferInventory_branchTo").val())}});

    },
    initElements: function() {
        jQ('.href-remove-reference-mr').unbind('click').bind('click', transferInventory_form.doRemoveReferenceMr);
    },
    initDataTable: function() {

        const callBack = function() {
        };
        const datalist = jQ('#materialRequisition-datalist').DataTable({
            processing: true,
            serverSide: true,
            ajax: {
                url: transferInventory_form.settings.mrAjaxUrl
            },
            columnDefs: [
                { orderable: false, targets: 8 },
                { searchable: false, targets: 8 }
            ],
            orderClasses: false,
            order: [[1, 'desc']],
            drawCallback: callBack,
            createdRow: function(tr, data) {
                jQ(tr).addClass('tr-select-reference-mr').unbind('click').bind('click', function(){
                    transferInventory_form.doAddReferenceMr(data.DT_DATA);
                });
            },
            responsive: {
                details: {
                    renderer: function( api,rowIdx ) {
                        return global.dataTableResponsiveCallBack(api, rowIdx, callBack);
                    }
                }
            }
        });
    },
    doAddReferenceMr: function(data) {
        if(jQ('#otherForm-mrs-id-' + data.id).length) {
            return;
        }
        //const idx = jQ('#table-reference-mr tbody tr').length,
        const idx = data.id,
            tr = jQ('<tr/>').attr({id: 'tr-reference-mr-' + idx}),
            hdn = jQ('<input/>').attr({
                type: 'hidden',
                name: 'otherForm[mrs][' + idx + '][id]',
                id: 'otherForm-mrs-id-' + data.id,
                value: data.id
            }),
            td1 = jQ('<td/>').text(data.mrNo),
            td2 = jQ('<td/>').text(data.requestDate),
            td3 = jQ('<td/>').text(data.branchDesc),
            td4 = jQ('<td/>').text(data.departmentDesc),
            td5 = jQ('<td/>').text(data.purposeDesc),
            td6 = jQ('<td/>').text(null !== data.specificPurpose ? data.specificPurpose : ''),
            td7 = jQ('<td/>'),
            a = jQ('<a/>').attr({href: 'javascript:void(0)', class: 'href-remove-reference-mr', 'data-idx': idx}).text('X');
        td1.appendTo(tr);
        td2.appendTo(tr);
        td3.appendTo(tr);
        td4.appendTo(tr);
        td5.appendTo(tr);
        td6.appendTo(tr);
        td7.appendTo(tr);
        hdn.appendTo(td7);
        a.appendTo(td7);
        jQ('#table-reference-mr tbody').append(tr);
        jQ('.href-remove-reference-mr').unbind('click').bind('click', transferInventory_form.doRemoveReferenceMr);
    },
    doRemoveReferenceMr: function() {
        jQ('#tr-reference-mr-' + jQ(this).data('idx')).remove();
    }
};