<?php
session_start();
$nombre= $_SESSION['nombre'];

$servername = "localhost";
$username = "itladevprubycom_enma";
$password = "53gNpj4t.xDc";
$database = "itladevprubycom_enma1";

$conexion = new mysqli($servername, $username, $password, $database);

if ($conexion->connect_error) {
    die("Error de conexión: " . $conexion->connect_error);
}

$query = "SELECT * FROM Usuarios";
$todo = $conexion->query($query);

if ($todo->num_rows == 0) {
    echo "No hay datos en la base de datos.";
} else {
    $datos = [];
    while ($total = $todo->fetch_assoc()) {
        $datos[] = $total;
    }
}

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- Incluir estilos y scripts de UIkit -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/uikit@3.18.1/dist/css/uikit.min.css" />
    <script src="https://cdn.jsdelivr.net/npm/uikit@3.18.1/dist/js/uikit.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/uikit@3.18.1/dist/js/uikit-icons.min.js"></script>
</head>
<body>
    <?php include "navbar.php"?>
<div class="uk-container">
    <div class="uk-margin-medium-top">
        <div uk-filter="target: .js-filter">
            <div class="uk-flex uk-flex-center">
                <ul class="uk-subnav uk-subnav-pill">
                    <li uk-filter-control=""><a href="#">Todos</a></li>
                    <li uk-filter-control=".usuario"><a href="#">Usuarios</a></li>
                    <li uk-filter-control=".admin"><a href="#">Administradores</a></li>
                </ul>
            </div>
            <ul class="js-filter uk-child-width-1-2 uk-child-width-1-3@m uk-text-center" uk-grid>
                <?php foreach($datos as $value) {
                    $clase = $value["tipo"] === "admin" ? "admin" : "usuario"; ?>
                    <li class="<?php echo $clase; ?>">
                        <div class="uk-card uk-card-body"><?php echo $value["nombre"]; ?></div>
                        <a class="uk-button uk-button-danger" onclick="confirmarEliminacion(<?php echo $value["id"]; ?>)">Eliminar usuario</a>
                    </li>
                <?php }?>
            </ul>
        </div>
        </div>
    </div>
<script>
    function confirmarEliminacion(id) {
        if (confirm("¿Estás seguro de que quieres eliminar este usuario?")) {
        window.location.href = "eliminar.php?id=" + id;
    }
}
</script>
</body>
</html>
