<?php $this->start('body');
    $rows   = $this->data['rows']   ?? [];
    $status = $this->data['status'] ?? '';
    $statusPill = [
        'DRAFT'     => ['#f59e0b', 'Draft'],
        'SUBMITTED' => ['#0ea5e9', 'Submitted'],
        'APPROVED'  => ['#22c55e', 'Approved'],
        'ISSUED'    => ['#7A9E7E', 'Issued'],
        'RETIRING'  => ['#a78bfa', 'Retiring'],
        'RETIRED'   => ['#7A8BA1', 'Retired'],
        'CLOSED'    => ['#94a3b8', 'Closed'],
        'DECLINED'  => ['#dc2626', 'Declined'],
        'CANCELLED' => ['#94a3b8', 'Cancelled'],
    ];
    $filters = ['','DRAFT','SUBMITTED','APPROVED','ISSUED','RETIRING','RETIRED','DECLINED','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-hand-holding-usd me-1 text-muted"></i> Service Requisitions</h5>
        <div class="text-muted" style="font-size:.72rem;"><?= count($rows); ?> row<?= count($rows) === 1 ? '' : 's' ?><?= $status ? ' · filtered to ' . htmlspecialchars($status) : ''; ?></div>
    </div>
    <a href="<?= PROOT; ?>ServiceRequisitions/myQueue" class="btn btn-sm btn-outline-secondary"><i class="fas fa-inbox me-1"></i> My Queue</a>
</div>

<div class="btn-group mb-3">
    <?php foreach ($filters as $f): ?>
        <a href="?<?= $f ? 'status=' . urlencode($f) : ''; ?>" class="btn btn-sm <?= $status === $f ? 'btn-primary' : 'btn-outline-secondary'; ?>"><?= $f === '' ? 'All' : htmlspecialchars($f); ?></a>
    <?php endforeach; ?>
</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>Project</th>
                    <th>Title</th>
                    <th class="text-end">Amount</th>
                    <th>Status</th>
                    <th style="width:110px;">Submitted</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['req_no']); ?></code></td>
                        <td>
                            <a href="<?= PROOT; ?>Projects/view/<?= (int)$r['project_id']; ?>"><strong><?= htmlspecialchars($r['project_no']); ?></strong></a>
                            <div class="text-muted small"><?= htmlspecialchars($r['project_name']); ?></div>
                        </td>
                        <td>
                            <?= htmlspecialchars($r['title']); ?>
                            <?php if (!empty($r['context_ref'])): ?><div class="text-muted small"><?= htmlspecialchars($r['context_ref']); ?></div><?php endif; ?>
                        </td>
                        <td class="text-end"><?= number_format((float)$r['total_amount']); ?></td>
                        <td><span class="badge" style="background:<?= $sp[0]; ?>;color:#fff;"><?= htmlspecialchars($sp[1]); ?></span></td>
                        <td class="text-muted small"><?= htmlspecialchars($r['submitted_on'] ?? '—'); ?></td>
                        <td class="text-end"><a href="<?= PROOT; ?>ServiceRequisitions/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 requisitions match this filter.</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
<?php $this->end(); ?>
