var materialRequisition_view = {
    settings: {
        materialRequisition_id: null,
        voidUrl: '',
        materialRequisitionNoteFormUrl: '',
        materialRequisitionNoteFormActionUrl: '',
        noteTable: null
    },
    init: function() {
        materialRequisition_view.initDataTable();
        materialRequisition_view.bindHrefMaterialRequisitionVoid();
        materialRequisition_view.bindHrefMaterialRequisitionNoteForm();
    },
    bindHrefMaterialRequisitionVoid: function() {

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

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

                if(confirm('Are you sure you want to void this request?')) {

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

                    global.ajaxCancel(requestName);

                    var settings = {
                        url: materialRequisition_view.settings.voidUrl,
                        type: 'POST',
                        data: {
                            materialRequisition_id: materialRequisition_view.settings.materialRequisition_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);
                }
            });
        }
    },
    initDataTable: function() {

        jQ('#materialRequisitionItem-datalist').DataTable();
        jQ('#materialRequisitionStatusHistory-datalist').DataTable({
            'order': [[0, 'desc']]
        });
        jQ('#canvass-datalist').DataTable();
        jQ('#purchaseOrder-datalist').DataTable();
        jQ('#materialRequisitionNote-datalist').DataTable();
        jQ('#purchaseOrderReceive-datalist').DataTable();
        jQ('#transmittal-datalist').DataTable();
        jQ('.content-container').removeClass('has-loading'); jQ('.content-container-content').removeClass('hide');
    },

    bindHrefMaterialRequisitionNoteForm: function() {

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

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

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

                global.ajaxCancel(requestName);

                var settings = {
                    url: materialRequisition_view.settings.materialRequisitionNoteFormUrl,
                    type: 'GET',
                    data: {
                        materialRequisition_id: materialRequisition_view.settings.materialRequisition_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');
                        modal.css('top', (jQ(window).scrollTop() + 50) + 'px').css('margin-top', '0');
                        global.bindFormSubmitted();
                        materialRequisition_view.bindHrefMaterialRequisitionNoteAction();

                    }
                };

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

        jQ('.href-materialRequisitionNote-action').unbind('click.materialRequisitionNoteAction').bind('click.materialRequisitionNoteAction', function(){

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

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

            var settings = {
                url: materialRequisition_view.settings.materialRequisitionNoteFormActionUrl,
                type: 'POST',
                data: jQ('#form-materialRequisitionNote').serialize(),
                success: function(json) {
                    global.formSubmittedReverse();
                    if(json.success) {
                        global.parseFlashMessages(global.settings.vars.flashMessagesContentId, 'success', json.successMessages);
                        modal.foundation('close');
                        location.reload();
                    } else {
                        global.parseFlashMessages('#flash-messages-content-modal', 'alert', json.errorMessages);
                    }
                }
            };

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


};