<?php /* payroll/advances.php */ ?>
<div class="d-flex justify-content-between align-items-center mb-3">
  <h5 class="fw-bold mb-0"><i class="bi bi-wallet2 me-2 text-warning"></i>Salary Advances</h5>
  <a href="<?=APP_URL?>/payroll" class="btn btn-sm btn-outline-secondary">← Payroll</a>
</div>
<?= flashMessages() ?>
<div class="row g-3">
  <div class="col-lg-5">
    <div class="card">
      <div class="card-header fw-semibold small bg-transparent"><i class="bi bi-plus-circle me-1 text-success"></i>Record Advance</div>
      <div class="card-body">
        <form method="POST" action="<?=APP_URL?>/payroll/advances">
          <?=Auth::csrfField()?>
          <div class="mb-3">
            <label class="form-label small fw-semibold">Employee</label>
            <select name="employee_id" class="form-select" required>
              <option value="">Select Employee…</option>
              <?php foreach($employees as $e):?><option value="<?=$e['id']?>"><?=e($e['full_name'])?> (<?=e($e['emp_code'])?>)</option><?php endforeach;?>
            </select>
          </div>
          <div class="mb-3">
            <label class="form-label small fw-semibold">Amount</label>
            <div class="input-group"><span class="input-group-text"><?=setting('currency_symbol','Rs.')?></span>
              <input type="number" name="amount" class="form-control fw-bold" step="0.01" min="1" required placeholder="0.00">
            </div>
          </div>
          <div class="row g-2 mb-3">
            <div class="col-6"><label class="form-label small fw-semibold">Advance Date</label>
              <input type="date" name="advance_date" class="form-control" value="<?=date('Y-m-d')?>">
            </div>
            <div class="col-3"><label class="form-label small fw-semibold">Deduct Month</label>
              <select name="deduct_month" class="form-select form-select-sm">
                <?php for($m=1;$m<=12;$m++):?><option value="<?=$m?>" <?=$m==(int)date('n')?'selected':''?>><?=date('M',mktime(0,0,0,$m,1))?></option><?php endfor;?>
              </select>
            </div>
            <div class="col-3"><label class="form-label small fw-semibold">Year</label>
              <input type="number" name="deduct_year" class="form-control form-control-sm" value="<?=date('Y')?>">
            </div>
          </div>
          <div class="mb-3">
            <label class="form-label small fw-semibold">Reason</label>
            <textarea name="reason" class="form-control" rows="2" placeholder="Reason for advance…"></textarea>
          </div>
          <button type="submit" class="btn btn-warning w-100 fw-semibold">
            <i class="bi bi-plus-circle me-1"></i>Record Advance
          </button>
        </form>
      </div>
    </div>
  </div>
  <div class="col-lg-7">
    <div class="card">
      <div class="card-header fw-semibold small bg-dark text-white">Recent Advances</div>
      <table class="table table-sm table-hover mb-0">
        <thead class="table-secondary"><tr><th>Employee</th><th>Amount</th><th>Date</th><th>Deduct</th><th>Status</th></tr></thead>
        <tbody>
          <?php foreach($advances as $a):
            $sc=['pending'=>'bg-warning text-dark','deducted'=>'bg-success','cancelled'=>'bg-secondary'][$a['status']]??'bg-secondary';
          ?>
          <tr>
            <td><div class="small fw-semibold"><?=e($a['full_name'])?></div><code style="font-size:.62rem"><?=e($a['emp_code'])?></code></td>
            <td class="fw-bold text-warning"><?=currency($a['amount'])?></td>
            <td class="small"><?=fDate($a['advance_date'])?></td>
            <td class="small text-muted"><?=date('M Y',mktime(0,0,0,$a['deduct_month'],1,$a['deduct_year']))?></td>
            <td><span class="badge <?=$sc?>" style="font-size:.6rem"><?=ucfirst($a['status'])?></span></td>
          </tr>
          <?php endforeach;?>
          <?php if(empty($advances)):?><tr><td colspan="5" class="text-center text-muted py-3">No advances recorded.</td></tr><?php endif;?>
        </tbody>
      </table>
    </div>
  </div>
</div>