var referringProvider_detail = {
    settings: {
        referringProvider_id: null,
        referringMedicalFacilities_ajaxUrl: '',
        referringMedicalFacilities_formUrl: '',
        referringMedicalFacilities_formActionUrl: '',
    },
    referringMedicalFacilitiesDataList: null,
    init: function() {
        referringProvider_detail.initDataTable();
        referringProvider_detail.bindReferringProviderMedicalFacilityForm();
    },
    bindReferringProviderMedicalFacilityForm: function() {
        if(jQ('.href-referring-medical-facility-form').length > 0) {

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

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

                global.ajaxCancel(requestName);

                var settings = {
                    url: referringProvider_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();
                        referringProvider_detail.bindReferringProviderMedicalFacilityFormAction();
                    }
                };

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

    bindReferringProviderMedicalFacilityFormAction: function() {

        jQ('.href-referringProvider-medical-facility-action').unbind('click.referringProviderMedicalFacilityFormAction').bind('click.referringProviderMedicalFacilityFormAction', function(){

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

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

            var settings = {
                url: referringProvider_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() {
            referringProvider_detail.bindReferringProviderMedicalFacilityForm()
        };

        referringProvider_detail.referringMedicalFacilitiesDataList = jQ('#referringMedicalFacilities-datalist').DataTable({
            'processing': true,
            'serverSide': true,
            'ajax': {
                'url': referringProvider_detail.settings.referringMedicalFacilities_ajaxUrl,
                'data': function(d) {
                    d.url = global.settings.url;
                    d.referringProvider_id = referringProvider_detail.settings.referringProvider_id;
                }
            },
            'deferRender': true,
            'columnDefs': [
                { 'orderable': false, 'targets': 9 },
                { 'searchable': false, 'targets': 9 }
            ],
            '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');
    }
};