var referringHealthFacility_detail = {
    settings: {
        referringHealthFacility_id: null,
        referringMedicalFacilities_ajaxUrl: '',
        referringMedicalFacilities_formUrl: '',
        referringMedicalFacilities_formActionUrl: '',

        referringSalesAgent_ajaxUrl: '',
        referringSalesAgent_formUrl: '',
        referringSalesAgent_formActionUrl: '',
    },
    referringMedicalFacilitiesDataList: null,
    referringSalesAgentDataList: null,
    init: function() {
        referringHealthFacility_detail.initDataTable();
        referringHealthFacility_detail.initDataTable2();
        referringHealthFacility_detail.bindReferringMedicalFacilityProviderForm();
        referringHealthFacility_detail.bindReferringMedicalFacilitySalesAgentForm();
    },
    bindReferringMedicalFacilityProviderForm: function() {
        if(jQ('.href-referring-provider-form').length > 0) {

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

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

                global.ajaxCancel(requestName);

                var settings = {
                    url: referringHealthFacility_detail.settings.referringMedicalFacilities_formUrl + '/' + jQ(this).data('action') + '/' + 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();
                        referringHealthFacility_detail.bindReferringMedicalFacilityProviderFormAction();
                    }
                };

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

    bindReferringMedicalFacilitySalesAgentForm: function() {
        if(jQ('.href-referring-sales_agent-form').length > 0) {

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

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

                global.ajaxCancel(requestName);

                var settings = {
                    url: referringHealthFacility_detail.settings.referringSalesAgent_formUrl + '/' + jQ(this).data('action') + '/' + 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();
                        referringHealthFacility_detail.bindReferringMedicalFacilitySalesAgentFormAction();
                    }
                };

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

    bindReferringMedicalFacilityProviderFormAction: function() {

        jQ('.href-referring-provider-action').unbind('click.referringHealthFacilityMedicalFacilityFormAction').bind('click.referringHealthFacilityMedicalFacilityFormAction', function(){

            var requestName = 'referringHealthFacilityMedicalFacilityFormAction',
                modal = jQ('#modal'),
                modalContent = jQ('#modal-content'),
                _form = jQ('#form-referringProvider'),
            axn = jQ(this).data('axn');
            _form.find('.form-action').val(axn);

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

            var settings = {
                url: referringHealthFacility_detail.settings.referringMedicalFacilities_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 callBack1 = function() {
            referringHealthFacility_detail.bindReferringMedicalFacilityProviderForm()
        };
        referringHealthFacility_detail.referringMedicalFacilitiesDataList = jQ('#referringProviders-datalist').DataTable({
            'processing': true,
            'serverSide': true,
            'ajax': {
                'url': referringHealthFacility_detail.settings.referringMedicalFacilities_ajaxUrl,
                'data': function(d) {
                    d.url = global.settings.url;
                    d.referringHealthFacility_id = referringHealthFacility_detail.settings.referringHealthFacility_id;
                }
            },
            'deferRender': true,
            'columnDefs': [
                { 'orderable': false, 'targets': 7 },
                { 'searchable': false, 'targets': 7 }
            ],
            'orderClasses': false,
            drawCallback: function() {
                callBack1();
            },
            responsive: {
                details: {
                    renderer: function( api,rowIdx ) {
                        return global.dataTableResponsiveCallBack(api, rowIdx, callBack1);
                    }
                }
            }
        });

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

    bindReferringMedicalFacilitySalesAgentFormAction: function() {

        jQ('.href-referring-sales-agent-action').unbind('click.referringHealthFacilitySalesAgentFormAction').bind('click.referringHealthFacilitySalesAgentFormAction', function(){

            var requestName = 'referringHealthFacilitySalesAgentFormAction',
                modal = jQ('#modal'),
                modalContent = jQ('#modal-content'),
                _form = jQ('#form-salesAgent'),
                axn = jQ(this).data('axn');
            _form.find('.form-action').val(axn);

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

            var settings = {
                url: referringHealthFacility_detail.settings.referringSalesAgent_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);
        });
    },
    initDataTable2: function() {

        var callBack2 = function() {
            referringHealthFacility_detail.bindReferringMedicalFacilitySalesAgentForm()
        };

        referringHealthFacility_detail.referringSalesAgentDataList = jQ('#referringSalesAgent-datalist').DataTable({
            'processing': true,
            'serverSide': true,
            'ajax': {
                'url': referringHealthFacility_detail.settings.referringSalesAgent_ajaxUrl,
                'data': function(d) {
                    d.url = global.settings.url;
                    d.referringHealthFacility_id = referringHealthFacility_detail.settings.referringHealthFacility_id;
                }
            },
            'deferRender': true,
            'columnDefs': [
                { 'orderable': false, 'targets': 3 },
                { 'searchable': false, 'targets': 3 }
            ],
            'orderClasses': false,
            drawCallback: function() {
                callBack2();
            },
            responsive: {
                details: {
                    renderer: function( api,rowIdx ) {
                        return global.dataTableResponsiveCallBack(api, rowIdx, callBack2);
                    }
                }
            }
        });

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