<?php

require_once '../../core/config.php';
require_once '../../core/session.php';
require_once '../../core/database.php';
require_once '../../core/format.php';
require_once '../../core/permissions.php';

require_login();
require_permission('admin.users.edit');

$pdo = db();

$id = isset($_GET['id']) ? (int) $_GET['id'] : 0;

$stmt = $pdo->prepare("
    SELECT *
    FROM users
    WHERE id = ?
    LIMIT 1
");

$stmt->execute(array($id));
$editUser = $stmt->fetch();

if (!$editUser) {
    die('Usuario no encontrado.');
}

$page_title = 'Editar usuario';

require_once '../../layouts/header.php';
require_once '../../layouts/sidebar.php';

?>

<main class="app-main">
    <?php require_once '../../layouts/topbar.php'; ?>

    <section class="content">

        <div class="page-header page-header-compact">
            <div class="page-header-content">
                <span class="label">Administración</span>
                <h1>Editar usuario</h1>
                <p class="text-secondary"><?php echo safe_text($editUser['email']); ?></p>
            </div>
        </div>

        <form class="opportunity-form" action="/modules/admin/user_update.php" method="POST">

            <input type="hidden" name="id" value="<?php echo (int) $editUser['id']; ?>">

            <section class="card form-card">
                <div class="card-header">
                    <div>
                        <span class="label">Usuario</span>
                        <h3>Acceso y permisos</h3>
                    </div>
                </div>

                <div class="card-body">
                    <div class="form-grid form-grid-2">

                        <div class="form-field">
                            <label class="label">Nombre</label>
                            <input class="input" type="text" value="<?php echo safe_text($editUser['name']); ?>" disabled>
                        </div>

                        <div class="form-field">
                            <label class="label">Email</label>
                            <input class="input" type="email" value="<?php echo safe_text($editUser['email']); ?>" disabled>
                        </div>

                        <div class="form-field">
                            <label class="label">Rol</label>
                            <select class="select" name="role" required>
                                <option value="admin" <?php echo $editUser['role'] === 'admin' ? 'selected' : ''; ?>>Admin</option>
                                <option value="administracion" <?php echo $editUser['role'] === 'administracion' ? 'selected' : ''; ?>>Administración</option>
                                <option value="comercial" <?php echo $editUser['role'] === 'comercial' ? 'selected' : ''; ?>>Comercial</option>
                                <option value="operaciones" <?php echo $editUser['role'] === 'operaciones' ? 'selected' : ''; ?>>Operaciones</option>
                            </select>
                        </div>

                        <div class="form-field">
                            <label class="label">Estado</label>
                            <select class="select" name="status" required>
                                <option value="active" <?php echo $editUser['status'] === 'active' ? 'selected' : ''; ?>>Activo</option>
                                <option value="inactive" <?php echo $editUser['status'] === 'inactive' ? 'selected' : ''; ?>>Inactivo</option>
                            </select>
                        </div>

                    </div>

                    <div class="form-actions">
                        <button class="btn btn-primary" type="submit">
                            <i data-lucide="save"></i>
                            <span>Guardar cambios</span>
                        </button>

                        <a class="btn btn-outline" href="/modules/admin/users.php">
                            Cancelar
                        </a>
                    </div>
                </div>
            </section>

        </form>

    </section>
</main>

<?php require_once '../../layouts/footer.php'; ?>