<?php $this->start('body');
    $rows = $this->data['rows'] ?? [];
    $f    = $this->data['filter'] ?? [];
    $catLabel = ['G'=>'Goods','W'=>'Works','S'=>'Services','N'=>'Non-Consulting'];
    $catColor = ['G'=>'#7A8BA1','W'=>'#334155','S'=>'#7A9E7E','N'=>'#94A3B8'];
    $statusColor = ['active'=>'#7A9E7E','pursuing'=>'#D4A574','awarded'=>'#166534','cancelled'=>'#C48579','complete'=>'#94A3B8','withdrawn'=>'#94A3B8','OPEN'=>'#7A9E7E'];
?>

<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-globe-africa me-2" style="color:#7A8BA1;"></i>NeST Public Tenders</h5>
        <div class="text-muted small mt-1" style="color:#64748B;">Tanzania PPRA OCDS feed · auto-synced via <code>app/cli/nest_incremental.php</code></div>
    </div>
    <div class="d-flex gap-2">
        <?php if (UserGroups::can('NestSync')): ?><a href="<?= PROOT; ?>NestSync" class="btn btn-sm" style="background:#F1F5F9;border:1px solid #E2E8F0;color:#334155;"><i class="fas fa-satellite-dish me-1" style="color:#7A8BA1;"></i>Sync dashboard</a><?php endif; ?>
        <?php if (UserGroups::can('NestTenders','add')): ?><a href="<?= PROOT; ?>NestTenders/add" class="btn btn-sm" style="background:#7A8BA1;border-color:#7A8BA1;color:#fff;font-weight:600;"><i class="fas fa-plus me-1"></i>Add manually</a><?php endif; ?>
    </div>
</div>

<form method="get" action="<?= PROOT; ?>NestTenders" class="card border-0 shadow-sm mb-3" style="background:#F8FAFC;">
    <div class="card-body p-2">
        <div class="row g-2 align-items-end">
            <div class="col-md-4"><input type="text" name="q" value="<?= htmlspecialchars((string)($f['q'] ?? '')); ?>" placeholder="tender, buyer, OCID, ref…" class="form-control form-control-sm"></div>
            <div class="col-md-2"><select name="category" class="form-select form-select-sm">
                <option value="">All categories</option>
                <?php foreach ($catLabel as $k => $lbl): ?><option value="<?= $k; ?>" <?= ($f['category'] ?? '') === $k ? 'selected' : ''; ?>><?= $lbl; ?></option><?php endforeach; ?>
            </select></div>
            <div class="col-md-2"><input type="text" name="region" value="<?= htmlspecialchars((string)($f['region'] ?? '')); ?>" placeholder="Region" class="form-control form-control-sm"></div>
            <div class="col-md-2"><input type="text" name="fy" value="<?= htmlspecialchars((string)($f['fy'] ?? '')); ?>" placeholder="FY e.g. 2025/2026" class="form-control form-control-sm"></div>
            <div class="col-md-2"><button type="submit" class="btn btn-sm w-100" style="background:#7A8BA1;border-color:#7A8BA1;color:#fff;font-weight:600;"><i class="fas fa-filter me-1"></i>Filter</button></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 style="background:#F1F5F9;">
                <tr>
                    <th style="color:#334155;">Tender</th>
                    <th style="color:#334155;">Procuring entity</th>
                    <th style="color:#334155;">Cat</th>
                    <th class="text-end" style="color:#334155;">Value</th>
                    <th style="color:#334155;">Closes</th>
                    <th style="color:#334155;">Region</th>
                    <th style="color:#334155;">Status</th>
                    <th style="color:#334155;">Tracked / Project</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($rows as $r):
                    $col = $statusColor[strtolower((string)$r['tender_status'])] ?? '#94A3B8';
                    $overdue = !empty($r['tender_end']) && strtotime($r['tender_end']) < time() && strtolower((string)$r['tender_status']) === 'active';
                    $ct = $r['category_acronym'] ?? '';
                ?>
                    <tr>
                        <td style="color:#334155;font-weight:500;"><?= htmlspecialchars((string)$r['tender_title']); ?>
                            <div style="color:#94A3B8;font-size:.66rem;">OCID <?= htmlspecialchars((string)$r['ocid']); ?><?php if (!empty($r['tender_ref'])): ?> · ref <?= htmlspecialchars((string)$r['tender_ref']); ?><?php endif; ?><?php if (!empty($r['financial_year'])): ?> · FY <?= htmlspecialchars((string)$r['financial_year']); ?><?php endif; ?></div></td>
                        <td class="text-muted small" style="color:#64748B;"><?= htmlspecialchars((string)$r['buyer_name']); ?></td>
                        <td><?php if ($ct): ?><span class="badge" style="background:<?= $catColor[$ct] ?? '#94A3B8'; ?>;color:#fff;font-size:.62rem;"><?= htmlspecialchars($catLabel[$ct] ?? $ct); ?></span><?php else: ?>—<?php endif; ?></td>
                        <td class="text-end" style="color:#334155;"><?= $r['amount'] !== null ? number_format((float)$r['amount']) : '—'; ?></td>
                        <td class="text-muted small" style="color:<?= $overdue ? '#C48579' : '#64748B'; ?>;"><?= $r['tender_end'] ? date('Y-m-d', strtotime($r['tender_end'])) : '—'; ?></td>
                        <td class="text-muted small" style="color:#64748B;"><?= htmlspecialchars((string)$r['region']); ?></td>
                        <td><span class="badge" style="background:<?= $col; ?>;color:#fff;font-size:.62rem;text-transform:capitalize;"><?= htmlspecialchars((string)$r['tender_status']); ?></span></td>
                        <td class="text-muted small">
                            <?php if (!empty($r['internal_tender_no'])): ?><a href="<?= PROOT; ?>Tenders" style="color:#166534;font-weight:500;"><?= htmlspecialchars($r['internal_tender_no']); ?></a><?php endif; ?>
                            <?php if (!empty($r['project_no'])): ?><div style="color:#166534;font-size:.66rem;">→ <?= htmlspecialchars($r['project_no']); ?></div><?php endif; ?>
                            <?php if (empty($r['internal_tender_no']) && empty($r['project_no'])): ?>—<?php endif; ?>
                        </td>
                        <td class="text-end"><a href="<?= PROOT; ?>NestTenders/view/<?= (int)$r['c0d']; ?>" class="btn btn-sm" style="background:#F8FAFC;border:1px solid #E2E8F0;color:#334155;"><i class="fas fa-eye me-1" style="color:#7A8BA1;"></i>View</a></td>
                    </tr>
                <?php endforeach; ?>
                <?php if (empty($rows)): ?>
                    <tr><td colspan="9" class="text-center py-4" style="color:#94A3B8;">
                        No tenders in DB yet. Configure the sync per <code>app/cli/NEST_README.md</code>,<br>
                        or run: <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(); ?>
