var transmittalItem = {
    settings: {
        ajaxUrl: '',
        ajaxUrlForLoading: '',
        ajaxUrlItemDelivery: '',
        loadUrl: '',
        unloadUrl: '',
        sendUrl: '',
        transmittalItemDeliveryFormAll_url: '',
        transmittalItemDeliveryFormAllAction_url: '',
        transmittalItemDeliveryForm_url: '',
        transmittalItemDeliveryFormAction_url: '',
        transmittal_id: null,
        transmittalItemDataList: null,
        transmittalItemForLoadingDataList: null,
        transmittalItemDeliveryDataList: null
    },
    init: function() {
        transmittalItem.initDataTable();
        transmittalItem.bindHrefTransmittalSend();
        transmittalItem.bindHrefTransmittalItemDeliveryFormAll();
    },
    bindHrefTransmittalItemLoad: function() {

        jQ('.href-transmittalItem-load').unbind('click.transmittalItemLoad').bind('click.transmittalItemLoad', function(){

            var qty = prompt('Enter Quantity');

            if(global.parseNum(qty) > 0) {

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

                global.ajaxCancel(requestName);

                var settings = {
                    url: transmittalItem.settings.loadUrl,
                    type: 'POST',
                    data: {
                        transmittal_id: transmittalItem.settings.transmittal_id,
                        purchaseOrderBreakdownItem_id: jQ(this).data('id'),
                        quantity: qty
                    },
                    beforeSend: function() {
                        modalContent.html('').addClass('has-loading');
                        modal.foundation('open');
                    },
                    success: function(json) {
                        modal.foundation('close');
                        if(json.success) {
                            transmittalItem.settings.transmittalItemForLoadingDataList.draw();
                            transmittalItem.settings.transmittalItemDataList.draw();
                            global.parseFlashMessages(global.settings.vars.flashMessagesContentId, 'success', json.successMessages);
                        } else {
                            global.parseFlashMessages(global.settings.vars.flashMessagesContentId, 'alert', json.errorMessages);
                        }
                    }
                };

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

        jQ('.href-transmittalItem-unload').unbind('click.transmittalItemUnload').bind('click.transmittalItemUnload', function(){

            var qty = prompt('Enter Quantity');

            if(global.parseNum(qty) > 0) {

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

                global.ajaxCancel(requestName);

                var settings = {
                    url: transmittalItem.settings.unloadUrl,
                    type: 'POST',
                    data: {
                        transmittalItem_id: jQ(this).data('id'),
                        quantity: qty
                    },
                    beforeSend: function() {
                        modalContent.html('').addClass('has-loading');
                        modal.foundation('open');
                    },
                    success: function(json) {
                        modal.foundation('close');
                        if(json.success) {
                            transmittalItem.settings.transmittalItemForLoadingDataList.draw();
                            transmittalItem.settings.transmittalItemDataList.draw();
                            global.parseFlashMessages(global.settings.vars.flashMessagesContentId, 'success', json.successMessages);
                        } else {
                            global.parseFlashMessages(global.settings.vars.flashMessagesContentId, 'alert', json.errorMessages);
                        }
                    }
                };

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

        if(jQ('#href-transmittal-send').length > 0) {

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

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

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

                    global.ajaxCancel(requestName);

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

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

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

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

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

                global.ajaxCancel(requestName);

                var settings = {
                    url: transmittalItem.settings.transmittalItemDeliveryFormAll_url,
                    type: 'GET',
                    data: {
                        transmittal_id: transmittalItem.settings.transmittal_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();
                        transmittalItem.bindHrefTransmittalItemDeliveryActionAll();
                        jQ('#transmittalItemList-datalist').DataTable({
                            'searching': false,
                            'paging': false,
                            'columnDefs': [
                                {'orderable': false, 'targets': [0, 1, 2, 3]}
                            ]
                        });
                        modal.foundation('open');
                    }
                };

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

        jQ('#href-transmittalItemDelivery-actionAll').unbind('click.transmittalItemDeliveryActionAll').bind('click.transmittalItemDeliveryActionAll', function(){

            var requestName = 'transmittalItemDeliveryActionAll';

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

            var settings = {
                url: transmittalItem.settings.transmittalItemDeliveryFormAllAction_url,
                type: 'POST',
                data: jQ('#form-transmittalItemDelivery').serialize(),
                success: function(json) {
                    if(json.success) {
                        location.reload();
                    } else {
                        global.parseFlashMessages('#flash-messages-content-modal', 'alert', json.errorMessages);
                        global.formSubmittedReverse();
                    }
                },
                error: function(xhr) {
                    console.log(xhr.responseText);
                }
            };

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

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

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

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

                global.ajaxCancel(requestName);

                var settings = {
                    url: transmittalItem.settings.transmittalItemDeliveryForm_url,
                    type: 'GET',
                    data: {
                        transmittalItem_id: jQ(this).data('ref-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);
                        global.bindFormSubmitted();
                        modal.foundation('open');
                        transmittalItem.bindHrefTransmittalItemDeliveryAction();
                    }
                };

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

        jQ('.href-transmittalItemDelivery-action').unbind('click.transmittalItemDeliveryAction').bind('click.transmittalItemDeliveryAction', function(){

            var requestName = 'transmittalItemDeliveryAction';

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

            var settings = {
                url: transmittalItem.settings.transmittalItemDeliveryFormAction_url,
                type: 'POST',
                data: jQ('#form-transmittalItemDelivery').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() {
            transmittalItem.bindHrefTransmittalItemUnload();
        };

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

        if(jQ('#transmittalItemForLoading-datalist').length > 0) {

            var callBackForLoading = function() {
                transmittalItem.bindHrefTransmittalItemLoad();
            };

            transmittalItem.settings.transmittalItemForLoadingDataList = jQ('#transmittalItemForLoading-datalist').DataTable({
                
                'processing': true,
                'serverSide': true,
                'ajax': {
                    'url': transmittalItem.settings.ajaxUrlForLoading
                },
                'columnDefs': [
                    { 'orderable': false, 'targets': [1, 4, 5] },
                    { 'searchable': false, 'targets': [1, 4, 5] }
                ],
                drawCallback: function() {
                    callBackForLoading();
                },
                responsive: {
                    details: {
                        renderer: function( api,rowIdx ) {
                            return global.dataTableResponsiveCallBack(api, rowIdx, callBackForLoading);
                        }
                    }
                }
            });
        }

        if(jQ('#transmittalItemDelivery-datalist').length > 0) {

            var callBackItemDelivery = function() {
                transmittalItem.bindHrefTransmittalItemDeliveryForm();
            };

            transmittalItem.settings.transmittalItemDeliveryDataList = jQ('#transmittalItemDelivery-datalist').DataTable({
                
                'processing': true,
                'serverSide': true,
                'ajax': {
                    'url': transmittalItem.settings.ajaxUrlItemDelivery,
                    'data': function(d) {
                        d.transmittal_id = transmittalItem.settings.transmittal_id;
                    }
                },
                'columnDefs': [
                    { 'orderable': false, 'targets': 4 },
                    { 'searchable': false, 'targets': 4 }
                ],
                drawCallback: function() {
                    callBackItemDelivery();
                },
                responsive: {
                    details: {
                        renderer: function( api,rowIdx ) {
                            return global.dataTableResponsiveCallBack(api, rowIdx, callBackItemDelivery);
                        }
                    }
                }
            });
        }

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