<?php $this->start('body'); ?>
<?php
$r = $this->data['r']; $year = $this->data['year']; $month = $this->data['month'];
$this->data['form_action'] = PROOT . 'TaxReturns/statutory';

$render_due = function (string $ymd) {
    $ts = strtotime($ymd); $t = strtotime('today');
    if ($t > $ts)                       return '<span class="badge bg-danger">Overdue</span>';
    if (($ts - $t) < 7 * 86400)         return '<span class="badge bg-warning text-dark">Due soon</span>';
    return '<span class="badge bg-light text-muted">' . humanDate($ymd) . '</span>';
};
?>

<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-people-arrows me-2 text-muted"></i>Statutory Summary</h5>
    <div class="text-muted small"><?= date('F Y', strtotime($r['from'])); ?> · <?= (int)$r['employees']; ?> employee(s)</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>
    <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: Gross + total statutory -->
<div class="row g-2 mb-3">
  <div class="col-md-6"><div class="card border-0 shadow-sm h-100"><div class="card-body py-2">
    <div class="text-uppercase text-muted small">Gross salary bill</div>
    <div class="fs-3 fw-bold">TZS <?= number_format($r['gross'], 0); ?></div>
  </div></div></div>
  <div class="col-md-6"><div class="card border-0 shadow-sm h-100"><div class="card-body py-2">
    <div class="text-uppercase text-muted small">Total statutory (PAYE + NSSF + SDL + WCF)</div>
    <div class="fs-3 fw-bold text-primary">TZS <?= number_format($r['total_statutory'], 0); ?></div>
    <?php if ($r['gross'] > 0): ?>
      <div class="small text-muted"><?= number_format($r['total_statutory'] / $r['gross'] * 100, 1); ?>% of gross</div>
    <?php endif; ?>
  </div></div></div>
</div>

<!-- Four-tile breakdown -->
<div class="row g-3">
  <?php
  $tiles = [
    ['PAYE',      'paye', $r['paye'],        '4004 PAYE Payable',    'paye', $r['ledger']['paye']],
    ['NSSF',      'nssf', $r['nssf_total'],  '4003 NSSF Payable',    'nssf', $r['ledger']['nssf']],
    ['SDL',       'sdl',  $r['sdl'],         '4006 SDL Payable',     'sdl',  $r['ledger']['sdl']],
    ['WCF',       'wcf',  $r['wcf'],         '4008 WCF Payable',     'wcf',  $r['ledger']['wcf']],
  ];
  foreach ($tiles as [$title, $key, $val, $coaLabel, $dueKey, $ledger]):
      $ledgerNet = round((float)$ledger['credit'] - (float)$ledger['debit'], 2);
      $reconciles = abs($ledgerNet - $val) < 1;
  ?>
    <div class="col-md-6 col-xl-3">
      <div class="card border-0 shadow-sm h-100">
        <div class="card-body">
          <div class="d-flex justify-content-between align-items-start">
            <div>
              <div class="text-uppercase text-muted small" style="letter-spacing:.06em;"><?= $title; ?></div>
              <div class="fs-4 fw-bold">TZS <?= number_format($val, 0); ?></div>
            </div>
            <?= $render_due($r['due_dates'][$dueKey]); ?>
          </div>
          <?php if ($title === 'NSSF' && $r['gross'] > 0): ?>
            <div class="small text-muted mt-1">
              Ee <?= number_format($r['nssf_ee'], 0); ?> + Er <?= number_format($r['nssf_er'], 0); ?>
            </div>
          <?php endif; ?>

          <hr class="my-2">
          <div class="small">
            <div class="text-uppercase text-muted mb-1" style="font-size:.65em;letter-spacing:.08em;"><?= $coaLabel; ?></div>
            <div>Credit <strong class="font-monospace"><?= number_format((float)$ledger['credit'], 2); ?></strong>
            &minus; Debit <strong class="font-monospace"><?= number_format((float)$ledger['debit'], 2); ?></strong></div>
            <div class="mt-1">
              <?php if ($reconciles): ?>
                <span class="badge bg-success"><i class="fas fa-check me-1"></i>Reconciles</span>
              <?php else: ?>
                <span class="badge bg-warning text-dark"><i class="fas fa-triangle-exclamation me-1"></i>Off by <?= number_format(abs($ledgerNet - $val), 2); ?></span>
              <?php endif; ?>
            </div>
          </div>
        </div>
      </div>
    </div>
  <?php endforeach; ?>
</div>

<?php if ($r['employees'] === 0): ?>
  <div class="alert alert-info small mt-3">
    <i class="fas fa-info-circle me-1"></i>
    No approved payroll records for this period. Compute and approve payroll in <a href="<?= PROOT; ?>Payroll">Payroll</a> to populate this summary.
  </div>
<?php endif; ?>
<?php $this->end(); ?>
