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

// Configurar título de página
$pageTitle = "Gestión de Docentes";
$nombreColegio = APP_NAME;
$usuarioNombre = $_SESSION['user_name'] ?? 'Administrador';
$usuarioRol = 'Administrador';

// Conectar a la base de datos para obtener docentes
global $db;
$docentes = $db->fetchAll("
    SELECT d.id, d.especialidad, d.estado,
           u.id as usuario_id, u.nombre, u.apellido, u.email, u.telefono,
           u.fecha_creacion,
           (SELECT COUNT(*) FROM docente_curso_seccion dcs WHERE dcs.docente_id = d.id) AS num_asignaciones
    FROM docentes d 
    JOIN usuarios u ON d.usuario_id = u.id 
    ORDER BY u.apellido, u.nombre
");
?>

<!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 los mismos estilos que el dashboard -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link href="<?= APP_URL ?>/assets/css/bootstrap.min.css" rel="stylesheet">
    
    <!-- DataTables CSS -->
    <link href="<?= APP_URL ?>/assets/css/dataTables.bootstrap5.min.css" rel="stylesheet">
    
    <style>
        
        :root {
            --primary: #2c3e50;
            --secondary: #3498db;
            --accent: #e74c3c;
            --light: #ecf0f1;
            --dark: #34495e;
            --success: #2ecc71;
            --warning: #f39c12;
            --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 */
        .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: 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: var(--secondary);
        }

        .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;
        }

        .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(--secondary);
        }

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

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

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

        .badge {
            position: absolute;
            right: 25px;
            background-color: var(--accent);
            color: white;
            border-radius: 50%;
            width: 22px;
            height: 22px;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 12px;
            font-weight: bold;
        }

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

        .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(--secondary);
            box-shadow: 0 0 0 3px rgba(52, 152, 219, 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;
        }

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

        .notification i,
        .user-profile 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;
        }

        .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(--secondary), var(--primary));
            border-radius: 15px;
            padding: 30px;
            color: white;
            margin-bottom: 30px;
            box-shadow: 0 10px 20px rgba(52, 152, 219, 0.2);
        }

        .welcome-section h1 {
            font-size: 28px;
            margin-bottom: 10px;
            font-weight: 600;
        }

        .welcome-section p {
            font-size: 16px;
            max-width: 700px;
            opacity: 0.9;
            line-height: 1.6;
        }

        .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: transform 0.3s ease;
        }

        .stat-card:hover {
            transform: translateY(-5px);
        }

        .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(231, 76, 60, 0.15);
            color: var(--accent);
        }

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

        .stat-3 {
            background-color: rgba(52, 152, 219, 0.15);
            color: var(--secondary);
        }

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

        .stat-info h3 {
            font-size: 24px;
            margin-bottom: 5px;
        }

        .stat-info p {
            color: #7f8c8d;
            font-size: 15px;
        }

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

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

        .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: 20px;
            text-align: center;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.05);
            transition: all 0.3s ease;
            cursor: pointer;
        }

        .action-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 8px 20px rgba(0, 0, 0, 0.1);
            background-color: 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(--secondary);
        }

        .action-card p {
            font-weight: 500;
            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: 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: 24px;
            }

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

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

        .quick-actions {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
            gap: 20px;
            margin-bottom: 30px;
        }
        /* Estilos específicos para la tabla */
        .table-container {
            background-color: white;
            border-radius: 12px;
            padding: 25px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.05);
            margin-bottom: 30px;
        }
        
        .table-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
        }
        
        .table-title {
            font-size: 20px;
            font-weight: 600;
            color: var(--dark);
        }
        
        .btn-add {
            background-color: var(--secondary);
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 8px;
            font-weight: 500;
            transition: all 0.3s ease;
        }
        
        .btn-add:hover {
            background-color: var(--primary);
            transform: translateY(-2px);
        }
        
        .badge-asignaciones {
            background-color: var(--secondary);
            color: white;
            padding: 5px 10px;
            border-radius: 15px;
            font-size: 12px;
            font-weight: 500;
        }
        
        .estado-badge {
            padding: 5px 10px;
            border-radius: 15px;
            font-size: 12px;
            font-weight: 500;
        }
        
        .estado-activo {
            background-color: var(--success);
            color: white;
        }
        
        .estado-inactivo {
            background-color: var(--accent);
            color: white;
        }
        
        .estado-licencia {
            background-color: var(--warning);
            color: white;
        }
    </style>
</head>

