var referring_provider_chart_form = {
    settings: {
        healthFacility_autocomplete_url: '',
        account_user_autocomplete_url: '',
        marketing_staff_autocomplete_url: '',
        diagnosis_autocomplete_url: '',
        chart_type_method_autocomplete_url: '',

        chart_note_ajaxUrl: '',
        referringProvider_chart_note_formActionUrl: '',
        referringProvider_chart_note_formUrl: '',

        referringProviderChart_id: '',

        note_template_autocomplete_url: '',
        note_template_data_url: '',
        noteTemplateAccess: '',
    },

    noteDataList:null,

    init: function() {
        referring_provider_chart_form.initDataTable();
        referring_provider_chart_form.initAutocomplete();
        referring_provider_chart_form.endorsedShowEvent();
        referring_provider_chart_form.bindElements();
        referring_provider_chart_form.bindNoteTemplate();

        if(referring_provider_chart_form.settings.noteTemplateAccess == false) {
            jQ('#referringProviderChart_note_type').children('option[value=Template]').addClass('hide');
        }
    },
    initAutocomplete: function() {
        global.autocomplete.bind(referring_provider_chart_form.settings.healthFacility_autocomplete_url, '#referringProviderChart_health_facility_desc', '#referringProviderChart_health_facility');
        global.autocomplete.bind(referring_provider_chart_form.settings.account_user_autocomplete_url, '#referringProviderChart_endorsereferringProviderChartent_chart_account_user_endorsed_by');
        global.autocomplete.bind(referring_provider_chart_form.settings.marketing_staff_autocomplete_url, '#referringProviderChart_marketingStaff_desc', '#referringProviderChart_marketingStaff');
        global.autocomplete.bind(referring_provider_chart_form.settings.ticket_status_autocomplete_url, '#referringProviderChart_ticket_status', '#referringProviderChart_ticket_status_id');
        global.autocomplete.bind(referring_provider_chart_form.settings.ticket_severity_autocomplete_url, '#referringProviderChart_ticket_severity', '#referringProviderChart_ticket_severity_id');
    },
    initDataTable: function() {
        var callBack = function() {
            referring_provider_chart_form.bindReferringProviderChartNoteForm();
        };

        referring_provider_chart_form.noteDataList = jQ('#referring-provider-chart-note-datalist').DataTable({
            'processing': true,
            'serverSide': true,
            'ajax': {
                'url': referring_provider_chart_form.settings.chart_note_ajaxUrl,
                'data': function(d) {
                    d.url = global.settings.url;
                    d.referringProviderChart_id = referring_provider_chart_form.settings.referringProviderChart_id;
                }
            },
            'deferRender': true,
            'columnDefs': [
                { 'orderable': false, 'targets': 3 },
                { 'searchable': false, 'targets': 3 }
            ],
            'orderClasses': false,
            drawCallback: function() {
                callBack();
            },
            responsive: {
                details: {
                    renderer: function( api,rowIdx ) {
                        return global.dataTableResponsiveCallBack(api, rowIdx, callBack);
                    }
                }
            }
        });

        jQ('.content-container').removeClass('has-loading');
        jQ('.content-container-content').removeClass('hide');
    },
    bindElements: function(){
        jQ('#referringProviderChart_chart_status').chained('#referringProviderChart_chart_type');
    },
    bindReferringProviderChartNoteForm: function() {
        if(jQ('.href-referring-provider-chart-note-form').length > 0) {

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

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

                global.ajaxCancel(requestName);

                var settings = {
                    url: referring_provider_chart_form.settings.referringProvider_chart_note_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();
                        referring_provider_chart_form.bindReferringProviderChartNoteFormAction();
                    }
                };

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

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

            var requestName = 'referringProviderChartNoteFormAction',
                _form = jQ('#form-referringProviderChartNote'),
                modal = jQ('#modal-small'),
                axn = jQ(this).data('axn');
            _form.find('.form-action').val(axn);

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

            var settings = {
                url: referring_provider_chart_form.settings.referringProvider_chart_note_formActionUrl,
                type: 'POST',
                data: _form.serialize(),
                success: function (json) {
                    if (json.success) {
                        modal.foundation('close');
                        global.parseFlashMessages('#flash-messages-content-page', 'success', json.successMessages);
                        referring_provider_chart_form.noteDataList.draw();
                    } else {
                        global.parseFlashMessages('#flash-messages-content-page', 'alert', json.errorMessages);
                    }
                    global.formSubmittedReverse();
                }
            };

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

    jQ('#referringProviderChart_is_endorsed').unbind('click').bind('click', function () {
        if(this.checked){
            jQ('#referringProviderChart_endorsed_by').fadeIn(300);
            jQ('#label_status').fadeIn(300);
            jQ('#label_severity').fadeIn(300);
            jQ('#referringProviderChart_ticket_status').fadeIn(300);
            jQ('#referringProviderChart_ticket_severity').fadeIn(300);
        }else{
            jQ('#referringProviderChart_endorsed_by').slideUp(300);
            jQ('#referringProviderChart_endorsed_by').val(null);
            jQ('#label_status').slideUp(300);
            jQ('#referringProviderChart_ticket_status').slideUp(300);
            jQ('#referringProviderChart_ticket_status').val(null);
            jQ('#label_severity').slideUp(300);
            jQ('#referringProviderChart_ticket_severity').slideUp(300);
            jQ('#referringProviderChart_ticket_severity').val(null);
        }
    });

    if(jQ('#referringProviderChart_is_endorsed').prop('checked')){
        jQ('#referringProviderChart_endorsed_by').fadeIn(300);
        jQ('#label_status').fadeIn(300);
        jQ('#referringProviderChart_ticket_status').fadeIn(300);
        jQ('#label_severity').fadeIn(300);
        jQ('#referringProviderChart_ticket_severity').fadeIn(300);
    }
    },
    bindNoteTemplate: function () {
        jQ('#referringProviderChart_note_type').unbind('change').bind('change', function () {
            if (jQ(this).val() == 'Template') {
                jQ('#referringProviderChart_note_template_description').removeClass('hide');
            } else {
                jQ('#referringProviderChart_note_template_description').addClass('hide');
            }
        });

        global.autocomplete.bind(referring_provider_chart_form.settings.note_template_autocomplete_url, '#referringProviderChart_note_template_description', '#referringProviderChart_noteTemplate', {
            onSelect: function (d) {
                global.ajax({
                    url: referring_provider_chart_form.settings.note_template_data_url+'/'+d.noteTemplate_description,
                    type: 'GET',
                    beforeSend: function() {
                        jQ('#referringProviderChart_note').val('');
                    },
                    success: function(json) {
                        jQ('#referringProviderChart_note').val(json.note);
                    },
                });
            }
        });
    },
};
