var inventory_form = {
    settings: {
        branch_autocomplete_url: null,
        location_autocomplete_url: null,
        item_autocomplete_url: null
    },
    snTable: null,
    init: function() {
        inventory_form.bindAutocomplete();
        inventory_form.initDt();
    },
    initDt: function () {
        inventory_form.snTable = jQ('#serial_no_datalist').DataTable({
            ordering: false,
        });
        inventory_form.snTableFilter();

        jQ('#history').DataTable({
            ordering: false,
            info: false,
            paging: false,
        });
    },
    snTableFilter: function () {
        const filterStatus = () => {
            const selectedValue = jQ('#serial_no_datalist_filter-status').val();
            inventory_form.snTable.column( 2 ) .search( selectedValue ) .draw();
        };

        jQ('#serial_no_datalist_filter-status').change(filterStatus);
        filterStatus();
    },
    bindAutocomplete: function() {
        global.autocomplete.bind(inventory_form.settings.branch_autocomplete_url, '#inventory_form_branch_description', '#inventory_form_healthFacility', {
            onSelect: function (d) {
                jQ('#inventory_location_description').devbridgeAutocomplete('setOptions', {params: {branchId: btoa(d.id)}});
            }
        });
        global.autocomplete.bind(inventory_form.settings.location_autocomplete_url, '#inventory_form_location_description', '#inventory_form_location', {
            onSelect: function(d){
                jQ('#inventory_branch_description').val(d.branch_description);
                jQ('#inventory_branch_id').val(d.branch_id);
            }
        });

        global.autocomplete.bind(inventory_form.settings.item_autocomplete_url, '#inventory_form_item_description', '#inventory_form_item');
    }
};