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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Consultar Registros por Bimestre</title>
    <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>
    <?php include 'menuconsultas.php'; ?>

    <div class="container mt-5">
        <h2>Registro de Faltas por Bimestre</h2>
        <form action="" method="POST" class="mb-4">
            <div class="form-group">
                <label for="bimestre">Seleccione Bimestre:</label>
                <select class="form-control" name="bimestre" id="bimestre" required>
                    <option disabled selected>---Seleccione bimestre---</option>
                    <option value="1">Bimestre 1</option>
                    <option value="2">Bimestre 2</option>
                    <option value="3">Bimestre 3</option>
                    <option value="4">Bimestre 4</option>
                </select>
            </div>
            <button type="submit" class="btn btn-primary mt-2">Consultar</button>
        </form>

        <?php
        include("../config/conexion.php");

        if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['bimestre'])) {
            $bimestre = intval($_POST['bimestre']);

            $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.bimestre = ?";

            $stmt = $conexion->prepare($sql);
            $stmt->bind_param("i", $bimestre);
            $stmt->execute();
            $result = $stmt->get_result();

            if ($result->num_rows > 0) {
                echo "<div class='alert alert-info'>Registros encontrados para el bimestre <strong>$bimestre</strong></div>";

                // Formulario oculto para exportar
                echo '<form action="exportar_excel_bimestre.php" method="POST" class="no-print mb-3">';
                echo '<input type="hidden" name="bimestre" value="' . $bimestre . '">';
                echo '<button type="submit" class="btn btn-success">Exportar a Excel (.xls)</button>';
                echo '</form>';

                echo '<button class="btn btn-secondary no-print mb-3" onclick="window.print()">Imprimir</button>';

                echo "<table class='table table-bordered table-striped'>";
                echo "<thead class='table-dark'><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>";

                while ($row = $result->fetch_assoc()) {
                    echo "<tr>";
                    foreach ($row as $campo) {
                        echo "<td>" . htmlspecialchars($campo) . "</td>";
                    }
                    echo "</tr>";
                }

                echo "</tbody></table>";
            } else {
                echo "<div class='alert alert-warning'>No se encontraron registros para el bimestre seleccionado.</div>";
            }

            $stmt->close();
        }
        ?>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>

</html>