<?php
include("conexion.php");

session_start();

if (!isset($_SESSION['usuario'])) {
    header('Location: login.php');
    exit;
}

function h($value) {
    return htmlspecialchars((string) $value, ENT_QUOTES, 'UTF-8');
}

$estatus_labels = array(
    'TODOS'      => 'Todos',
    'ACTIVO'     => 'Activos',
    'SUSPENDIDO' => 'Suspendidos',
    'EGRESADO'   => 'Egresados'
);

$estatus = strtoupper(trim($_GET['estatus'] ?? 'ACTIVO'));
if (!array_key_exists($estatus, $estatus_labels)) {
    $estatus = 'ACTIVO';
}

$buscar = trim($_GET['buscar'] ?? ($_POST['buscar'] ?? ''));

$where = array();
$params = array();
$types = '';

if ($estatus !== 'TODOS') {
    $where[] = 'estatus_laboral = ?';
    $params[] = $estatus;
    $types .= 's';
}

if ($buscar !== '') {
    $like = '%' . $buscar . '%';
    $where[] = '(nombre LIKE ? OR apellido LIKE ? OR cedula LIKE ? OR CAST(id_turno AS CHAR) LIKE ? OR departamento LIKE ? OR area LIKE ?)';
    for ($i = 0; $i < 6; $i++) {
        $params[] = $like;
        $types .= 's';
    }
}

$sql = "SELECT * FROM personas";
if (!empty($where)) {
    $sql .= " WHERE " . implode(' AND ', $where);
}
$sql .= " ORDER BY FIELD(estatus_laboral, 'ACTIVO', 'SUSPENDIDO', 'EGRESADO'), apellido ASC, nombre ASC";

$stmt = mysqli_prepare($conexion, $sql);
if (!$stmt) {
    die('Error preparando consulta principal: ' . mysqli_error($conexion));
}

if (!empty($params)) {
    mysqli_stmt_bind_param($stmt, $types, ...$params);
}

mysqli_stmt_execute($stmt);
$resultado = mysqli_stmt_get_result($stmt);
if (!$resultado) {
    die('Error leyendo consulta principal: ' . mysqli_error($conexion));
}

$total_personas = mysqli_num_rows($resultado);

$stats = array('total' => 0, 'activos' => 0, 'suspendidos' => 0, 'egresados' => 0);
$stats_sql = "
    SELECT
        COUNT(*) AS total,
        SUM(CASE WHEN estatus_laboral = 'ACTIVO' THEN 1 ELSE 0 END) AS activos,
        SUM(CASE WHEN estatus_laboral = 'SUSPENDIDO' THEN 1 ELSE 0 END) AS suspendidos,
        SUM(CASE WHEN estatus_laboral = 'EGRESADO' THEN 1 ELSE 0 END) AS egresados
    FROM personas
";
$stats_result = mysqli_query($conexion, $stats_sql);
if ($stats_result) {
    $row_stats = mysqli_fetch_assoc($stats_result);
    $stats['total'] = (int) ($row_stats['total'] ?? 0);
    $stats['activos'] = (int) ($row_stats['activos'] ?? 0);
    $stats['suspendidos'] = (int) ($row_stats['suspendidos'] ?? 0);
    $stats['egresados'] = (int) ($row_stats['egresados'] ?? 0);
}

