var provider_detail = {
    settings: {
        provider_id: null,
        medicalFacilities_ajaxUrl: '',
        workSchedule_ajaxUrl: '',
        medicalFacilities_formUrl: '',
        medicalFacilities_formActionUrl: '',
        workSchedule_formUrl: '',
        workSchedule_formActionUrl: '',
        medicalFacility_autocompleteUrl: '',
        workScheduleHistory_ajaxUrl: ''
    },
    medicalFacilitiesDataList: null,
    workScheduleDataList: null,
    workScheduleHistoryDataList: null,
    init: function() {
        // provider_detail.initDataTable();
        provider_detail.bindProviderMedicalFacilityForm();
        provider_detail.bindProviderWorkScheduleForm();

        jQ('#provider-tabs').on('change.zf.tabs', function () {
            const id = jQ(this).find('.tabs-title.is-active').data('dt-id');
            provider_detail.initDataTable(id);
        });

        const id = jQ('#provider-tabs').find('.tabs-title.is-active').data('dt-id');
        provider_detail.initDataTable(id);
    },
    bindProviderMedicalFacilityForm: function() {
        if(jQ('.href-medical-facility-form').length > 0) {

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

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

                global.ajaxCancel(requestName);

                var settings = {
                    url: provider_detail.settings.medicalFacilities_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();
                        provider_detail.bindProviderMedicalFacilityFormAction();
                        global.autocomplete.bind(provider_detail.settings.medicalFacility_autocompleteUrl, '#provider_health_facility_health_facility_desc', '#provider_health_facility_health_facility');
                    }
                };

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

    bindProviderMedicalFacilityFormAction: function() {

        jQ('.href-provider-medical-facility-action').unbind('click.providerMedicalFacilityFormAction').bind('click.providerMedicalFacilityFormAction', function(){

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

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

            var settings = {
                url: provider_detail.settings.medicalFacilities_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);
        });
    },
    bindProviderWorkScheduleForm: function() {
        if(jQ('.href-work-schedule-form').length > 0) {

            jQ('.href-work-schedule-form').unbind('click').bind('click', function(){

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

                global.ajaxCancel(requestName);
                var axn = jQ(this).data('action');

                var settings = {
                    url: provider_detail.settings.workSchedule_formUrl + '/' + axn + '/' + 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();
                        provider_detail.bindProviderWorkScheduleFormAction();
                        global.autocomplete.bind(provider_detail.settings.medicalFacility_autocompleteUrl, '#providerWorkSchedule_health_facility_desc', '#providerWorkSchedule_health_facility');

                        jQ('#providerWorkSchedule_type').unbind('change').bind('change', function () {
                            if(jQ(this).val() === 'Regular'){
                                jQ('.remove-val').val('');
                                jQ('#workSchedule-fromTime').removeClass('hide');
                                jQ('#workSchedule-toTime').removeClass('hide');
                                jQ('#workSchedule-dayName').removeClass('hide');
                                jQ('#workSchedule-facility').removeClass('hide');
                                jQ('#workSchedule-fromDate').addClass('hide');
                                jQ('#workSchedule-toDate').addClass('hide');
                                jQ('#workSchedule-note').addClass('hide');
                            }else if(jQ(this).val() === 'Irregular'){
                                jQ('.remove-val').val('');
                                jQ('#workSchedule-fromTime').removeClass('hide');
                                jQ('#workSchedule-toTime').removeClass('hide');
                                jQ('#workSchedule-fromDate').removeClass('hide');
                                jQ('#workSchedule-toDate').removeClass('hide');
                                jQ('#workSchedule-facility').removeClass('hide');
                                jQ('#workSchedule-dayName').addClass('hide');
                                jQ('#workSchedule-note').addClass('hide');
                            }else if(jQ(this).val() === 'Out'){
                                jQ('.remove-val').val('');
                                jQ('#workSchedule-fromTime').removeClass('hide');
                                jQ('#workSchedule-toTime').removeClass('hide');
                                jQ('#workSchedule-fromDate').removeClass('hide');
                                jQ('#workSchedule-toDate').removeClass('hide');
                                jQ('#workSchedule-note').removeClass('hide');
                                jQ('#workSchedule-facility').addClass('hide');
                                jQ('#workSchedule-dayName').addClass('hide');
                            }else{
                                jQ('.remove-val').val('');
                                jQ('#workSchedule-fromTime').addClass('hide');
                                jQ('#workSchedule-toTime').addClass('hide');
                                jQ('#workSchedule-fromDate').addClass('hide');
                                jQ('#workSchedule-toDate').addClass('hide');
                                jQ('#workSchedule-note').addClass('hide');
                                jQ('#workSchedule-facility').addClass('hide');
                                jQ('#workSchedule-dayName').addClass('hide');
                            }
                        });
                        if(axn !== 'n'){
                            if(jQ('#providerWorkSchedule_type').val() === 'Regular'){
                                jQ('#workSchedule-fromTime').removeClass('hide');
                                jQ('#workSchedule-toTime').removeClass('hide');
                                jQ('#workSchedule-dayName').removeClass('hide');
                                jQ('#workSchedule-facility').removeClass('hide');
                                jQ('#workSchedule-fromDate').addClass('hide');
                                jQ('#workSchedule-toDate').addClass('hide');
                                jQ('#workSchedule-note').addClass('hide');
                            }else if(jQ('#providerWorkSchedule_type').val() === 'Irregular'){
                                jQ('#workSchedule-fromTime').removeClass('hide');
                                jQ('#workSchedule-toTime').removeClass('hide');
                                jQ('#workSchedule-fromDate').removeClass('hide');
                                jQ('#workSchedule-toDate').removeClass('hide');
                                jQ('#workSchedule-facility').removeClass('hide');
                                jQ('#workSchedule-dayName').addClass('hide');
                                jQ('#workSchedule-note').addClass('hide');
                            }else if(jQ('#providerWorkSchedule_type').val() === 'Out'){
                                jQ('#workSchedule-fromTime').removeClass('hide');
                                jQ('#workSchedule-toTime').removeClass('hide');
                                jQ('#workSchedule-fromDate').removeClass('hide');
                                jQ('#workSchedule-toDate').removeClass('hide');
                                jQ('#workSchedule-note').removeClass('hide');
                                jQ('#workSchedule-facility').addClass('hide');
                                jQ('#workSchedule-dayName').addClass('hide');
                            }
                        }
                    }
                };

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

        jQ('.href-provider-work-schedule-action').unbind('click.providerWorkScheduleFormAction').bind('click.providerWorkScheduleFormAction', function(){

            var requestName = 'providerWorkScheduleFormAction',
                _form = jQ('#form-workSchedule'),
                axn = jQ(this).data('axn');
            _form.find('.form-action').val(axn);

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

            var settings = {
                url: provider_detail.settings.workSchedule_formActionUrl,
                type: 'POST',
                data: _form.serialize(),
                success: function(json) {
                    if(json.success) {
                        //location.reload();
                        let url = window.location.href;
                        if (url.indexOf('?') > -1){
                            url += '&tab=ps'
                        }else{
                            url += '?tab=ps'
                        }
                        window.location.href = url;
                    } else {
                        global.formSubmittedReverse();
                        global.parseFlashMessages('#flash-messages-content-modal', 'alert', json.errorMessages);
                    }
                }
            };

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

        if (jQ.fn.DataTable.isDataTable('#'+dt_id)) {
            return;
        }

        switch (dt_id) {

            case 'medicalFacilities-datalist':

                var callBack1 = function() {
                    provider_detail.bindProviderMedicalFacilityForm()
                };
                provider_detail.medicalFacilitiesDataList = jQ('#medicalFacilities-datalist').DataTable({
                    'processing': true,
                    'serverSide': true,
                    'ajax': {
                        'url': provider_detail.settings.medicalFacilities_ajaxUrl,
                        'data': function(d) {
                            d.url = global.settings.url;
                            d.provider_id = provider_detail.settings.provider_id;
                        }
                    },
                    'deferRender': true,
                    'columnDefs': [
                        { 'orderable': false, 'targets': 9 },
                        { 'searchable': false, 'targets': 9 }
                    ],
                    'orderClasses': false,
                    drawCallback: function() {
                        callBack1();
                    },
                    responsive: {
                        details: {
                            renderer: function( api,rowIdx ) {
                                return global.dataTableResponsiveCallBack(api, rowIdx, callBack1);
                            }
                        }
                    }
                });

                break;
            case 'workSchedule-datalist':

                var callBack2 = function() {
                    provider_detail.bindProviderWorkScheduleForm()
                };
                provider_detail.workScheduleDataList = jQ('#workSchedule-datalist').DataTable({
                    'processing': true,
                    'serverSide': true,
                    'ajax': {
                        'url': provider_detail.settings.workSchedule_ajaxUrl,
                        'data': function(d) {
                            d.url = global.settings.url;
                            d.provider_id = provider_detail.settings.provider_id;
                            d.type = jQ('#work-schedule-filter-type').val();
                        }
                    },
                    'deferRender': true,
                    'columnDefs': [
                        { 'orderable': false, 'targets': 8},
                        { 'searchable': false, 'targets': 8},
                    ],
                    'orderClasses': false,
                    drawCallback: function() {
                        callBack2();
                    },
                    responsive: {
                        details: {
                            renderer: function( api,rowIdx ) {
                                return global.dataTableResponsiveCallBack(api, rowIdx, callBack2);
                            }
                        }
                    }
                });

                jQ('#work-schedule-filter-type').on('change', function(){
                    provider_detail.workScheduleDataList.draw();
                });

                if(jQ('#workScheduleHistory-datalist').length > 0) {
                    var callBack3 = function() {
                    };
                    provider_detail.workScheduleHistoryDataList = jQ('#workScheduleHistory-datalist').DataTable({
                        'processing': true,
                        'serverSide': true,
                        'ajax': {
                            'url': provider_detail.settings.workScheduleHistory_ajaxUrl,
                            'data': function(d) {
                                d.url = global.settings.url;
                                d.provider_id = provider_detail.settings.provider_id;
                            }
                        },
                        'deferRender': true,
                        'columnDefs': [
                            { "orderable": false, "targets": 0 },
                            { "orderable": false, "targets": 1 },
                            { "orderable": false, "targets": 2 },
                            { "orderable": false, "targets": 3 },
                        ],
                        'orderClasses': false,
                        drawCallback: function() {
                            callBack3();
                        },
                        responsive: {
                            details: {
                                renderer: function( api,rowIdx ) {
                                    return global.dataTableResponsiveCallBack(api, rowIdx, callBack2);
                                }
                            }
                        }
                    });
                }

                break;
        }

        jQ('.content-container').removeClass('has-loading');
        jQ('.content-container-content').removeClass('hide');
    }
};