var patient_schedule_form = {
    settings: {
        noteTemplateAccess: '',
        medical_facility_autocomplete_url: '',
        note_template_data_url: '',
        provider_autocomplete_url: '',
        patient_referring_provider_autocompleteUrl: '',
        patient_referring_health_facility_autocompleteUrl: '',
        patient_insurance_autocompleteUrl: '',
        chartType_autocompleteUrl: '',
        patientId: '',
        scheduleId: '',
        axn: '',
        appointmentNoticeFax_ajaxUrl: ''
    },
    init: function() {
        this.initAutocomplete();
        this.bindNoteTemplate();
        this.initDataTable();
        if(patient_schedule_form.settings.noteTemplateAccess == false) {
            jQ('#patient_schedule_note_type').children('option[value=Template]').addClass('hide');
        }
    },
    initAutocomplete: function() {
        global.autocomplete.bind(this.settings.medical_facility_autocomplete_url, '#patient_schedule_health_facility_desc', '#patient_schedule_health_facility');
        global.autocomplete.bind(this.settings.provider_autocomplete_url, '#patient_schedule_provider_fullName', '#patient_schedule_provider');
        global.autocomplete.bind(this.settings.chartType_autocompleteUrl, '#patient_schedule_chartType_desc', '#patient_schedule_chartType');
        var
            params = {
                patient_id : this.settings.patientId,
                schedule_id: this.settings.scheduleId
            };
        global.autocomplete.bind(this.settings.patient_referring_provider_autocompleteUrl, '#appointment_notice_fax_patient_referring_provider_desc', '#appointment_notice_fax_patient_referring_provider', {});
        global.autocomplete.bind(this.settings.patient_referring_health_facility_autocompleteUrl, '#appointment_notice_fax_patient_referring_health_facility_desc', '#appointment_notice_fax_patient_referring_health_facility');
        global.autocomplete.bind(this.settings.patient_insurance_autocompleteUrl, '#appointment_notice_fax_patient_insurance_desc', '#appointment_notice_fax_patient_insurance');
        jQ('#appointment_notice_fax_patient_referring_provider_desc').devbridgeAutocomplete('setOptions', {params: params});
        jQ('#appointment_notice_fax_patient_referring_health_facility_desc').devbridgeAutocomplete('setOptions', {params: params});
        jQ('#appointment_notice_fax_patient_insurance_desc').devbridgeAutocomplete('setOptions', {params: params});

        jQ("#appointment_notice_fax_affiliate_type").unbind('change').bind('change',function () {
            if(jQ(this).val() === 'Insurance'){
                jQ('#affiliate-Insurance').removeClass('hide');
                jQ('#create-new-appointment-notice-save').removeClass('hide');
                jQ('#affiliate-Referring-HealthFacility, #affiliate-Referring-Provider').addClass('hide');
                jQ('#appointment_notice_fax_patient_referring_health_facility_desc, #appointment_notice_fax_patient_referring_health_facility, #appointment_notice_fax_patient_referring_provider_desc, #appointment_notice_fax_patient_referring_provider').val(null);
            }else if(jQ(this).val() === 'Referring Affiliate'){
                jQ('#affiliate-Referring-HealthFacility').removeClass('hide');
                jQ('#create-new-appointment-notice-save').removeClass('hide');
                jQ('#affiliate-Referring-Provider, #affiliate-Insurance').addClass('hide');
                jQ('#patient_letter_patient_insurance_desc, #patient_letter_patient_insurance, #appointment_notice_fax_patient_referring_provider_desc, #appointment_notice_fax_patient_referring_provider').val(null);
            }else if(jQ(this).val() === 'Referring Physician'){
                jQ('#affiliate-Referring-Provider').removeClass('hide');
                jQ('#create-new-appointment-notice-save').removeClass('hide');
                jQ('#affiliate-Referring-HealthFacility, #affiliate-Insurance').addClass('hide');
                jQ('#appointment_notice_fax_patient_insurance_desc, #appointment_notice_fax_patient_insurance, #appointment_notice_fax_patient_referring_health_facility_desc, #appointment_notice_fax_patient_referring_health_facility').val(null);
            }else{
                jQ('#affiliate-Referring-HealthFacility, #affiliate-Referring-Provider').addClass('hide');
                jQ('#affiliate-Referring-Provider, #affiliate-Insurance').addClass('hide');
                jQ('#affiliate-Referring-HealthFacility, #affiliate-Insurance').addClass('hide');
                jQ('#create-new-appointment-notice-save').addClass('hide');
                jQ('#appointment_notice_fax_patient_insurance_desc, #appointment_notice_fax_patient_insurance, #appointment_notice_fax_patient_referring_health_facility_desc, #appointment_notice_fax_patient_referring_health_facility').val(null);
            }
        });
    },
    bindNoteTemplate: function () {
        jQ('#patient_schedule_note_type').unbind('change').bind('change', function () {
            if (jQ(this).val() == 'Template') {
                jQ('#patient_schedule_note_template_description').removeClass('hide');
            } else {
                jQ('#patient_schedule_note_template_description').addClass('hide');
            }
        });

        global.autocomplete.bind(patient_schedule_form.settings.note_template_autocomplete_url, '#patient_schedule_note_template_description', '#patient_schedule_noteTemplate', {
            onSelect: function (d) {
                global.ajax({
                    url: patient_schedule_form.settings.note_template_data_url+'/'+d.noteTemplate_description,
                    type: 'GET',
                    beforeSend: function() {
                        jQ('#patient_schedule_note').val('');
                    },
                    success: function(json) {
                        jQ('#patient_schedule_note').val(json.note);
                    },
                });
            }
        });
    },
    initDataTable: function() {
        var callBack = function() {
        };

        var dataList = jQ('#appointmentNoticeFax-datalist').DataTable({
            'processing': true,
            'serverSide': true,
            'ajax': {
                'url': this.settings.appointmentNoticeFax_ajaxUrl,
                'data': function(d) {
                    d.url = global.settings.url;
                    d.patient_id = patient_schedule_form.settings.patientId;
                    d.scheduleId = patient_schedule_form.settings.scheduleId;
                }
            },
            'deferRender': true,
            'columnDefs': [
                { 'orderable': false, 'targets': 4 },
                { 'searchable': false, 'targets': 4 }
            ],
            drawCallback: function() {
                callBack();
            },
            responsive: {
                details: {
                    renderer: function( api,rowIdx ) {
                        return global.dataTableResponsiveCallBack(api, rowIdx, callBack);
                    }
                }
            }
        });
    }
};