<?php
require_once '../../includes/auth.php';
require_once __DIR__ . '/../../includes/database.php';
require_once __DIR__ . '/../../includes/header.php';
requireRole('estudiante');

// Verificar y definir constantes
if (!defined('APP_NAME')) define('APP_NAME', 'MI COLE');
if (!defined('APP_URL')) define('APP_URL', '');

// Obtener datos del usuario
$nombreColegio = APP_NAME;
$usuarioNombre = $_SESSION['user_name'] ?? 'Estudiante';
$usuarioId = $_SESSION['user_id'] ?? 0;

// Obtener información del estudiante
global $db;
$estudiante = $db->fetchOne("SELECT * FROM estudiantes WHERE usuario_id = ?", [$usuarioId]);

// Valores por defecto
$nivel = 'secundaria';
$grado = '5';
$seccion = 'A';
$estudianteId = 0;

if ($estudiante) {
    $nivel = $estudiante['nivel'] ?? $nivel;
    $grado = $estudiante['grado'] ?? $grado;
    $seccion = $estudiante['seccion'] ?? $seccion;
    $estudianteId = $estudiante['id'] ?? 0;
    $usuarioRol = "Estudiante - $grado $nivel";
} else {
    $usuarioRol = "Estudiante";
}

// Calcular fechas de la semana actual (lunes a viernes)
$hoy = new DateTime();
$lunes = (clone $hoy)->modify('Monday this week');
$viernes = (clone $lunes)->modify('+4 days');
$fechasSemana = [];

// Generar fechas de lunes a viernes
for ($i = 0; $i < 5; $i++) {
    $fecha = clone $lunes;
    $fecha->modify("+$i days");
    $fechasSemana[] = $fecha->format('Y-m-d');
}

