var addInventoryItem = {
    settings: {
        ajaxUrl: '',
        formUrl: '',
        formActionUrl: '',
        sendUrl: '',
        item_autocomplete_url: '',
        bank_autocomplete_url: '',
        location_autocomplete_url: '',
        addInventory_id: null,
        addInventoryItemDataList: null,
        branch_id: null
    },
    init: function() {
        addInventoryItem.initDataTable();
        addInventoryItem.bindHrefAddInventoryItemForm();
        addInventoryItem.bindHrefAddInventoryItemSend();
    },
    bindHrefAddInventoryItemForm: function() {

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

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

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

                global.ajaxCancel(requestName);

                var settings = {
                    url: addInventoryItem.settings.formUrl,
                    type: 'GET',
                    data: {
                        addInventory_id: addInventoryItem.settings.addInventory_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(addInventoryItem.settings.item_autocomplete_url, '#addInventoryItem_item_description', '#addInventoryItem_item');
                        global.autocomplete.bind(addInventoryItem.settings.location_autocomplete_url, '#addInventoryItem_location_description', '#addInventoryItem_location');
                        jQ('#addInventoryItem_location_description').devbridgeAutocomplete('setOptions', {params: {branchId: addInventoryItem.settings.branch_id}});
                        addInventoryItem.bindHrefAddInventoryItemAction();
                    }
                };

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

        jQ('.href-addInventoryItem-action').unbind('click.addInventoryItemAction').bind('click.addInventoryItemAction', function(){

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

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

            var settings = {
                url: addInventoryItem.settings.formActionUrl,
                type: 'POST',
                data: jQ('#form-addInventoryItem').serialize(),
                success: function(json) {
                    global.formSubmittedReverse();
                    if(json.success) {
                        addInventoryItem.settings.addInventoryItemDataList.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);
        });
    },
    bindHrefAddInventoryItemSend: function() {

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

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

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

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

                    global.ajaxCancel(requestName);

                    var settings = {
                        url: addInventoryItem.settings.sendUrl,
                        type: 'POST',
                        data: {
                            addInventory_id: addInventoryItem.settings.addInventory_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 = 'addInventorySend',
                modal = jQ('#modal'),
                modalContent = jQ('#modal-content');

            global.ajaxCancel(requestName);

            var settings = {
                url: addInventoryItem.settings.sendUrl,
                type: 'POST',
                data: {
                    addInventory_id: addInventoryItem.settings.addInventory_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);
        });
    },
    bindPaymentForm: function() {

        if(jQ('.href-add-inventory-payment-form').length > 0) {

            jQ('.href-add-inventory-payment-form').unbind('click').bind('click', function(){

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

                global.ajaxCancel(requestName);

                var settings = {
                    url: addInventoryItem.settings.payment_formUrl + '/' + jQ(this).data('action') + '/' + jQ(this).data('id'),
                    type: 'GET',
                    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(
                             addInventoryItem.settings.bank_autocomplete_url,
                            '#addInventoryPayment_bank_description',
                            '#addInventoryPayment_bank'
                            // {
                            //     onSelect : function(s){
                            //         jQ('#addInventoryPayment_check_no').val(parseFloat(s.checkNo) + 1);
                            //     }
                            // }
                        );
                        addInventoryItem.bindPaymentFormAction();
                    }
                };

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

        jQ('.href-addInventoryPayment-action').unbind('click.addInventoryPaymentFormAction').bind('click.addInventoryPaymentFormAction', function(){

            var requestName = 'addInventoryPaymentFormAction',
                modal = jQ('#modal'),
                modalContent = jQ('#modal-content'),
                _form = jQ('#form-addInventoryPayment');

            _form.find('.form-action').val(jQ(this).data('axn'));

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

            var settings = {
                url: addInventoryItem.settings.payment_formActionUrl,
                type: 'POST',
                data: _form.serialize(),
                success: function(json) {
                    global.formSubmittedReverse();
                    if(json.success) {
                        addInventoryItem.dataListPayment.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() {
            addInventoryItem.bindHrefAddInventoryItemForm();
        };

        addInventoryItem.settings.addInventoryItemDataList = jQ('#addInventoryItem-datalist').DataTable({
            'processing': true,
            'serverSide': true,
            'ajax': {
                'url': addInventoryItem.settings.ajaxUrl,
                'data': function(d) {
                    d.addInventory_id = addInventoryItem.settings.addInventory_id;
                }
            },
            'paging': false,
            'ordering': false,
            'searching': false,
            drawCallback: function() {
                callBack();
            },
            responsive: {
                details: {
                    renderer: function( api,rowIdx ) {
                        return global.dataTableResponsiveCallBack(api, rowIdx, callBack);
                    }
                }
            }
        });

        var callBackPayment = function() {
            addInventoryItem.bindPaymentForm();
        };

        this.dataListPayment = jQ('#addInventoryPayment-datalist').DataTable({
            'processing': true,
            'serverSide': true,
            'ajax': {
                'url': addInventoryItem.settings.payment_ajaxUrl,
                'data': function(d) {
                    d.addInventory_id = addInventoryItem.settings.addInventory_id;
                }
            },
            'ordering': false,
            'searching': false,
            'paging': false,
            drawCallback: function() {
                callBackPayment();
            },
            responsive: {
                details: {
                    renderer: function( api,rowIdx ) {
                        return global.dataTableResponsiveCallBack(api, rowIdx, callBackPayment);
                    }
                }
            }
        });

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