<?php
    use RinnovoBooking\Core\RinnovoBookingService;
    use RinnovoBooking\Core\Notification;
    global $wpdb, $dataApi;
    $idSede = sanitize_text_field($_GET['idsede']);
    $table_name = $wpdb->prefix.RINNOVOBOOKINGPREFIX.'_time_slots';
    $table_name2 = $wpdb->prefix.RINNOVOBOOKINGPREFIX.'_sedi';
    $table_name3 = $wpdb->prefix.RINNOVOBOOKINGPREFIX.'_slots_except';
    //controllo se è stata inviata una azione
    if(isset($_GET['azione'])){

        //gestione delle azioni
        switch ($_GET['azione']) {

            case 'modifica':  
                $id = intval($_GET['id']);          
                $results = $wpdb->get_results("SELECT * FROM $table_name WHERE `id` = '$id'");
                $results[0]->services = json_decode($results[0]->services) ?? [];
                break;

            case 'cancella':    
                $id = intval($_GET['id']);  
                $results = $wpdb->get_results("SELECT * FROM $table_name WHERE `id` = '$id'");        
                $wpdb->delete($table_name, array('id' => $id), array('%d'));
                $RinnovoBookingService = new RinnovoBookingService();
                $RinnovoBookingService->scrivilog($_SESSION['user_surname'].' '.$_SESSION['user_name'], 'Cancellata fascia oraria con id '.$id.': sede->'.$results[0]->idsede.'; giorno-> '.$results[0]->day_of_week.'; inizio-> '.$results[0]->start_time.'; fine-> '.$results[0]->end_time.'; n_prenotazioni-> '.$results[0]->max_bookings.'; slot-> '.$results[0]->max_slots.'; minuti-> '.$results[0]->minutes_slots);
                Notification::display_notification('Cancellata fascia oraria. Giorno: '.$results[0]->day_of_week.' dalle '.$results[0]->start_time.' alle '.$results[0]->end_time, 'info', 'Attenzione!');
                break;  

            case 'addTimeSlot':        
                $day_of_week = sanitize_text_field($_POST['day_of_week']);
                $start_time = sanitize_text_field($_POST['start_time']);
                $end_time = sanitize_text_field($_POST['end_time']);
                $max_bookings = intval($_POST['max_bookings']);
                $max_slots = intval($_POST['max_slots']);
                $minutes_slots = intval($_POST['minutes_slots']);
                $services = json_encode($_POST['services']);

                $wpdb->insert($table_name, [
                    'idsede'=> $idSede, 
                    'day_of_week' => $day_of_week,
                    'start_time' => $start_time,
                    'end_time' => $end_time,
                    'max_bookings' => $max_bookings,
                    'max_slots' => $max_slots,
                    'minutes_slots' => $minutes_slots,
                    'services' => $services,
                ]);
                $RinnovoBookingService = new RinnovoBookingService();
                $RinnovoBookingService->scrivilog($_SESSION['user_surname'].' '.$_SESSION['user_name'], 'Creata oraria: sede->'.$idSede.'; giorno-> '.$day_of_week.'; inizio-> '.$start_time.'; fine-> '.$end_time.'; n_prenotazioni-> '.$max_bookings.'; slot-> '.$max_slots.'; minuti-> '.$minutes_slots);
                Notification::display_notification('Nuova fascia oraria creata.', 'info', 'Attenzione!');
                break;

            case 'updTimeSlot':         
                $day_of_week = sanitize_text_field($_POST['upd_day_of_week']);
                $start_time = sanitize_text_field($_POST['upd_start_time']);
                $end_time = sanitize_text_field($_POST['upd_end_time']);
                $max_bookings = intval($_POST['upd_max_bookings']);
                $max_slots = intval($_POST['upd_max_slots']);
                $minutes_slots = intval($_POST['upd_minutes_slots']);
                $services = json_encode($_POST['upd_services']);

                $wpdb->update($table_name, [
                    'idsede' => $idSede,
                    'day_of_week' => $day_of_week,
                    'start_time' => $start_time,
                    'end_time' => $end_time,
                    'max_bookings' => $max_bookings,
                    'max_slots' => $max_slots,
                    'minutes_slots' => $minutes_slots,
                    'services' => $services,
                ], ['id' => intval($_GET['id'])]); 
                $RinnovoBookingService = new RinnovoBookingService();
                $RinnovoBookingService->scrivilog($_SESSION['user_surname'].' '.$_SESSION['user_name'], 'Modificata oraria con id '. $_GET['id'].': sede->'.$idSede.'; giorno-> '.$day_of_week.'; inizio-> '.$start_time.'; fine-> '.$end_time.'; n_prenotazioni-> '.$max_bookings.'; slot-> '.$max_slots.'; minuti-> '.$minutes_slots);
                Notification::display_notification('Fascia oraria modificata. Giorno: '.$day_of_week.' dalle '.$start_time.' alle '.$end_time, 'info', 'Attenzione!');
                break;  

            case 'modificaextra':  
                $id = intval($_GET['id']);          
                $results = $wpdb->get_results("SELECT * FROM $table_name3 WHERE `id` = '$id'");
                $results[0]->services = json_decode($results[0]->services) ?? [];
                break;

            case 'cancellaextra':    
                $id = intval($_GET['id']);  
                $results = $wpdb->get_results("SELECT * FROM $table_name3 WHERE `id` = '$id'");        
                $wpdb->delete($table_name3, array('id' => $id), array('%d'));
                $RinnovoBookingService = new RinnovoBookingService();
                $RinnovoBookingService->scrivilog($_SESSION['user_surname'].' '.$_SESSION['user_name'], 'Cancellata fascia oraria con id '.$id.': sede->'.$results[0]->idsede.'; giorno-> '.$results[0]->day_of_week.'; inizio-> '.$results[0]->start_time.'; fine-> '.$results[0]->end_time.'; n_prenotazioni-> '.$results[0]->max_bookings.'; slot-> '.$results[0]->max_slots.'; minuti-> '.$results[0]->minutes_slots);
                Notification::display_notification('Cancellata fascia oraria extra. Giorno: '.$results[0]->day_of_week.' dalle '.$results[0]->start_time.' alle '.$results[0]->end_time, 'info', 'Attenzione!');
                break;   
                
            case 'addTimeSlotExtra':        
                $stato = sanitize_text_field($_POST['stato']);
                $day_of_week = sanitize_text_field($_POST['day_of_week_extra']);
                $start_time = sanitize_text_field($_POST['start_time_extra']);
                $end_time = sanitize_text_field($_POST['end_time_extra']);
                $max_bookings = intval($_POST['max_bookings_extra']);
                $max_slots = intval($_POST['max_slots_extra']);
                $minutes_slots = intval($_POST['minutes_slots_extra']);
                $services = json_encode($_POST['services_extra']);

                $wpdb->insert($table_name3, [
                    'idsede'=> $idSede, 
                    'stato' => $stato,
                    'day_of_week' => $day_of_week,
                    'start_time' => $start_time,
                    'end_time' => $end_time,
                    'max_bookings' => $max_bookings,
                    'max_slots' => $max_slots,
                    'minutes_slots' => $minutes_slots,
                    'services' => $services,
                ]);
                $RinnovoBookingService = new RinnovoBookingService();
                $RinnovoBookingService->scrivilog($_SESSION['user_surname'].' '.$_SESSION['user_name'], 'Creata oraria: sede->'.$idSede.'; giorno-> '.$day_of_week.'; inizio-> '.$start_time.'; fine-> '.$end_time.'; n_prenotazioni-> '.$max_bookings.'; slot-> '.$max_slots.'; minuti-> '.$minutes_slots);
                Notification::display_notification('Nuova fascia oraria extra creata.', 'info', 'Attenzione!');
                break;   

            case 'updTimeSlotExtra':         
                $stato = sanitize_text_field($_POST['upd_stato']);
                $day_of_week = sanitize_text_field($_POST['upd_day_of_week_extra']);
                $start_time = sanitize_text_field($_POST['upd_start_time_extra']);
                $end_time = sanitize_text_field($_POST['upd_end_time_extra']);
                $max_bookings = intval($_POST['upd_max_bookings_extra']);
                $max_slots = intval($_POST['upd_max_slots_extra']);
                $minutes_slots = intval($_POST['upd_minutes_slots_extra']);
                $services = json_encode($_POST['upd_services_extra']);
                
                $wpdb->update($table_name3, [
                    'idsede' => $idSede,
                    'stato' => $stato,
                    'day_of_week' => $day_of_week,
                    'start_time' => $start_time,
                    'end_time' => $end_time,
                    'max_bookings' => $max_bookings,
                    'max_slots' => $max_slots,
                    'minutes_slots' => $minutes_slots,
                    'services' => $services,
                ], ['id' => intval($_GET['id'])]); 
                $RinnovoBookingService = new RinnovoBookingService();
                $RinnovoBookingService->scrivilog($_SESSION['user_surname'].' '.$_SESSION['user_name'], 'Modificata oraria con id '. $_GET['id'].': sede->'.$idSede.'; giorno-> '.$day_of_week.'; inizio-> '.$start_time.'; fine-> '.$end_time.'; n_prenotazioni-> '.$max_bookings.'; slot-> '.$max_slots.'; minuti-> '.$minutes_slots);
                Notification::display_notification('Fascia oraria extra modificata. Giorno: '.$day_of_week.' dalle '.$start_time.' alle '.$end_time, 'info', 'Attenzione!');
                break;  

        } //fine switch
    } //fine if(isset($_GET['azione']))
    
    //recupero i dati
    $time_slots = $wpdb->get_results("SELECT * FROM $table_name  WHERE `idsede`= '$idSede' ORDER BY `day_of_week`,`start_time`");
    $nomeSede = $wpdb->get_results("SELECT nome FROM $table_name2 WHERE `idsede`= '$idSede'");  
    $time_slots_extra = $wpdb->get_results("SELECT * FROM $table_name3 WHERE `idsede`= '$idSede' ORDER BY `day_of_week`,`start_time`");