// Obtener asistencia para la semana actual
$asistencia = $db->fetchAll("
    SELECT fecha, estado 
    FROM asistencias_estudiantes 
    WHERE estudiante_id = ?
    AND fecha BETWEEN ? AND ?
", [$estudianteId, $fechasSemana[0], $fechasSemana[4]]);

// Contar días asistidos (incluyendo tardanza como asistido)
$diasAsistidos = 0;
foreach ($asistencia as $registro) {
    if (in_array($registro['estado'], ['presente', 'justificado', 'tardanza'])) {
        $diasAsistidos++;
    }
}

// Obtener contadores de notificaciones
$contadores = $db->fetchOne("
    SELECT 
        COUNT(*) AS total,
        SUM(CASE WHEN leida = 0 THEN 1 ELSE 0 END) AS no_leidas
    FROM notificaciones 
    WHERE usuario_id = ?
", [$usuarioId]);

$totalNotificaciones = $contadores['total'] ?? 0;
$notificacionesNoLeidas = $contadores['no_leidas'] ?? 0;

// Obtener solo las notificaciones NO LEÍDAS para el dropdown
$notificaciones = $db->fetchAll("
    SELECT * 
    FROM notificaciones 
    WHERE usuario_id = ? AND leida = 0
    ORDER BY fecha_envio DESC
", [$usuarioId]);

// Configurar título de página
$pageTitle = "Panel de Estudiante";
?>
<!DOCTYPE html>
<html lang="es">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= $pageTitle ?> - <?= $nombreColegio ?></title>

    <!-- Cargar Font Awesome desde CDN -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">

    <!-- Cargar Bootstrap CSS local -->
    <link href="<?= APP_URL ?>/assets/css/bootstrap.min.css" rel="stylesheet">

    <!-- Estilos personalizados -->
    <style>
        :root {
            --primary: #3498db;
            --secondary: #2980b9;
            --accent: #e74c3c;
            --light: #ecf0f1;
            --dark: #2c3e50;
            --success: #2ecc71;
            --warning: #f39c12;
            --info: #1abc9c;
            --sidebar-width: 280px;
            --header-height: 70px;
        }

        body {
            background-color: #f5f7fa;
            color: #333;
            display: flex;
            min-height: 100vh;
            margin: 0;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }

        /* Sidebar para estudiantes */
        .sidebar {
            width: var(--sidebar-width);
            background: linear-gradient(to bottom, var(--primary), var(--secondary));
            color: white;
            height: 100vh;
            position: fixed;
            overflow-y: auto;
            transition: all 0.3s ease;
            box-shadow: 3px 0 15px rgba(0, 0, 0, 0.1);
            z-index: 100;
        }

        .logo-container {
            padding: 20px 25px;
            border-bottom: 1px solid rgba(255, 255, 255, 0.1);
            background-color: rgba(0, 0, 0, 0.1);
        }

        .logo {
            display: flex;
            align-items: center;
            gap: 12px;
        }

        .logo i {
            font-size: 28px;
            color: white;
        }

        .logo h1 {
            font-size: 22px;
            font-weight: 600;
        }

        .menu-section {
            padding: 25px 0;
        }

        .menu-title {
            padding: 0 25px 10px;
            font-size: 14px;
            text-transform: uppercase;
            letter-spacing: 1px;
            color: rgba(255, 255, 255, 0.7);
            font-weight: 500;
        }

        .menu-items {
            list-style: none;
            padding: 0;
        }

        .menu-item {
            padding: 12px 25px;
            display: flex;
            align-items: center;
            gap: 15px;
            cursor: pointer;
            transition: all 0.2s ease;
            border-left: 4px solid transparent;
            position: relative;
        }

        .menu-item:hover {
            background-color: rgba(255, 255, 255, 0.1);
            border-left-color: white;
        }

        .menu-item.active {
            background-color: rgba(255, 255, 255, 0.15);
            border-left-color: white;
        }

        .menu-item i {
            width: 24px;
            text-align: center;
            font-size: 18px;
        }

        .menu-item span {
            font-size: 16px;
            font-weight: 500;
        }

        /* Main Content */
        .main-content {
            flex: 1;
            margin-left: var(--sidebar-width);
            padding-bottom: 30px;
        }

        /* Header */
        .header {
            height: var(--header-height);
            background-color: white;
            display: flex;
            align-items: center;
            justify-content: space-between;
            padding: 0 30px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
            position: sticky;
            top: 0;
            z-index: 90;
        }

        .header-left {
            display: flex;
            align-items: center;
            gap: 15px;
        }

        .toggle-menu {
            display: none;
            font-size: 24px;
            cursor: pointer;
            color: var(--dark);
        }

        .user-info {
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .user-avatar {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            background: linear-gradient(135deg, var(--secondary), var(--primary));
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-weight: bold;
            font-size: 18px;
        }

        .user-details {
            display: flex;
            flex-direction: column;
        }

        .user-name {
            font-weight: 600;
            font-size: 15px;
        }

        .user-role {
            font-size: 13px;
            color: #7f8c8d;
        }

        /* Content Area */
        .content {
            padding: 30px;
        }

        .welcome-section {
            background: linear-gradient(135deg, var(--primary), var(--secondary));
            border-radius: 15px;
            padding: 30px;
            color: white;
            margin-bottom: 30px;
            box-shadow: 0 10px 20px rgba(52, 152, 219, 0.2);
            position: relative;
            overflow: hidden;
        }

        .welcome-section::before {
            content: "";
            position: absolute;
            top: -50%;
            right: -50%;
            width: 200%;
            height: 200%;
            background: radial-gradient(circle, rgba(255, 255, 255, 0.1) 0%, rgba(255, 255, 255, 0) 70%);
            transform: rotate(30deg);
            z-index: 0;
        }

        .welcome-content {
            position: relative;
            z-index: 1;
        }

        .welcome-section h1 {
            font-size: 32px;
            margin-bottom: 10px;
            font-weight: 600;
            text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        }

        .welcome-section p {
            font-size: 18px;
            max-width: 700px;
            opacity: 0.9;
            line-height: 1.6;
            text-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
        }

        .stats-container {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
            gap: 20px;
            margin-bottom: 30px;
        }

        .stat-card {
            background-color: white;
            border-radius: 12px;
            padding: 25px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.05);
            display: flex;
            align-items: center;
            gap: 20px;
            transition: all 0.3s ease;
            border-left: 4px solid var(--primary);
        }

        .stat-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
        }

        .stat-icon {
            width: 60px;
            height: 60px;
            border-radius: 15px;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 28px;
        }

        .stat-1 {
            background-color: rgba(52, 152, 219, 0.15);
            color: var(--primary);
            border-color: var(--primary);
        }

        .stat-2 {
            background-color: rgba(46, 204, 113, 0.15);
            color: var(--success);
            border-color: var(--success);
        }

        .stat-3 {
            background-color: rgba(155, 89, 182, 0.15);
            color: #9b59b6;
            border-color: #9b59b6;
        }

        .stat-4 {
            background-color: rgba(241, 196, 15, 0.15);
            color: var(--warning);
            border-color: var(--warning);
        }

        .stat-info h3 {
            font-size: 28px;
            margin-bottom: 5px;
            font-weight: 700;
        }

        .stat-info p {
            color: #7f8c8d;
            font-size: 16px;
            font-weight: 500;
            margin-bottom: 5px;
        }

        .stat-info small {
            display: block;
            font-size: 13px;
            color: #95a5a6;
        }

        .section-title {
            font-size: 24px;
            margin: 30px 0 20px;
            color: var(--dark);
            display: flex;
            align-items: center;
            gap: 10px;
            position: relative;
            padding-bottom: 10px;
        }

        .section-title::after {
            content: "";
            position: absolute;
            bottom: 0;
            left: 0;
            width: 60px;
            height: 3px;
            background: var(--primary);
            border-radius: 3px;
        }

        .section-title i {
            color: var(--primary);
        }

        .quick-actions {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
            gap: 20px;
            margin-bottom: 30px;
        }

        .action-card {
            background-color: white;
            border-radius: 12px;
            padding: 25px 20px;
            text-align: center;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.05);
            transition: all 0.3s ease;
            cursor: pointer;
            border: 1px solid #eaeaea;
        }

        .action-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 8px 20px rgba(0, 0, 0, 0.1);
            background: linear-gradient(135deg, var(--primary), var(--secondary));
            color: white;
        }

        .action-card:hover i,
        .action-card:hover p {
            color: white;
        }

        .action-card i {
            font-size: 36px;
            margin-bottom: 15px;
            color: var(--primary);
            transition: all 0.3s ease;
        }

        .action-card p {
            font-weight: 600;
            color: var(--dark);
            transition: all 0.3s ease;
            margin: 0;
        }

        /* Footer */
        .footer {
            padding: 20px 30px;
            text-align: center;
            color: #7f8c8d;
            font-size: 14px;
            border-top: 1px solid #eee;
            margin-top: 30px;
            background: white;
        }

        /* Responsive Design */
        @media (max-width: 992px) {
            .sidebar {
                transform: translateX(-100%);
            }

            .sidebar.active {
                transform: translateX(0);
            }

            .main-content {
                margin-left: 0;
            }

            .toggle-menu {
                display: block;
            }

            .search-bar input {
                width: 200px;
            }
        }

        @media (max-width: 768px) {
            .search-bar {
                display: none;
            }

            .stats-container {
                grid-template-columns: 1fr;
            }

            .welcome-section h1 {
                font-size: 26px;
            }

            .welcome-section p {
                font-size: 16px;
            }

            .section-title {
                font-size: 20px;
            }
        }

        /* Notificaciones */
        .notification-container {
            position: relative;
        }

        .notification-dropdown {
            position: absolute;
            top: 40px;
            right: 0;
            width: 400px;
            background: white;
            border-radius: 12px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
            z-index: 1000;
            display: none;
            overflow: hidden;
            transform: translateY(10px);
            opacity: 0;
            transition: transform 0.3s ease, opacity 0.3s ease;
        }

        .notification-container.active .notification-dropdown {
            display: block;
            transform: translateY(0);
            opacity: 1;
        }

        .notification-header {
            padding: 15px 20px;
            border-bottom: 1px solid #eee;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .notification-header h4 {
            margin: 0;
            font-size: 16px;
        }

        .notification-header small {
            color: var(--primary);
            cursor: pointer;
            font-weight: 500;
        }

        .notification-list {
            max-height: 400px;
            overflow-y: auto;
        }

        .notification-item {
            padding: 15px 20px;
            border-bottom: 1px solid #f8f9fa;
            display: flex;
            gap: 15px;
            cursor: pointer;
            transition: background 0.2s;
        }

        .notification-item:hover {
            background-color: #f8f9fa;
        }

        .notification-icon {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            background: rgba(52, 152, 219, 0.1);
            display: flex;
            align-items: center;
            justify-content: center;
            color: var(--primary);
            flex-shrink: 0;
        }

        .notification-content {
            flex: 1;
        }

        .notification-title {
            display: flex;
            justify-content: space-between;
            margin-bottom: 5px;
        }

        .notification-title strong {
            font-weight: 600;
            color: var(--dark);
        }

        .notification-title small {
            color: #95a5a6;
            font-size: 12px;
        }

        .notification-message {
            color: #7f8c8d;
            font-size: 14px;
            line-height: 1.4;
            margin-bottom: 10px;
        }

        .notification-actions {
            display: flex;
            gap: 10px;
            margin-top: 8px;
        }

        .notification-actions a {
            padding: 5px 12px;
            background: rgba(52, 152, 219, 0.1);
            border-radius: 20px;
            color: var(--primary);
            font-size: 13px;
            font-weight: 500;
            text-decoration: none;
            display: flex;
            align-items: center;
            gap: 5px;
        }

        .notification-actions a:hover {
            background: rgba(52, 152, 219, 0.2);
        }

        .notification-empty {
            padding: 30px 20px;
            text-align: center;
            color: #95a5a6;
        }

        .notification-empty i {
            font-size: 36px;
            margin-bottom: 10px;
        }

        .notification-footer {
            padding: 12px 20px;
            text-align: center;
            border-top: 1px solid #eee;
            background: #f8f9fa;
        }

        .notification-footer a {
            color: var(--primary);
            text-decoration: none;
            font-size: 14px;
            font-weight: 500;
        }
        
        /* SOLUCIÓN: Reorganizar elementos en el header-right */
        .header-right {
            display: flex;
            align-items: center;
            gap: 20px;
        }
        
        /* ANIMACIÓN DE NOTIFICACIONES AÑADIDA */
        .notification-trigger {
            position: relative;
            cursor: pointer;
        }

        .notification-trigger i {
            font-size: 20px;
            color: var(--dark);
        }

        .notification-badge {
            position: absolute;
            top: -5px;
            right: -5px;
            background-color: var(--accent);
            color: white;
            border-radius: 50%;
            width: 18px;
            height: 18px;
            font-size: 12px;
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: bold;
            animation: pulse 1.5s infinite;
        }

        @keyframes pulse {
            0% { transform: scale(1); }
            50% { transform: scale(1.2); }
            100% { transform: scale(1); }
        }
    </style>
