var convertInventoryItem = {
    settings: {
        ajaxUrl: '',
        formUrl: '',
        formActionUrl: '',
        biAjaxUrl: '',
        biFormUrl: '',
        biFormActionUrl: '',
        biFormCostingUrl: '',
        biFormCostingActionUrl: '',
        sendUrl: '',
        inventory_autocomplete_url: '',
        item_autocomplete_url: '',
        location_autocomplete_url: '',
        convertInventory_id: null,
        convertInventoryItemDataList: null,
        convertInventoryItemBiDataList: null,
        health_facility_id: null
    },
    init: function() {
        convertInventoryItem.initDataTable();
        convertInventoryItem.bindHrefConvertInventoryItemForm();
        convertInventoryItem.bindHrefConvertInventoryItemBiForm();
        convertInventoryItem.bindHrefConvertInventoryItemSend();
    },
    bindHrefConvertInventoryItemForm: function() {

        if(jQ('.href-convertInventoryItem-form').length > 0) {

            jQ('.href-convertInventoryItem-form').unbind('click').bind('click', function(){

                var requestName = 'convertInventoryItemForm',
                    modal = jQ('#modal-small'),
                    modalContent = jQ('#modal-content-small');

                global.ajaxCancel(requestName);

                var settings = {
                    url: convertInventoryItem.settings.formUrl,
                    type: 'GET',
                    data: {
                        convertInventory_id: convertInventoryItem.settings.convertInventory_id,
                        action: jQ(this).data('action'),
                        id: jQ(this).data('id')
                    },
                    beforeSend: function() {
                        modalContent.html('').addClass('has-loading');
                        modal.foundation('open');
                    },
                    success: function(json) {
                        modal.foundation('close');
                        modalContent.removeClass('has-loading').html(json.html);
                        modal.foundation('open');
                    }
                };

                global.ajax(settings, requestName);
            });
        }
    },
    bindHrefConvertInventoryItemAction: function() {

        jQ('.href-convertInventoryItem-action').unbind('click.convertInventoryItemAction').bind('click.convertInventoryItemAction', function(){

            var requestName = 'convertInventoryItemAction',
                modal = jQ('#modal-small'),
                modalContent = jQ('#modal-content-small');

            global.formSubmitted();
            global.ajaxCancel(requestName);

            var settings = {
                url: convertInventoryItem.settings.formActionUrl,
                type: 'POST',
                data: jQ('#form-convertInventoryItem').serialize(),
                success: function(json) {
                    global.formSubmittedReverse();
                    if(json.success) {
                        convertInventoryItem.settings.convertInventoryItemDataList.draw();
                        convertInventoryItem.settings.convertInventoryItemBiDataList.draw();
                        global.flashMessage('success', json.successMessages);
                        modal.foundation('close');
                    } else {
                        global.flashMessage('alert', json.errorMessages);
                    }
                }
            };

            global.ajax(settings, requestName);
        });
    },
    bindHrefConvertInventoryItemSend: function() {

        /*if(jQ('#href-convertInventory-send').length > 0) {

            jQ('#href-convertInventory-send').unbind('click').bind('click', function(){

                if(confirm('Are you sure you want to send this transaction?')) {

                    var requestName = 'convertInventorySend',
                        modal = jQ('#modal'),
                        modalContent = jQ('#modal-content');

                    global.ajaxCancel(requestName);

                    var settings = {
                        url: convertInventoryItem.settings.sendUrl,
                        type: 'POST',
                        data: {
                            convertInventory_id: convertInventoryItem.settings.convertInventory_id
                        },
                        beforeSend: function() {
                            modalContent.html('').addClass('has-loading');
                            modal.foundation('open');
                        },
                        success: function(json) {
                            if(json.success) {
                                location.reload();
                            } else {
                                global.parseFlashMessages(global.settings.vars.flashMessagesContentId, 'alert', json.errorMessages);
                                modal.foundation('close');
                            }
                        }
                    };

                    global.ajax(settings, requestName);
                }
            });
        }*/

        jQ('#btn-send-ask-modal-continue').unbind('click').bind('click', function(){

            var requestName = 'convertInventorySend',
                modal = jQ('#modal'),
                modalContent = jQ('#modal-content');

            global.ajaxCancel(requestName);

            var settings = {
                url: convertInventoryItem.settings.sendUrl,
                type: 'POST',
                data: {
                    convertInventory_id: convertInventoryItem.settings.convertInventory_id
                },
                beforeSend: function() {
                    modalContent.html('').addClass('has-loading');
                    modal.foundation('open');
                },
                success: function(json) {
                    if(json.success) {
                        location.reload();
                    } else {
                        global.parseFlashMessages(global.settings.vars.flashMessagesContentId, 'alert', json.errorMessages);
                        modal.foundation('close');
                    }
                }
            };

            global.ajax(settings, requestName);
        });
    },
    bindHrefConvertInventoryItemBiForm: function() {

        if(jQ('.href-convertInventoryItemBi-form').length > 0) {

            jQ('.href-convertInventoryItemBi-form').unbind('click').bind('click', function(){

                var requestName = 'convertInventoryItemBiForm',
                    modal = jQ('#modal-small'),
                    modalContent = jQ('#modal-content-small');

                global.ajaxCancel(requestName);

                var settings = {
                    url: convertInventoryItem.settings.biFormUrl,
                    type: 'GET',
                    data: {
                        convertInventory_id: convertInventoryItem.settings.convertInventory_id,
                        action: jQ(this).data('action'),
                        id: jQ(this).data('id')
                    },
                    beforeSend: function() {
                        modalContent.html('').addClass('has-loading');
                        modal.foundation('open');
                    },
                    success: function(json) {
                        modal.foundation('close');
                        modalContent.removeClass('has-loading').html(json.html);
                        modal.foundation('open');
                        global.bindFormSubmitted();
                        global.autocomplete.bind(convertInventoryItem.settings.item_autocomplete_url, '#convertInventoryItemBi_item_description', '#convertInventoryItemBi_item');
                        global.autocomplete.bind(convertInventoryItem.settings.location_autocomplete_url, '#convertInventoryItemBi_location_description', '#convertInventoryItemBi_location');
                        jQ('#convertInventoryItemBi_location_description').devbridgeAutocomplete('setOptions', {params: {health_facility_id: convertInventoryItem.settings.health_facility_id}});
                        convertInventoryItem.bindHrefConvertInventoryItemBiAction();
                    }
                };

                global.ajax(settings, requestName);
            });
        }
    },
    bindHrefConvertInventoryItemBiAction: function() {

        jQ('.href-convertInventoryItemBi-action').unbind('click.convertInventoryItemBiAction').bind('click.convertInventoryItemBiAction', function(){

            var requestName = 'convertInventoryItemBiAction',
                modal = jQ('#modal-small'),
                modalContent = jQ('#modal-content-small');

            global.formSubmitted();
            global.ajaxCancel(requestName);

            var settings = {
                url: convertInventoryItem.settings.biFormActionUrl,
                type: 'POST',
                data: jQ('#form-convertInventoryItemBi').serialize(),
                success: function(json) {
                    global.formSubmittedReverse();
                    if(json.success) {
                        convertInventoryItem.settings.convertInventoryItemDataList.draw();
                        convertInventoryItem.settings.convertInventoryItemBiDataList.draw();
                        global.parseFlashMessages(global.settings.vars.flashMessagesContentId, 'success', json.successMessages);
                        modal.foundation('close');
                    } else {
                        global.parseFlashMessages('#flash-messages-content-modal', 'alert', json.errorMessages);
                    }
                }
            };

            global.ajax(settings, requestName);
        });
    },
    bindHrefConvertInventoryItemBiFormCosting: function() {

        if(jQ('.href-convertInventoryItemBi-form-costing').length > 0) {

            jQ('.href-convertInventoryItemBi-form-costing').unbind('click').bind('click', function(){

                var requestName = 'convertInventoryItemBiFormCosting',
                    modal = jQ('#modal'),
                    modalContent = jQ('#modal-content');

                global.ajaxCancel(requestName);

                var settings = {
                    url: convertInventoryItem.settings.biFormCostingUrl,
                    type: 'GET',
                    data: {
                        convertInventory_id: convertInventoryItem.settings.convertInventory_id,
                        id: jQ(this).data('id')
                    },
                    beforeSend: function() {
                        modalContent.html('').addClass('has-loading');
                        modal.foundation('open');
                    },
                    success: function(json) {
                        modal.foundation('close');
                        modalContent.removeClass('has-loading').html(json.html);
                        modal.foundation('open');
                        global.bindFormSubmitted();
                        convertInventoryItem.bindHrefConvertInventoryItemBiCostingAction();
                    }
                };

                global.ajax(settings, requestName);
            });
        }
    },
    bindHrefConvertInventoryItemBiCostingAction: function() {

        jQ('.href-convertInventoryItemBi-action-costing').unbind('click.convertInventoryItemBiCostingAction').bind('click.convertInventoryItemBiCostingAction', function(){

            var requestName = 'convertInventoryItemBiCostingAction',
                modal = jQ('#modal'),
                modalContent = jQ('#modal-content');

            global.formSubmitted();
            global.ajaxCancel(requestName);

            var settings = {
                url: convertInventoryItem.settings.biFormCostingActionUrl,
                type: 'POST',
                data: jQ('#form-convertInventoryItemBi-costing').serialize(),
                success: function(json) {
                    global.formSubmittedReverse();
                    if(json.success) {
                        convertInventoryItem.settings.convertInventoryItemBiDataList.draw();
                        global.parseFlashMessages(global.settings.vars.flashMessagesContentId, 'success', json.successMessages);
                        modal.foundation('close');
                    } else {
                        global.parseFlashMessages('#flash-messages-content-modal', 'alert', json.errorMessages);
                    }
                }
            };

            global.ajax(settings, requestName);
        });
    },
    initDataTable: function() {

        var callBack = function() {
            convertInventoryItem.bindHrefConvertInventoryItemForm();
        };

        convertInventoryItem.settings.convertInventoryItemDataList = jQ('#convertInventoryItem-datalist').DataTable({
            'processing': true,
            'serverSide': true,
            'ajax': {
                'url': convertInventoryItem.settings.ajaxUrl,
                'data': function(d) {
                    d.convertInventory_id = convertInventoryItem.settings.convertInventory_id;
                }
            },
            'columnDefs': [
                { 'orderable': false, 'targets': 2 },
                { 'searchable': false, 'targets': 2 }
            ],
            drawCallback: function() {
                callBack();
            },
            responsive: {
                details: {
                    renderer: function( api,rowIdx ) {
                        return global.dataTableResponsiveCallBack(api, rowIdx, callBack);
                    }
                }
            }
        });

        var callBack2 = function() {
            convertInventoryItem.bindHrefConvertInventoryItemBiForm();
            convertInventoryItem.bindHrefConvertInventoryItemBiFormCosting();
        };

        convertInventoryItem.settings.convertInventoryItemBiDataList = jQ('#convertInventoryItemBi-datalist').DataTable({
            'processing': true,
            'serverSide': true,
            'ajax': {
                'url': convertInventoryItem.settings.biAjaxUrl,
                'data': function(d) {
                    d.convertInventory_id = convertInventoryItem.settings.convertInventory_id;
                }
            },
            'paging': false,
            'searching': false,
            'ordering': false,
            drawCallback: function() {
                callBack2();
            },
            responsive: {
                details: {
                    renderer: function( api,rowIdx ) {
                        return global.dataTableResponsiveCallBack(api, rowIdx, callBack2);
                    }
                }
            }
        });

        jQ('#convertInventoryStatusHistory-datalist').DataTable({
            'order': [[0, 'desc']],
            'paging': false,
            'ordering': false,
            'searching': false
        });

        jQ('.content-container').removeClass('has-loading'); jQ('.content-container-content').removeClass('hide');
    }
};