<?php
session_start();
include("../config/conexion.php");
include 'menuconsultas.php';
?>

<head>
    <meta charset="UTF-8">
    <title>Consultar Registros de Faltas por Maestro</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <!-- Bootstrap 5.3.3 CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">

    <style>
        @media print {
            .no-print {
                display: none;
            }
        }
    </style>
</head>


<body>

    <div class="container mt-5">
        <h2>Registro General de Faltas</h2>

        <!-- Formulario -->
        <form method="POST" class="mb-4">
            <div class="form-group">
                <label for="maestro">Seleccione Maestro:</label>
                <select class="form-control" name="maestro" id="maestro" required>
                    <option value="" disabled selected>-- Seleccione un maestro --</option>
                    <?php
                    $maestros = $conexion->query("SELECT nombre FROM maestro");
                    while ($row = $maestros->fetch_assoc()) {
                        echo "<option value='" . htmlspecialchars($row['nombre'], ENT_QUOTES) . "'>{$row['nombre']}</option>";
                    }
                    ?>
                </select>
            </div>
            <button type="submit" name="consultar" class="btn btn-primary">Consultar</button>
        </form>

        <?php
        if (isset($_POST['maestro'])):
            $maestro = $_POST['maestro'];

            $sql = "SELECT rf.idregistro, rf.carnet, rf.nombre, rf.apellidos, rf.carrera, rf.grado, rf.seccion,
                       rf.falta, rf.bimestre, rf.fecha, rf.observaciones, u.nombre AS usuario
                FROM registro_falta rf
                INNER JOIN usuarios u ON rf.idusuario = u.id
                WHERE rf.maestro = ?";

            $stmt = $conexion->prepare($sql);
            $stmt->bind_param("s", $maestro);
            $stmt->execute();
            $resultado = $stmt->get_result();

            if ($resultado->num_rows > 0): ?>

                <form action="exportar_excel_maestro.php" method="POST" class="no-print">
                    <input type="hidden" name="maestro" value="<?= htmlspecialchars($maestro) ?>">
                    <button type="submit" class="btn btn-success mb-3">Exportar a Excel (.xlsx)</button>
                </form>

                <div class="alert alert-info">Maestro seleccionado: <?= htmlspecialchars($maestro) ?></div>

                <button class="btn btn-secondary no-print" onclick="window.print()">Imprimir</button>
                <table class="table table-bordered">
                    <thead class="table-secondary">
                        <tr>
                            <th>ID</th>
                            <th>Carnet</th>
                            <th>Nombre</th>
                            <th>Apellidos</th>
                            <th>Carrera</th>
                            <th>Grado</th>
                            <th>Sección</th>
                            <th>Falta</th>
                            <th>Bimestre</th>
                            <th>Fecha</th>
                            <th>Observaciones</th>
                            <th>Usuario</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php while ($row = $resultado->fetch_assoc()): ?>
                            <tr>
                                <?php foreach ($row as $valor): ?>
                                    <td><?= htmlspecialchars($valor, ENT_QUOTES) ?></td>
                                <?php endforeach; ?>
                            </tr>
                        <?php endwhile; ?>
                    </tbody>
                </table>

            <?php else: ?>
                <div class="alert alert-warning">No se encontraron registros para el maestro seleccionado.</div>
        <?php endif;
            $stmt->close();
        endif;
        ?>

    </div>
    <!-- Bootstrap 5.3.3 JS -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>

</body>

</html>