</head>

<body>
    <div class="sidebar">
        <div class="logo-container">
            <div class="logo">
                <i class="fas fa-graduation-cap"></i>
                <h1><?= $nombreColegio ?></h1>
            </div>
        </div>

        <div class="menu-section">
            <h3 class="menu-title">Principal</h3>
            <ul class="menu-items">
                <li class="menu-item active">
                    <a href="<?= APP_URL ?>/modules/estudiante/dashboard.php" style="display: block; color: inherit; text-decoration: none;">
                        <i class="fas fa-home"></i>
                        <span>Menu</span>
                    </a>
                </li>
            </ul>
        </div>

        <div class="menu-section">
            <h3 class="menu-title">Académico</h3>
            <ul class="menu-items">
                <li class="menu-item">
                    <a href="<?= APP_URL ?>/modules/estudiante/ver_asistencia.php" style="display: block; color: inherit; text-decoration: none;">
                        <i class="fas fa-calendar-alt"></i>
                        <span>Ver Asistencia</span>
                    </a>
                </li>
                <li class="menu-item">
                    <a href="<?= APP_URL ?>/modules/estudiante/notificaciones.php" style="display: block; color: inherit; text-decoration: none;">
                        <i class="fas fa-inbox"></i>
                        <span>Ver Mensajería</span>
                    </a>
                </li>
                <li class="menu-item">
                    <a href="<?= APP_URL ?>/modules/estudiante/enviar_correo.php" style="display: block; color: inherit; text-decoration: none;">
                        <i class="fas fa-envelope"></i>
                        <span>Enviar comunicado</span>
                    </a>
                </li>
                <li class="menu-item">
                    <a href="<?= APP_URL ?>/modules/estudiante/historial_de_correos_enviados.php" style="display: block; color: inherit; text-decoration: none;">
                        <i class="fas fa-history"></i>
                        <span>Historial de Comunicados</span>
                    </a>
                </li>
            </ul>
        </div>
