<?php
if (session_status() == PHP_SESSION_NONE) {
    session_start();
}
require_once __DIR__ . '/../includes/db.php';
require_once __DIR__ . '/../includes/auth.php';
require_once __DIR__ . '/../includes/helpers.php';

check_role('administrador');

$page_title = 'Consultas y Reportes';

// Get careers for selection
$stmt = $pdo->query("SELECT * FROM careers ORDER BY name");
$all_careers = $stmt->fetchAll();

$content = '
<div class="row mb-3">
    <div class="col-12">
        <a href="' . route_url('admin_dashboard') . '" class="btn btn-secondary">
            <i class="fas fa-arrow-left"></i> Volver al Dashboard
        </a>
    </div>
</div>
<div class="row">
    <div class="col-md-10 offset-md-1">
        <div class="card card-primary">
            <div class="card-header">
                <h3 class="card-title"><i class="fas fa-filter"></i> Generador de Reportes Personalizados</h3>
            </div>
            <form method="post" action="export-custom-report.php" target="_blank">
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="start_date">Fecha de Inicio (Registro)</label>
                                <input type="date" class="form-control" id="start_date" name="start_date">
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="end_date">Fecha de Fin (Registro)</label>
                                <input type="date" class="form-control" id="end_date" name="end_date">
                            </div>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>Tipo de Usuario</label>
                                <div class="form-check">
                                    <input class="form-check-input" type="radio" name="user_type" id="type_student" value="student" checked>
                                    <label class="form-check-label" for="type_student">Alumnos</label>
                                </div>
                                <div class="form-check">
                                    <input class="form-check-input" type="radio" name="user_type" id="type_graduate" value="graduate">
                                    <label class="form-check-label" for="type_graduate">Egresados</label>
                                </div>
                                <div class="form-check">
                                    <input class="form-check-input" type="radio" name="user_type" id="type_both" value="both">
                                    <label class="form-check-label" for="type_both">Ambos</label>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>Género</label>
                                <div class="form-check">
                                    <input class="form-check-input" type="radio" name="gender" id="gender_all" value="" checked>
                                    <label class="form-check-label" for="gender_all">Todos</label>
                                </div>
                                <div class="form-check">
                                    <input class="form-check-input" type="radio" name="gender" id="gender_male" value="Masculino">
                                    <label class="form-check-label" for="gender_male">Masculino</label>
                                </div>
                                <div class="form-check">
                                    <input class="form-check-input" type="radio" name="gender" id="gender_female" value="Femenino">
                                    <label class="form-check-label" for="gender_female">Femenino</label>
                                </div>
                                <div class="form-check">
                                    <input class="form-check-input" type="radio" name="gender" id="gender_other" value="Otro">
                                    <label class="form-check-label" for="gender_other">Otro</label>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="form-group">
                        <label>Programas de Estudio (si no selecciona ninguno, se incluirán todos)</label>
                        <div class="border p-3" style="max-height: 200px; overflow-y: auto;">
                            <div class="form-check">
                                <input class="form-check-input" type="checkbox" id="checkAllCareers">
                                <label class="form-check-label" for="checkAllCareers">
                                    <strong>Seleccionar Todos</strong>
                                </label>
                            </div>
                            <hr>
                            ' . implode('', array_map(function($career) {
                                return '<div class="form-check">
                                    <input class="form-check-input career-checkbox" type="checkbox" name="careers[]" value="' . $career['id'] . '" id="career_' . $career['id'] . '">
                                    <label class="form-check-label" for="career_' . $career['id'] . '">
                                        ' . htmlspecialchars($career['name']) . '
                                    </label>
                                </div>';
                            }, $all_careers)) . '
                        </div>
                    </div>

                    <div class="form-group">
                        <label>Formato de Exportación</label>
                        <div class="form-check">
                            <input class="form-check-input" type="radio" name="format" id="format_excel" value="excel" checked>
                            <label class="form-check-label" for="format_excel">Excel (CSV)</label>
                        </div>
                        <div class="form-check">
                            <input class="form-check-input" type="radio" name="format" id="format_pdf" value="pdf">
                            <label class="form-check-label" for="format_pdf">PDF</label>
                        </div>
                    </div>
                </div>

                <div class="card-footer">
                    <button type="submit" class="btn btn-success">
                        <i class="fas fa-file-download"></i> Generar Reporte
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>
<script>
document.addEventListener("DOMContentLoaded", function() {
    const checkAll = document.getElementById("checkAllCareers");
    const checkboxes = document.querySelectorAll(".career-checkbox");

    checkAll.addEventListener("change", function() {
        checkboxes.forEach(checkbox => {
            checkbox.checked = checkAll.checked;
        });
    });
});
</script>
';

include '../includes/layout.php';
?>