<?php
// El header.php ahora se encarga de iniciar la sesión y la seguridad.
// Debe ser lo primero que se incluya en las páginas protegidas.
$page_title = 'Menú Principal';
include __DIR__ . '/header.php';

// Ahora que la sesión está iniciada por header.php, podemos verificar el rol.
require_auth(); // Función de secure_session.php que verifica si hay un user_id.

// Obtener el rol para el control de acceso
$rol = strtolower($_SESSION['rol'] ?? '');

include __DIR__ . '/navbar.php';
?>
<div class="container-fluid mt-3">
    <div class="row">
        <!-- Dashboard Lateral -->
        <div class="col-lg-3 col-xl-2">
            <div class="dashboard-sidebar animate-fade-in-left">
                <div class="d-flex justify-content-between align-items-center mb-3">
                    <h6 class="sidebar-title mb-0">
                        <i class="bi bi-graph-up me-2"></i>Matrícula
                    </h6>
                    <select id="dashboardAnioSelect" class="form-select form-select-sm w-auto py-0 ps-2 pe-4 fw-bold" style="height: 28px; font-size: 0.85rem; background-color: #FFFFFF; color: #3B82F6; border: 1px solid #3B82F6; text-align: center;">
                        <option value="2025" <?php echo (date('Y') == '2025') ? 'selected' : ''; ?>>2025</option>
                        <option value="2026" <?php echo (date('Y') == '2026') ? 'selected' : ''; ?>>2026</option>
                    </select>
                </div>

                <!-- Total -->
                <div class="stat-card-compact">
                    <div class="stat-icon">
                        <i class="bi bi-people-fill"></i>
                    </div>
                    <div class="stat-info">
                        <div class="stat-value" id="totalMatricula">
                            <div class="spinner-border spinner-border-sm" role="status"></div>
                        </div>
                        <div class="stat-label">Total</div>
                    </div>
                </div>

                <!-- Por curso (compacto) -->
                <div id="matriculaPorCurso" class="stats-list">
                    <!-- JS insertará aquí -->
                    <div class="text-center py-3">
                        <div class="spinner-border spinner-border-sm text-muted" role="status">
                            <span class="visually-hidden">Cargando desglose...</span>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Menú Principal -->
        <div class="col-lg-9 col-xl-10">
            <div class="d-flex justify-content-between align-items-center mb-3 animate-fade-in-down">
                <h3 class="m-0 text-primary">
                    <i class="bi bi-grid-3x3-gap me-2"></i>Menú Principal
                </h3>
                <div class="text-end d-none d-md-block">
                    <?php
                    $nombre = isset($_SESSION['nombre']) ? htmlspecialchars($_SESSION['nombre']) : 'Usuario';
                    $rol_display = isset($_SESSION['rol']) ? htmlspecialchars(ucfirst($_SESSION['rol'])) : '';
                    ?>
                    <span class="text-muted small">Bienvenido,</span>
                    <div class="fw-bold text-dark"><?php echo $nombre; ?> <span class="badge bg-light text-secondary border ms-1"><?php echo $rol_display; ?></span></div>
                </div>
            </div>

            <div class="row g-3 justify-content-center align-items-center w-100">
                <?php if (in_array($rol, ['administrador', 'digitador'])) : ?>
                    <div class="col-12 col-md-6 col-lg-4 animate-fade-in-up delay-100">
                        <div class="menu-card-modern" data-bs-toggle="modal" data-bs-target="#modalRegistroAlumno">
                            <div class="icon-container gradient-blue">
                                <svg xmlns="http://www.w3.org/2000/svg" width="56" height="56" fill="none" stroke="white" stroke-width="1.5" viewBox="0 0 24 24">
                                    <circle cx="12" cy="8" r="4" />
                                    <path d="M4 20c0-2.5 3.5-4.5 8-4.5s8 2 8 4.5" />
                                    <g transform="translate(4, -7) scale(2)">
                                        <path d="M8 3v4" stroke-width="0.8" />
                                        <path d="M10 5h-4" stroke-width="0.8" />
                                    </g>
                                </svg>
                            </div>
                            <h5>Registro de alumnos</h5>
                            <p>Alta de alumnos en el sistema</p>
                            <span class="badge badge-gradient-primary"><i class="bi bi-shield-check me-1"></i>Admin/Digitador</span>
                        </div>
                    </div>
                <?php endif; ?>

                <?php if (in_array($rol, ['administrador', 'digitador'])) : ?>
                    <div class="col-12 col-md-6 col-lg-4 animate-fade-in-up delay-200">
                        <a href="<?php echo url('views/matricula.php'); ?>" class="menu-card-modern">
                            <div class="icon-container gradient-green">
                                <svg xmlns="http://www.w3.org/2000/svg" width="56" height="56" fill="none" stroke="white" stroke-width="1.5" viewBox="0 0 24 24">
                                    <rect x="4" y="4" width="16" height="16" rx="2" />
                                    <path d="M8 2v4M16 2v4M4 10h16" />
                                </svg>
                            </div>
                            <h5>Gestión de matrícula</h5>
                            <p>Procesos de matrícula escolar</p>
                            <span class="badge badge-gradient-success"><i class="bi bi-check-circle me-1"></i>Activo</span>
                        </a>
                    </div>
                <?php endif; ?>

                <?php if (in_array($rol, ['administrador', 'digitador', 'convivencia', 'inspector'])) : ?>
                    <div class="col-12 col-md-6 col-lg-4 animate-fade-in-up delay-300">
                        <a href="<?php echo view('libro_matricula.php'); ?>" class="menu-card-modern">
                            <div class="icon-container gradient-orange">
                                <svg xmlns="http://www.w3.org/2000/svg" width="56" height="56" fill="none" stroke="white" stroke-width="1.5" viewBox="0 0 24 24">
                                    <rect x="4" y="4" width="16" height="16" rx="2" />
                                    <path d="M8 8h8M8 12h8M8 16h8" />
                                </svg>
                            </div>
                            <h5>Libro de matrícula</h5>
                            <p><?php echo in_array($rol, ['administrador', 'digitador']) ? 'Consulta, edición y reportes' : 'Consulta y reportes'; ?></p>
                            <span class="badge badge-gradient-warning"><i class="bi bi-journal-text me-1"></i><?php echo in_array($rol, ['administrador', 'digitador']) ? 'Edición' : 'Consulta'; ?></span>
                        </a>
                    </div>
                <?php endif; ?>

                <?php if (in_array($rol, ['administrador', 'convivencia'])) : ?>
                    <div class="col-12 col-md-6 col-lg-4 animate-fade-in-up delay-400">
                        <a href="<?php echo url('views/convivencia_educativa.php'); ?>" class="menu-card-modern">
                            <div class="icon-container gradient-cyan">
                                <svg xmlns="http://www.w3.org/2000/svg" width="56" height="56" fill="none" stroke="white" stroke-width="1.5" viewBox="0 0 24 24">
                                    <rect x="4" y="4" width="16" height="16" rx="2" />
                                    <path d="M8 12h8M8 16h8" />
                                    <circle cx="8" cy="8" r="1.5" />
                                </svg>
                            </div>
                            <h5>Convivencia educativa</h5>
                            <p>Gestión de convivencia escolar</p>
                            <span class="badge badge-gradient-info"><i class="bi bi-shield-fill-check me-1"></i>Convivencia</span>
                        </a>
                    </div>
                <?php endif; ?>

                <?php if (in_array($rol, ['administrador', 'inspector', 'convivencia', 'digitador'])) : ?>
                    <div class="col-12 col-md-6 col-lg-4 animate-fade-in-up delay-500">
                        <a href="<?php echo url('views/contactos.php'); ?>" class="menu-card-modern">
                            <div class="icon-container gradient-purple">
                                <svg xmlns="http://www.w3.org/2000/svg" width="56" height="56" fill="none" stroke="white" stroke-width="1.5" viewBox="0 0 24 24">
                                    <path d="M22 16.92v3a2 2 0 0 1-2.18 2A19.86 19.86 0 0 1 3.08 5.18 2 2 0 0 1 5 3h3a2 2 0 0 1 2 1.72c.13.81.36 1.6.68 2.34a2 2 0 0 1-.45 2.11l-1.27 1.27a16 16 0 0 0 6.29 6.29l1.27-1.27a2 2 0 0 1 2.11-.45c.74.32 1.53.55 2.34.68A2 2 0 0 1 22 16.92z" />
                                </svg>
                            </div>
                            <h5>Contactos</h5>
                            <p>Información de contacto</p>
                        </a>
                    </div>
                <?php endif; ?>

                <?php if (in_array($rol, ['administrador', 'digitador'])) : ?>
                    <div class="col-12 col-md-6 col-lg-4 animate-fade-in-up delay-600">
                        <a href="<?php echo url('views/importacion.php'); ?>" class="menu-card-modern">
                            <div class="icon-container gradient-indigo">
                                <svg xmlns="http://www.w3.org/2000/svg" width="56" height="56" viewBox="0 0 24 24" fill="none" stroke="white" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round">
                                    <path d="M16 16l-4-4-4 4M12 12v9"></path>
                                    <path d="M20.39 18.39A5 5 0 0 0 18 9h-1.26A8 8 0 1 0 3 16.3"></path>
                                    <path d="M16 16l-4-4-4 4"></path>
                                </svg>
                            </div>
                            <h5>Importación Masiva</h5>
                            <p>Alumnos, familiares y matrículas</p>
                            <span class="badge badge-gradient-primary"><i class="bi bi-cloud-upload me-1"></i>Admin</span>
                        </a>
                    </div>
                <?php endif; ?>

                <?php if (in_array($rol, ['administrador', 'digitador', 'convivencia', 'inspector'])) : ?>
                    <!-- Card Reportes: visible para administradores, digitadores, convivencia e inspectores -->
                    <div class="col-12 col-md-6 col-lg-4 animate-fade-in-up delay-700">
                        <a href="<?php echo url('views/reportes.php'); ?>" class="menu-card-modern">
                            <div class="icon-container gradient-green">
                                <!-- Ícono de gráfico/estadística -->
                                <svg xmlns="http://www.w3.org/2000/svg" width="56" height="56" fill="none" stroke="white" stroke-width="1.5" viewBox="0 0 24 24">
                                    <rect x="3" y="12" width="4" height="8" rx="1" />
                                    <rect x="9" y="8" width="4" height="12" rx="1" />
                                    <rect x="15" y="4" width="4" height="16" rx="1" />
                                </svg>
                            </div>
                            <h5>Reportes</h5>
                            <p>Reportes estadísticos del sistema</p>
                            <span class="badge badge-gradient-success"><i class="bi bi-file-earmark-bar-graph me-1"></i>Estadísticas</span>
                        </a>
                    </div>
                <?php endif; ?>

                <?php if ($rol === 'administrador') : ?>
                    <div class="col-12 col-md-6 col-lg-4 animate-fade-in-up delay-800">
                        <a href="<?php echo url('views/gestion_usuarios.php'); ?>" class="menu-card-modern">
                            <div class="icon-container gradient-red">
                                <svg xmlns="http://www.w3.org/2000/svg" width="56" height="56" fill="none" stroke="white" stroke-width="1.5" viewBox="0 0 24 24">
                                    <circle cx="12" cy="12" r="3" />
                                    <path d="M19.4 15a1.65 1.65 0 0 0 .33 1.82l.06.06a2 2 0 0 1-2.83 2.83l-.06-.06a1.65 1.65 0 0 0-1.82-.33 1.65 1.65 0 0 0-1 1.51V21a2 2 0 0 1-4 0v-.09a1.65 1.65 0 0 0-1-1.51 1.65 1.65 0 0 0-1.82.33l-.06.06a2 2 0 0 1-2.83-2.83l.06-.06a1.65 1.65 0 0 0 .33-1.82 1.65 1.65 0 0 0-1.51-1H3a2 2 0 0 1 0-4h.09a1.65 1.65 0 0 0 1.51-1 1.65 1.65 0 0 0-.33-1.82l-.06-.06a2 2 0 0 1 2.83-2.83l.06.06a1.65 1.65 0 0 0 1.82.33h.09A1.65 1.65 0 0 0 9 3.09V3a2 2 0 0 1 4 0v.09a1.65 1.65 0 0 0 1 1.51h.09a1.65 1.65 0 0 0 1.82-.33l.06-.06a2 2 0 0 1 2.83 2.83l-.06.06a1.65 1.65 0 0 0-.33 1.82v.09a1.65 1.65 0 0 0 1.51 1H21a2 2 0 0 1 0 4h-.09a1.65 1.65 0 0 0-1.51 1z" />
                                </svg>
                            </div>
                            <h5>Panel de administración</h5>
                            <p>Gestión de usuarios y sistema</p>
                            <span class="badge bg-danger"><i class="bi bi-shield-lock me-1"></i>Solo Admin</span>
                        </a>
                    </div>
                    <div class="col-12 col-md-6 col-lg-4 animate-fade-in-up delay-900">
                        <a href="<?php echo url('views/log_viewer.php'); ?>" class="menu-card-modern">
                            <div class="icon-container gradient-indigo">
                                <svg xmlns="http://www.w3.org/2000/svg" width="56" height="56" viewBox="0 0 24 24" fill="none" stroke="white" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round">
                                    <path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"></path>
                                    <polyline points="14 2 14 8 20 8"></polyline>
                                    <line x1="16" y1="13" x2="8" y2="13"></line>
                                    <line x1="16" y1="17" x2="8" y2="17"></line>
                                    <polyline points="10 9 9 9 8 9"></polyline>
                                </svg>
                            </div>
                            <h5>Registro de Actividad</h5>
                            <p>Ver logs del sistema</p>
                            <span class="badge bg-secondary"><i class="bi bi-file-text me-1"></i>Logs</span>
                        </a>
                    </div>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>

<!-- Modales específicos de la página -->
<?php include __DIR__ . '/modal_registro_alumno.php'; ?>

<?php include __DIR__ . '/footer.php'; ?>