<?php include __DIR__ . '/../includes/header.php'; ensure_can_manage_master_data(); require_admin();
$id = (int)($_GET['id'] ?? 0);
$stmt = db()->prepare("SELECT * FROM positions WHERE id = ?");
$stmt->execute([$id]);
$item = $stmt->fetch();
if (!$item) { set_flash('danger','Registro no encontrado.'); redirect('positions/index.php'); }
if (is_post()) {
  $stmt = db()->prepare("UPDATE positions SET name=?, description=? WHERE id=?");
  $stmt->execute([trim($_POST['name']), trim($_POST['description']), $id]);
  audit_log('update', 'positions', $id, 'Edición cargo');
  set_flash('success', 'Cargo actualizado.');
  redirect('positions/index.php');
}
?>
<div class="form-wrap">
  <h4>Editar cargo</h4>
  <form method="post">
    <div class="mb-3"><label class="form-label">Nombre</label><input name="name" class="form-control" required value="<?= e($item['name']) ?>"></div>
    <div class="mb-3"><label class="form-label">Descripción</label><textarea name="description" class="form-control"><?= e($item['description']) ?></textarea></div>
    <button class="btn btn-primary">Actualizar</button>
    <a class="btn btn-secondary" href="<?= e(base_url('positions/index.php')) ?>">Volver</a>
  </form>
</div>
<?php include __DIR__ . '/../includes/footer.php'; ?>
