<?php
include_once '../../controllers/MateriaController.php';
include_once '../../controllers/ModalidadController.php';
include_once '../../controllers/TurnoController.php';

$materiaController = new MateriaController();
$modalidadController = new ModalidadController();
$turnoController = new TurnoController();

$modalidades = $modalidadController->listarModalidades();
$turnos = $turnoController->listarTurnos();


if (isset($_GET['id'])) {
    $materia = $materiaController->obtenerMateriaPorId($_GET['id']);
    $horarios = $materiaController->obtenerHorariosPorMateriaId($_GET['id']); // Método para obtener los horarios
}

if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['actualizar'])) {
    // Procesar horarios
    $horarios = [];
    for ($i = 0; $i < count($_POST['dia']); $i++) {
        $horarios[] = [
            'dia' => $_POST['dia'][$i],
            'hora_ingreso' => $_POST['hora_ingreso'][$i],
            'hora_salida' => $_POST['hora_salida'][$i]
        ];
    }

    $resultado = $materiaController->actualizarMateria(
        $_POST['codigo_reg_int'],
        $_POST['nombre_mat_varchar'],
        $_POST['semestre'],
        $_POST['modalidad_id'],
        $_POST['turno_id'],
        $horarios
    );
    if ($resultado) {
        echo "<script>alert('Materia actualizada correctamente.'); window.location.href = 'gestionar_materias.php';</script>";
    } else {
        echo "<script>alert('Error al actualizar materia.');</script>";
    }
}
?>


<!DOCTYPE html>
<html lang="es">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Editar Materia</title>
    <script src="https://cdn.tailwindcss.com"></script>
</head>