<div class="menu-section">
        <h3 class="menu-title">Recursos</h3>
        <ul class="menu-items">
            <li class="menu-item">
                <a href="<?= APP_URL ?>/modules/estudiante/videos_tutoriales.php" style="display: block; color: inherit; text-decoration: none;">
                    <i class="fas fa-video"></i>
                    <span>Videos Tutoriales</span>
                </a>
            </li>
        </ul>
    </div>
        <div class="menu-section">
            <h3 class="menu-title">Cuenta</h3>
            <ul class="menu-items">
                <li class="menu-item">
                    <a href="<?= APP_URL ?>/modules/estudiante/cambiar_contrasena.php" style="display: block; color: inherit; text-decoration: none;">
                        <i class="fas fa-key"></i>
                        <span>Cambiar Contraseña</span>
                    </a>
                </li>
                <li class="menu-item">
                    <a href="<?= APP_URL ?>/auth/logout.php" style="display: block; color: inherit; text-decoration: none;">
                        <i class="fas fa-sign-out-alt"></i>
                        <span>Cerrar Sesión</span>
                    </a>
                </li>
            </ul>
        </div>
    </div>


    <!-- Main Content -->
    <div class="main-content">
        <!-- Header -->
        <header class="header">
            <div class="header-left">
                <div class="toggle-menu">
                    <i class="fas fa-bars"></i>
                </div>

            </div>

            <div class="header-right">
                <!-- Contenedor de notificaciones con animación -->
                <div class="notification-container" id="notificationContainer">
                    <div class="notification-trigger" id="notificationTrigger">
                        <i class="fas fa-bell"></i>
                        <?php if ($notificacionesNoLeidas > 0): ?>
                            <div class="notification-badge"><?= $notificacionesNoLeidas ?></div>
                        <?php endif; ?>
                    </div>

                    <div class="notification-dropdown">
                        
                        <div class="notification-list">
                            <?php if ($notificacionesNoLeidas > 0): ?>
                                <?php foreach ($notificaciones as $notif): 
                                    // Extraer el ID del correo del enlace
                                    $correo_id = explode('=', $notif['enlace'])[1] ?? 0;
                                ?>
                                    <div class="notification-item" data-id="<?= $notif['id'] ?>">
                                        <div class="notification-icon">
                                            <i class="fas fa-envelope"></i>
                                        </div>
                                        <div class="notification-content">
                                            <div class="notification-title">
                                                <strong><?= htmlspecialchars($notif['titulo']) ?></strong>
                                                <small><?= date('d M H:i', strtotime($notif['fecha_envio'])) ?></small>
                                            </div>
                                            <div class="notification-message">
                                                <?= htmlspecialchars($notif['mensaje']) ?>
                                            </div>
                                            <div class="notification-actions">
                                                <a href="<?= APP_URL ?>/modules/estudiante/ver_correo.php?correo_id=<?= $correo_id ?>" class="btn-view">
                                                    <i class="fas fa-eye"></i> Ver comunicado
                                                </a>
                                                
                                            </div>
                                        </div>
                                    </div>
                                <?php endforeach; ?>
                            <?php else: ?>
                                <div class="notification-empty">
                                    <i class="fas fa-inbox"></i>
                                    <p>No tienes notificaciones nuevas</p>
                                </div>
                            <?php endif; ?>
                        </div>
                        <div class="notification-footer">
                            <a href="notificaciones.php">Ver todas las notificaciones</a>
                        </div>
                    </div>
                </div>

                <!-- Perfil de usuario -->
                <div class="user-profile">
                    <div class="user-info">
                        <div class="user-avatar">
                            <?=
                            substr($usuarioNombre, 0, 1) .
                                (strpos($usuarioNombre, ' ') !== false ?
                                    substr($usuarioNombre, strpos($usuarioNombre, ' ') + 1, 1) :
                                    substr($usuarioNombre, 1, 1))
                            ?>
                        </div>
                        <div class="user-details">
                            <div class="user-name"><?= $usuarioNombre ?></div>
                            <div class="user-role"><?= $usuarioRol ?></div>
                        </div>
                    </div>
                </div>
            </div>
        </header>

        <!-- Content Area -->
        <div class="content">
            <div class="welcome-section">
                <div class="welcome-content">
                    <h1>Bienvenido, <?= $usuarioNombre ?></h1>
                    <p>Revisa tu asistencia y mensajeria desde este panel estudiantil.</p>
                </div>
            </div>

            <div class="stats-container">

                <div class="stat-card">
                    <div class="stat-icon stat-2">
                        <i class="fas fa-clipboard-check"></i>
                    </div>
                    <div class="stat-info">
                        <h3><?= $diasAsistidos ?></h3>
                        <p>Días asistidos</p>
                        <small>Semana del <?= date('d/m', strtotime($fechasSemana[0])) ?> al <?= date('d/m', strtotime($fechasSemana[4])) ?></small>
                    </div>
                </div>

                <div class="stat-card">
                    <div class="stat-icon stat-4">
                        <i class="fas fa-inbox"></i>
                    </div>
                    <div class="stat-info">
                        <h3><?= $totalNotificaciones ?></h3>
                        <p>Mensajeria</p>
                    </div>
                </div>
            </div>

            <h2 class="section-title"><i class="fas fa-bolt"></i> Acciones Rápidas</h2>
            <div class="quick-actions">

                <a href="<?= APP_URL ?>/modules/estudiante/ver_asistencia.php" style="text-decoration: none; color: inherit;">
                    <div class="action-card">
                        <i class="fas fa-calendar-alt"></i>
                        <p>Ver Asistencia</p>
                    </div>
                </a>

                <a href="<?= APP_URL ?>/modules/estudiante/notificaciones.php" style="text-decoration: none; color: inherit;">
                    <div class="action-card">
                        <i class="fas fa-inbox"></i>
                        <p>Ver Mensajeria</p>
                    </div>
                </a>

                <a href="<?= APP_URL ?>/modules/estudiante/cambiar_contrasena.php" style="text-decoration: none; color: inherit;">
                    <div class="action-card">
                        <i class="fas fa-key"></i>
                        <p>Cambiar Contraseña</p>
                    </div>
                </a>
                <a href="<?= APP_URL ?>/modules/estudiante/enviar_correo.php" style="text-decoration: none; color: inherit;">
                    <div class="action-card">
                        <i class="fas fa-envelope"></i>
                        <p>Enviar comunicado</p>
                    </div>
                </a>
                <a href="<?= APP_URL ?>/modules/estudiante/historial_de_correos_enviados.php" style="text-decoration: none; color: inherit;">
                    <div class="action-card">
                        <i class="fas fa-history"></i>
                        <p>Historial de Comunicados</p>
                    </div>
                </a>
            </div>
        </div>

        <!-- Footer -->
        <footer class="footer">
            <p>Sistema Escolar Integrado v3.0 © <?= date('Y') ?> - Todos los derechos reservados</p>
        </footer>
    </div>

    <script src="<?= APP_URL ?>/assets/js/bootstrap.bundle.min.js"></script>
    <script>
        // Toggle sidebar on mobile
        document.querySelector('.toggle-menu').addEventListener('click', function() {
            document.querySelector('.sidebar').classList.toggle('active');
        });

        // Set active menu item
        const menuItems = document.querySelectorAll('.menu-item');
        menuItems.forEach(item => {
            item.addEventListener('click', function() {
                menuItems.forEach(i => i.classList.remove('active'));
                this.classList.add('active');
            });
        });

        // Notificaciones
        document.addEventListener('DOMContentLoaded', function() {
            const notificationContainer = document.getElementById('notificationContainer');
            const notificationTrigger = document.getElementById('notificationTrigger');
            const notificationDropdown = notificationContainer.querySelector('.notification-dropdown');

            // Abrir/cerrar menú de notificaciones
            notificationTrigger.addEventListener('click', function(e) {
                e.stopPropagation();
                notificationContainer.classList.toggle('active');
            });

            // Cerrar menú al hacer clic fuera
            document.addEventListener('click', function(e) {
                if (!notificationContainer.contains(e.target)) {
                    notificationContainer.classList.remove('active');
                }
            });

            // Prevenir cierre al hacer clic dentro del menú
            notificationDropdown.addEventListener('click', function(e) {
                e.stopPropagation();
            });

            // Marcar notificación como leída
            document.querySelectorAll('.notification-item').forEach(item => {
                item.addEventListener('click', function() {
                    const notifId = this.getAttribute('data-id');
                    fetch('marcar_leida.php', {
                            method: 'POST',
                            headers: {
                                'Content-Type': 'application/json'
                            },
                            body: JSON.stringify({
                                id: notifId
                            })
                        }).then(response => response.json())
                        .then(data => {
                            if (data.success) {
                                this.style.opacity = '0.7';
                            }
                        });
                });
            });

            // Marcar todas como leídas
            document.getElementById('markAllAsRead').addEventListener('click', function(e) {
                e.stopPropagation();
                fetch('marcar_todas_leidas.php', {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/json'
                        },
                        body: JSON.stringify({
                            usuario_id: <?= $usuarioId ?>
                        })
                    }).then(response => response.json())
                    .then(data => {
                        if (data.success) {
                            // Actualizar la interfaz
                            document.querySelectorAll('.notification-item').forEach(item => {
                                item.style.opacity = '0.7';
                            });
                            if (document.querySelector('.notification-badge')) {
                                document.querySelector('.notification-badge').style.display = 'none';
                            }
                        }
                    });
            });
        });
    </script>
</body>

</html>