<?php
require_once '../../includes/auth.php';
requireRole('direccion');

// Obtener datos del usuario desde la sesión
$nombreColegio = APP_NAME;
$usuarioNombre = $_SESSION['user_name'] ?? 'Director';
$usuarioId = $_SESSION['user_id'] ?? 0;
$usuarioRol = 'Director';

// Obtener notificaciones no leídas
global $db;
$notificaciones = $db->fetchAll("
    SELECT * 
    FROM notificaciones 
    WHERE usuario_id = ? AND leida = 0
    ORDER BY fecha_envio DESC
    LIMIT 5
", [$usuarioId]);

$notificacionesNoLeidas = count($notificaciones);

// Temas para videos
$temas = [
    ['id' => 'usuarios', 'nombre' => 'Gestión de Usuarios', 'icono' => 'fas fa-users'],
    ['id' => 'asistencia', 'nombre' => 'Listar Personal Eduticativo y Alumnos', 'icono' => 'fas fa-clipboard-check'],
    ['id' => 'reportes', 'nombre' => 'Reportes y Estadísticas', 'icono' => 'fas fa-chart-bar'],
    ['id' => 'notificaciones', 'nombre' => 'Sistema de Notificaciones', 'icono' => 'fas fa-bell'],
    ['id' => 'correo', 'nombre' => 'Correos Administrativos', 'icono' => 'fas fa-envelope'],
    ['id' => 'configuracion', 'nombre' => 'Configuración del Sistema', 'icono' => 'fas fa-cog'],
];

// Configurar título de página
$pageTitle = "Videos Tutoriales";
?>

<!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 (copiados 1:1 de tu dashboard) -->
    <style>
        :root {
            --primary: #2c3e50;
            --secondary: #3498db;
            --accent: #e74c3c;
            --light: #ecf0f1;
            --dark: #34495e;
            --success: #2ecc71;
            --warning: #f39c12;
            --sidebar-width: 280px;
            --header-height: 70px;
            --director-color: #9b59b6;
        }

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

        /* Sidebar */
        .sidebar {
            width: var(--sidebar-width);
            background: linear-gradient(to bottom, var(--primary), var(--dark));
            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: 1000;
            left: 0;
            transform: translateX(0);
        }

        .sidebar-close {
            display: none;
            position: absolute;
            top: 20px;
            right: 20px;
            color: white;
            font-size: 24px;
            cursor: pointer;
            z-index: 1001;
        }

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

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

        .logo i {
            font-size: 28px;
            color: var(--director-color);
        }

        .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.6);
            font-weight: 500;
        }

        .menu-items {
            list-style: none;
            padding: 0;
            margin: 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.05);
            border-left-color: var(--director-color);
        }

        .menu-item.active {
            background-color: rgba(255, 255, 255, 0.1);
            border-left-color: var(--director-color);
        }

        .menu-item i {
            width: 24px;
            text-align: center;
            font-size: 18px;
            color: var(--director-color);
        }

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

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

        /* Overlay para móviles */
        .sidebar-overlay {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background-color: rgba(0, 0, 0, 0.5);
            z-index: 999;
            opacity: 0;
            transition: opacity 0.3s ease;
        }

        .sidebar-overlay.active {
            display: block;
            opacity: 1;
        }

        /* 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);
        }

        .search-bar {
            position: relative;
        }

        .search-bar input {
            padding: 10px 15px 10px 40px;
            border-radius: 30px;
            border: 1px solid #ddd;
            width: 300px;
            transition: all 0.3s ease;
        }

        .search-bar input:focus {
            outline: none;
            border-color: var(--director-color);
            box-shadow: 0 0 0 3px rgba(155, 89, 182, 0.2);
        }

        .search-bar i {
            position: absolute;
            left: 15px;
            top: 50%;
            transform: translateY(-50%);
            color: #95a5a6;
        }

        .header-right {
            display: flex;
            align-items: center;
            gap: 25px;
        }

        .user-profile {
            position: relative;
            cursor: pointer;
        }

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

        .user-avatar {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            background: linear-gradient(135deg, var(--director-color), #8e44ad);
            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;
        }

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

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

        .folder-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
            gap: 25px;
            margin-top: 20px;
        }

        .folder-card {
            background: white;
            border-radius: 14px;
            padding: 25px 20px;
            text-align: center;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.08);
            transition: all 0.3s ease;
            cursor: pointer;
            text-decoration: none;
            color: inherit;
        }

        .folder-card:hover {
            transform: translateY(-6px);
            box-shadow: 0 8px 25px rgba(155, 89, 182, 0.25);
            background: linear-gradient(to bottom, #f9f7ff, white);
        }

        .folder-icon {
            font-size: 48px;
            color: var(--director-color);
            margin-bottom: 15px;
        }

        .folder-name {
            font-size: 18px;
            font-weight: 600;
            color: var(--dark);
        }

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

        /* Responsive Design */
        @media (max-width: 1200px) {
            .charts-container {
                grid-template-columns: 1fr;
            }
        }

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

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

            .sidebar-close {
                display: block;
            }

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

            .toggle-menu {
                display: block;
            }

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

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

            .folder-grid {
                grid-template-columns: 1fr;
            }
        }

        /* ---------------------- */
        /* NOTIFICACIONES (NUEVO) */
        /* ---------------------- */
        .notification-container {
            position: relative;
        }

        .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: 20px;
            height: 20px;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 12px;
            font-weight: bold;
        }

        .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(--director-color);
            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(155, 89, 182, 0.1);
            display: flex;
            align-items: center;
            justify-content: center;
            color: var(--director-color);
            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;
        }

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

        .notification-actions a:hover {
            background: rgba(155, 89, 182, 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(--director-color);
            text-decoration: none;
            font-size: 14px;
            font-weight: 500;
        }
    </style>
</head>

<body>
    <!-- Overlay para móviles -->
    <div class="sidebar-overlay" id="sidebarOverlay"></div>

    <!-- Sidebar Menu -->
    <div class="sidebar" id="sidebar">
        <div class="sidebar-close" id="sidebarClose">
            <i class="fas fa-times"></i>
        </div>
        <div class="logo-container">
            <div class="logo">
                <i class="fas fa-user-tie"></i>
                <h1><?= $nombreColegio ?></h1>
            </div>
        </div>

        <div class="menu-section">
            <h3 class="menu-title">Principal</h3>
            <ul class="menu-items">
                <li class="menu-item">
                    <a href="<?= APP_URL ?>/modules/direccion/dashboard.php" style="color: inherit; text-decoration: none;">
                        <i class="fas fa-home"></i>
                        <span>Menú Principal</span>
                    </a>
                </li>
                <li class="menu-item">
                    <a href="<?= APP_URL ?>/modules/direccion/notificaciones.php" style="color: inherit; text-decoration: none;">
                        <i class="fas fa-inbox"></i>
                        <span>Buzón de Mensajería</span>
                    </a>
                </li>
                <li class="menu-item">
                    <a href="<?= APP_URL ?>/modules/direccion/historial_de_correos.php" style="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">Funciones</h3>
            <ul class="menu-items">
                <li class="menu-item">
                    <a href="<?= APP_URL ?>/modules/direccion/lista_alumnos.php" style="color: inherit; text-decoration: none;">
                        <i class="fas fa-chart-bar"></i>
                        <span>Lista de Alumnos</span>
                    </a>
                </li>
                <li class="menu-item">
                    <a href="<?= APP_URL ?>/modules/direccion/lista_docentes.php" style="color: inherit; text-decoration: none;">
                        <i class="fas fa-chalkboard-teacher"></i>
                        <span>Personal Académico</span>
                    </a>
                </li>
                <li class="menu-item">
                    <a href="<?= APP_URL ?>/modules/direccion/usuarios.php" style="color: inherit; text-decoration: none;">
                        <i class="fas fa-user-graduate"></i>
                        <span>Añadir Estudiantes</span>
                    </a>
                </li>
                <li class="menu-item">
                    <a href="<?= APP_URL ?>/modules/direccion/agregar_docente.php" style="color: inherit; text-decoration: none;">
                        <i class="fas fa-chalkboard-teacher"></i>
                        <span>Añadir Docente</span>
                    </a>
                </li>
                <li class="menu-item">
                    <a href="<?= APP_URL ?>/modules/direccion/anadir_auxiliar.php" style="color: inherit; text-decoration: none;">
                        <i class="fas fa-user-graduate"></i>
                        <span>Añadir Auxiliar</span>
                    </a>
                </li>
                <li class="menu-item">
                    <a href="<?= APP_URL ?>/modules/direccion/correo_administrativo.php" style="color: inherit; text-decoration: none;">
                        <i class="fas fa-users"></i>
                        <span>Correo Administrativo</span>
                    </a>
                </li>
                <li class="menu-item">
                    <a href="<?= APP_URL ?>/modules/direccion/reportes.php" style="color: inherit; text-decoration: none;">
                        <i class="fas fa-chart-bar"></i>
                        <span>Reportes de Asistencia</span>
                    </a>
                </li>
            </ul>
        </div>

        <div class="menu-section">
            <h3 class="menu-title">Recursos</h3>
            <ul class="menu-items">
                <li class="menu-item active">
                    <a href="<?= APP_URL ?>/modules/direccion/videos_tutoriales.php" style="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">Administración</h3>
            <ul class="menu-items">
                <li class="menu-item">
                    <a href="<?= APP_URL ?>/modules/direccion/cambiar_contrasena.php" style="color: inherit; text-decoration: none;">
                        <i class="fas fa-cog"></i>
                        <span>Cambiar de Contraseña</span>
                    </a>
                </li>
                <li class="menu-item">
                    <a href="<?= APP_URL ?>/auth/logout.php" style="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" id="toggleMenu">
                    <i class="fas fa-bars"></i>
                </div>
            </div>

            <div class="header-right">
                <!-- Sistema de notificaciones (igual que en dashboard) -->
                <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): ?>
                                    <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/direccion/ver_correo.php?id=<?= $notif['id'] ?>" class="btn-view">
                                                    <i class="fas fa-eye"></i> Ver
                                                </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>

                <div class="user-profile">
                    <div class="user-info">
                        <div class="user-avatar"><?= substr($usuarioNombre, 0, 1) . substr($usuarioNombre, strpos($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">
            <h2 class="section-title"><i class="fas fa-video"></i> Videos Tutoriales</h2>
            <p style="color: #7f8c8d; margin-bottom: 30px;">Selecciona una categoría para ver los tutoriales disponibles.</p>

            <div class="folder-grid">
                <?php foreach ($temas as $tema): ?>
                    <a href="<?= APP_URL ?>/modules/direccion/videos_por_seccion.php?id=<?= urlencode($tema['id']) ?>" class="folder-card">
                        <div class="folder-icon">
                            <i class="<?= $tema['icono'] ?>"></i>
                        </div>
                        <div class="folder-name"><?= htmlspecialchars($tema['nombre']) ?></div>
                    </a>
                <?php endforeach; ?>
            </div>
        </div>

        
    </div>

    <script src="<?= APP_URL ?>/assets/js/bootstrap.bundle.min.js"></script>
    <script>
        // Toggle sidebar on mobile
        const sidebar = document.getElementById('sidebar');
        const toggleMenu = document.getElementById('toggleMenu');
        const sidebarClose = document.getElementById('sidebarClose');
        const sidebarOverlay = document.getElementById('sidebarOverlay');

        function toggleSidebar() {
            sidebar.classList.toggle('active');
            sidebarOverlay.classList.toggle('active');
            document.body.style.overflow = sidebar.classList.contains('active') ? 'hidden' : '';
        }

        function closeSidebar() {
            sidebar.classList.remove('active');
            sidebarOverlay.classList.remove('active');
            document.body.style.overflow = '';
        }

        toggleMenu?.addEventListener('click', function(e) {
            e.stopPropagation();
            toggleSidebar();
        });

        sidebarClose?.addEventListener('click', closeSidebar);
        sidebarOverlay?.addEventListener('click', closeSidebar);

        // Cerrar sidebar al hacer clic en un enlace (en móviles)
        if (window.innerWidth <= 992) {
            document.querySelectorAll('.menu-item a').forEach(link => {
                link.addEventListener('click', closeSidebar);
            });
        }

        // 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');
            });
        });

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

            notificationTrigger.addEventListener('click', function(e) {
                e.stopPropagation();
                notificationContainer.classList.toggle('active');
            });

            document.addEventListener('click', function(e) {
                if (!notificationContainer.contains(e.target)) {
                    notificationContainer.classList.remove('active');
                }
            });

            notificationDropdown.addEventListener('click', function(e) {
                e.stopPropagation();
            });

            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';
                            const badge = document.querySelector('.notification-badge');
                            if (badge) {
                                badge.textContent = parseInt(badge.textContent) - 1;
                                if (parseInt(badge.textContent) === 0) {
                                    badge.style.display = 'none';
                                }
                            }
                        }
                    });
                });
            });
        });
    </script>
</body>

</html>