<body class="bg-gray-100">
    <div class="max-w-4xl mx-auto p-6">
        <!-- Título -->
        <h2 class="text-3xl font-bold text-gray-800 text-center mb-8">Editar Materia</h2>

        <!-- Formulario de edición -->
        <form action="editar_materia.php" method="post" class="bg-white p-6 rounded-lg shadow-md">
    <input type="hidden" name="codigo_reg_int" value="<?php echo $materia['codigo_reg_int']; ?>">
    <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
        <div>
            <label for="nombre_mat_varchar" class="block text-sm font-medium text-gray-700">Nombre de la Materia:</label>
            <input type="text" id="nombre_mat_varchar" name="nombre_mat_varchar" value="<?php echo $materia['nombre_mat_varchar']; ?>" required
                   class="mt-1 block w-full px-4 py-2 border border-gray-300 rounded-lg shadow-sm focus:outline-none focus:ring-2 focus:ring-blue-500">
        </div>
        <div>
            <label for="semestre" class="block text-sm font-medium text-gray-700">Semestre:</label>
            <select id="semestre" name="semestre" required
                    class="mt-1 block w-full px-4 py-2 border border-gray-300 rounded-lg shadow-sm focus:outline-none focus:ring-2 focus:ring-blue-500">
                <option value="1" <?php echo $materia['semestre'] == 1 ? 'selected' : ''; ?>>1er Semestre</option>
                <option value="2" <?php echo $materia['semestre'] == 2 ? 'selected' : ''; ?>>2do Semestre</option>
                <option value="3" <?php echo $materia['semestre'] == 3 ? 'selected' : ''; ?>>3er Semestre</option>
                <option value="4" <?php echo $materia['semestre'] == 4 ? 'selected' : ''; ?>>4to Semestre</option>
                <option value="5" <?php echo $materia['semestre'] == 5 ? 'selected' : ''; ?>>5to Semestre</option>
                <option value="6" <?php echo $materia['semestre'] == 6 ? 'selected' : ''; ?>>6to Semestre</option>
            </select>
        </div>
        <div>
            <label for="modalidad_id" class="block text-sm font-medium text-gray-700">Modalidad:</label>
            <select id="modalidad_id" name="modalidad_id" required
                    class="mt-1 block w-full px-4 py-2 border border-gray-300 rounded-lg shadow-sm focus:outline-none focus:ring-2 focus:ring-blue-500">
                <?php foreach ($modalidades as $modalidad): ?>
                    <option value="<?php echo $modalidad['id']; ?>" <?php echo $materia['modalidad_id'] == $modalidad['id'] ? 'selected' : ''; ?>>
                        <?php echo $modalidad['modalidad']; ?>
                    </option>
                <?php endforeach; ?>
            </select>
        </div>
        <div>
            <label for="turno_id" class="block text-sm font-medium text-gray-700">Turno:</label>
            <select id="turno_id" name="turno_id" required
                    class="mt-1 block w-full px-4 py-2 border border-gray-300 rounded-lg shadow-sm focus:outline-none focus:ring-2 focus:ring-blue-500">
                <?php foreach ($turnos as $turno): ?>
                    <option value="<?php echo $turno['id']; ?>" <?php echo $materia['turno_id'] == $turno['id'] ? 'selected' : ''; ?>>
                        <?php echo $turno['turno']; ?>
                    </option>
                <?php endforeach; ?>
            </select>
        </div>
        <div class="col-span-full">
            <h4 class="text-lg font-semibold text-gray-700 mb-2">Horarios:</h4>
            <div id="horarios-container">
                <?php foreach ($horarios as $horario): ?>
                    <div class="grid grid-cols-3 gap-4 mt-4">
                        <div>
                            <label for="dia" class="block text-sm font-medium text-gray-700">Día:</label>
                            <select name="dia[]" required
                                    class="mt-1 block w-full px-4 py-2 border border-gray-300 rounded-lg shadow-sm focus:outline-none focus:ring-2 focus:ring-blue-500">
                                <option value="Lunes" <?php echo $horario['dia'] == 'Lunes' ? 'selected' : ''; ?>>Lunes</option>
                                <option value="Martes" <?php echo $horario['dia'] == 'Martes' ? 'selected' : ''; ?>>Martes</option>
                                <option value="Miércoles" <?php echo $horario['dia'] == 'Miércoles' ? 'selected' : ''; ?>>Miércoles</option>
                                <option value="Jueves" <?php echo $horario['dia'] == 'Jueves' ? 'selected' : ''; ?>>Jueves</option>
                                <option value="Viernes" <?php echo $horario['dia'] == 'Viernes' ? 'selected' : ''; ?>>Viernes</option>
                                <option value="Sábado" <?php echo $horario['dia'] == 'Sábado' ? 'selected' : ''; ?>>Sábado</option>
                            </select>
                        </div>
                        <div>
                            <label for="hora_ingreso" class="block text-sm font-medium text-gray-700">Hora de Ingreso:</label>
                            <input type="time" name="hora_ingreso[]" value="<?php echo $horario['hora_ingreso']; ?>" required
                                   class="mt-1 block w-full px-4 py-2 border border-gray-300 rounded-lg shadow-sm focus:outline-none focus:ring-2 focus:ring-blue-500">
                        </div>
                        <div>
                            <label for="hora_salida" class="block text-sm font-medium text-gray-700">Hora de Salida:</label>
                            <input type="time" name="hora_salida[]" value="<?php echo $horario['hora_salida']; ?>" required
                                   class="mt-1 block w-full px-4 py-2 border border-gray-300 rounded-lg shadow-sm focus:outline-none focus:ring-2 focus:ring-blue-500">
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
            <button type="button" onclick="addHorario()"
                    class="mt-4 bg-green-500 text-white px-4 py-2 rounded-lg hover:bg-green-600 transition duration-300">
                Añadir Horario
            </button>
        </div>
    </div>
    <div class="mt-6 text-center">
    <button type="submit" name="actualizar"
                        class="mt-6 bg-blue-500 text-white px-8 py-3 rounded-lg hover:bg-blue-600 transition duration-300">
                    Guardar Cambios
                </button>
            </div>
        </div>
    </form>
</div>

<script>
function addHorario() {
    const container = document.getElementById('horarios-container');
    const div = document.createElement('div');
    div.classList.add('grid', 'grid-cols-3', 'gap-4', 'mt-4');
    div.innerHTML = `
        <div>
            <label for="dia" class="block text-sm font-medium text-gray-700">Día:</label>
            <select name="dia[]" required
                    class="mt-1 block w-full px-4 py-2 border border-gray-300 rounded-lg shadow-sm focus:outline-none focus:ring-2 focus:ring-blue-500">
                <option value="Lunes">Lunes</option>
                <option value="Martes">Martes</option>
                <option value="Miércoles">Miércoles</option>
                <option value="Jueves">Jueves</option>
                <option value="Viernes">Viernes</option>
                <option value="Sábado">Sábado</option>
            </select>
        </div>
        <div>
            <label for="hora_ingreso" class="block text-sm font-medium text-gray-700">Hora de Ingreso:</label>
            <input type="time" name="hora_ingreso[]" required
                   class="mt-1 block w-full px-4 py-2 border border-gray-300 rounded-lg shadow-sm focus:outline-none focus:ring-2 focus:ring-blue-500">
        </div>
        <div>
            <label for="hora_salida" class="block text-sm font-medium text-gray-700">Hora de Salida:</label>
            <input type="time" name="hora_salida[]" required
                   class="mt-1 block w-full px-4 py-2 border border-gray-300 rounded-lg shadow-sm focus:outline-none focus:ring-2 focus:ring-blue-500">
        </div>
    `;
    container.appendChild(div);
}
</script>
</body>
</html>