<?php
include 'config/db.php';
session_start();
if (!isset($_SESSION['usuario'])) {
    header("Location: login.php");
    exit;
}

// Verificar si el usuario tiene privilegios de administrador
if ($_SESSION['rol'] !== 'admin') {
    echo "<!DOCTYPE html>
    <html lang='es'>
    <head>
        <meta charset='UTF-8'>
        <meta name='viewport' content='width=device-width, initial-scale=1.0'>
        <title>Acceso Denegado</title>
        <link rel='stylesheet' href='css/style.css'>
        <style>
            body {
                font-family: 'Poppins', Arial, sans-serif;
                margin: 0;
                padding: 0;
                display: flex;
                align-items: center;
                justify-content: center;
                height: 100vh;
                background-color: #f8f9fa;
            }
            .denied-container {
                text-align: center;
                background-color: #ffffff;
                padding: 40px;
                border-radius: 10px;
                box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
                max-width: 500px;
                width: 90%;
            }
            .denied-container h1 {
                font-size: 24px;
                color: #e74c3c;
                margin-bottom: 10px;
            }
            .denied-container p {
                font-size: 16px;
                color: #6c757d;
                margin-bottom: 20px;
            }
            .denied-container a {
                display: inline-block;
                padding: 10px 20px;
                font-size: 16px;
                color: #ffffff;
                background-color: #1abc9c;
                text-decoration: none;
                border-radius: 5px;
                transition: background-color 0.3s ease;
            }
            .denied-container a:hover {
                background-color: #16a085;
            }
        </style>
    </head>
    <body>
        <div class='denied-container'>
            <h1>Acceso Denegado</h1>
            <p>No tienes los permisos necesarios para realizar esta acción.<br>Si crees que esto es un error, contacta al administrador del sistema.</p>
            <a href='index.php'>Volver al inicio</a>
        </div>
    </body>
    </html>";
    exit;
}

$sql = "SELECT * FROM usuarios";
$result = $conexion->query($sql);
?>

<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Gestionar Usuarios</title>
    <link rel="stylesheet" href="css/style.css">
    <style>
        /* Estilos base */
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f7f7f7;
        }

        .sidebar {
            width: 250px;
            background-color: #2c3e50;
            height: 100vh;
            position: fixed;
            top: 0;
            left: 0;
            color: white;
            padding: 20px 10px;
            transition: all 0.3s ease;
        }

        .sidebar h2 {
            text-align: center;
            font-size: 24px;
            color: #1abc9c;
            margin-bottom: 30px;
        }

        .sidebar ul {
            list-style: none;
            padding: 0;
        }

        .sidebar ul li {
            margin: 20px 0;
        }

        .sidebar ul li a {
            text-decoration: none;
            color: white;
            padding: 12px;
            display: block;
            font-size: 18px;
            transition: background-color 0.3s ease;
        }

        .sidebar ul li a:hover,
        .sidebar ul li a.active {
            background-color: #1abc9c;
        }

        .main-content {
            margin-left: 270px;
            padding: 30px;
        }

        .main-content h1 {
            font-size: 28px;
            color: #0984e3;
            margin-bottom: 20px;
            text-align: center;
        }

        .btn {
            padding: 10px 20px;
            font-size: 16px;
            background-color: #1abc9c;
            color: white;
            text-decoration: none;
            border-radius: 5px;
            margin-bottom: 20px;
            display: inline-block;
        }

        .btn:hover {
            background-color: #16a085;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
            background-color: white;
            border: 1px solid #ddd;
        }

        table th, table td {
            text-align: center;
            padding: 10px;
            border: 1px solid #ddd;
            font-size: 14px;
        }

        table th {
            background-color: #2c3e50;
            color: white;
        }

        .btn-edit {
            padding: 5px 10px;
            background-color: #3498db;
            color: white;
            text-decoration: none;
            border-radius: 3px;
        }

        .btn-edit:hover {
            background-color: #2980b9;
        }

        .btn-delete {
            padding: 5px 10px;
            background-color: #e74c3c;
            color: white;
            text-decoration: none;
            border-radius: 3px;
        }

        .btn-delete:hover {
            background-color: #c0392b;
        }

        /* Estilo responsivo */
        @media (max-width: 768px) {
            .sidebar {
                position: static;
                width: 100%;
                height: auto;
                padding: 10px 0;
            }

            .main-content {
                margin-left: 0;
                padding: 15px;
            }

            table th,
            table td {
                font-size: 12px;
                padding: 8px;
            }
        }

        @media (max-width: 576px) {
            .main-content {
                padding: 10px;
            }

            .btn {
                font-size: 14px;
                padding: 8px 15px;
            }

            table th,
            table td {
                font-size: 10px;
                padding: 6px;
            }

            table {
                overflow-x: auto;
                display: block;
                white-space: nowrap;
            }
        }
    </style>
</head>
<body>
    <div class="sidebar">
        <h2>PYP Conservación Auditiva</h2>
        <ul>
            <li><a href="index.php">Inicio</a></li>
            <li><a href="listar_usuarios.php" class="active">Gestionar Usuarios</a></li>
        </ul>
    </div>

    <div class="main-content">
        <h1>Gestionar Usuarios</h1>
        <a href="agregar_usuario.php" class="btn">Agregar Usuario</a>
        <table>
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Nombre Usuario</th>
                    <th>Nombre Completo</th>
                    <th>Acciones</th>
                </tr>
            </thead>
            <tbody>
                <?php if ($result->num_rows > 0): ?>
                    <?php while ($row = $result->fetch_assoc()): ?>
                        <tr>
                            <td><?= $row['id'] ?></td>
                            <td><?= $row['nombre_usuario'] ?></td>
                            <td><?= $row['nombre_completo'] ?></td>
                            <td>
                                <a href="editar_usuario.php?id=<?= $row['id'] ?>" class="btn-edit">Editar</a>
                                <a href="eliminar_usuario.php?id=<?= $row['id'] ?>" class="btn-delete" onclick="return confirm('¿Estás seguro de eliminar este usuario?')">Eliminar</a>
                            </td>
                        </tr>
                    <?php endwhile; ?>
                <?php else: ?>
                    <tr>
                        <td colspan="4">No se encontraron usuarios.</td>
                    </tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</body>
</html>