function build_filter_url($estatus_filter, $buscar_actual = '') {
    $query = array('estatus' => $estatus_filter);
    if (trim($buscar_actual) !== '') {
        $query['buscar'] = trim($buscar_actual);
    }
    return 'index.php?' . http_build_query($query);
}
?>
<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Sistema de Gestión del Talento Humano | Distribuidora Riga0803 C.A.</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <style>
        :root {
            --primary-color: #1a3c8b;
            --secondary-color: #4CAF50;
            --accent-color: #FF9800;
            --light-color: #f8f9fa;
            --dark-color: #2c3e50;
            --gray-light: #f5f7fa;
            --border-color: #e1e5eb;
        }
        
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Inter', sans-serif;
            background-color: var(--gray-light);
            color: var(--dark-color);
            line-height: 1.6;
        }
        
        .app-container {
            display: flex;
            min-height: 100vh;
        }
        
        /* Sidebar */
        .sidebar {
            width: 260px;
            background: linear-gradient(180deg, var(--primary-color) 0%, #15326f 100%);
            color: white;
            padding: 0;
            box-shadow: 3px 0 15px rgba(0, 0, 0, 0.1);
            z-index: 100;
            position: fixed;
            height: 100vh;
            overflow-y: auto;
        }
        
        .sidebar-header {
            padding: 25px 20px;
            border-bottom: 1px solid rgba(255, 255, 255, 0.1);
            text-align: center;
        }
        
        .company-logo {
            width: 70px;
            height: 70px;
            background-color: white;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            margin: 0 auto 15px;
            box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
        }
        
        .company-logo img {
            max-width: 50px;
            max-height: 50px;
        }
        
        .company-name {
            font-size: 1.1rem;
            font-weight: 600;
            margin-bottom: 5px;
        }
        
        .system-name {
            font-size: 0.85rem;
            opacity: 0.9;
            font-weight: 400;
        }
        
        .user-info {
            padding: 20px;
            border-bottom: 1px solid rgba(255, 255, 255, 0.1);
            display: flex;
            align-items: center;
        }
        
        .user-avatar {
            width: 45px;
            height: 45px;
            background: rgba(255, 255, 255, 0.1);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            margin-right: 12px;
        }
        
        .user-avatar i {
            font-size: 1.2rem;
        }
        
        .user-details h4 {
            font-size: 1rem;
            font-weight: 600;
            margin-bottom: 2px;
        }
        
        .user-details p {
            font-size: 0.8rem;
            opacity: 0.8;
        }
        
        .sidebar-menu {
            padding: 20px 0;
        }
        
        .menu-section {
            margin-bottom: 25px;
        }
        
        .menu-title {
            font-size: 0.8rem;
            text-transform: uppercase;
            letter-spacing: 1px;
            opacity: 0.7;
            padding: 0 20px 10px;
            margin-bottom: 10px;
            border-bottom: 1px solid rgba(255, 255, 255, 0.05);
        }
        
        .menu-item {
            display: flex;
            align-items: center;
            padding: 12px 20px;
            color: rgba(255, 255, 255, 0.8);
            text-decoration: none;
            transition: all 0.3s;
            border-left: 3px solid transparent;
        }
        
        .menu-item:hover, .menu-item.active {
            background: rgba(255, 255, 255, 0.1);
            color: white;
            border-left-color: var(--accent-color);
        }
        
        .menu-item i {
            margin-right: 12px;
            width: 20px;
            text-align: center;
            font-size: 1.1rem;
        }
        
        .logout-item {
            margin-top: 20px;
            border-top: 1px solid rgba(255, 255, 255, 0.1);
            padding-top: 20px;
        }
        
        /* Main Content */
        .main-content {
            flex: 1;
            margin-left: 260px;
            padding: 0;
        }
        
        .top-navbar {
            background-color: white;
            padding: 15px 30px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
            display: flex;
            justify-content: space-between;
            align-items: center;
            position: sticky;
            top: 0;
            z-index: 99;
        }
        
        .page-title h1 {
            font-size: 1.5rem;
            font-weight: 600;
            color: var(--dark-color);
            margin: 0;
        }
        
        .page-title p {
            font-size: 0.9rem;
            color: #666;
            margin: 5px 0 0;
        }
        
        .search-box {
            width: 300px;
        }
        
        .search-box .input-group {
            border-radius: 8px;
            overflow: hidden;
            border: 1px solid var(--border-color);
        }
        
        .search-box .form-control {
            border: none;
            padding: 10px 15px;
        }
        
        .search-box .btn {
            background-color: var(--primary-color);
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 0 8px 8px 0;
        }
        
        .content-wrapper {
            padding: 30px;
        }
        
        /* Dashboard Cards */
        .dashboard-cards {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
            gap: 20px;
            margin-bottom: 30px;
        }
        
        .dashboard-card {
            background: white;
            border-radius: 12px;
            padding: 25px;
            box-shadow: 0 4px 15px rgba(0, 0, 0, 0.05);
            transition: transform 0.3s, box-shadow 0.3s;
            border: 1px solid var(--border-color);
        }
        
        .dashboard-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 8px 25px rgba(0, 0, 0, 0.1);
        }
        
        .card-icon {
            width: 60px;
            height: 60px;
            border-radius: 12px;
            display: flex;
            align-items: center;
            justify-content: center;
            margin-bottom: 15px;
            font-size: 1.5rem;
        }
        
        .icon-primary {
            background-color: rgba(26, 60, 139, 0.1);
            color: var(--primary-color);
        }
        
        .icon-success {
            background-color: rgba(76, 175, 80, 0.1);
            color: var(--secondary-color);
        }
        
        .icon-warning {
            background-color: rgba(255, 152, 0, 0.1);
            color: var(--accent-color);
        }
        
        .card-value {
            font-size: 2rem;
            font-weight: 700;
            margin: 5px 0;
        }
        
        .card-label {
            font-size: 0.9rem;
            color: #666;
        }
        
        /* Actions Bar */
        .actions-bar {
            background: white;
            border-radius: 12px;
            padding: 20px;
            margin-bottom: 25px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
            display: flex;
            justify-content: space-between;
            align-items: center;
            flex-wrap: wrap;
            gap: 15px;
        }
        
        .actions-left {
            display: flex;
            gap: 10px;
            flex-wrap: wrap;
        }
        
        .btn-action {
            background-color: var(--primary-color);
            color: white;
            border: none;
            padding: 10px 18px;
            border-radius: 8px;
            font-weight: 500;
            transition: all 0.3s;
            display: flex;
            align-items: center;
            gap: 8px;
        }
        
        .btn-action:hover {
            background-color: #15326f;
            color: white;
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(26, 60, 139, 0.2);
        }
        
        .btn-action i {
            font-size: 1rem;
        }
        
        .btn-success {
            background-color: var(--secondary-color);
        }
        
        .btn-success:hover {
            background-color: #3d8b40;
        }
        
        .btn-warning {
            background-color: var(--accent-color);
        }
        
        .btn-warning:hover {
            background-color: #e68900;
        }
        
        /* Data Table */
        .data-table-container {
            background: white;
            border-radius: 12px;
            overflow: hidden;
            box-shadow: 0 4px 15px rgba(0, 0, 0, 0.05);
            margin-top: 20px;
        }
        
        .table-header {
            padding: 20px 25px;
            border-bottom: 1px solid var(--border-color);
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        
        .table-header h3 {
            font-size: 1.2rem;
            font-weight: 600;
            margin: 0;
        }
        
        .table-actions {
            display: flex;
            gap: 10px;
        }
        
        .table-responsive {
            overflow-x: auto;
        }
        
        .table {
            margin: 0;
            border-collapse: separate;
            border-spacing: 0;
            width: 100%;
        }
        
        .table thead th {
            background-color: #f8fafc;
            border: none;
            padding: 15px 20px;
            font-weight: 600;
            color: var(--dark-color);
            border-bottom: 2px solid var(--border-color);
            white-space: nowrap;
        }
        
        .table tbody tr {
            transition: background-color 0.2s;
        }
        
        .table tbody tr:hover {
            background-color: #f8fafc;
        }
        
        .table tbody td {
            padding: 15px 20px;
            border-bottom: 1px solid var(--border-color);
            vertical-align: middle;
        }
        
        .action-buttons {
            display: flex;
            gap: 8px;
        }
        
        .btn-icon {
            width: 36px;
            height: 36px;
            border-radius: 8px;
            display: flex;
            align-items: center;
            justify-content: center;
            border: none;
            transition: all 0.2s;
        }
        
        .btn-edit {
            background-color: rgba(255, 193, 7, 0.1);
            color: #ffc107;
        }
        
        .btn-edit:hover {
            background-color: #ffc107;
            color: white;
        }
        
        .btn-delete {
            background-color: rgba(220, 53, 69, 0.1);
            color: #dc3545;
        }
        
        .btn-delete:hover {
            background-color: #dc3545;
            color: white;
        }
        
        .btn-info {
            background-color: rgba(23, 162, 184, 0.1);
            color: #17a2b8;
        }
        
        .btn-info:hover {
            background-color: #17a2b8;
            color: white;
        }
        
        .status-badge {
            padding: 5px 12px;
            border-radius: 20px;
            font-size: 0.8rem;
            font-weight: 500;
        }
        
        .badge-active {
            background-color: rgba(76, 175, 80, 0.1);
            color: var(--secondary-color);
        }
        
        .badge-inactive {
            background-color: rgba(108, 117, 125, 0.1);
            color: #6c757d;
        }
        
        /* Estilos para enlaces de nombre */
        .name-link {
            color: var(--primary-color);
            text-decoration: none;
            font-weight: 600;
            transition: all 0.2s;
            padding: 3px 5px;
            border-radius: 4px;
        }
        
        .name-link:hover {
            background-color: rgba(26, 60, 139, 0.1);
            text-decoration: underline;
        }
        

        .filter-panel {
            background: white;
            border-radius: 12px;
            padding: 18px 22px;
            margin-bottom: 25px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
            border: 1px solid var(--border-color);
            display: flex;
            align-items: center;
            justify-content: space-between;
            gap: 15px;
            flex-wrap: wrap;
        }

        .filter-title {
            font-weight: 600;
            color: var(--dark-color);
            display: flex;
            align-items: center;
            gap: 8px;
        }

        .filter-title small {
            display: block;
            color: #6b7280;
            font-weight: 400;
            margin-top: 3px;
        }

        .filter-chips {
            display: flex;
            gap: 10px;
            flex-wrap: wrap;
        }

        .filter-chip {
            display: inline-flex;
            align-items: center;
            gap: 8px;
            padding: 9px 14px;
            border-radius: 999px;
            border: 1px solid var(--border-color);
            background: #f8fafc;
            color: #334155;
            text-decoration: none;
            font-weight: 600;
            font-size: 0.9rem;
            transition: all 0.2s;
        }

        .filter-chip:hover {
            text-decoration: none;
            transform: translateY(-1px);
            box-shadow: 0 4px 12px rgba(15, 23, 42, 0.08);
        }

        .filter-chip.active {
            background: rgba(26, 60, 139, 0.10);
            color: var(--primary-color);
            border-color: rgba(26, 60, 139, 0.25);
        }

        .filter-chip .count {
            background: white;
            border-radius: 999px;
            padding: 2px 8px;
            color: #475569;
            border: 1px solid rgba(148, 163, 184, 0.35);
        }

        .status-badge.badge-suspended {
            background-color: rgba(255, 152, 0, 0.12);
            color: #b45309;
        }

        .status-badge.badge-egresado {
            background-color: rgba(100, 116, 139, 0.12);
            color: #475569;
        }

        .table tbody tr.row-suspended {
            background: rgba(255, 251, 235, 0.55);
        }

        .table tbody tr.row-egresado {
            background: rgba(248, 250, 252, 0.85);
            color: #64748b;
        }

        .table tbody tr.row-egresado .name-link {
            color: #475569;
        }

        /* Footer */
        .footer {
            padding: 20px 30px;
            text-align: center;
            color: #666;
            font-size: 0.85rem;
            border-top: 1px solid var(--border-color);
            margin-top: 30px;
        }
        
        /* Responsive */
        @media (max-width: 992px) {
            .sidebar {
                width: 70px;
                overflow: visible;
            }
            
            .sidebar-header, .company-name, .system-name, .user-details, .menu-title, .menu-item span {
                display: none;
            }
            
            .company-logo {
                width: 40px;
                height: 40px;
                margin-bottom: 0;
            }
            
            .company-logo img {
                max-width: 25px;
                max-height: 25px;
            }
            
            .user-info {
                justify-content: center;
                padding: 15px 10px;
            }
            
            .user-avatar {
                margin-right: 0;
            }
            
            .menu-item {
                justify-content: center;
                padding: 15px 10px;
            }
            
            .menu-item i {
                margin-right: 0;
                font-size: 1.3rem;
            }
            
            .main-content {
                margin-left: 70px;
            }
        }
        
        @media (max-width: 768px) {
            .top-navbar {
                flex-direction: column;
                align-items: flex-start;
                gap: 15px;
                padding: 15px 20px;
            }
            
            .search-box {
                width: 100%;
            }
            
            .content-wrapper {
                padding: 20px 15px;
            }
            
            .dashboard-cards {
                grid-template-columns: 1fr;
            }
            
            .actions-bar, .filter-panel {
                flex-direction: column;
                align-items: flex-start;
            }
            
            .actions-left {
                width: 100%;
                justify-content: center;
            }
            
            .action-buttons {
                flex-wrap: wrap;
                justify-content: center;
            }
        }
        
        /* Custom scrollbar */
        ::-webkit-scrollbar {
            width: 6px;
        }
        
        ::-webkit-scrollbar-track {
            background: #f1f1f1;
        }
        
        ::-webkit-scrollbar-thumb {
            background: #c1c1c1;
            border-radius: 3px;
        }
        
        ::-webkit-scrollbar-thumb:hover {
            background: #a8a8a8;
        }
    </style>
</head>
<body>
    <div class="app-container">
        <!-- Sidebar -->
        <aside class="sidebar">
            <div class="sidebar-header">
                <div class="company-logo">
                    <!-- Reemplaza con tu logo -->
                    <i class="fas fa-building" style="color: var(--primary-color); font-size: 1.8rem;"></i>
                    <!-- <img src="image/logo_distribuidora.png" alt="Logo"> -->
                </div>
                <div class="company-name">Distribuidora Riga0803 C.A.</div>
                <div class="system-name">Sistema de Talento Humano</div>
            </div>
            
            <div class="user-info">
                <div class="user-avatar">
                    <i class="fas fa-user-circle"></i>
                </div>
                <div class="user-details">
                    <h4><?php echo $_SESSION['usuario']; ?></h4>
                    <p><?php echo ucfirst($_SESSION['rol']); ?></p>
                </div>
            </div>
            
            <nav class="sidebar-menu">
                <div class="menu-section">
                    <div class="menu-title">Principal</div>
                    <a href="index.php" class="menu-item active">
                        <i class="fas fa-users"></i>
                        <span>Personal</span>
                    </a>
                    <a href="asistencia.php" class="menu-item">
                        <i class="fas fa-clipboard-check"></i>
                        <span>Asistencias</span>
                    </a>
                </div>
                
                <div class="menu-section">
                    <div class="menu-title">Módulos</div>
                    <a href="ficha.php" class="menu-item">
                        <i class="fas fa-id-card"></i>
                        <span>Fichas</span>
                    </a>
                    <a href="turnos.php" class="menu-item">
                        <i class="fas fa-clock"></i>
                        <span>Turnos</span>
                    </a>
                    <a href="cargos.php" class="menu-item">
                        <i class="fas fa-briefcase"></i>
                        <span>Cargos</span>
                    </a>
                    <a href="carga_familiar.php" class="menu-item">
                        <i class="fas fa-family"></i>
                        <span>Carga Familiar</span>
                    </a>
                </div>
                

                <div class="menu-section">
                    <div class="menu-title">Nómina</div>
                    <a href="periodos_nomina.php" class="menu-item">
                        <i class="fas fa-calendar-alt"></i>
                        <span>1. Períodos</span>
                    </a>
                    <a href="novedades_periodo.php" class="menu-item">
                        <i class="fas fa-edit"></i>
                        <span>2. Novedades</span>
                    </a>
                    <a href="nomina_calcular_actualizado.php" class="menu-item">
                        <i class="fas fa-calculator"></i>
                        <span>3. Calcular Nómina</span>
                    </a>
                    <a href="nomina_recibo.php" class="menu-item">
                        <i class="fas fa-receipt"></i>
                        <span>4. Recibos</span>
                    </a>
                    <a href="nomina_vacaciones.php" class="menu-item">
                        <i class="fas fa-umbrella-beach"></i>
                        <span>5. Vacaciones legales</span>
                    </a>
                    <a href="nomina_prestaciones.php" class="menu-item">
                        <i class="fas fa-piggy-bank"></i>
                        <span>6. Prestaciones sociales</span>
                    </a>
                    <a href="nomina_parametros_legales.php" class="menu-item">
                        <i class="fas fa-scale-balanced"></i>
                        <span>7. Parámetros legales</span>
                    </a>
                    <a href="nomina_aportes_patronales.php" class="menu-item">
                        <i class="fas fa-file-invoice-dollar"></i>
                        <span>8. Aportes patronales</span>
                    </a>
                    <a href="conceptos_nomina.php" class="menu-item">
                        <i class="fas fa-list-check"></i>
                        <span>Conceptos</span>
                    </a>
                </div>

                <div class="menu-section">
                    <div class="menu-title">Reportes</div>
                    <a href="include/reportico/index.php" class="menu-item">
                        <i class="fas fa-chart-bar"></i>
                        <span>Reportes</span>
                    </a>
                    <a href="codigo_barras/generar_codigo.php" class="menu-item">
                        <i class="fas fa-barcode"></i>
                        <span>Códigos de Barras</span>
                    </a>
                </div>
                
                <div class="menu-section logout-item">
                    <a href="logout.php" class="menu-item">
                        <i class="fas fa-sign-out-alt"></i>
                        <span>Cerrar Sesión</span>
                    </a>
                </div>
            </nav>
        </aside>
        
        <!-- Main Content -->
        <main class="main-content">
            <!-- Top Navbar -->
            <header class="top-navbar">
                <div class="page-title">
                    <h1>Gestión del Talento Humano</h1>
                    <p>Administración del personal de Distribuidora Riga0803 C.A.</p>
                </div>
                
                <form action="index.php" method="get" class="search-box">
                    <input type="hidden" name="estatus" value="<?php echo h($estatus); ?>">
                    <div class="input-group">
                        <input type="text" name="buscar" class="form-control" placeholder="Buscar personal..." value="<?php echo h($buscar); ?>">
                        <div class="input-group-append">
                            <button type="submit" class="btn">
                                <i class="fas fa-search"></i>
                            </button>
                            <?php if ($buscar !== ''): ?>
                                <a href="<?php echo h(build_filter_url($estatus)); ?>" class="btn btn-secondary" title="Limpiar búsqueda">
                                    <i class="fas fa-times"></i>
                                </a>
                            <?php endif; ?>
                        </div>
                    </div>
                </form>
            </header>
            
            <!-- Content Area -->
            <div class="content-wrapper">
                <!-- Dashboard Cards -->
                <div class="dashboard-cards">
                    <div class="dashboard-card">
                        <div class="card-icon icon-primary">
                            <i class="fas fa-users"></i>
                        </div>
                        <div class="card-value"><?php echo $stats['total']; ?></div>
                        <div class="card-label">Total de Empleados</div>
                    </div>
                    
                    <div class="dashboard-card">
                        <div class="card-icon icon-success">
                            <i class="fas fa-user-check"></i>
                        </div>
                        <div class="card-value"><?php echo $stats['activos']; ?></div>
                        <div class="card-label">Empleados Activos</div>
                    </div>
                    
                    <div class="dashboard-card">
                        <div class="card-icon icon-warning">
                            <i class="fas fa-user-clock"></i>
                        </div>
                        <div class="card-value"><?php echo $stats['suspendidos']; ?></div>
                        <div class="card-label">Suspendidos</div>
                    </div>
                    
                    <div class="dashboard-card">
                        <div class="card-icon icon-primary">
                            <i class="fas fa-user-slash"></i>
                        </div>
                        <div class="card-value"><?php echo $stats['egresados']; ?></div>
                        <div class="card-label">Egresados</div>
                    </div>
                </div>

                <!-- Filtros de estatus laboral -->
                <div class="filter-panel">
                    <div class="filter-title">
                        <i class="fas fa-filter"></i>
                        <div>
                            Filtrar personal por estatus laboral
                            <small>Vista actual: <?php echo h($estatus_labels[$estatus]); ?><?php echo $buscar !== '' ? ' · Búsqueda: ' . h($buscar) : ''; ?></small>
                        </div>
                    </div>
                    <div class="filter-chips">
                        <a class="filter-chip <?php echo $estatus === 'TODOS' ? 'active' : ''; ?>" href="<?php echo h(build_filter_url('TODOS', $buscar)); ?>">
                            <i class="fas fa-layer-group"></i> Todos <span class="count"><?php echo $stats['total']; ?></span>
                        </a>
                        <a class="filter-chip <?php echo $estatus === 'ACTIVO' ? 'active' : ''; ?>" href="<?php echo h(build_filter_url('ACTIVO', $buscar)); ?>">
                            <i class="fas fa-user-check"></i> Activos <span class="count"><?php echo $stats['activos']; ?></span>
                        </a>
                        <a class="filter-chip <?php echo $estatus === 'SUSPENDIDO' ? 'active' : ''; ?>" href="<?php echo h(build_filter_url('SUSPENDIDO', $buscar)); ?>">
                            <i class="fas fa-user-clock"></i> Suspendidos <span class="count"><?php echo $stats['suspendidos']; ?></span>
                        </a>
                        <a class="filter-chip <?php echo $estatus === 'EGRESADO' ? 'active' : ''; ?>" href="<?php echo h(build_filter_url('EGRESADO', $buscar)); ?>">
                            <i class="fas fa-user-slash"></i> Egresados <span class="count"><?php echo $stats['egresados']; ?></span>
                        </a>
                    </div>
                </div>
                
                <!-- Actions Bar -->
                <div class="actions-bar">
                    <div class="actions-left">
                        <a href="crear.php" class="btn-action">
                            <i class="fas fa-user-plus"></i>
                            <span>Registrar Persona</span>
                        </a>
                        
                        <a href="include/reportico/index.php" class="btn-action btn-success">
                            <i class="fas fa-chart-bar"></i>
                            <span>Reportes</span>
                        </a>
                        
                        <?php if ($_SESSION['rol'] === 'administrador'): ?>
                        <a href="asistencia.php" class="btn-action btn-warning">
                            <i class="fas fa-clipboard-check"></i>
                            <span>Asistencias</span>
                        </a>
                        <?php endif; ?>
                    </div>
                    
                    <div class="actions-right">
                        <a href="consultar_carga_familiar.php" class="btn-action">
                            <i class="fas fa-family"></i>
                            <span>Consulta Carga Familiar</span>
                        </a>
                    </div>
                </div>
                
                <!-- Data Table -->
                <div class="data-table-container">
                    <div class="table-header">
                        <h3>Registro de Personal</h3>
                        <div class="table-actions">
                            <span class="text-muted">Mostrando <?php echo $total_personas; ?> registros · <?php echo h($estatus_labels[$estatus]); ?></span>
                        </div>
                    </div>
                    
                    <div class="table-responsive">
                        <table class="table">
                            <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>Nombre</th>
                                    <th>Apellido</th>
                                    <th>Cédula</th>
                                    <th>Estado</th>
                                    <th>Teléfono</th>
                                    <th>Fecha Ingreso</th>
                                    <th>Turno</th>
                                    <th>Acciones</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php while ($fila = mysqli_fetch_assoc($resultado)) {
                                    $estatus_laboral = strtoupper($fila['estatus_laboral'] ?? 'ACTIVO');
                                    $row_class = '';
                                    $badge_class = 'badge-active';
                                    if ($estatus_laboral === 'SUSPENDIDO') {
                                        $row_class = 'row-suspended';
                                        $badge_class = 'badge-suspended';
                                    } elseif ($estatus_laboral === 'EGRESADO') {
                                        $row_class = 'row-egresado';
                                        $badge_class = 'badge-egresado';
                                    }
                                ?>
                                    <tr class="<?php echo $row_class; ?>">
                                        <td><?php echo $fila["id"]; ?></td>
                                        <td>
                                            <a href="ficha.php?cedula=<?php echo urlencode($fila["cedula"]); ?>" 
                                               class="name-link"
                                               title="Ver ficha completa">
                                                <?php echo htmlspecialchars($fila["nombre"]); ?>
                                            </a>
                                        </td>
                                        <td><?php echo htmlspecialchars($fila["apellido"]); ?></td>
                                        <td>
                                            <span class="status-badge badge-active"><?php echo h($fila["cedula"]); ?></span>
                                        </td>
                                        <td>
                                            <span class="status-badge <?php echo $badge_class; ?>"><?php echo h($estatus_laboral); ?></span>
                                            <?php if ($estatus_laboral === 'EGRESADO' && !empty($fila["fecha_egreso"])): ?>
                                                <small class="d-block text-muted mt-1">Egreso: <?php echo date('d/m/Y', strtotime($fila["fecha_egreso"])); ?></small>
                                            <?php endif; ?>
                                        </td>
                                        <td><?php echo h($fila["telefono"] ?? ''); ?></td>
                                        <td><?php echo !empty($fila["fecha_ingreso"]) ? date('d/m/Y', strtotime($fila["fecha_ingreso"])) : '-'; ?></td>
                                        <td>
                                            <span class="status-badge badge-inactive">Turno <?php echo h($fila["id_turno"]); ?></span>
                                        </td>
                                        <td>
                                            <div class="action-buttons">
                                                <a href="ficha.php?cedula=<?php echo urlencode($fila["cedula"]); ?>" 
                                                   class="btn-icon btn-info" 
                                                   title="Ver Ficha">
                                                    <i class="fas fa-id-card"></i>
                                                </a>
                                                <a href="editar.php?id=<?php echo $fila["id"]; ?>" class="btn-icon btn-edit" title="Editar">
                                                    <i class="fas fa-pencil-alt"></i>
                                                </a>
                                                <a href="eliminar.php?id=<?php echo $fila["id"]; ?>" class="btn-icon btn-delete" title="Eliminar" onclick="return confirm('¿Está seguro de eliminar este registro?');">
                                                    <i class="fas fa-trash-alt"></i>
                                                </a>
                                            </div>
                                        </td>
                                    </tr>
                                <?php } ?>
                                <?php if ($total_personas == 0): ?>
                                    <tr>
                                        <td colspan="9" class="text-center py-4">
                                            <i class="fas fa-users fa-2x text-muted mb-3"></i>
                                            <p class="text-muted">No se encontraron registros de personal</p>
                                            <a href="crear.php" class="btn btn-primary mt-2">Registrar primer empleado</a>
                                        </td>
                                    </tr>
                                <?php endif; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
                
                <!-- Footer -->
                <div class="footer">
                    <p>Sistema de Gestión del Talento Humano V2.0 &copy; <?php echo date('Y'); ?> Distribuidora Riga0803 C.A. | Todos los derechos reservados</p>
                </div>
            </div>
        </main>
    </div>
    
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.12.9/dist/umd/popper.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/js/bootstrap.min.js"></script>
    
    <script>
        // Activar menú actual
        document.addEventListener('DOMContentLoaded', function() {
            const currentPage = window.location.pathname.split('/').pop();
            const menuItems = document.querySelectorAll('.menu-item');
            
            menuItems.forEach(item => {
                const href = item.getAttribute('href');
                if (href === currentPage || (currentPage === '' && href === 'index.php')) {
                    item.classList.add('active');
                }
            });
            
            // Añadir efecto de carga suave
            document.body.style.opacity = '0';
            setTimeout(() => {
                document.body.style.transition = 'opacity 0.3s';
                document.body.style.opacity = '1';
            }, 50);
        });
    </script>
</body>
</html>