var purchaseOrderBreakdownItemDelivery_view = {
    settings: {
        purchaseOrderBreakdown_id: null,
        ajaxUrl: '',
        voidUrl: '',
        voidActionUrl: '',
        addQuantityUrl: '',
        purchaseOrderBreakdownItemDeliveryFormAll_url: '',
        purchaseOrderBreakdownItemDeliveryFormAllAction_url: '',
        purchaseOrderBreakdownItemDeliveryForm_url: '',
        purchaseOrderBreakdownItemDeliveryFormAction_url: '',
        purchaseOrderBreakdownItemDeliveryDataList: null,
        bank_autocomplete_url: '',
        supplier_autocomplete_url: ''
    },
    init: function() {
        purchaseOrderBreakdownItemDelivery_view.initDataTable();
        purchaseOrderBreakdownItemDelivery_view.bindHrefPurchaseOrderBreakdownItemDeliveryFormAll();
        purchaseOrderBreakdownItemDelivery_view.bindHrefPurchaseOrderBreakdownVoid();
        purchaseOrderBreakdownItemDelivery_view.bindHrefPurchaseOrderBreakdownAddQuantity();
    },
    bindHrefPurchaseOrderBreakdownItemDeliveryFormAll: function() {

        if(jQ('#href-purchaseOrderBreakdownItemDelivery-formAll').length > 0) {

            jQ('#href-purchaseOrderBreakdownItemDelivery-formAll').unbind('click').bind('click', function(){

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

                global.ajaxCancel(requestName);

                var settings = {
                    url: purchaseOrderBreakdownItemDelivery_view.settings.purchaseOrderBreakdownItemDeliveryFormAll_url,
                    type: 'GET',
                    data: {
                        purchaseOrderBreakdown_id: purchaseOrderBreakdownItemDelivery_view.settings.purchaseOrderBreakdown_id
                    },
                    beforeSend: function() {
                        modalContent.html('').addClass('has-loading');
                        modal.foundation('open');
                    },
                    success: function(json) {
                        modal.foundation('close');
                        modalContent.removeClass('has-loading').html(json.html);
                        global.bindFormSubmitted();
                        purchaseOrderBreakdownItemDelivery_view.bindHrefPurchaseOrderBreakdownItemDeliveryActionAll();
                        jQ('#purchaseOrderBreakdownItem-datalist').DataTable({
                            'searching': false,
                            'paging': false,
                            'columnDefs': [
                                {'orderable': false, 'targets': [0, 1, 2, 3]}
                            ]
                        });
                        global.autocomplete.bind(purchaseOrderBreakdownItemDelivery_view.settings.bank_autocomplete_url, '#purchaseOrderBreakdownItemDelivery_bank_description', '#purchaseOrderBreakdownItemDelivery_bank_id');
                        modal.foundation('open');
                    }
                };

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

        jQ('#href-purchaseOrderBreakdownItemDelivery-actionAll').unbind('click.purchaseOrderBreakdownItemDeliveryActionAll').bind('click.purchaseOrderBreakdownItemDeliveryActionAll', function(){

            var requestName = 'purchaseOrderBreakdownItemDeliveryActionAll';

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

            var settings = {
                url: purchaseOrderBreakdownItemDelivery_view.settings.purchaseOrderBreakdownItemDeliveryFormAllAction_url,
                type: 'POST',
                data: jQ('#form-purchaseOrderBreakdownItemDelivery').serialize(),
                success: function(json) {
                    if(json.success) {
                        location.reload();
                    } else {
                        global.parseFlashMessages('#flash-messages-content-modal', 'alert', json.errorMessages);
                        global.formSubmittedReverse();
                    }
                }
            };

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

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

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

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

                global.ajaxCancel(requestName);

                var settings = {
                    url: purchaseOrderBreakdownItemDelivery_view.settings.purchaseOrderBreakdownItemDeliveryForm_url,
                    type: 'GET',
                    data: {
                        purchaseOrderBreakdownItem_id: jQ(this).data('ref-id'),
                        ids: jQ(this).data('ids'),
                        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);
                        global.bindFormSubmitted();
                        global.autocomplete.bind(purchaseOrderBreakdownItemDelivery_view.settings.bank_autocomplete_url, '#purchaseOrderBreakdownItemDelivery_bank_description', '#purchaseOrderBreakdownItemDelivery_bank_id');
                        modal.foundation('open');
                        purchaseOrderBreakdownItemDelivery_view.bindHrefPurchaseOrderBreakdownItemDeliveryAction();
                        jQ('#purchaseOrderBreakdownItem-datalist-update').DataTable({
                            'searching': false,
                            'paging': false,
                            'ordering': false,
                        });
                        global.maskInput();
                    }
                };

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

        jQ('.href-purchaseOrderBreakdownItemDelivery-action').unbind('click.purchaseOrderBreakdownItemDeliveryAction').bind('click.purchaseOrderBreakdownItemDeliveryAction', function(){

            var requestName = 'purchaseOrderBreakdownItemDeliveryAction';

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

            var settings = {
                url: purchaseOrderBreakdownItemDelivery_view.settings.purchaseOrderBreakdownItemDeliveryFormAction_url,
                type: 'POST',
                data: jQ('#form-purchaseOrderBreakdownItemDelivery').serialize(),
                success: function(json) {
                    if(json.success) {
                        location.reload();
                    } else {
                        global.parseFlashMessages('#flash-messages-content-modal', 'alert', json.errorMessages);
                        global.formSubmittedReverse();
                    }
                }
            };

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

        if(jQ('#href-purchaseOrderBreakdown-void').length > 0) {

            jQ('#href-purchaseOrderBreakdown-void').unbind('click').bind('click', function(){

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

                global.ajaxCancel(requestName);

                var settings = {
                    url: purchaseOrderBreakdownItemDelivery_view.settings.voidUrl,
                    type: 'GET',
                    data: {
                        purchaseOrderBreakdown_id: purchaseOrderBreakdownItemDelivery_view.settings.purchaseOrderBreakdown_id
                    },
                    beforeSend: function() {
                        modalContent.html('').addClass('has-loading');
                        modal.foundation('open');
                    },
                    success: function(json) {
                        modal.foundation('close');
                        modalContent.removeClass('has-loading').html(json.html);
                        global.bindFormSubmitted();
                        purchaseOrderBreakdownItemDelivery_view.bindHrefPurchaseOrderBreakdownVoidAction();
                        jQ('#purchaseOrderBreakdownItemVoid-datalist').DataTable({
                            'searching': false,
                            'paging': false,
                            'columnDefs': [
                                {'orderable': false, 'targets': [0, 1, 2, 3]}
                            ]
                        });
                        modal.foundation('open');
                    }
                };

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

        jQ('#href-purchaseOrderBreakdownVoid-action').unbind('click.purchaseOrderBreakdownVoidAction').bind('click.purchaseOrderBreakdownVoidAction', function(){

            var requestName = 'purchaseOrderBreakdownVoidAction';

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

            var settings = {
                url: purchaseOrderBreakdownItemDelivery_view.settings.voidActionUrl,
                type: 'POST',
                data: jQ('#form-purchaseOrderBreakdownVoid').serialize(),
                success: function(json) {
                    if(json.success) {
                        location.reload();
                    } else {
                        global.parseFlashMessages('#flash-messages-content-modal', 'alert', json.errorMessages);
                        global.formSubmittedReverse();
                    }
                }
            };

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

        var callBack = function() {
            purchaseOrderBreakdownItemDelivery_view.bindHrefPurchaseOrderBreakdownItemDeliveryForm();
        };

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

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

    bindHrefPurchaseOrderBreakdownAddQuantity: function() {

        if(jQ('#href-purchaseOrderBreakdown-addQuantity').length > 0) {

            jQ('#href-purchaseOrderBreakdown-addQuantity').unbind('click').bind('click', function(){

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

                global.ajaxCancel(requestName);

                var settings = {
                    url: purchaseOrderBreakdownItemDelivery_view.settings.addQuantityUrl,
                    type: 'GET',
                    data: {
                        purchaseOrderBreakdown_id: purchaseOrderBreakdownItemDelivery_view.settings.purchaseOrderBreakdown_id
                    },
                    beforeSend: function() {
                        modalContent.html('').addClass('has-loading');
                        modal.foundation('open');
                    },
                    success: function(json) {
                        modal.foundation('close');
                        modalContent.removeClass('has-loading').html(json.html);
                        global.bindFormSubmitted();
                        purchaseOrderBreakdownItemDelivery_view.bindHrefPurchaseOrderBreakdownAddQuantityAction();
                        jQ('#purchaseOrderBreakdownItemAddQuantity-datalist').DataTable({
                            'searching': false,
                            'paging': false,
                            'columnDefs': [
                                {'orderable': false, 'targets': [0, 1, 2, 3]}
                            ]
                        });
                        modal.foundation('open');
                    }
                };

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

    bindHrefPurchaseOrderBreakdownAddQuantityAction: function() {

        jQ('#href-purchaseOrderBreakdownAddQuantity-action').unbind('click.purchaseOrderBreakdownAddQuantityAction').bind('click.purchaseOrderBreakdownAddQuantityAction', function(){

            var requestName = 'purchaseOrderBreakdownAddQuantityAction';

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

            var settings = {
                url: purchaseOrderBreakdownItemDelivery_view.settings.addQuantityActionUrl,
                type: 'POST',
                data: jQ('#form-purchaseOrderBreakdownAddQuantity').serialize(),
                success: function(json) {
                    if(json.success) {
                        location.reload();
                    } else {
                        global.parseFlashMessages('#flash-messages-content-modal', 'alert', json.errorMessages);
                        global.formSubmittedReverse();
                    }
                }
            };

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