<?php $this->start('body');
    $s        = $this->data['summary']       ?? [];
    $arRows   = $this->data['overdue_ar']    ?? [];
    $chipBg   = ['success'=>'#0E9F6E','warning'=>'#F59E0B','danger'=>'#DC2626'][$s['chip'] ?? 'success'] ?? '#0E9F6E';
    $this->data['chip'] = ['bg' => $chipBg, 'label' => $s['chip_label'] ?? ''];
    include __DIR__ . '/_dash_header.php';
    /* Zone 1 — KPI strip */
    include __DIR__ . '/_kpi_strip.php';
?>

<!-- Zone 2 · Charts -->
<div class="row g-3">
  <div class="col-lg-8"><div class="card border-0 shadow-sm h-100"><div class="card-body">
    <div class="d-flex justify-content-between align-items-baseline mb-2">
      <h6 class="text-uppercase text-muted small mb-0">Revenue &middot; last 12 months</h6>
      <a href="<?= PROOT; ?>FinancialReports/pnl" class="small text-decoration-none">P&amp;L &rarr;</a>
    </div>
    <div id="chart-revenue" style="min-height: 260px;"></div>
  </div></div></div>
  <div class="col-lg-4"><div class="card border-0 shadow-sm h-100"><div class="card-body">
    <div class="d-flex justify-content-between align-items-baseline mb-2">
      <h6 class="text-uppercase text-muted small mb-0">Cash cover ratio</h6>
      <a href="<?= PROOT; ?>FinancialReports/cashFlow" class="small text-decoration-none">Flow &rarr;</a>
    </div>
    <div id="chart-cover" style="min-height: 260px;"></div>
  </div></div></div>
</div>

<div class="row g-3 mt-1">
  <div class="col-lg-6"><div class="card border-0 shadow-sm h-100"><div class="card-body">
    <div class="d-flex justify-content-between align-items-baseline mb-2">
      <h6 class="text-uppercase text-muted small mb-0">Receivables aging</h6>
      <a href="<?= PROOT; ?>AgedReceivables" class="small text-decoration-none">All &rarr;</a>
    </div>
    <div id="chart-ar" style="min-height: 260px;"></div>
  </div></div></div>
  <div class="col-lg-6"><div class="card border-0 shadow-sm h-100"><div class="card-body">
    <div class="d-flex justify-content-between align-items-baseline mb-2">
      <h6 class="text-uppercase text-muted small mb-0">Revenue vs Expenses &middot; last 6 months</h6>
      <a href="<?= PROOT; ?>FinancialReports/pnl" class="small text-decoration-none">P&amp;L &rarr;</a>
    </div>
    <div id="chart-rev-vs-exp" style="min-height: 260px;"></div>
  </div></div></div>
</div>

<!-- Zone 3 · Attention -->
<div class="row g-3 mt-1">
  <div class="col-lg-6"><?php include __DIR__ . '/_feed_compliance.php'; ?></div>
  <div class="col-lg-6">
    <div class="card border-0 shadow-sm h-100"><div class="card-body p-0">
      <div class="p-3 pb-2 d-flex justify-content-between align-items-baseline">
        <h6 class="text-uppercase text-muted small mb-0">Top overdue receivables</h6>
        <a href="<?= PROOT; ?>AgedReceivables" class="small text-decoration-none">All &rarr;</a>
      </div>
      <?php if (empty($arRows)): ?>
        <div class="text-center text-muted small py-4">No overdue invoices &mdash; nice.</div>
      <?php else: ?>
        <table class="table table-sm mb-0"><tbody>
          <?php foreach ($arRows as $r): ?>
            <tr>
              <td>
                <a href="<?= PROOT; ?>CustomerInvoices/view/<?= (int)$r['c0d']; ?>" class="text-decoration-none">
                  <div class="font-monospace small"><?= htmlspecialchars($r['invoice_no']); ?></div>
                  <div class="text-muted small"><?= htmlspecialchars($r['customer_name'] ?? '—'); ?></div>
                </a>
              </td>
              <td class="text-end">
                <div class="fw-bold font-monospace small">TZS <?= number_format((float)$r['outstanding'], 0); ?></div>
                <div class="badge bg-danger small"><?= (int)$r['days_overdue']; ?> days</div>
              </td>
            </tr>
          <?php endforeach; ?>
        </tbody></table>
      <?php endif; ?>
    </div></div>
  </div>
</div>

<!-- Zone 4 · Activity -->
<div class="row g-3 mt-1">
  <div class="col-12"><?php include __DIR__ . '/_feed_trips.php'; ?></div>
</div>

<?php $this->end(); $this->start('section_js'); ?>
<script>
document.addEventListener('DOMContentLoaded', function () {
  new PacificChart('#chart-revenue',    'area',      <?= json_encode(array_merge($this->data['charts']['revenue']    ?? [], ['tzs' => true])); ?>);
  new PacificChart('#chart-cover',      'radialBar', <?= json_encode($this->data['charts']['cover']       ?? []); ?>);
  new PacificChart('#chart-ar',         'donut',     <?= json_encode(array_merge($this->data['charts']['ar_aging']   ?? [], ['tzs' => true, 'colors' => 'aging'])); ?>);
  new PacificChart('#chart-rev-vs-exp', 'bar',       <?= json_encode(array_merge($this->data['charts']['rev_vs_exp'] ?? [], ['tzs' => true, 'colors' => 'duo'])); ?>);
});
</script>
<?php $this->end(); ?>
