var referral_form = {
    settings: {
        city_autocomplete_url: '',
        referringProvider_autocomplete_url: '',
        referringProviderHealthFacility_autocomplete_url: '',
        referringProvider_ajax_listUrl: '',
        postalCode_autocomplete_url: '',
        healthFacility_autocomplete_url: '',
        patient_autocomplete_url: '',
        faxPreviewUrl: '',
        sFaxDetailsID: '',
        sDirection: '',
    },
    referringProviderDataList: null,
    referringHealthFacilityDataList: null,
    init: function () {
        this.initAutocomplete();
        this.initRemove();
        this.initDataTable();
        this.bindViewFaxButton();

        jQ('#referral_source').on('change', function(e) {
            jQ('.referredByText').addClass('hide');
            if(jQ('option:selected',this).text() === 'Referred By Patient'){
                jQ('.referredByText').removeClass('hide');
            }
        });
    },

    bindViewFaxButton: function(){
        jQ(".href-view-fax").click(function (e) {
            e.preventDefault();
            window.open(referral_form.settings.faxPreviewUrl,'_blank');
        })
    },

    initAutocomplete: function () {
        global.autocomplete.bind(this.settings.city_autocomplete_url, '#referral_city_desc', '#referral_city');
        global.autocomplete.bind(this.settings.city_autocomplete_url, '#referral_referringProvider_city_desc', '#referral_referringProvider_city');
        global.autocomplete.bind(this.settings.city_autocomplete_url, '#referral_referringHealthFacility_city_desc', '#referral_referringHealthFacility_city');
        global.autocomplete.bind(this.settings.postalCode_autocomplete_url, '#referral_postalCode_desc', '#referral_postalCode',{
            onSelect: function (c) {
                jQ('#referral_city_desc').val(c.city);
                jQ('#referral_city').val(c.cityId);
            }
        });
        global.autocomplete.bind(this.settings.postalCode_autocomplete_url, '#referral_referringProvider_postalCode_desc', '#referral_referringProvider_postalCode',{
            onSelect: function (c) {
                jQ('#referral_referringProvider_city_desc').val(c.city);
                jQ('#referral_referringProvider_city').val(c.cityId);
            }
        });
        global.autocomplete.bind(this.settings.postalCode_autocomplete_url, '#referral_referringHealthFacility_postalCode_desc', '#referral_referringHealthFacility_postalCode',{
            onSelect: function (c) {
                jQ('#referral_referringHealthFacility_city_desc').val(c.city);
                jQ('#referral_referringHealthFacility_city').val(c.cityId);
            }
        });
        global.autocomplete.bind(this.settings.healthFacility_autocomplete_url, '#referral_healthFacility_desc', '#referral_healthFacility');
        global.autocomplete.bind(this.settings.patient_autocomplete_url, '#referral_referredByPatient_fullName', '#referral_referredBy');
    },

    initRemove: function () {
        jQ('.remove-referringProvider-id').unbind('keyup.referringProvider').bind('keyup.referringProvider', function () {
            jQ('#referral_referringProvider_id').val('');
            referral_form.referringHealthFacilityDataList.draw();
        });
        jQ('.remove-referringProvider-id').unbind('change.referringProvider').bind('change.referringProvider', function () {
            jQ('#referral_referringProvider_id').val('');
            referral_form.referringHealthFacilityDataList.draw();
        });
        jQ('.remove-referringHealthFacility-id').unbind('keyup.referringHealthFacility').bind('keyup.referringHealthFacility', function () {
            jQ('#referral_referringHealthFacility_id').val('');
            referral_form.referringProviderDataList.draw();
        });
    },

    initDataTable: function () {
        var callBack = function() {
        };

        referral_form.referringProviderDataList = jQ('#referringProvider-datalist').DataTable({
            'processing': true,
            'serverSide': true,
            'ajax': {
                'url': referral_form.settings.referringProvider_ajax_listUrl,
                'data': function(d) {
                    d.referringHealthFacility_id = jQ('#referral_referringHealthFacility_id').val()
                }
            },
            'deferRender': true,
            drawCallback: callBack,
            responsive: {
                details: {
                    renderer: function( api,rowIdx ) {
                        return global.dataTableResponsiveCallBack(api, rowIdx, callBack);
                    }
                }
            },
            'createdRow': function (row, data) {
                jQ(row).on('click', function () {
                   jQ('#referral_referringProvider_first_name').val(data.DT_DATA.firstName);
                   jQ('#referral_referringProvider_middle_name').val(data.DT_DATA.middleName);
                   jQ('#referral_referringProvider_last_name').val(data.DT_DATA.lastName);
                   jQ('#referral_referringProvider_gender').val(data.DT_DATA.gender);
                   jQ('#referral_referringProvider_city_desc').val(data.DT_DATA.cityDesc);
                   jQ('#referral_referringProvider_city').val(data.DT_DATA.cityId);
                   jQ('#referral_referringProvider_street').val(data.DT_DATA.street);
                   jQ('#referral_referringProvider_zip_code').val(data.DT_DATA.zipCode);
                   jQ('#referral_referringProvider_id').val(data.DT_DATA.id);
                   referral_form.referringHealthFacilityDataList.draw();
                   jQ('#modal-referringProvider').foundation('close');
                   jQ('.reveal-overlay').hide();
                });
            }
        });

        var callBack2 = function() {
        };

        referral_form.referringHealthFacilityDataList = jQ('#referringHealthFacility-datalist').DataTable({
            'processing': true,
            'serverSide': true,
            'ajax': {
                'url': referral_form.settings.referringHealthFacility_ajax_listUrl,
                'data': function(d) {
                    d.referringProvider_id = jQ('#referral_referringProvider_id').val()
                }
            },
            'deferRender': true,
            drawCallback: callBack2,
            responsive: {
                details: {
                    renderer: function( api,rowIdx ) {
                        return global.dataTableResponsiveCallBack(api, rowIdx, callBack2);
                    }
                }
            },
            'createdRow': function (row, data) {
                jQ(row).on('click', function () {
                    jQ('#referral_referringHealthFacility_description').val(data.DT_DATA.description);
                    jQ('#referral_referringHealthFacility_phone_no').val(data.DT_DATA.phoneNo);
                    jQ('#referral_referringHealthFacility_fax_no').val(data.DT_DATA.faxNo);
                    jQ('#referral_referringHealthFacility_city_desc').val(data.DT_DATA.cityDesc);
                    jQ('#referral_referringHealthFacility_city').val(data.DT_DATA.cityId);
                    jQ('#referral_referringHealthFacility_street').val(data.DT_DATA.street);
                    jQ('#referral_referringHealthFacility_zip_code').val(data.DT_DATA.zipCode);
                    jQ('#referral_referringHealthFacility_id').val(data.DT_DATA.id);
                    referral_form.referringProviderDataList.draw();
                    jQ('#modal-referringHealthFacility').foundation('close');
                    jQ('.reveal-overlay').hide();
                });
            }
        });
    }


};