var purchaseInvoice_view = {
    settings: {
        purchaseInvoice_id: null,
        sendUrl: '',
        otherCost_form: '',
    },
    init: function() {
        purchaseInvoice_view.initDataTable();
        purchaseInvoice_view.bindHrefPurchaseInvoiceSend();
        purchaseInvoice_view.bindOtherCostAction();
    },
    bindHrefPurchaseInvoiceSend: function() {

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

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

            global.ajaxCancel(requestName);

            var settings = {
                url: purchaseInvoice_view.settings.sendUrl,
                type: 'POST',
                data: {
                    purchaseInvoice_id: purchaseInvoice_view.settings.purchaseInvoice_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);
        });
    },
    bindOtherCostAction: function() {

        jQ('.other-cost-action').unbind('click').bind('click', function(){

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

            global.ajaxCancel(requestName);

            var settings = {
                url: purchaseInvoice_view.settings.otherCost_form,
                type: 'GET',
                data: {
                    id: jQ(this).data('id'),
                    action: jQ(this).data('action'),
                    purchaseInvoice_id: purchaseInvoice_view.settings.purchaseInvoice_id,
                },
                beforeSend: function() {
                    modalContent.html('').addClass('has-loading');
                    modal.foundation('open');
                },
                success: function(json) {
                    modal.foundation('close');
                    if(json.success) {
                        modalContent.html(json.html).removeClass('has-loading');
                        modal.foundation('open');
                        global.bindFormSubmitted();
                    } else {
                        global.flashMessage('alert', json.errorMessages);
                    }
                }
            };

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

        var callBack = function() {
        };

        var datalist = jQ('#purchaseInvoicePayment-datalist').DataTable({
            'ordering': false,
            'searching': false,
            'paging': false,
            order: [[0, 'desc']],
            drawCallback: function() {
                callBack();
            },
            responsive: {
                details: {
                    renderer: function( api,rowIdx ) {
                        return global.dataTableResponsiveCallBack(api, rowIdx, callBack);
                    }
                }
            }
        });

        jQ('#purchaseOrderBreakdownItem-datalist').DataTable({
            paging: false,
            searching: false,
            ordering: false,
            info: false
        });

        jQ('#purchaseOrderBreakdownItemDelivery-datalist').DataTable({
            paging: false,
            searching: false,
            ordering: false,
            info: false
        });

        var datalistCredit = jQ('#purchaseInvoiceCreditPayment-datalist').DataTable({
            'ordering': false,
            'searching': false,
            'paging': false,
            order: [[0, 'desc']],
            drawCallback: function() {
                callBack();
            },
            responsive: {
                details: {
                    renderer: function( api,rowIdx ) {
                        return global.dataTableResponsiveCallBack(api, rowIdx, callBack);
                    }
                }
            }
        });

        jQ('#otherCost-datalist').DataTable({
            'ordering': false,
            'searching': false,
            'paging': false,
        });

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