<?php $this->start('body');
    $rows = $this->data['rows'] ?? [];
    $statusPill = [
        'PLANNED'   => ['#f59e0b', 'Planned'],
        'ACTIVE'    => ['#22c55e', 'Active'],
        'CLOSED'    => ['#7A8BA1', 'Closed'],
        'CANCELLED' => ['#dc2626', 'Cancelled'],
    ];
?>
<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-layer-group me-1 text-muted"></i>
            Phases · <?= count($rows); ?> across all works projects
        </h5>
        <div class="text-muted" style="font-size:.72rem;">Active phases surfaced first, then Planned.</div>
    </div>
</div>

<div class="card border-0 shadow-sm">
    <div class="card-body p-0">
        <table class="table table-sm mb-0 align-middle">
            <thead class="table-light">
                <tr>
                    <th style="width:80px;">Code</th>
                    <th>Phase</th>
                    <th>Project</th>
                    <th style="width:110px;">Start</th>
                    <th style="width:110px;">End</th>
                    <th style="width:100px;">Status</th>
                    <th style="width:80px;" class="text-end">Open</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($rows as $r):
                    $sp = $statusPill[$r['status']] ?? ['#94a3b8', $r['status']];
                ?>
                    <tr>
                        <td><code><?= htmlspecialchars($r['code'] ?: '—'); ?></code></td>
                        <td>
                            <strong><?= (int)$r['phase_no']; ?> · <?= htmlspecialchars($r['title']); ?></strong>
                        </td>
                        <td class="small">
                            <?= htmlspecialchars($r['project_no']); ?>
                            <div class="text-muted"><?= htmlspecialchars($r['project_name']); ?></div>
                        </td>
                        <td class="text-muted small"><?= htmlspecialchars($r['planned_start'] ?? '—'); ?></td>
                        <td class="text-muted small"><?= htmlspecialchars($r['planned_end']   ?? '—'); ?></td>
                        <td><span class="badge" style="background:<?= $sp[0]; ?>;color:#fff;"><?= htmlspecialchars($sp[1]); ?></span></td>
                        <td class="text-end"><a href="<?= PROOT; ?>WorksPhases/view/<?= (int)$r['c0d']; ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-right"></i></a></td>
                    </tr>
                <?php endforeach; ?>
                <?php if (empty($rows)): ?>
                    <tr><td colspan="7" class="text-center text-muted py-4">No phases planned yet — open a project's BOQ to start planning.</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
<?php $this->end(); ?>
