var purchaseOrder_view = {
    settings: {
        purchaseOrder_id: null,
        ajaxUrl: '',
        voidUrl: '',
        addQuantityUrl: '',
        addQuantityActionUrl: '',
        voidActionUrl: '',
        purchaseOrderSend_url: '',
        cancelActionUrl:'',
        purchaseOrderBreakdownDetails_url: '',
        purchaseOrderBreakdownSend_url: '',
        purchaseOrderBreakdownForm_url: '',
        purchaseOrderBreakdownFormAction_url: '',
        purchaseOrderBreakdownDataList: null
    },
    init: function() {
        purchaseOrder_view.initDataTable();
        purchaseOrder_view.bindHrefPurchaseOrderBreakdownForm();
        purchaseOrder_view.bindHrefPurchaseOrderSend();
        purchaseOrder_view.bindHrefPurchaseOrderVoid();
        purchaseOrder_view.bindHrefPurchaseOrderAddQuantity();
        purchaseOrder_view.bindHrefPurchaseOrderCancel();
    },
    bindHrefPurchaseOrderCancel: function() {

        if(jQ('#href-purchaseOrder-cancel').length > 0) {

            jQ('#href-purchaseOrder-cancel').unbind('click').bind('click', function(){

                if(confirm('Are you sure you want to cancel this PO?')) {

                    var purchaseOrder_id = jQ(this).data('id'),
                        requestName = 'purchaseOrderCancel',
                        modal = jQ('#modal'),
                        modalContent = jQ('#modal-content');

                    global.ajaxCancel(requestName);

                    var settings = {
                        url: purchaseOrder_view.settings.cancelActionUrl,
                        type: 'POST',
                        data: {
                            purchaseOrder_id: purchaseOrder_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);
                }
            });
        }
    },
    bindHrefPurchaseOrderSend: function() {

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

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

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

                    var purchaseOrder_id = jQ(this).data('id'),
                        requestName = 'purchaseOrderSend',
                        modal = jQ('#modal'),
                        modalContent = jQ('#modal-content');

                    global.ajaxCancel(requestName);

                    var settings = {
                        url: purchaseOrder_view.settings.purchaseOrderSend_url,
                        type: 'POST',
                        data: {
                            purchaseOrder_id: purchaseOrder_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);
                }
            });
        }*/

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

            var purchaseOrder_id = jQ(this).data('id'),
                requestName = 'purchaseOrderSend',
                modal = jQ('#modal'),
                modalContent = jQ('#modal-content');

            global.ajaxCancel(requestName);

            var settings = {
                url: purchaseOrder_view.settings.purchaseOrderSend_url,
                type: 'POST',
                data: {
                    purchaseOrder_id: purchaseOrder_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);
        });
    },
    bindHrefPurchaseOrderBreakdownForm: function() {

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

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

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

                global.ajaxCancel(requestName);

                var settings = {
                    url: purchaseOrder_view.settings.purchaseOrderBreakdownForm_url,
                    type: 'GET',
                    data: {
                        purchaseOrder_id: purchaseOrder_view.settings.purchaseOrder_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();
                        jQ('#purchaseOrderItem-datalist').DataTable({
                            'searching': false,
                            'paging': false,
                            'columnDefs': [
                                {'orderable': false, 'targets': [0, 1]}
                            ]
                        });
                        modal.foundation('open');
                        purchaseOrder_view.bindHrefPurchaseOrderBreakdownAction();
                    }
                };

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

        jQ('.href-purchaseOrderBreakdown-action').unbind('click.purchaseOrderBreakdownAction').bind('click.purchaseOrderBreakdownAction', function(){

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

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

            var settings = {
                url: purchaseOrder_view.settings.purchaseOrderBreakdownFormAction_url,
                type: 'POST',
                data: jQ('#form-purchaseOrderBreakdown').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);
        });
    },
    bindHrefPurchaseOrderBreakdownDetails: function() {

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

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

                var purchaseOrderBreakdown_id = jQ(this).data('id'),
                    requestName = 'purchaseOrderBreakdownDetails',
                    modal = jQ('#modal'),
                    modalContent = jQ('#modal-content');

                global.ajaxCancel(requestName);

                var settings = {
                    url: purchaseOrder_view.settings.purchaseOrderBreakdownDetails_url,
                    type: 'GET',
                    data: {
                        purchaseOrderBreakdown_id: 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);
                        modal.foundation('open');
                        jQ('#purchaseOrderBreakdownItem-datalist').DataTable({
                            'paging': false,
                            'searching': false,
                            'ordering': false,
                            'info': false
                        });
                    }
                };

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

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

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

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

                    var purchaseOrderBreakdown_id = jQ(this).data('id'),
                        requestName = 'purchaseOrderBreakdownSend',
                        modal = jQ('#modal'),
                        modalContent = jQ('#modal-content');

                    global.ajaxCancel(requestName);

                    var settings = {
                        url: purchaseOrder_view.settings.purchaseOrderBreakdownSend_url,
                        type: 'POST',
                        data: {
                            purchaseOrderBreakdown_id: purchaseOrderBreakdown_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);
                }
            });
        }
    },
    bindHrefPurchaseOrderAddQuantity: function() {

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

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

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

                global.ajaxCancel(requestName);

                var settings = {
                    url: purchaseOrder_view.settings.addQuantityUrl,
                    type: 'GET',
                    data: {
                        purchaseOrder_id: purchaseOrder_view.settings.purchaseOrder_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();
                        purchaseOrder_view.bindHrefPurchaseOrderAddQuantityAction();
                        jQ('#purchaseOrderItemAddQuantity-datalist').DataTable({
                            'searching': false,
                            'paging': false,
                            'columnDefs': [
                                {'orderable': false, 'targets': [0, 1, 2, 3]}
                            ]
                        });
                        modal.foundation('open');
                    }
                };

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

        jQ('#href-purchaseOrderAddQuantity-action').unbind('click.purchaseOrderAddQuantityAction').bind('click.purchaseOrderAddQuantityAction', function(){

            var requestName = 'purchaseOrderAddQuantityAction',
                modal = jQ('#modal');

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

            var settings = {
                url: purchaseOrder_view.settings.addQuantityActionUrl,
                type: 'POST',
                data: jQ('#form-purchaseOrderVoid').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);
        });
    },
    bindHrefPurchaseOrderVoid: function() {

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

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

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

                global.ajaxCancel(requestName);

                var settings = {
                    url: purchaseOrder_view.settings.voidUrl,
                    type: 'GET',
                    data: {
                        purchaseOrder_id: purchaseOrder_view.settings.purchaseOrder_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();
                        purchaseOrder_view.bindHrefPurchaseOrderVoidAction();
                        jQ('#purchaseOrderItemVoid-datalist').DataTable({
                            'searching': false,
                            'paging': false,
                            'columnDefs': [
                                {'orderable': false, 'targets': [0, 1, 2, 3]}
                            ]
                        });
                        modal.foundation('open');
                    }
                };

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

        jQ('#href-purchaseOrderVoid-action').unbind('click.purchaseOrderVoidAction').bind('click.purchaseOrderVoidAction', function(){

            var requestName = 'purchaseOrderVoidAction',
                modal = jQ('#modal');

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

            var settings = {
                url: purchaseOrder_view.settings.voidActionUrl,
                type: 'POST',
                data: jQ('#form-purchaseOrderVoid').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() {
            purchaseOrder_view.bindHrefPurchaseOrderBreakdownForm();
            purchaseOrder_view.bindHrefPurchaseOrderBreakdownDetails();
            purchaseOrder_view.bindHrefPurchaseOrderBreakdownSend();
        };

        purchaseOrder_view.settings.purchaseOrderBreakdownDataList = jQ('#purchaseOrderBreakdown-datalist').DataTable({

            'processing': true,
            'serverSide': true,
            'ajax': {
                'url': purchaseOrder_view.settings.ajaxUrl,
                'data': function(d) {
                    d.url = global.settings.url;
                    d.purchaseOrder_id = purchaseOrder_view.settings.purchaseOrder_id;
                }
            },
            'columnDefs': [
                { 'orderable': false, 'targets': [3, 4] },
                { 'searchable': false, 'targets': [3, 4] }
            ],
            drawCallback: function() {
                callBack();
            },
            responsive: {
                details: {
                    renderer: function( api,rowIdx ) {
                        return global.dataTableResponsiveCallBack(api, rowIdx, callBack);
                    }
                }
            }
        });

        jQ('#purchaseOrderStatusHistory-datalist').DataTable({
            'order': [[0, 'desc']]
        });

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