<?php $this->start('body'); ?>
<?php
$r = $this->data['r']; $year = $this->data['year']; $month = $this->data['month'];
$this->data['form_action'] = PROOT . 'TaxReturns/paye';
$dueTs = strtotime($r['due_date']);
$today = strtotime('today');
$overdue  = $today > $dueTs;
$dueSoon  = !$overdue && ($dueTs - $today) < 7 * 86400;
?>

<div class="d-flex align-items-center justify-content-between mb-3 flex-wrap gap-2">
  <div>
    <h5 class="mb-0 fw-semibold"><i class="fas fa-user-tie me-2 text-muted"></i>PAYE Return</h5>
    <div class="text-muted small"><?= date('F Y', strtotime($r['from'])); ?> · due <?= humanDate($r['due_date']); ?>
      <?php if ($overdue): ?><span class="badge bg-danger ms-1">Overdue</span>
      <?php elseif ($dueSoon): ?><span class="badge bg-warning text-dark ms-1">Due soon</span>
      <?php endif; ?>
    </div>
  </div>
  <div class="d-flex gap-2">
    <a href="<?= PROOT; ?>TaxReturns" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-left me-1"></i>Returns</a>
    <a href="<?= PROOT; ?>TaxReturns/payeCsv/<?= $year; ?>/<?= $month; ?>" class="btn btn-sm btn-outline-primary"><i class="fas fa-file-csv me-1"></i>CSV for TRA</a>
    <a href="<?= PROOT; ?>Payroll/p9Batch/<?= $year; ?>" target="_blank" class="btn btn-sm btn-outline-primary"><i class="fas fa-file-invoice me-1"></i>P9 batch (<?= $year; ?>)</a>
    <button class="btn btn-sm btn-outline-primary" onclick="window.print()"><i class="fas fa-print me-1"></i>Print</button>
  </div>
</div>

<?php include __DIR__ . '/_month_form.php'; ?>

<!-- KPI strip -->
<div class="row g-2 mb-3">
  <div class="col-6 col-md-3"><div class="card border-0 shadow-sm h-100"><div class="card-body py-2">
    <div class="text-uppercase text-muted small">Employees</div>
    <div class="fs-4 fw-bold"><?= count($r['employees']); ?></div>
  </div></div></div>
  <div class="col-6 col-md-3"><div class="card border-0 shadow-sm h-100"><div class="card-body py-2">
    <div class="text-uppercase text-muted small">Gross</div>
    <div class="fs-4 fw-bold">TZS <?= number_format($r['total_gross'], 0); ?></div>
  </div></div></div>
  <div class="col-6 col-md-3"><div class="card border-0 shadow-sm h-100"><div class="card-body py-2">
    <div class="text-uppercase text-muted small">PAYE due</div>
    <div class="fs-4 fw-bold text-primary">TZS <?= number_format($r['total_paye'], 0); ?></div>
    <?php if ($r['total_gross'] > 0): ?>
      <div class="small text-muted">eff. <?= number_format($r['total_paye'] / $r['total_gross'] * 100, 1); ?>%</div>
    <?php endif; ?>
  </div></div></div>
  <div class="col-6 col-md-3"><div class="card border-0 shadow-sm h-100"><div class="card-body py-2">
    <div class="text-uppercase text-muted small">Ledger accrual</div>
    <div class="fs-5 fw-bold">TZS <?= number_format($r['ledger_credit'], 0); ?></div>
    <div class="small text-muted">Credit to 4004 PAYE Payable</div>
  </div></div></div>
</div>

<!-- Employee schedule -->
<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 style="width:130px;">Employee #</th>
          <th>Full Name</th>
          <th class="money">Gross</th>
          <th class="money">Base</th>
          <th class="money">Commission</th>
          <th class="money">Allowances</th>
          <th class="money">NSSF Ee</th>
          <th class="money">PAYE</th>
        </tr>
      </thead>
      <tbody>
        <?php if (empty($r['employees'])): ?>
          <tr><td colspan="8" class="text-center text-muted py-5">
            No approved payroll records for this period. Compute and approve payroll in <a href="<?= PROOT; ?>Payroll">Payroll</a> to populate this return.
          </td></tr>
        <?php else: ?>
          <?php foreach ($r['employees'] as $e): ?>
            <tr>
              <td class="font-monospace small"><?= htmlspecialchars($e['employee_no'] ?: '—'); ?></td>
              <td><a href="<?= PROOT; ?>Drivers/view/<?= (int)$e['driver_id']; ?>" class="text-decoration-none"><?= htmlspecialchars($e['full_name']); ?></a>
                <?php if ($e['id_no']): ?><div class="small text-muted font-monospace"><?= htmlspecialchars($e['id_no']); ?></div><?php endif; ?>
              </td>
              <td class="money font-monospace"><?= number_format((float)$e['gross_pay'], 2); ?></td>
              <td class="money font-monospace small"><?= number_format((float)$e['base_salary'], 2); ?></td>
              <td class="money font-monospace small"><?= (float)$e['commission'] > 0 ? number_format((float)$e['commission'], 2) : '—'; ?></td>
              <td class="money font-monospace small"><?= (float)$e['allowances'] > 0 ? number_format((float)$e['allowances'], 2) : '—'; ?></td>
              <td class="money font-monospace small text-danger"><?= number_format((float)$e['nssf_ee'], 2); ?></td>
              <td class="money font-monospace fw-bold text-primary"><?= number_format((float)$e['paye'], 2); ?></td>
            </tr>
          <?php endforeach; ?>
        <?php endif; ?>
      </tbody>
      <tfoot class="table-light fw-bold">
        <tr>
          <th colspan="2" class="text-uppercase small">Totals</th>
          <th class="money font-monospace"><?= number_format($r['total_gross'], 2); ?></th>
          <th colspan="3"></th>
          <th class="money font-monospace text-danger"><?= number_format($r['total_nssf_ee'], 2); ?></th>
          <th class="money font-monospace text-primary"><?= number_format($r['total_paye'], 2); ?></th>
        </tr>
      </tfoot>
    </table>
  </div>
</div>

<!-- Ledger reconciliation -->
<?php
$diff = round($r['ledger_net'] - $r['total_paye'], 2);
$reconciles = abs($diff) < 1;
?>
<div class="card border-0 shadow-sm mt-3">
  <div class="card-body py-2 small">
    <div class="text-uppercase text-muted mb-1" style="font-size:.65em;letter-spacing:.08em;">Ledger cross-check · 4004 PAYE Payable</div>
    Credit <strong class="font-monospace"><?= number_format($r['ledger_credit'], 2); ?></strong>
    &minus; Debit <strong class="font-monospace"><?= number_format($r['ledger_debit'], 2); ?></strong>
    = <strong class="font-monospace"><?= number_format($r['ledger_net'], 2); ?></strong>
    <?php if ($reconciles): ?>
      <span class="badge bg-success ms-2"><i class="fas fa-check me-1"></i>Reconciles</span>
    <?php else: ?>
      <span class="badge bg-warning text-dark ms-2"><i class="fas fa-triangle-exclamation me-1"></i>Off by <?= number_format($diff, 2); ?></span>
      <div class="text-muted mt-1">Payroll totals differ from ledger accrual — likely because the period was compiled but not fully posted, or an adjustment JE hit the account outside of payroll.</div>
    <?php endif; ?>
  </div>
</div>
<?php $this->end(); ?>
