exec("CREATE TABLE IF NOT EXISTS appointment_slots ( id INT AUTO_INCREMENT PRIMARY KEY, id_departamento INT NOT NULL, day_of_week TINYINT NOT NULL, -- 1 for Monday, 7 for Sunday start_time TIME NOT NULL, end_time TIME NOT NULL, is_active BOOLEAN DEFAULT true, UNIQUE KEY (id_departamento, day_of_week, start_time, end_time), FOREIGN KEY (id_departamento) REFERENCES departamentos(id) ON DELETE CASCADE );"); // Fetch departments $departamentos = $pdo->query("SELECT id, nombre FROM departamentos ORDER BY nombre")->fetchAll(PDO::FETCH_ASSOC); // Get selected department (from GET param or first available) $selected_depto_id = $_GET['id_departamento'] ?? ($departamentos[0]['id'] ?? null); } catch (PDOException $e) { $message = 'Error de conexión con la base de datos: ' . $e->getMessage(); $message_type = 'danger'; } // Handle POST request to update slots if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($pdo) && isset($_POST['id_departamento'])) { $id_departamento_post = $_POST['id_departamento']; try { // Deactivate all existing slots for the specific department first $stmt_deactivate = $pdo->prepare("UPDATE appointment_slots SET is_active = false WHERE id_departamento = ?"); $stmt_deactivate->execute([$id_departamento_post]); if (isset($_POST['slots'])) { $slots = $_POST['slots']; $stmt_upsert = $pdo->prepare( "INSERT INTO appointment_slots (id_departamento, day_of_week, start_time, end_time, is_active) VALUES (:id_departamento, :day_of_week, :start_time, :end_time, true) ON DUPLICATE KEY UPDATE is_active = true" ); foreach ($slots as $day => $times) { foreach ($times as $time_range => $on) { list($start_time, $end_time) = explode('-', $time_range); $stmt_upsert->execute([ ':id_departamento' => $id_departamento_post, ':day_of_week' => $day, ':start_time' => $start_time, ':end_time' => $end_time ]); } } } $message = 'Horarios actualizados correctamente para el departamento seleccionado.'; $message_type = 'success'; $selected_depto_id = $id_departamento_post; // Keep the current department selected } catch (PDOException $e) { $message = 'Error al actualizar los horarios: ' . $e->getMessage(); $message_type = 'danger'; } } // Fetch current active slots for the selected department $active_slots = []; if (isset($pdo) && $selected_depto_id) { try { $stmt = $pdo->prepare("SELECT day_of_week, start_time, end_time FROM appointment_slots WHERE is_active = true AND id_departamento = ? ORDER BY day_of_week, start_time"); $stmt->execute([$selected_depto_id]); while($row = $stmt->fetch(PDO::FETCH_ASSOC)) { $active_slots[$row['day_of_week']][date('H:i', strtotime($row['start_time'])) . '-' . date('H:i', strtotime($row['end_time']))] = true; } } catch (PDOException $e) { $message = 'Error al obtener los horarios: ' . $e->getMessage(); $message_type = 'danger'; } } $days_of_week = [1 => 'Lunes', 2 => 'Martes', 3 => 'Miércoles', 4 => 'Jueves', 5 => 'Viernes', 6 => 'Sábado', 7 => 'Domingo']; $time_ranges = []; for ($h = 8; $h < 20; $h++) { $start = str_pad($h, 2, '0', STR_PAD_LEFT) . ':00'; $end = str_pad($h + 1, 2, '0', STR_PAD_LEFT) . ':00'; $time_ranges[] = $start . '-' . $end; } ?>

Gestión de Horarios para Citas

Seleccionar Departamento
Marque los bloques de tiempo disponibles

Seleccione las franjas horarias disponibles para el departamento seleccionado. Los cambios guardados se aplicarán solo a este departamento.

Día
$day_name): ?>
>
Por favor, seleccione un departamento para ver y gestionar sus horarios.