<?php $this->start('body');
    $tot = $this->data['totals'] ?? [];
    $last = $this->data['last_run'] ?? null;
    $runs = $this->data['runs'] ?? [];
    $byFy = $this->data['by_fy'] ?? [];
    $byCat = $this->data['by_cat'] ?? [];

    $catLabels = ['G' => 'Goods', 'W' => 'Works', 'S' => 'Services', 'N' => 'Non-Consulting'];
    $statusColor = ['running' => '#D4A574', 'done' => '#7A9E7E', 'failed' => '#C48579'];
    $syncTypeColor = ['bulk' => '#7A8BA1', 'incremental' => '#334155', 'manual' => '#94A3B8', 'digest' => '#7A9E7E', 'recover' => '#C48579'];
?>

<div class="d-flex align-items-center justify-content-between mb-3">
    <div>
        <h5 class="mb-0" style="font-weight:600;color:#334155;"><i class="fas fa-satellite-dish me-2" style="color:#7A8BA1;"></i>NeST Sync · Admin</h5>
        <div class="text-muted small mt-1" style="color:#64748B;">CLI jobs feed <code>crm_nest_tenders</code>. Configure Windows Task Scheduler per <code>app/cli/NEST_README.md</code>.</div>
    </div>
    <a href="<?= PROOT; ?>NestTenders" class="btn btn-sm" style="background:#F8FAFC;border:1px solid #E2E8F0;color:#334155;"><i class="fas fa-arrow-left me-1"></i>Back to feed</a>
</div>

<div class="row g-3 mb-3">
    <div class="col-md-3"><div class="card border-0 shadow-sm h-100" style="border-left:4px solid #7A8BA1 !important;background:#F8FAFC;"><div class="card-body p-3">
        <div style="text-transform:uppercase;letter-spacing:.4px;font-size:.66rem;color:#64748B;">Total in DB</div>
        <div style="font-weight:700;font-size:1.4rem;color:#334155;"><?= number_format((int)($tot['total'] ?? 0)); ?></div>
    </div></div></div>
    <div class="col-md-3"><div class="card border-0 shadow-sm h-100" style="border-left:4px solid #7A9E7E !important;background:#F8FAFC;"><div class="card-body p-3">
        <div style="text-transform:uppercase;letter-spacing:.4px;font-size:.66rem;color:#64748B;">Open now</div>
        <div style="font-weight:700;font-size:1.4rem;color:#7A9E7E;"><?= number_format((int)($tot['open_now'] ?? 0)); ?></div>
        <div class="text-muted small" style="color:#64748B;font-size:.72rem;">deadline in future</div>
    </div></div></div>
    <div class="col-md-3"><div class="card border-0 shadow-sm h-100" style="border-left:4px solid #D4A574 !important;background:#F8FAFC;"><div class="card-body p-3">
        <div style="text-transform:uppercase;letter-spacing:.4px;font-size:.66rem;color:#64748B;">Being tracked</div>
        <div style="font-weight:700;font-size:1.4rem;color:#334155;"><?= number_format((int)($tot['tracked'] ?? 0)); ?></div>
        <div class="text-muted small" style="color:#64748B;font-size:.72rem;">linked to internal tender</div>
    </div></div></div>
    <div class="col-md-3"><div class="card border-0 shadow-sm h-100" style="background:#F8FAFC;"><div class="card-body p-3">
        <div style="text-transform:uppercase;letter-spacing:.4px;font-size:.66rem;color:#64748B;">Last synced</div>
        <div style="font-weight:600;font-size:1rem;color:#334155;"><?= htmlspecialchars((string)($tot['last_synced'] ?? '—')); ?></div>
    </div></div></div>
</div>

<?php if ($last): ?>
<div class="alert py-2 mb-3" style="background:<?= $last['status'] === 'failed' ? '#FEE2E2' : '#F1F5F9'; ?>;border:1px solid <?= $last['status'] === 'failed' ? '#FCA5A5' : '#E2E8F0'; ?>;color:#334155;font-size:.86rem;">
    <i class="fas fa-<?= $last['status'] === 'failed' ? 'exclamation-triangle' : ($last['status'] === 'running' ? 'spinner fa-spin' : 'check-circle'); ?> me-1" style="color:<?= $statusColor[$last['status']] ?? '#334155'; ?>;"></i>
    <strong>Last run:</strong> <?= htmlspecialchars((string)$last['sync_type']); ?> · <?= htmlspecialchars((string)$last['started_at']); ?>
    · fetched <?= (int)$last['records_fetched']; ?>, inserted <?= (int)$last['records_inserted']; ?>, updated <?= (int)$last['records_updated']; ?>, skipped <?= (int)$last['records_skipped']; ?>, errors <?= (int)$last['error_count']; ?>
    · status <strong><?= htmlspecialchars($last['status']); ?></strong>
</div>
<?php endif; ?>

