<?php include __DIR__ . '/../includes/header.php'; ensure_can_manage_payroll();
if (is_post()) {
  $stmt = db()->prepare("INSERT INTO payroll_periods (year, month, start_date, end_date, status, created_at) VALUES (?, ?, ?, ?, ?, ?)");
  $stmt->execute([$_POST['year'], $_POST['month'], $_POST['start_date'], $_POST['end_date'], 'Borrador', now()]);
  audit_log('create', 'payroll_periods', db()->lastInsertId(), 'Alta período nómina');
  set_flash('success', 'Período creado.');
  redirect('payroll/index.php');
}
?>
<div class="form-wrap">
  <h4>Nuevo período de nómina</h4>
  <form method="post">
    <div class="row">
      <div class="col-md-3 mb-3"><label class="form-label">Año</label><input type="number" name="year" class="form-control" required value="<?= date('Y') ?>"></div>
      <div class="col-md-3 mb-3"><label class="form-label">Mes</label><input type="number" name="month" min="1" max="12" class="form-control" required value="<?= date('n') ?>"></div>
      <div class="col-md-3 mb-3"><label class="form-label">Desde</label><input type="date" name="start_date" class="form-control" required value="<?= date('Y-m-01') ?>"></div>
      <div class="col-md-3 mb-3"><label class="form-label">Hasta</label><input type="date" name="end_date" class="form-control" required value="<?= date('Y-m-t') ?>"></div>
    </div>
    <button class="btn btn-primary">Guardar</button>
    <a class="btn btn-secondary" href="<?= e(base_url('payroll/index.php')) ?>">Volver</a>
  </form>
</div>
<?php include __DIR__ . '/../includes/footer.php'; ?>
