<?php $this->start('body'); ?>
<?php $f = $this->data['filters']; ?>

<div class="d-flex align-items-center justify-content-between mb-3 flex-wrap gap-2">
  <h5 class="mb-0 fw-semibold"><i class="fas fa-money-check-alt me-2 text-muted"></i>Payroll
    <span class="text-muted fw-normal fs-6">· <?= (int)$this->data['total']; ?> periods</span>
  </h5>
  <a href="<?= PROOT; ?>Payroll/add" class="btn btn-sm btn-primary"><i class="fas fa-plus me-1"></i> New period</a>
</div>

<?= flashHtml(); ?>

<form method="GET" action="<?= PROOT; ?>Payroll/index/1" class="card border-0 shadow-sm mb-3">
  <div class="card-body py-2">
    <div class="row g-2 align-items-end">
      <div class="col-md-3"><label class="form-label small text-muted mb-1">Status</label>
        <select name="status" class="form-select form-select-sm"><option value="">All</option>
          <?php foreach (['draft','computed','approved','paid','void'] as $s): ?>
            <option value="<?= $s; ?>" <?= $f['status']===$s?'selected':''; ?>><?= ucfirst($s); ?></option>
          <?php endforeach; ?>
        </select></div>
      <div class="col-md-3"><label class="form-label small text-muted mb-1">Year</label>
        <select name="year" class="form-select form-select-sm"><option value="0">All</option>
          <?php for ($y = (int)date('Y'); $y >= 2020; $y--): ?><option value="<?= $y; ?>" <?= $f['year']===$y?'selected':''; ?>><?= $y; ?></option><?php endfor; ?>
        </select></div>
      <div class="col-md-2 d-flex gap-1">
        <button class="btn btn-sm btn-primary flex-grow-1"><i class="fas fa-filter"></i></button>
        <a href="<?= PROOT; ?>Payroll" class="btn btn-sm btn-outline-secondary"><i class="fas fa-times"></i></a>
      </div>
    </div>
  </div>
</form>

<div class="card border-0 shadow-sm">
  <div class="table-responsive">
    <table class="table table-sm mb-0 align-middle">
      <thead class="table-light">
        <tr><th>Period</th><th>From → To</th><th class="money">Drivers</th><th class="money">Gross</th><th class="money">Deductions</th><th class="money">Net</th><th class="money">Employer</th><th>Status</th><th class="text-end"></th></tr>
      </thead>
      <tbody>
        <?php foreach ($this->data['rows'] as $r):
          $bg = ['draft'=>'secondary','computed'=>'info','approved'=>'warning','paid'=>'success','void'=>'dark'][$r['status']] ?? 'secondary'; ?>
          <tr>
            <td class="small"><a href="<?= PROOT; ?>Payroll/view/<?= (int)$r['c0d']; ?>" class="fw-semibold font-monospace text-decoration-none"><?= htmlspecialchars($r['period_no']); ?></a></td>
            <td class="small text-muted"><?= humanDate($r['period_start']); ?> → <?= humanDate($r['period_end']); ?></td>
            <td class="money small"><?= (int)$r['driver_count']; ?></td>
            <td class="money small"><?= number_format((float)$r['total_gross'], 0); ?></td>
            <td class="money small text-danger"><?= number_format((float)$r['total_deductions'], 0); ?></td>
            <td class="money fw-semibold text-success"><?= number_format((float)$r['total_net'], 0); ?></td>
            <td class="money small text-muted"><?= number_format((float)$r['total_employer'], 0); ?></td>
            <td><span class="badge bg-<?= $bg; ?>" style="text-transform:capitalize;"><?= htmlspecialchars($r['status']); ?></span></td>
            <td class="text-end"><a href="<?= PROOT; ?>Payroll/view/<?= (int)$r['c0d']; ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-eye"></i></a></td>
          </tr>
        <?php endforeach; ?>
        <?php if (empty($this->data['rows'])): ?>
          <tr><td colspan="9" class="text-center text-muted py-5">No payroll periods — <a href="<?= PROOT; ?>Payroll/add">create the first one</a>.</td></tr>
        <?php endif; ?>
      </tbody>
    </table>
  </div>
  <?php if (!empty($this->data['pg_html'])): ?>
    <div class="card-footer bg-white d-flex justify-content-between py-2"><span class="small text-muted">Page <?= (int)$this->data['page']; ?> · <?= (int)$this->data['total']; ?> total</span><?= $this->data['pg_html']; ?></div>
  <?php endif; ?>
</div>

<?php $this->end(); ?>
