var marketing_staff_detail = {
    settings: {
        marketing_staff_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() {
        marketing_staff_detail.initDataTable();
        marketing_staff_detail.bindMarketingStaffMedicalFacilityForm();
        marketing_staff_detail.bindMarketingStaffWorkScheduleForm();
    },
    bindMarketingStaffMedicalFacilityForm: function() {
        if(jQ('.href-medical-facility-form').length > 0) {

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

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

                global.ajaxCancel(requestName);

                var settings = {
                    url: marketing_staff_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();
                        marketing_staff_detail.bindMarketingStaffMedicalFacilityFormAction();
                        global.autocomplete.bind(marketing_staff_detail.settings.medicalFacility_autocompleteUrl, '#marketing_staff_health_facility_health_facility_desc', '#marketing_staff_health_facility_health_facility');
                    }
                };

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

    bindMarketingStaffMedicalFacilityFormAction: function() {

        jQ('.href-marketing-staff-medical-facility-action').unbind('click.marketingStaffMedicalFacilityFormAction').bind('click.marketingStaffMedicalFacilityFormAction', function(){

            var requestName = 'marketingStaffMedicalFacilityFormAction',
                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: marketing_staff_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);
        });
    },
    bindMarketingStaffWorkScheduleForm: function() {
        if(jQ('.href-work-schedule-form').length > 0) {

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

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

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

                var settings = {
                    url: marketing_staff_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();
                        marketing_staff_detail.bindMarketingStaffWorkScheduleFormAction();
                        global.autocomplete.bind(marketing_staff_detail.settings.medicalFacility_autocompleteUrl, '#marketingStaffWorkSchedule_health_facility_desc', '#marketingStaffWorkSchedule_health_facility');

                        jQ('#marketingStaffWorkSchedule_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('#marketingStaffWorkSchedule_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('#marketingStaffWorkSchedule_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('#marketingStaffWorkSchedule_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);
            });
        }
    },
    bindMarketingStaffWorkScheduleFormAction: function() {

        jQ('.href-marketing-staff-work-schedule-action').unbind('click.marketingStaffWorkScheduleFormAction').bind('click.marketingStaffWorkScheduleFormAction', function(){

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

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

            var settings = {
                url: marketing_staff_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() {

        var callBack1 = function() {
            marketing_staff_detail.bindMarketingStaffMedicalFacilityForm()
        };

        marketing_staff_detail.medicalFacilitiesDataList = jQ('#medicalFacilities-datalist').DataTable({
            'processing': true,
            'serverSide': true,
            'ajax': {
                'url': marketing_staff_detail.settings.medicalFacilities_ajaxUrl,
                'data': function(d) {
                    d.url = global.settings.url;
                    d.marketing_staff_id = marketing_staff_detail.settings.marketing_staff_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);
                    }
                }
            }
        });

        var callBack2 = function() {
            marketing_staff_detail.bindMarketingStaffWorkScheduleForm()
        };

        marketing_staff_detail.workScheduleDataList = jQ('#workSchedule-datalist').DataTable({
            'processing': true,
            'serverSide': true,
            'ajax': {
                'url': marketing_staff_detail.settings.workSchedule_ajaxUrl,
                'data': function(d) {
                    d.url = global.settings.url;
                    d.marketing_staff_id = marketing_staff_detail.settings.marketing_staff_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(){
            marketing_staff_detail.workScheduleDataList.draw();
        });


        if(jQ('#workScheduleHistory-datalist').length > 0) {
            var callBack3 = function() {
            };
            marketing_staff_detail.workScheduleHistoryDataList = jQ('#workScheduleHistory-datalist').DataTable({
                'processing': true,
                'serverSide': true,
                'ajax': {
                    'url': marketing_staff_detail.settings.workScheduleHistory_ajaxUrl,
                    'data': function(d) {
                        d.url = global.settings.url;
                        d.marketing_staff_id = marketing_staff_detail.settings.marketing_staff_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);
                        }
                    }
                }
            });
        }


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