<?php include __DIR__ . '/../includes/header.php'; require_admin();
$id = (int)($_GET['id'] ?? 0);
$stmt = db()->prepare("SELECT * FROM users WHERE id=?");
$stmt->execute([$id]);
$item = $stmt->fetch();
if (!$item) { set_flash('danger','Usuario no encontrado.'); redirect('users/index.php'); }
if (is_post()) {
  if (!empty($_POST['password'])) {
    $stmt = db()->prepare("UPDATE users SET full_name=?, email=?, password=?, role=?, status=? WHERE id=?");
    $stmt->execute([trim($_POST['full_name']), trim($_POST['email']), password_hash($_POST['password'], PASSWORD_DEFAULT), $_POST['role'], $_POST['status'], $id]);
  } else {
    $stmt = db()->prepare("UPDATE users SET full_name=?, email=?, role=?, status=? WHERE id=?");
    $stmt->execute([trim($_POST['full_name']), trim($_POST['email']), $_POST['role'], $_POST['status'], $id]);
  }
  audit_log('update', 'users', $id, 'Edición usuario');
  set_flash('success','Usuario actualizado.');
  redirect('users/index.php');
}
?>
<div class="form-wrap"><h4>Editar usuario</h4><form method="post"><?php include __DIR__ . '/form.php'; ?><button class="btn btn-primary">Actualizar</button> <a class="btn btn-secondary" href="<?= e(base_url('users/index.php')) ?>">Volver</a></form></div>
<?php include __DIR__ . '/../includes/footer.php'; ?>
