<?php $this->start('body');
    $alerts = $this->data['alerts'] ?? [];
    $bySev  = $this->data['by_sev'] ?? [];
    $counts = $this->data['counts'] ?? [];

    $sevChip = [
        'HIGH'   => ['#B87A5A', '#F3E5D9', 'High'],
        'MEDIUM' => ['#5B5738', '#EAE7DA', 'Medium'],
        'LOW'    => ['#3D5B44', '#EAF2E8', 'Low'],
    ];
    $catChip = [
        'LOSS_MAKING'             => ['#F3E5D9', '#6B3F2A', 'Loss'],
        'OVER_COMMITTED'          => ['#F3E5D9', '#6B3F2A', 'Over-commit'],
        'STALE_DRAFT_IPC'         => ['#EDE7DC', '#5B4F3C', 'Stale IPC'],
        'CUST_RETENTION_PAST_DLP' => ['#EAE7DA', '#5B5738', 'Cust retention'],
        'SUPP_RETENTION_PAST_DLP' => ['#EDE7DC', '#5B4F3C', 'Supp retention'],
        'PHASE_OVERDUE'           => ['#DDE4E9', '#3D4E5B', 'Phase overdue'],
        'DIRECT_AWARD_UNJUSTIFIED'=> ['#F3E5D9', '#6B3F2A', 'Direct award ⚠'],
    ];
?>
<div class="d-flex align-items-center justify-content-between mb-3">
    <div>
        <h5 class="mb-0" style="font-weight:600;"><i class="fas fa-bell me-1 text-muted"></i>
            Works alerts · <?= (int)$counts['total']; ?>
        </h5>
        <div class="text-muted" style="font-size:.72rem;">Live exception feed across every works project — click through to act.</div>
    </div>
    <div>
        <span class="badge me-1" style="background:<?= $sevChip['HIGH'][0]; ?>;color:#fff;"><?= (int)$counts['HIGH']; ?> high</span>
        <span class="badge me-1" style="background:<?= $sevChip['MEDIUM'][0]; ?>;color:#fff;"><?= (int)$counts['MEDIUM']; ?> medium</span>
        <span class="badge"      style="background:<?= $sevChip['LOW'][0]; ?>;color:#fff;"><?= (int)$counts['LOW']; ?> low</span>
    </div>
</div>

<?php if ((int)$counts['total'] === 0): ?>
    <div class="card border-0 shadow-sm">
        <div class="card-body p-5 text-center">
            <i class="fas fa-check-circle" style="font-size:3rem;color:#7A9E7E;"></i>
            <div class="mt-3" style="color:#3D5B44;font-weight:600;">All clear.</div>
            <div class="text-muted small">No projects, IPCs, retentions or phases need attention right now.</div>
        </div>
    </div>
<?php else: ?>
<?php foreach (['HIGH','MEDIUM','LOW'] as $sev):
    $items = $bySev[$sev] ?? [];
    if (empty($items)) continue;
    $sc = $sevChip[$sev];
?>
<div class="card border-0 shadow-sm mb-3">
    <div class="card-header bg-white">
        <span class="badge me-2" style="background:<?= $sc[0]; ?>;color:#fff;"><?= htmlspecialchars($sc[2]); ?></span>
        <strong><?= count($items); ?> alert<?= count($items) === 1 ? '' : 's'; ?></strong>
    </div>
    <div class="card-body p-0">
        <table class="table table-sm mb-0 align-middle">
            <thead class="table-light">
                <tr>
                    <th style="width:140px;">Category</th>
                    <th style="width:160px;">Project</th>
                    <th>Summary</th>
                    <th style="width:80px;" class="text-end">Open</th>
                </tr>
            </thead>
            <tbody>
            <?php foreach ($items as $a):
                $cc = $catChip[$a['category']] ?? ['#e5e7eb', '#374151', $a['category']];
            ?>
                <tr>
                    <td><span class="badge" style="background:<?= $cc[0]; ?>;color:<?= $cc[1]; ?>;text-transform:capitalize;font-weight:600;"><?= htmlspecialchars($cc[2]); ?></span></td>
                    <td class="small">
                        <code><?= htmlspecialchars($a['project_no']); ?></code>
                        <div class="text-muted"><?= htmlspecialchars($a['project_name']); ?></div>
                    </td>
                    <td class="small"><?= htmlspecialchars($a['summary']); ?></td>
                    <td class="text-end"><a href="<?= htmlspecialchars($a['action_url']); ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-right"></i></a></td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
<?php endforeach; ?>
<?php endif; ?>
<?php $this->end(); ?>
