<?php include __DIR__ . '/../includes/header.php'; ensure_can_manage_master_data();
$id = (int)($_GET['id'] ?? 0);
$stmt = db()->prepare("SELECT * FROM employees WHERE id = ?");
$stmt->execute([$id]);
$item = $stmt->fetch();
if (!$item) { set_flash('danger', 'Empleado no encontrado.'); redirect('employees/index.php'); }
if (is_post()) {
  $stmt = db()->prepare("UPDATE employees SET full_name=?, document_number=?, department_id=?, position_id=?, hire_date=?, salary=?, email=?, phone=?, address=?, status=? WHERE id=?");
  $stmt->execute([
    trim($_POST['full_name']),
    trim($_POST['document_number']),
    $_POST['department_id'] ?: null,
    $_POST['position_id'] ?: null,
    $_POST['hire_date'],
    $_POST['salary'],
    trim($_POST['email']),
    trim($_POST['phone']),
    trim($_POST['address']),
    $_POST['status'],
    $id
  ]);
  audit_log('update', 'employees', $id, 'Edición empleado');
  set_flash('success', 'Empleado actualizado.');
  redirect('employees/index.php');
}
?>
<div class="form-wrap">
  <h4>Editar empleado</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('employees/index.php')) ?>">Volver</a>
  </form>
</div>
<?php include __DIR__ . '/../includes/footer.php'; ?>
