<?php $this->start('body');
    $rows = $this->data['rows'] ?? [];
    $statusPill = [
        'DRAFT'      => ['#f59e0b', 'Draft'],
        'SUBMITTED'  => ['#0ea5e9', 'Submitted'],
        'APPROVED'   => ['#22c55e', 'Approved'],
        'ACTIVE'     => ['#166534', 'Active'],
        'SUPERSEDED' => ['#94a3b8', 'Superseded'],
        'CLOSED'     => ['#7A8BA1', 'Closed'],
        'DECLINED'   => ['#dc2626', 'Declined'],
        'CANCELLED'  => ['#94a3b8', '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-file-invoice me-1 text-muted"></i>
            Consolidated Requests · <?= count($rows); ?> across all works projects
        </h5>
        <div class="text-muted" style="font-size:.72rem;">Active first, then Submitted, then Approved — the CR is the phase's execution package.</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>Ref</th>
                    <th>Title</th>
                    <th>Project · Phase</th>
                    <th style="width:80px;">Ver</th>
                    <th class="text-end">Grand total</th>
                    <th style="width:110px;">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['cr_no']); ?></code></td>
                        <td><?= htmlspecialchars($r['title']); ?></td>
                        <td class="small">
                            <?= htmlspecialchars($r['project_no']); ?>
                            <div class="text-muted">Ph.<?= (int)$r['phase_no']; ?> · <?= htmlspecialchars($r['phase_title']); ?></div>
                        </td>
                        <td class="small">v<?= (int)$r['version']; ?></td>
                        <td class="text-end"><strong><?= number_format((float)$r['grand_total']); ?></strong></td>
                        <td><span class="badge" style="background:<?= $sp[0]; ?>;color:#fff;"><?= htmlspecialchars($sp[1]); ?></span></td>
                        <td class="text-end"><a href="<?= PROOT; ?>WorksCrs/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 CRs raised yet — open a phase to draft one.</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
<?php $this->end(); ?>
