var referral = {
    settings: {
        ajaxUrl: '',
        referralPatientApproval_formUrl: '',
        referralPatientApproval_formActionUrl:'',
        referralPatientDenied_formActionUrl: '',
    },
    dataList: null,
    init: function() {
        referral.initDataTable();
        referral.bindReferralApprovalForm();
    },

    bindReferralApprovalForm: function() {

        if(jQ('.href-referral-patient-approval-form').length > 0) {

            jQ('.href-referral-patient-approval-form').unbind('click').bind('click', function(){

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

                global.ajaxCancel(requestName);

                var settings = {
                    url: referral.settings.referralPatientApproval_formUrl + '/' + jQ(this).data('id'),
                    type: 'GET',
                    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');
                        global.bindFormSubmitted();
                        referral.bindReferralApprovalFormAction();
                        referral.bindReferralDeniedFormAction();
                    }
                };

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

        jQ('.href-referral-patient-approved-action').unbind('click.referralApprovalFormAction').bind('click.referralApprovalFormAction', function(){

            var requestName = 'referralApprovalFormAction',
                _form = jQ('#form-referralApprovalFormAction');

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

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

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

        jQ('.href-referral-patient-denied-action').unbind('click.referralDeniedFormAction').bind('click.referralDeniedFormAction', function(){

            var requestName = 'referralDeniedFormAction',
                _form = jQ('#form-referralDeniedFormAction');

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

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

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

        var callBack = function() {
            referral.bindReferralApprovalForm();
        };

        referral.dataList = jQ('#referral-datalist').DataTable({
            'processing': true,
            'serverSide': true,
            'ajax': {
                'url': referral.settings.ajaxUrl,
                'data': function(d) {
                    d.url = global.settings.url;
                    d.month = jQ("#month-filter-type").val();
                }
            },
            'deferRender': true,
            'columnDefs': [
                { 'orderable': false, 'targets': [7, 8] },
                { 'searchable': false, 'targets': [7, 8] }
            ],
            drawCallback: function() {
                callBack();
            },
            responsive: {
                details: {
                    renderer: function( api,rowIdx ) {
                        return global.dataTableResponsiveCallBack(api, rowIdx, callBack);
                    }
                }
            }
        });

        jQ('#month-filter-type').on('change', function(){
            referral.dataList.draw();
        });

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