var salesAgent_detail = {
    settings: {
        salesAgent_id: null,
        referringMedicalFacilities_ajaxUrl: '',
        referringMedicalFacilities_formUrl: '',
        referringMedicalFacilities_formActionUrl: '',
    },
    referringMedicalFacilitiesDataList: null,
    init: function() {
        salesAgent_detail.initDataTable();
        salesAgent_detail.bindReferringMedicalFacilitySalesAgentForm();
    },
    bindReferringMedicalFacilitySalesAgentForm: function() {
        if(jQ('.href-referring-health-facility-form').length > 0) {

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

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

                global.ajaxCancel(requestName);

                var settings = {
                    url: salesAgent_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();
                        salesAgent_detail.bindReferringMedicalFacilitySalesAgentFormAction();
                    }
                };

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

    bindReferringMedicalFacilitySalesAgentFormAction: function() {

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

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

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

            var settings = {
                url: salesAgent_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() {
            salesAgent_detail.bindReferringMedicalFacilitySalesAgentForm()
        };

        salesAgent_detail.referringMedicalFacilitiesDataList = jQ('#referringHealthFacility-datalist').DataTable({
            'processing': true,
            'serverSide': true,
            'ajax': {
                'url': salesAgent_detail.settings.referringMedicalFacilities_ajaxUrl,
                'data': function(d) {
                    d.url = global.settings.url;
                    d.salesAgent_id = salesAgent_detail.settings.salesAgent_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');
    }
};