<body>
    <!-- Sidebar Menu -->
    <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">
                    <a href="<?= APP_URL ?>/modules/admin/dashboard.php" style="text-decoration: none; color: inherit;">
                        <i class="fas fa-home"></i>
                        <span>Dashboard</span>
                    </a>
                </li>
                <li class="menu-item">
                    <i class="fas fa-chart-line"></i>
                    <span>Estadísticas</span>
                </li>
            </ul>
        </div>

        <div class="menu-section">
            <h3 class="menu-title">Gestión Escolar</h3>
            <ul class="menu-items">
                <li class="menu-item">
                    <i class="fas fa-users"></i>
                    <span>Usuarios</span>
                </li>
                <li class="menu-item">
                    <a href="<?= APP_URL ?>/modules/admin/estudiantes.php" style="text-decoration: none; color: inherit;">
                        <i class="fas fa-user-graduate"></i>
                        <span>Estudiantes</span>
                    </a>
                </li>
                <li class="menu-item active">
                    <i class="fas fa-chalkboard-teacher"></i>
                    <span>Docentes</span>
                </li>
                <li class="menu-item">
                    <i class="fas fa-book"></i>
                    <span>Cursos</span>
                </li>
                <li class="menu-item">
                    <i class="fas fa-calendar-alt"></i>
                    <span>Horarios</span>
                </li>
            </ul>
        </div>

        <!-- Resto del menú igual que en dashboard.php -->
    </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 class="search-bar">
                    <i class="fas fa-search"></i>
                    <input type="text" placeholder="Buscar docentes...">
                </div>
            </div>

            <div class="header-right">
                <div class="notification">
                    <i class="fas fa-bell"></i>
                </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">
            <div class="table-container">
                <div class="table-header">
                    <h2 class="table-title"><i class="fas fa-chalkboard-teacher"></i> Listado de Docentes</h2>
                    <a href="<?= APP_URL ?>/modules/admin/docentes.php?action=new" class="btn-add">
                        <i class="fas fa-plus"></i> Nuevo Docente
                    </a>
                </div>
                
                <table id="docentesTable" class="table table-striped table-hover" style="width:100%">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>Nombre Completo</th>
                            <th>Especialidad</th>
                            <th>Estado</th>
                            <th>Asignaciones</th>
                            <th>Contacto</th>
                            <th>Acciones</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($docentes as $docente): ?>
                        <tr>
                            <td><?= $docente['id'] ?></td>
                            <td>
                                <?= 
                                strtoupper(htmlspecialchars($docente['apellido'] ?? '')).', '.
                                htmlspecialchars($docente['nombre'] ?? '') 
                                ?>
                            </td>
                            <td><?= htmlspecialchars($docente['especialidad'] ?? '') ?></td>
                            <td>
                                <?php 
                                $claseEstado = '';
                                if ($docente['estado'] === 'activo') {
                                    $claseEstado = 'estado-activo';
                                } elseif ($docente['estado'] === 'inactivo') {
                                    $claseEstado = 'estado-inactivo';
                                } elseif ($docente['estado'] === 'licencia') {
                                    $claseEstado = 'estado-licencia';
                                }
                                ?>
                                <span class="estado-badge <?= $claseEstado ?>">
                                    <?= ucfirst($docente['estado']) ?>
                                </span>
                            </td>
                            <td>
                                <span class="badge-asignaciones">
                                    <?= $docente['num_asignaciones'] ?> cursos
                                </span>
                            </td>
                            <td>
                                <div><?= htmlspecialchars($docente['email'] ?? '') ?></div>
                                <small><?= !empty($docente['telefono']) ? htmlspecialchars($docente['telefono']) : 'Sin teléfono' ?></small>
                            </td>
                            <td>
                                <div class="btn-group" role="group">
                                    <a href="<?= APP_URL ?>/modules/admin/docentes.php?action=edit&id=<?= $docente['id'] ?>" class="btn btn-sm btn-outline-primary">
                                        <i class="fas fa-edit"></i>
                                    </a>
                                    <a href="<?= APP_URL ?>/modules/admin/docentes.php?action=view&id=<?= $docente['id'] ?>" class="btn btn-sm btn-outline-info">
                                        <i class="fas fa-eye"></i>
                                    </a>
                                    <button class="btn btn-sm btn-outline-danger delete-docente" data-id="<?= $docente['id'] ?>">
                                        <i class="fas fa-trash"></i>
                                    </button>
                                </div>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
        
        <!-- Footer -->
        <footer class="footer">
            <p>Sistema Escolar Integrado v3.0 © <?= date('Y') ?> - Todos los derechos reservados</p>
        </footer>
    </div>

    <!-- Scripts -->
    <script src="<?= APP_URL ?>/assets/js/jquery-3.6.0.min.js"></script>
    <script src="<?= APP_URL ?>/assets/js/bootstrap.bundle.min.js"></script>
    <script src="<?= APP_URL ?>/assets/js/dataTables.bootstrap5.min.js"></script>
    <script src="<?= APP_URL ?>/assets/js/jquery.dataTables.min.js"></script>
    
    <script>
    $(document).ready(function() {
        $('#docentesTable').DataTable({
            language: {
                url: 'https://cdn.datatables.net/plug-ins/1.13.1/i18n/es-ES.json'
            },
            responsive: true,
            dom: '<"top"lf>rt<"bottom"ip><"clear">',
            initComplete: function() {
                // Eliminar el mensaje de error si persiste
                $('.dataTables_warning').remove();
            },
            columnDefs: [
                { responsivePriority: 1, targets: 1 }, // Nombre completo
                { responsivePriority: 2, targets: -1 }, // Acciones
                { orderable: false, targets: [4, 6] } // Asignaciones y Acciones
            ]
        });
    });
    </script>
</body>

</html>