<?php include __DIR__ . '/../includes/header.php'; ensure_can_manage_master_data();
if (is_post()) {
  $stmt = db()->prepare("INSERT INTO employees (full_name, document_number, department_id, position_id, hire_date, salary, email, phone, address, status, created_at)
  VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
  $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'],
    now()
  ]);
  audit_log('create', 'employees', db()->lastInsertId(), 'Alta empleado');
  set_flash('success', 'Empleado creado.');
  redirect('employees/index.php');
}
$item = [];
?>
<div class="form-wrap">
  <h4>Nuevo empleado</h4>
  <form method="post">
    <?php include __DIR__ . '/form.php'; ?>
    <button class="btn btn-primary">Guardar</button>
    <a class="btn btn-secondary" href="<?= e(base_url('employees/index.php')) ?>">Volver</a>
  </form>
</div>
<?php include __DIR__ . '/../includes/footer.php'; ?>
