<?php
$employees = db()->query("SELECT id, full_name, document_number, salary FROM employees WHERE status='active' ORDER BY full_name")->fetchAll();
?>
<div class="form-wrap">
  <h4><?= e($title ?? 'Adelanto') ?></h4>
  <form method="post">
    <div class="row">
      <div class="col-md-6 mb-3">
        <label class="form-label">Empleado</label>
        <select name="employee_id" class="form-select" required>
          <option value="">Seleccionar...</option>
          <?php foreach ($employees as $emp): ?>
            <option value="<?= e($emp['id']) ?>" <?= (string)($row['employee_id'] ?? '') === (string)$emp['id'] ? 'selected' : '' ?>><?= e($emp['full_name']) ?> · CI <?= e($emp['document_number']) ?> · Salario <?= money_gs($emp['salary']) ?></option>
          <?php endforeach; ?>
        </select>
      </div>
      <div class="col-md-3 mb-3"><label class="form-label">Fecha del adelanto</label><input type="date" name="advance_date" class="form-control" required value="<?= e($row['advance_date'] ?? date('Y-m-d')) ?>"></div>
      <div class="col-md-3 mb-3"><label class="form-label">Monto</label><input type="number" step="0.01" min="0" name="amount" class="form-control" required value="<?= e($row['amount'] ?? '') ?>"></div>
    </div>
    <div class="row">
      <div class="col-md-3 mb-3"><label class="form-label">Estado</label><select name="status" class="form-select"><option value="Activo" <?= ($row['status'] ?? 'Activo')==='Activo' ? 'selected' : '' ?>>Activo</option><option value="Anulado" <?= ($row['status'] ?? '')==='Anulado' ? 'selected' : '' ?>>Anulado</option></select></div>
      <div class="col-md-9 mb-3"><label class="form-label">Observación</label><input name="notes" class="form-control" value="<?= e($row['notes'] ?? '') ?>" placeholder="Ej.: adelanto quincena abril"></div>
    </div>
    <button class="btn btn-primary">Guardar</button>
    <a class="btn btn-secondary" href="<?= e(base_url('advances/index.php')) ?>">Volver</a>
  </form>
</div>
