var referring_health_facility_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: '',
        referringHealthFacility_chart_note_formActionUrl: '',
        referringHealthFacility_chart_note_formUrl: '',

        referringHealthFacilityChart_id: '',
    },

    noteDataList:null,

    init: function() {
        referring_health_facility_chart_form.initDataTable();
        referring_health_facility_chart_form.initAutocomplete();
        referring_health_facility_chart_form.endorsedShowEvent();
        referring_health_facility_chart_form.bindElements();
    },
    initAutocomplete: function() {
        global.autocomplete.bind(referring_health_facility_chart_form.settings.healthFacility_autocomplete_url, '#referringHealthFacilityChart_health_facility_desc', '#referringHealthFacilityChart_health_facility');
        global.autocomplete.bind(referring_health_facility_chart_form.settings.account_user_autocomplete_url, '#referringHealthFacilityChart_endorsereferringHealthFacilityChartent_chart_account_user_endorsed_by');
        global.autocomplete.bind(referring_health_facility_chart_form.settings.marketing_staff_autocomplete_url, '#referringHealthFacilityChart_marketingStaff_desc', '#referringHealthFacilityChart_marketingStaff');
        global.autocomplete.bind(referring_health_facility_chart_form.settings.ticket_status_autocomplete_url, '#referringHealthFacilityChart_ticket_status', '#referringHealthFacilityChart_ticket_status_id');
        global.autocomplete.bind(referring_health_facility_chart_form.settings.ticket_severity_autocomplete_url, '#referringHealthFacilityChart_ticket_severity', '#referringHealthFacilityChart_ticket_severity_id');
    },
    initDataTable: function() {
        var callBack = function() {
            referring_health_facility_chart_form.bindReferringHealthFacilityChartNoteForm();
        };

        referring_health_facility_chart_form.noteDataList = jQ('#referring-health-facility-chart-note-datalist').DataTable({
            'processing': true,
            'serverSide': true,
            'ajax': {
                'url': referring_health_facility_chart_form.settings.chart_note_ajaxUrl,
                'data': function(d) {
                    d.url = global.settings.url;
                    d.referringHealthFacilityChart_id = referring_health_facility_chart_form.settings.referringHealthFacilityChart_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('#referringHealthFacilityChart_chart_status').chained('#referringHealthFacilityChart_chart_type');
    },
    bindReferringHealthFacilityChartNoteForm: function() {
        if(jQ('.href-referring-health-facility-chart-note-form').length > 0) {

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

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

                global.ajaxCancel(requestName);

                var settings = {
                    url: referring_health_facility_chart_form.settings.referringHealthFacility_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_health_facility_chart_form.bindReferringHealthFacilityChartNoteFormAction();
                    }
                };

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

        jQ('.href-referring-health-facility-chart-note-action').unbind('click.referringHealthFacilityChartNoteFormAction').bind('click.referringHealthFacilityChartNoteFormAction', function () {

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

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

            var settings = {
                url: referring_health_facility_chart_form.settings.referringHealthFacility_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_health_facility_chart_form.noteDataList.draw();
                    } else {
                        global.parseFlashMessages('#flash-messages-content-page', 'alert', json.errorMessages);
                    }
                    global.formSubmittedReverse();
                }
            };

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

    jQ('#referringHealthFacilityChart_is_endorsed').unbind('click').bind('click', function () {
        if(this.checked){
            jQ('#referringHealthFacilityChart_endorsed_by').fadeIn(300);
            jQ('#label_status').fadeIn(300);
            jQ('#label_severity').fadeIn(300);
            jQ('#referringHealthFacilityChart_ticket_status').fadeIn(300);
            jQ('#referringHealthFacilityChart_ticket_severity').fadeIn(300);
        }else{
            jQ('#referringHealthFacilityChart_endorsed_by').slideUp(300);
            jQ('#referringHealthFacilityChart_endorsed_by').val(null);
            jQ('#label_status').slideUp(300);
            jQ('#referringHealthFacilityChart_ticket_status').slideUp(300);
            jQ('#referringHealthFacilityChart_ticket_status').val(null);
            jQ('#label_severity').slideUp(300);
            jQ('#referringHealthFacilityChart_ticket_severity').slideUp(300);
            jQ('#referringHealthFacilityChart_ticket_severity').val(null);
        }
    });

    if(jQ('#referringHealthFacilityChart_is_endorsed').prop('checked')){
        jQ('#referringHealthFacilityChart_endorsed_by').fadeIn(300);
        jQ('#label_status').fadeIn(300);
        jQ('#referringHealthFacilityChart_ticket_status').fadeIn(300);
        jQ('#label_severity').fadeIn(300);
        jQ('#referringHealthFacilityChart_ticket_severity').fadeIn(300);
    }
    },
};
