/*
Theme Name: NeopsStudios Child Theme
Theme URI: https://neopsstudios.com
Template: hello-elementor
Author: NeopsStudios Team
Author URI: https://neopsstudios.com
Description: Hello Elementor is a lightweight and minimalist WordPress theme that was built specifically to work seamlessly with the Elementor site builder plugin. The theme is free, open-source, and designed for users who want a flexible, easy-to-use, and customizable website. The theme, which is optimized for performance, provides a solid foundation for users to build their own unique designs using the Elementor drag-and-drop site builder. Its simplicity and flexibility make it a great choice for both beginners and experienced Web Creators.
Tags: accessibility-ready,flexible-header,custom-colors,custom-menu,custom-logo,featured-images,rtl-language-support,threaded-comments,translation-ready
Version: 3.0.1.1713868653
Updated: 2024-04-23 12:37:33

*/
var_dump('entra');
function insertar_script_personalizado() {
    ?>
    <script type="text/javascript">
        console.log('Script personalizado cargado.');

        jQuery(document).ready(function($) {
            const diasFestivos = ['2024-01-01', '2024-04-12', '2024-12-25']; // Días festivos

            const servicesByDay = {
                'fin_de_semana': [1], // ID del servicio de fin de semana
                'entre_semana': [2] // ID del servicio entre semana
            };

            function esDiaFestivo(fecha) {
                return diasFestivos.includes(fecha);
            }

            function obtenerServiceIdPorFecha(fecha) {
                const selectedDate = new Date(fecha);
                const dayOfWeek = selectedDate.getDay(); // 0 (domingo) a 6 (sábado)
                const formattedDate = selectedDate.toISOString().split('T')[0]; // yyyy-mm-dd

                if (dayOfWeek === 6 || dayOfWeek === 0 || esDiaFestivo(formattedDate)) {
                    // Fin de semana o día festivo
                    return servicesByDay.fin_de_semana[0];
                } else {
                    // Entre semana
                    return servicesByDay.entre_semana[0];
                }
            }

            // Interceptar la llamada AJAX
            $(document).ajaxSend(function(event, jqXHR, ajaxOptions) {
                if (ajaxOptions.data && ajaxOptions.data.indexOf('action=bookingpress_front_get_timings') !== -1) {
                    const params = new URLSearchParams(ajaxOptions.data);
                    const selectedDate = params.get('selected_date');

                    // Obtener el servicio correcto según la fecha seleccionada
                    const serviceId = obtenerServiceIdPorFecha(selectedDate);
                    
                    // Modificar el service_id en la llamada AJAX
                    params.set('service_id', serviceId);
                    params.set('appointment_data_obj', updateAppointmentDataObj(params.get('appointment_data_obj'), serviceId));

                    // Actualizar los datos de la solicitud AJAX
                    ajaxOptions.data = params.toString();
                }
            });

            function updateAppointmentDataObj(appointmentDataObj, serviceId) {
                const decodedObj = JSON.parse(decodeURIComponent(appointmentDataObj));

                // Actualizar el servicio seleccionado
                decodedObj.selected_service = serviceId;
                decodedObj.selected_service_name = serviceId === servicesByDay.fin_de_semana[0] ? "Fin de Semana" : "Entre Semana";
                
                // Reemplazar el objeto codificado con los nuevos datos
                return encodeURIComponent(JSON.stringify(decodedObj));
            }
        });
    </script>
    <?php
}
add_action('wp_footer', 'insertar_script_personalizado');
