<?php include __DIR__ . '/../includes/header.php'; ensure_can_manage_payroll();
$id = (int)($_GET['id'] ?? 0);
$stmt = db()->prepare("SELECT * FROM salary_advances WHERE id = ?");
$stmt->execute([$id]);
$row = $stmt->fetch();
if (!$row) { set_flash('danger', 'Adelanto no encontrado.'); redirect('advances/index.php'); }
if (is_post()) {
  $stmt = db()->prepare("UPDATE salary_advances SET employee_id=?, advance_date=?, amount=?, notes=?, status=? WHERE id=?");
  $stmt->execute([(int)$_POST['employee_id'], $_POST['advance_date'], (float)$_POST['amount'], trim($_POST['notes'] ?? ''), trim($_POST['status'] ?? 'Activo'), $id]);
  audit_log('update', 'salary_advances', $id, 'Edición adelanto salarial');
  set_flash('success', 'Adelanto actualizado.');
  redirect('advances/index.php');
}
$title = 'Editar adelanto';
include __DIR__ . '/form.php';
include __DIR__ . '/../includes/footer.php';
