<?php
session_start();

if (!isset($_SESSION['usuario'])) {
    header('Location: ../index.html');
    session_destroy();
    exit();
}

require_once __DIR__ . "/../controllers/ControlCandidatos.php";

$candidatoControl = new ControlCandidatos();
$resultado = $candidatoControl->obtenerCandidatosSinFechaSalida();

if(isset($resultado['estado'])){
    $hayCandidatos = false;
    if($resultado['estado'] == 'error'){
        // Enviar un console.error con $resultado['mensaje']
        echo "<script>console.error('Error: " . $resultado['mensaje'] . "');</script>";
    } else {
        echo "<script>console.log('Res: " . $resultado['mensaje'] . "');</script>";
    }
} else {
    $hayCandidatos = true;
}

?>

<!DOCTYPE html>
<html lang="es">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">


    <link rel="stylesheet" href="assets/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="../css/inicio.css">

    <link rel="stylesheet" href="assets/fontawesome/css/fontawesome.css">
    <link rel="stylesheet" href="assets/fontawesome/css/brands.css">
    <link rel="stylesheet" href="assets/fontawesome/css/solid.css">
	<title>Control de candidatos</title>
</head>
<body>
	
	<!-- SIDEBAR -->
	<section id="sidebar">
		<a href="inicio.html" class="brand"><i class='bx bx-code-alt icon' ></i> LANIA</a>

		<ul class="side-menu">
            <li><a href="inicio.html"><i class="fa-solid fa-square icon"></i> Dashboard</a></li>
            <li><a href="formulario-candidato.html" target="_blank"><i class="fa-solid fa-list icon"></i> Formulario de registro</a></li>
            <li><a href="control-candidatos.php" class="active"><i class="fa-solid fa-users-line icon"></i>Control candidatos</a></li>
            <li><a href="control-usuarios.php"><i class="fa-solid fa-users-gear icon"></i>Control usuario</a></li>
			<li><a href="consultar-api.html"><i class="fa-solid fa-globe icon"></i> API</a></li>
            <li><a href="../controllers/cerrarSesion.php"><i class="fa-solid fa-right-to-bracket icon"></i> Cerrar sesión</a></li>
		</ul>


	</section>
	<!-- .SIDEBAR -->


    <section id="content">
		<!-- ========== MAIN ========== -->
		<main>

			<h1 class="title" style="margin: 2% 1%">Control</h1>

            <!-- ------------------------------ div qué contiene la tabla ------------------------------------------ -->
			<div style="margin: 1%">
				<div class="table-responsive rounded-4" style="height: 100%;width: 100%;">
					<table class="table table-hover table-bordered border-dark-subtle shadow-sm">
						<thead>
						<tr class="table-dark">
							<th><i class="fa-solid fa-users"></i> Nombre</th>
							<th><i class="fa-solid fa-user-clock"></i> Entrada</th>
							<th><i class="fa-solid fa-user-gear"></i> Acciones</th>
						</tr>
						</thead>
						<tbody class="table-group-divider">
                            <?php if ($hayCandidatos): ?>
                                <?php foreach ($resultado as $candidato): ?>
                                <tr>
                                    <td><?php echo $candidato['nombre_completo'] ?></td>
                                    <td><?php echo $candidato['fecha_entrada'] ?></td>
                                    <td>
                                        <a class="btn border rounded-3 shadow" role="button" style="background-color: #35245b;color: white;" href="formulario-datos-candidato.php?id_candidato=<?php echo $candidato['id_candidato']?>" target="_blank"><i class="fa-solid fa-share-from-square"></i> Abrir formulario</a>
                                        <button class="boton-eliminar btn border rounded-3 shadow" role="button" style="background-color: #35245b;color: white;" data-id-candidato="<?php echo $candidato['id_candidato'] ?>"><i class="fa-solid fa-user-xmark"></i> Eliminar</button>
                                    </td>
                                <tr>
                                <?php endforeach; ?>
                            <?php endif; ?>
						</tr>
						</tbody>
					</table>
				</div>
			</div>
            <!-- ------------------------------ /div qué contiene la tabla ----------------------------------------- -->
		</main>
		<!-- .......... MAIN .......... -->
	</section>


	<script src="assets/bootstrap/js/bootstrap.min.js"></script>

	<script src="../js/sidebar-navbar.js"></script>
    <script src="../js/control-candidato.js"></script>
</body>
</html>