<div class="row g-3 mb-3">
    <div class="col-md-6">
        <div class="card border-0 shadow-sm h-100">
            <div class="card-header bg-white" style="border-bottom:1px solid #E2E8F0;"><strong style="color:#334155;"><i class="fas fa-calendar me-2" style="color:#7A8BA1;"></i>By financial year</strong></div>
            <div class="table-responsive">
                <table class="table table-sm mb-0 align-middle">
                    <thead style="background:#F1F5F9;"><tr><th style="color:#334155;">FY</th><th class="text-end" style="color:#334155;">Count</th></tr></thead>
                    <tbody>
                        <?php foreach ($byFy as $r): ?><tr><td style="color:#334155;"><?= htmlspecialchars((string)$r['financial_year']); ?></td><td class="text-end" style="color:#334155;font-weight:500;"><?= number_format((int)$r['n']); ?></td></tr><?php endforeach; ?>
                        <?php if (empty($byFy)): ?><tr><td colspan="2" class="text-center py-3" style="color:#94A3B8;">No data yet.</td></tr><?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    <div class="col-md-6">
        <div class="card border-0 shadow-sm h-100">
            <div class="card-header bg-white" style="border-bottom:1px solid #E2E8F0;"><strong style="color:#334155;"><i class="fas fa-tag me-2" style="color:#7A8BA1;"></i>By category</strong></div>
            <div class="table-responsive">
                <table class="table table-sm mb-0 align-middle">
                    <thead style="background:#F1F5F9;"><tr><th style="color:#334155;">Category</th><th class="text-end" style="color:#334155;">Count</th></tr></thead>
                    <tbody>
                        <?php foreach ($byCat as $r): ?><tr><td style="color:#334155;"><?= htmlspecialchars($catLabels[$r['category_acronym']] ?? $r['category_acronym']); ?></td><td class="text-end" style="color:#334155;font-weight:500;"><?= number_format((int)$r['n']); ?></td></tr><?php endforeach; ?>
                        <?php if (empty($byCat)): ?><tr><td colspan="2" class="text-center py-3" style="color:#94A3B8;">No data yet.</td></tr><?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<div class="card border-0 shadow-sm">
    <div class="card-header bg-white d-flex align-items-center justify-content-between" style="border-bottom:1px solid #E2E8F0;">
        <strong style="color:#334155;"><i class="fas fa-list me-2" style="color:#7A8BA1;"></i>Recent sync runs</strong>
        <span class="text-muted small" style="color:#64748B;"><?= count($runs); ?> shown</span>
    </div>
    <div class="table-responsive">
        <table class="table table-sm mb-0 align-middle">
            <thead style="background:#F1F5F9;">
                <tr>
                    <th style="color:#334155;">Started</th>
                    <th style="color:#334155;">Type</th>
                    <th style="color:#334155;">Since / Page</th>
                    <th class="text-end" style="color:#334155;">Fetched</th>
                    <th class="text-end" style="color:#334155;">Inserted</th>
                    <th class="text-end" style="color:#334155;">Updated</th>
                    <th class="text-end" style="color:#334155;">Skipped</th>
                    <th class="text-end" style="color:#334155;">Errors</th>
                    <th style="color:#334155;">Status</th>
                    <th style="color:#334155;">Notes</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($runs as $r):
                    $sc = $statusColor[$r['status']] ?? '#94A3B8';
                    $tc = $syncTypeColor[$r['sync_type']] ?? '#94A3B8';
                ?>
                    <tr>
                        <td class="text-muted small" style="color:#64748B;white-space:nowrap;"><?= htmlspecialchars((string)$r['started_at']); ?></td>
                        <td><span class="badge" style="background:<?= $tc; ?>;color:#fff;font-size:.62rem;text-transform:capitalize;"><?= htmlspecialchars($r['sync_type']); ?></span></td>
                        <td class="text-muted small" style="color:#64748B;"><?= htmlspecialchars((string)($r['since_param'] ?? '')); ?><?php if ($r['page_start']): ?> · p<?= (int)$r['page_start']; ?>→<?= (int)$r['page_end']; ?><?php endif; ?></td>
                        <td class="text-end" style="color:#334155;"><?= number_format((int)$r['records_fetched']); ?></td>
                        <td class="text-end" style="color:<?= (int)$r['records_inserted'] > 0 ? '#7A9E7E' : '#94A3B8'; ?>;"><?= number_format((int)$r['records_inserted']); ?></td>
                        <td class="text-end" style="color:#334155;"><?= number_format((int)$r['records_updated']); ?></td>
                        <td class="text-end text-muted small" style="color:#64748B;"><?= number_format((int)$r['records_skipped']); ?></td>
                        <td class="text-end" style="color:<?= (int)$r['error_count'] > 0 ? '#C48579' : '#94A3B8'; ?>;"><?= number_format((int)$r['error_count']); ?></td>
                        <td><span class="badge" style="background:<?= $sc; ?>;color:#fff;font-size:.62rem;text-transform:capitalize;"><?= htmlspecialchars($r['status']); ?></span></td>
                        <td class="text-muted small" style="color:#64748B;font-size:.72rem;max-width:220px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;" title="<?= htmlspecialchars((string)$r['notes']); ?>"><?= htmlspecialchars((string)$r['notes']); ?></td>
                    </tr>
                <?php endforeach; ?>
                <?php if (empty($runs)): ?>
                    <tr><td colspan="10" class="text-center py-4" style="color:#94A3B8;">
                        No sync runs yet. Configure Task Scheduler per <code>app/cli/NEST_README.md</code>, or run:<br>
                        <code>php app/cli/nest_backfill.php --since=2025-07-01T00:00:00 --max-pages=200</code>
                    </td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

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