<?php
session_start();
$rolesPermitidos = ['capturista', 'capturistaRH', 'capturista_rrhh', 'adminTi'];
if (!isset($_SESSION['rol']) || !in_array($_SESSION['rol'], $rolesPermitidos)) {
    header('Location: ../../index.php');
    exit();
}

require_once('../../backend/db/conexion.php');
require_once __DIR__ . '/../includes/valida_posiciones.php';

$attuid = $_SESSION['attuid'] ?? '';

// Consulta para obtener aspirantes registrados por el capturista
$sql = "
SELECT a.folio, 
       CONCAT(a.nombre, ' ', a.apellido_paterno, ' ', a.apellido_materno) AS nombre_completo,
       a.estatus_actual
FROM aspirantes a
WHERE a.registrado_por = ?
ORDER BY a.id DESC";

$stmt = $conn->prepare($sql);
$stmt->bind_param("s", $attuid);
$stmt->execute();
$resultado = $stmt->get_result();
$aspirantes = $resultado->fetch_all(MYSQLI_ASSOC);
?>

<!DOCTYPE html>
<html lang="es">
<head>
  <meta charset="UTF-8">
  <title>Mis Aspirantes</title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
  <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
  <link href="../assets/css/styles.css" rel="stylesheet">
</head>
<body>
  <div class="d-flex min-vh-100" id="wrapper">
    <?php include('../includes/sidebar.php'); ?>
    <div id="page-content-wrapper" class="w-100">
      <?php include('../includes/topbar.php'); ?>

      <main class="p-4">
        <div class="container-fluid">
          <div class="d-flex justify-content-between align-items-center mb-4">
            <h2 class="mb-0">Mis Aspirantes</h2>
          </div>

          <div class="card shadow-sm">
            <div class="card-body p-4">
              <div class="table-responsive">
                <table class="table table-bordered table-hover">
                  <thead class="table-light">
                    <tr>
                      <th>Folio</th>
                      <th>Nombre Completo</th>
                      <th>Estatus Actual</th>
                    </tr>
                  </thead>
                  <tbody>
                    <?php if (count($aspirantes) > 0): ?>
                      <?php foreach ($aspirantes as $asp): ?>
                        <tr>
                          <td><?= htmlspecialchars($asp['folio']) ?></td>
                          <td><?= htmlspecialchars($asp['nombre_completo']) ?></td>
                          <td>
                            <?php
                              $estatus = strtolower($asp['estatus_actual']);
                              $badgeClass = match ($estatus) {
                                'asignado' => 'bg-success text-white',
                                'revision' => 'bg-warning text-dark',
                                'rechazado' => 'bg-danger text-white',
                                default => 'bg-secondary text-white'
                              };
                            ?>
                            <span class="badge <?= $badgeClass ?>">
                              <?= ucfirst(str_replace('_', ' ', $asp['estatus_actual'])) ?>
                            </span>
                          </td>
                        </tr>
                      <?php endforeach; ?>
                    <?php else: ?>
                      <tr>
                        <td colspan="3" class="text-center">No se han registrado aspirantes.</td>
                      </tr>
                    <?php endif; ?>
                  </tbody>
                </table>
              </div>
            </div>
          </div>

        </div>
      </main>
    </div>
  </div>
</body>
</html>