?>

<?php echo $this->handle_menu($idSede); ?>
<div class="container-table">
    <div class="content">

        <!-- Configura Fasce Orarie -->
        <div>                
            <form method="POST" class="form-ricerca" action="<?php echo esc_url(home_url()); ?>/<?php echo RINNOVOBOOKINGPANEL;?>/?idsede=<?php echo $_GET['idsede']; ?>&tipo=impostazioni&azione=addTimeSlot">                    
                <div style="display: grid; grid-template-columns: 3fr 2fr; gap: 10px;">
                    <div>
                        <h3 class="rb-h3">Configura Fasce Orarie</h3>
                        <div style="margin-bottom: 20px;">
                            <label>Seleziona la sede</label><br>
                            <select name="idsede" id="idsede" style="width: 340px;" required>
                                <?php
                                $table_name = $wpdb->prefix . RINNOVOBOOKINGPREFIX . '_sedi';
                                $sedi = $wpdb->get_results("SELECT idsede, nome FROM $table_name");
                                foreach ($sedi as $sede) {
                                    echo "<option value='{$sede->idsede}'>{$sede->nome}</option>";
                                }
                                ?>
                            </select>
                        </div>
                        <div style="display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 20px;">                                
                            <div>
                                <label for="day_of_week">Giorno della Settimana</label>
                                <select name="day_of_week" id="day_of_week" style="width: 120px;" required>
                                    <option value="Lunedì">Lunedì</option>
                                    <option value="Martedì">Martedì</option>
                                    <option value="Mercoledì">Mercoledì</option>
                                    <option value="Giovedì">Giovedì</option>
                                    <option value="Venerdì">Venerdì</option>
                                    <option value="Sabato">Sabato</option>
                                    <option value="Domenica">Domenica</option>
                                </select>
                            </div>
                            <div>
                                <label>Ora di Inizio</label><br>
                                <input type="time" name="start_time" id="start_time" style="width: 120px;" required>
                            </div>
                            <div>
                                <label>Ora di Fine</label><br>
                                <input type="time" name="end_time" id="end_time" style="width: 120px;" required>
                            </div>
                            <div>
                                <label>Prenotazioni per fascia</label><br>
                                <input type="number" name="max_bookings" id="max_bookings" value="1" style="width: 120px;" required>
                            </div>
                            <div>
                                <label>Numero di slot aperti</label><br>
                                <input type="number" name="max_slots" id="max_slots" value="3" style="width: 120px;" required>
                            </div>
                            <div>
                                <label>Minuti tra slots</label><br>
                                <input type="number" name="minutes_slots" id="minutes_slots" value="5" style="width: 120px;" required>
                            </div>                            
                        </div>
                    </div>
                    <div style="border-left: 1px solid #ddd; padding-left: 20px;">
                        <h3 class="rb-h3">Servizi Attivi</h3>
                            <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 10px;">
                                <div>
                                    <input type="checkbox" name="services[]" id="services" value="rinnovo" checked>
                                    <label for="services">Rinnovo</label>
                                </div>
                                <div>
                                    <input type="checkbox" name="services[]" id="services" value="conseguimento">
                                    <label for="services">Conseguimento</label>
                                </div>
                                <div>
                                    <input type="checkbox" name="services[]" id="services" value="porto_d_armi">
                                    <label for="services">Porto d'armi</label>
                                </div>
                                <div>
                                    <input type="checkbox" name="services[]" id="services" value="estensione">
                                    <label for="services">Estensione</label>
                                </div>
                                <div>
                                    <input type="checkbox" name="services[]" id="services" value="duplicato_documento">
                                    <label for="services">Duplicato documento</label>
                                </div>
                                <div>
                                    <input type="checkbox" name="services[]" id="services" value="rinnovo_patente_nautica">
                                    <label for="services">Rinnovo nautica</label>                                    
                                </div>
                            </div>

                    </div>
                </div>
                <div style="margin-top: 20px; display: flex; justify-content: center;">
                    <input type="submit" name="save_time_slot" class="action-button" value="Salva">
                </div>
            </form>
        </div>
        <!-- Modifica Fasce Orarie -->
        <?php 
            if ($_SERVER['REQUEST_METHOD'] == 'GET' && isset($_GET['azione']) && ($_GET['azione'] == 'modifica')) {

        ?>
            <div >
                <form method="POST" class="form-ricerca" action="<?php echo esc_url(home_url('/')); ?><?php echo RINNOVOBOOKINGPANEL;?>/?idsede=<?php echo $_GET['idsede']; ?>&tipo=impostazioni&azione=updTimeSlot&id=<?php echo $results[0]->id ?>">
                    <div style="background-color: lightblue; border-radius: 10px; padding: 10px;">
                        <input type="hidden" name="id" value="<?php echo $_GET['id'];?>">
                        <div style="display: grid; grid-template-columns: 3fr 2fr; gap: 10px;">
                            <div>
                                <h3 class="rb-h3">Modifica Fasce Orarie</h3>
                                <div style="margin-bottom: 20px;">
                                    <label>Seleziona la sede</label><br>
                                    <select name="idsede" id="idsede" style="width: 340px;" required>
                                        <?php
                                            $table_name = $wpdb->prefix . RINNOVOBOOKINGPREFIX . '_sedi';
                                            $sedi = $wpdb->get_results("SELECT idsede, nome FROM $table_name");
                                            foreach ($sedi as $sede) {
                                                $selected = ($sede->idsede == $results[0]->idsede) ? 'selected' : '';
                                                echo "<option value='{$sede->idsede}' {$selected}>{$sede->nome}</option>";
                                            }
                                            ?>
                                    </select>
                                </div>
                                <div style="display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 20px;">                                
                                    <div>
                                        <label for="upd_day_of_week">Giorno della Settimana</label>
                                        <select name="upd_day_of_week" id="upd_day_of_week" style="width: 120px;" required>
                                            <option value="Lunedì" <?php echo $results[0]->day_of_week == 'Lunedì' ? 'selected' : ''; ?> >Lunedì</option>
                                            <option value="Martedì" <?php echo $results[0]->day_of_week == 'Martedì' ? 'selected' : ''; ?> >Martedì</option>
                                            <option value="Mercoledì" <?php echo $results[0]->day_of_week == 'Mercoledì' ? 'selected' : ''; ?> >Mercoledì</option>
                                            <option value="Giovedì" <?php echo $results[0]->day_of_week == 'Giovedì' ? 'selected' : ''; ?> >Giovedì</option>
                                            <option value="Venerdì" <?php echo $results[0]->day_of_week == 'Venerdì' ? 'selected' : ''; ?> >Venerdì</option>
                                            <option value="Sabato" <?php echo $results[0]->day_of_week == 'Sabato' ? 'selected' : ''; ?> >Sabato</option>
                                            <option value="Domenica" <?php echo $results[0]->day_of_week == 'Domenica' ? 'selected' : ''; ?> >Domenica</option>
                                        </select>
                                    </div>
                                    <div>
                                        <label>Ora di Inizio</label><br>
                                        <input type="time" name="upd_start_time" id="upd_start_time" value="<?php echo $results[0]->start_time?>" style="width: 120px;" required>
                                    </div>
                                    <div>
                                        <label>Ora di Fine</label><br>
                                        <input type="time" name="upd_end_time" id="upd_end_time" value="<?php echo $results[0]->end_time?>" style="width: 120px;" required>
                                    </div>
                                    <div>
                                        <label>Prenotazioni per fascia</label><br>
                                        <input type="number" name="upd_max_bookings" id="upd_max_bookings" value="<?php echo $results[0]->max_bookings?>" style="width: 120px;" required>
                                    </div>
                                    <div>
                                        <label>Numero di slot aperti</label><br>
                                        <input type="number" name="upd_max_slots" id="upd_max_slots" value="<?php echo $results[0]->max_slots?>" style="width: 120px;" required>
                                    </div>
                                    <div>
                                        <label>Minuti tra slots</label><br>
                                        <input type="number" name="upd_minutes_slots" id="upd_minutes_slots" value="<?php echo $results[0]->minutes_slots?>" style="width: 120px;" required>
                                    </div>                            
                                </div>
                            </div>
                            <div style="border-left: 1px solid #ddd; padding-left: 20px;">
                                <h3 class="rb-h3">Modifica Servizi</h3>
                                    <div style="display: grid; grid-template-columns: 1fr 1fr ; gap: 20px;">
                                            <div>
                                                <input type="checkbox" name="upd_services[]" value="rinnovo" <?php echo in_array('rinnovo', $results[0]->services) ? 'checked' : ''; ?>>
                                                <label for="services">Rinnovo</label>
                                            </div>
                                            <div>
                                                <input type="checkbox" name="upd_services[]" value="conseguimento" <?php echo in_array('conseguimento', $results[0]->services) ? 'checked' : ''; ?>>
                                                <label for="services">Conseguimento</label>
                                            </div>
                                            <div>
                                                <input type="checkbox" name="upd_services[]" value="porto_d_armi" <?php echo in_array('porto_d_armi', $results[0]->services) ? 'checked' : ''; ?>>
                                                <label for="services">Porto d'armi</label>
                                            </div>
                                            <div>
                                                <input type="checkbox" name="upd_services[]" value="estensione" <?php echo in_array('estensione', $results[0]->services) ? 'checked' : ''; ?>>
                                                <label for="services">Estensione</label>
                                            </div>
                                            <div>
                                                <input type="checkbox" name="upd_services[]" value="duplicato_documento" <?php echo in_array('duplicato_documento', $results[0]->services) ? 'checked' : ''; ?>>
                                                <label for="services">Duplicato documento</label>
                                            </div>
                                            <div>
                                                <input type="checkbox" name="upd_services[]" value="rinnovo_patente_nautica" <?php echo in_array('rinnovo_patente_nautica', $results[0]->services) ? 'checked' : ''; ?>>
                                                <label for="services">Rinnovo nautica</label>
                                            </div>

                            </div>
                        </div>
                    </div>
                    <div style="margin-top: 20px; display: flex; justify-content: center;">                            
                        <input type="submit" name="update_time_slot" class="action-button" value="Aggiorna">
                    </div>
                </form>
            </div>
        <?php 
            }
        ?>
        <div class="content-border-search">
            <h4>Fasce Orarie Esistenti</h4>
            <table class="table">
                <thead>
                    <tr>
                        <th>Sede</th>
                        <th>Giorno</th>
                        <th>Ora di Inizio</th>
                        <th>Ora di Fine</th>
                        <th>Per Slot</th>
                        <th>Aperte</th>
                        <th>Minuti</th>
                        <th>Servizi</th>
                        <th>Azioni</th>
                    </tr>
                </thead>
                <tbody>
                    <?php 
                        
                            foreach ($time_slots as $slot) :
                    ?>
                        <tr>
                            <td>
                                <?php
                                $table_name = $wpdb->prefix . RINNOVOBOOKINGPREFIX . '_sedi';
                                $sede = $wpdb->get_var($wpdb->prepare("SELECT nome FROM $table_name WHERE idsede = %s", $slot->idsede));
                                echo esc_html($sede);
                                ?>
                            </td>
                            <td><?php echo esc_html($slot->day_of_week); ?></td>
                            <td><?php echo esc_html($slot->start_time); ?></td>
                            <td><?php echo esc_html($slot->end_time); ?></td>
                            <td><?php echo esc_html($slot->max_bookings); ?></td>
                            <td><?php echo esc_html($slot->max_slots); ?></td>
                            <td><?php echo esc_html($slot->minutes_slots); ?></td>
                            <td>
                                <?php
                                    $services = json_decode($slot->services);
                                    $servizi = implode(", ", $services);
                                    $services = array_map(function($service) {
                                        $service = str_replace('_', ' ', $service);
                                        return ucwords($service);
                                    }, $services);
                                    $servizi = implode(", ", $services);
                                    echo esc_html($servizi);
                                ?>
                            </td>
                            <td>
                                <a href="<?php echo esc_url(home_url('/')); ?>panel/?idsede=<?php echo $_GET['idsede']; ?>&tipo=impostazioni&azione=modifica&id=<?php echo $slot->id ?>" >
                                    <svg xmlns="http://www.w3.org/2000/svg" fill="none" stroke-width="1.5" stroke="currentColor">
                                        <path stroke-linecap="round" stroke-linejoin="round" d="m16.862 4.487 1.687-1.688a1.875 1.875 0 1 1 2.652 2.652L6.832 19.82a4.5 4.5 0 0 1-1.897 1.13l-2.685.8.8-2.685a4.5 4.5 0 0 1 1.13-1.897L16.863 4.487Zm0 0L19.5 7.125" />
                                    </svg>
                                </a>
                                <a href="<?php echo esc_url(home_url('/')); ?>panel/?idsede=<?php echo $_GET['idsede']; ?>&tipo=impostazioni&azione=cancella&id=<?php echo $slot->id ?>" onclick="return confirm('Sei sicuro di voler eliminare:\n <?php echo strtoupper($slot->day_of_week);?> ?' );">
                                    <svg xmlns="http://www.w3.org/2000/svg" fill="none" stroke-width="1.5" stroke="currentColor">
                                        <path stroke-linecap="round" stroke-linejoin="round" d="m14.74 9-.346 9m-4.788 0L9.26 9m9.968-3.21c.342.052.682.107 1.022.166m-1.022-.165L18.16 19.673a2.25 2.25 0 0 1-2.244 2.077H8.084a2.25 2.25 0 0 1-2.244-2.077L4.772 5.79m14.456 0a48.108 48.108 0 0 0-3.478-.397m-12 .562c.34-.059.68-.114 1.022-.165m0 0a48.11 48.11 0 0 1 3.478-.397m7.5 0v-.916c0-1.18-.91-2.164-2.09-2.201a51.964 51.964 0 0 0-3.32 0c-1.18.037-2.09 1.022-2.09 2.201v.916m7.5 0a48.667 48.667 0 0 0-7.5 0" />
                                    </svg>                                 
                                </a>
                            </td>
                        </tr>
                    <?php 
                            endforeach; 
                            
                    ?>
                </tbody>
            </table>
        </div>

        <!-- Configura Fasce Orarie Straordinarie-->
        <div class="content-border-search">
            <form method="POST" class="form-ricerca" action="<?php echo esc_url(home_url('/')); ?><?php echo RINNOVOBOOKINGPANEL;?>/?idsede=<?php echo $_GET['idsede']; ?>&tipo=impostazioni&azione=addTimeSlotExtra">
            
                <div style="display: grid; grid-template-columns: 3fr 2fr; gap: 10px;">
                    <div>
                        <h3 class="rb-h3">Configura Fasce Orarie Straordinarie</h3>
                        <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 10px;">
                            <div>
                                <label>Sede</label><br>
                                <select name="idsede" id="idsede" style="width: 100%;" required>
                                    <?php
                                    $table_name = $wpdb->prefix . RINNOVOBOOKINGPREFIX . '_sedi';
                                    $sedi = $wpdb->get_results("SELECT idsede, nome FROM $table_name");
                                    foreach ($sedi as $sede) {
                                        echo "<option value='{$sede->idsede}'>{$sede->nome}</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                            <div>
                                <label>Stato</label><br>
                                <select name="stato" id="sstato" style="width: 120px;" required>
                                    <option value="open">Aperta</option>
                                    <option value="close">Chiusa</option>
                                </select>
                            </div>
                        </div>
                        <div style="display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 20px; margin-top: 20px;">                                
                            <div>
                                <label for="day_of_week_extra">Giorno</label><br>
                                <input type="date" name="day_of_week_extra" id="day_of_week_extra" style="width: 120px;" required>
                            </div>
                            <div>
                                <label>Ora di Inizio</label><br>
                                <input type="time" name="start_time_extra" id="start_time_extra" style="width: 120px;" required>
                            </div>
                            <div>
                                <label>Ora di Fine</label><br>
                                <input type="time" name="end_time_extra" id="end_time_extra" style="width: 120px;" required>
                            </div>
                            <div>
                                <label>Numero di prenotazioni</label><br>
                                <input type="number" name="max_bookings_extra" id="max_bookings_extra" value="1" style="width: 120px;" required>
                            </div>
                            <div>
                                <label>Numero di slot aperti</label><br>
                                <input type="number" name="max_slots_extra" id="max_slots_extra" value="3" style="width: 120px;" required>
                            </div>
                            <div>
                                <label>Minuti tra slots</label><br>
                                <input type="number" name="minutes_slots_extra" id="minutes_slots_extra" value="5" style="width: 120px;" required>
                            </div>
                        </div>
                    </div>
                    <div style="border-left: 1px solid #ddd; padding-left: 20px;">
                        <h3 class="rb-h3">Servizi Attivi</h3>
                        <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 10px;">
                            <div>
                                <input type="checkbox" name="services_extra[]" id="services_extra" value="rinnovo" checked>
                                <label for="services">Rinnovo</label>
                            </div>
                            <div>
                                <input type="checkbox" name="services_extra[]" id="services_extra" value="conseguimento">
                                <label for="services">Conseguimento</label>
                            </div>
                            <div>
                                <input type="checkbox" name="services_extra[]" id="services_extra" value="porto_d_armi">
                                <label for="services">Porto d'armi</label>
                            </div>
                            <div>
                                <input type="checkbox" name="services_extra[]" id="services_extra" value="estensione">
                                <label for="services">Estensione</label>
                            </div>
                            <div>
                                <input type="checkbox" name="services_extra[]" id="services_extra" value="duplicato_documento">
                                <label for="services">Duplicato documento</label>
                            </div>
                            <div>
                                <input type="checkbox" name="services_extra[]" id="services_extra" value="rinnovo_patente_nautica">
                                <label for="services">Rinnovo nautica</label>                                    
                            </div>
                        </div>
                    </div>
                </div>

                <div style="text-align: center; margin-top: 20px;"><input type="submit" name="save_time_slot_extra" class="action-button" value="Salva"></div>
            </form>
        </div>
        <!-- Modifica Fasce Orarie Straordinarie-->
        <?php 
            if ($_SERVER['REQUEST_METHOD'] == 'GET' && isset($_GET['azione']) && ($_GET['azione'] == 'modificaextra')) {

        ?>
            <div>
                <form method="POST" class="form-ricerca" action="<?php echo esc_url(home_url('/')); ?><?php echo RINNOVOBOOKINGPANEL;?>/?idsede=<?php echo $_GET['idsede']; ?>&tipo=impostazioni&azione=updTimeSlotExtra&id=<?php echo $results[0]->id ?>">
                    <div style="background-color: lightblue; border-radius: 10px; padding: 10px;">
                        <input type="hidden" name="id" value="<?php echo $_GET['id'];?>">
                        <div style="display: grid; grid-template-columns: 3fr 2fr; gap: 10px;">
                        <div>
                            <h3 class="rb-h3">Modifica Fasce Orarie Straordinarie</h3>
                            <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 10px;">
                                <div>
                                    <label>Sede</label><br>
                                    <select name="idsede" id="idsede" style="width: 100%;" required>
                                        <?php
                                        $table_name = $wpdb->prefix . RINNOVOBOOKINGPREFIX . '_sedi';
                                        $sedi = $wpdb->get_results("SELECT idsede, nome FROM $table_name");
                                        foreach ($sedi as $sede) {
                                            $selected = ($sede->idsede == $results[0]->idsede) ? 'selected' : '';
                                            echo "<option value='{$sede->idsede}' {$selected}>{$sede->nome}</option>";
                                        }
                                        ?>
                                    </select>
                                </div>
                                <div>
                                    <label>Stato</label><br>
                                    <select name="upd_stato" id="upd_stato" style="width: 120px;" required>
                                        <option value="open" <?php echo $results[0]->stato == 'open' ? 'selected' : ''; ?>>Aperta</option>
                                        <option value="close" <?php echo $results[0]->stato == 'close' ? 'selected' : ''; ?>>Chiusa</option>
                                    </select>
                                </div>
                            </div>
                            <div style="display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 20px; margin-top: 20px;">                                
                                <div>
                                    <label for="upd_day_of_week">Giorno</label><br>
                                    <input type="date" name="upd_day_of_week_extra" id="upd_day_of_week_extra" value="<?php echo $results[0]->day_of_week?>" style="width: 120px;" required>
                                </div>
                                <div>
                                    <label>Ora di Inizio</label><br>
                                    <input type="time" name="upd_start_time_extra" id="upd_start_time_extra" value="<?php echo $results[0]->start_time?>" style="width: 120px;" required>
                                </div>
                                <div>
                                    <label>Ora di Fine</label><br>
                                    <input type="time" name="upd_end_time_extra" id="upd_end_time_extra" value="<?php echo $results[0]->end_time?>" style="width: 120px;" required>
                                </div>
                                <div>
                                    <label>Numero di prenotazioni</label><br>
                                    <input type="number" name="upd_max_bookings_extra" id="upd_max_bookings_extra" value="<?php echo $results[0]->max_bookings?>" style="width: 120px;" required>
                                </div>
                                <div>
                                    <label>Numero di slot aperti</label><br>
                                    <input type="number" name="upd_max_slots_extra" id="upd_max_slots_extra" value="<?php echo $results[0]->max_slots?>" style="width: 120px;" required>
                                </div>
                                <div>
                                    <label>Minuti tra slots</label><br>
                                    <input type="number" name="upd_minutes_slots_extra" id="upd_minutes_slots_extra" value="<?php echo $results[0]->minutes_slots?>" style="width: 120px;" required>
                                </div>
                            </div>
                        </div>
                        <div style="border-left: 1px solid #ddd; padding-left: 20px;">
                            <h3 class="rb-h3">Servizi Attivi</h3>
                            <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 10px;">
                                <div>
                                    <input type="checkbox" name="upd_services_extra[]" id="upd_services_extra" value="rinnovo" <?php echo in_array('rinnovo', $results[0]->services) ? 'checked' : ''; ?>>
                                    <label for="services">Rinnovo</label>
                                </div>
                                <div>
                                    <input type="checkbox" name="upd_services_extra[]" id="upd_services_extra" value="conseguimento" <?php echo in_array('conseguimento', $results[0]->services) ? 'checked' : ''; ?>>
                                    <label for="services">Conseguimento</label>
                                </div>
                                <div>
                                    <input type="checkbox" name="upd_services_extra[]" id="upd_services_extra" value="porto_d_armi" <?php echo in_array('porto_d_armi', $results[0]->services) ? 'checked' : ''; ?>>
                                    <label for="services">Porto d'armi</label>
                                </div>
                                <div>
                                    <input type="checkbox" name="upd_services_extra[]" id="upd_services_extra" value="estensione" <?php echo in_array('estensione', $results[0]->services) ? 'checked' : ''; ?>>
                                    <label for="services">Estensione</label>
                                </div>
                                <div>
                                    <input type="checkbox" name="upd_services_extra[]" id="upd_services_extra" value="duplicato_documento" <?php echo in_array('duplicato_documento', $results[0]->services) ? 'checked' : ''; ?>>
                                    <label for="services">Duplicato documento</label>
                                </div>
                                <div>
                                    <input type="checkbox" name="upd_services_extra[]" id="upd_services_extra" value="rinnovo_patente_nautica" <?php echo in_array('rinnovo_patente_nautica', $results[0]->services) ? 'checked' : ''; ?>>
                                    <label for="services">Rinnovo nautica</label>                                    
                                </div>
                            </div>
                        </div>
                    </div>
                    <div style="text-align: center; margin-top: 20px;">
                        <input type="hidden" name="id" value="<?php echo $_GET['id'];?>">
                        <input type="submit" name="update_time_slot_extra" class="action-button" value="Aggiorna">
                    </div>
                </form>
            </div>
        <?php 
            }
        ?>
        <div class="content-border-search">
            <h4>Fasce Orarie Straordinarie</h4>
            <table class="table">
                <thead>
                    <tr>
                        <th>Sede</th>
                        <th>Stato</th>
                        <th>Giorno</th>
                        <th>Ora di Inizio</th>
                        <th>Ora di Fine</th>
                        <th>Per Slot</th>
                        <th>Aperte</th>
                        <th>Minuti</th>
                        <th>Servizi</th>
                        <th>Azioni</th>
                    </tr>
                </thead>
                <tbody>
                    <?php 
                        
                    foreach ($time_slots_extra as $slot) {
                    ?>
                        <tr>
                            <td>
                                <?php
                                $table_name = $wpdb->prefix . RINNOVOBOOKINGPREFIX . '_sedi';
                                $sede = $wpdb->get_var($wpdb->prepare("SELECT nome FROM $table_name WHERE idsede = %s", $slot->idsede));
                                echo esc_html($sede);
                                ?>
                            </td>
                            <td><?php echo esc_html($slot->stato); ?></td>
                            <td><?php echo esc_html(date('d/m/Y', strtotime($slot->day_of_week))); ?></td>                         
                            <td><?php echo esc_html($slot->start_time); ?></td>
                            <td><?php echo esc_html($slot->end_time); ?></td>
                            <td><?php echo esc_html($slot->max_bookings); ?></td>
                            <td><?php echo esc_html($slot->max_slots); ?></td>
                            <td><?php echo esc_html($slot->minutes_slots); ?></td>
                            <td>
                                <?php
                                    $services = json_decode($slot->services);
                                    $servizi = implode(", ", $services);
                                    $services = array_map(function($service) {
                                        $service = str_replace('_', ' ', $service);
                                        return ucwords($service);
                                    }, $services);
                                    $servizi = implode(", ", $services);
                                    echo esc_html($servizi);
                                ?>
                            </td>
                            <td>
                                <a href="<?php echo esc_url(home_url('/')); ?><?php echo RINNOVOBOOKINGPANEL;?>/?idsede=<?php echo $_GET['idsede']; ?>&tipo=impostazioni&azione=modificaextra&id=<?php echo $slot->id ?>" >
                                    <svg xmlns="http://www.w3.org/2000/svg" fill="none" stroke-width="1.5" stroke="currentColor">
                                        <path stroke-linecap="round" stroke-linejoin="round" d="m16.862 4.487 1.687-1.688a1.875 1.875 0 1 1 2.652 2.652L6.832 19.82a4.5 4.5 0 0 1-1.897 1.13l-2.685.8.8-2.685a4.5 4.5 0 0 1 1.13-1.897L16.863 4.487Zm0 0L19.5 7.125" />
                                    </svg>
                                </a>
                                <a href="<?php echo esc_url(home_url('/')); ?><?php echo RINNOVOBOOKINGPANEL;?>/?idsede=<?php echo $_GET['idsede']; ?>&tipo=impostazioni&azione=cancellaextra&id=<?php echo $slot->id ?>" onclick="return confirm('Sei sicuro di voler eliminare:\n <?php echo strtoupper($slot->day_of_week);?> ?' );">
                                    <svg xmlns="http://www.w3.org/2000/svg" fill="none" stroke-width="1.5" stroke="currentColor">
                                        <path stroke-linecap="round" stroke-linejoin="round" d="m14.74 9-.346 9m-4.788 0L9.26 9m9.968-3.21c.342.052.682.107 1.022.166m-1.022-.165L18.16 19.673a2.25 2.25 0 0 1-2.244 2.077H8.084a2.25 2.25 0 0 1-2.244-2.077L4.772 5.79m14.456 0a48.108 48.108 0 0 0-3.478-.397m-12 .562c.34-.059.68-.114 1.022-.165m0 0a48.11 48.11 0 0 1 3.478-.397m7.5 0v-.916c0-1.18-.91-2.164-2.09-2.201a51.964 51.964 0 0 0-3.32 0c-1.18.037-2.09 1.022-2.09 2.201v.916m7.5 0a48.667 48.667 0 0 0-7.5 0" />
                                    </svg> 
                                </a>
                            </td>
                        </tr>
                    <?php 
                    }
                            
                    ?>
                </tbody>
            </table>
        </div>
    </div>   
</div>