<?php $this->start('body');
    $mode = $this->data['mode'] ?? 'mine';
    $statusPill = [
        'DRAFT'     => ['#f59e0b', 'Draft'],
        'SUBMITTED' => ['#0ea5e9', 'Submitted'],
        'APPROVED'  => ['#22c55e', 'Ready to issue'],
        'ISSUED'    => ['#7A9E7E', 'Issued'],
        'RETIRING'  => ['#a78bfa', 'Retiring'],
        'RETIRED'   => ['#7A8BA1', 'Retired'],
        '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-inbox me-1 text-muted"></i>
            <?php if ($mode === 'accountant'): ?>Requisition Queues<?php else: ?>My Requisitions<?php endif; ?>
        </h5>
    </div>
    <a href="<?= PROOT; ?>ServiceRequisitions" class="btn btn-sm btn-outline-secondary"><i class="fas fa-list me-1"></i> All requisitions</a>
</div>

<?php if ($mode === 'accountant'):
    $issueRows  = $this->data['issue_rows']  ?? [];
    $retireRows = $this->data['retire_rows'] ?? [];
    $closeRows  = $this->data['close_rows']  ?? [];
?>

<!-- Issue queue -->
<div class="card border-0 shadow-sm mb-3">
    <div class="card-header bg-white d-flex align-items-center justify-content-between">
        <div><strong>Ready to Issue</strong>
            <span class="text-muted small ms-2"><?= count($issueRows); ?> awaiting cash release · older first</span>
        </div>
        <span class="badge" style="background:#22c55e;color:#fff;"><?= count($issueRows); ?></span>
    </div>
    <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 style="width:130px;">Approved on</th>
                    <th style="width:100px;" class="text-end">Action</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($issueRows as $r): ?>
                    <tr>
                        <td><code><?= htmlspecialchars($r['req_no']); ?></code></td>
                        <td><strong><?= htmlspecialchars($r['project_no']); ?></strong><div class="text-muted small"><?= htmlspecialchars($r['project_name']); ?></div></td>
                        <td><?= htmlspecialchars($r['title']); ?><div class="text-muted small">by <?= htmlspecialchars($r['created_by_name']); ?></div></td>
                        <td class="text-end"><strong><?= number_format((float)$r['total_amount']); ?></strong></td>
                        <td class="text-muted small"><?= htmlspecialchars($r['approved_on'] ?? '—'); ?></td>
                        <td class="text-end"><a href="<?= PROOT; ?>ServiceRequisitions/view/<?= (int)$r['c0d']; ?>" class="btn btn-sm btn-success"><i class="fas fa-hand-holding-usd me-1"></i> Issue</a></td>
                    </tr>
                <?php endforeach; ?>
                <?php if (empty($issueRows)): ?>
                    <tr><td colspan="6" class="text-center text-muted py-3">Nothing to issue right now.</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<!-- Retirement review queue -->
<div class="card border-0 shadow-sm">
    <div class="card-header bg-white d-flex align-items-center justify-content-between">
        <div><strong>Retirement Reviews</strong>
            <span class="text-muted small ms-2"><?= count($retireRows); ?> submitted for approval</span>
        </div>
        <span class="badge" style="background:#a78bfa;color:#fff;"><?= count($retireRows); ?></span>
    </div>
    <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">Issued</th>
                    <th class="text-end">Retired</th>
                    <th class="text-end">Variance</th>
                    <th style="width:130px;">Submitted on</th>
                    <th style="width:100px;" class="text-end">Action</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($retireRows as $r):
                    $var = (float)$r['variance_amount'];
                    $vc = $var < 0 ? '#dc2626' : '#166534';
                ?>
                    <tr>
                        <td><code><?= htmlspecialchars($r['req_no']); ?></code></td>
                        <td><strong><?= htmlspecialchars($r['project_no']); ?></strong><div class="text-muted small"><?= htmlspecialchars($r['project_name']); ?></div></td>
                        <td><?= htmlspecialchars($r['title']); ?><div class="text-muted small">by <?= htmlspecialchars($r['created_by_name']); ?></div></td>
                        <td class="text-end"><?= number_format((float)$r['issue_amount']); ?></td>
                        <td class="text-end"><strong style="color:#0ea5e9;"><?= number_format((float)$r['retired_amount']); ?></strong></td>
                        <td class="text-end"><strong style="color:<?= $vc; ?>;"><?= number_format($var); ?></strong></td>
                        <td class="text-muted small"><?= htmlspecialchars($r['retirement_submitted_on']); ?></td>
                        <td class="text-end"><a href="<?= PROOT; ?>ServiceRequisitions/view/<?= (int)$r['c0d']; ?>" class="btn btn-sm btn-primary" style="background:#a78bfa;border-color:#a78bfa;"><i class="fas fa-search me-1"></i> Review</a></td>
                    </tr>
                <?php endforeach; ?>
                <?php if (empty($retireRows)): ?>
                    <tr><td colspan="8" class="text-center text-muted py-3">Nothing to review right now.</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<!-- Close (GL post) queue -->
<div class="card border-0 shadow-sm mt-3">
    <div class="card-header bg-white d-flex align-items-center justify-content-between">
        <div><strong>Ready to Close (post to GL)</strong>
            <span class="text-muted small ms-2"><?= count($closeRows); ?> RETIRED requisitions awaiting journal post</span>
        </div>
        <span class="badge" style="background:#334155;color:#fff;"><?= count($closeRows); ?></span>
    </div>
    <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">Retired</th>
                    <th class="text-end">Variance</th>
                    <th style="width:130px;">Retired on</th>
                    <th style="width:100px;" class="text-end">Action</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($closeRows as $r):
                    $var = (float)$r['variance_amount'];
                    $vc = $var < 0 ? '#dc2626' : '#166534';
                ?>
                    <tr>
                        <td><code><?= htmlspecialchars($r['req_no']); ?></code></td>
                        <td><strong><?= htmlspecialchars($r['project_no']); ?></strong><div class="text-muted small"><?= htmlspecialchars($r['project_name']); ?></div></td>
                        <td><?= htmlspecialchars($r['title']); ?></td>
                        <td class="text-end"><strong><?= number_format((float)$r['retired_amount']); ?></strong></td>
                        <td class="text-end"><strong style="color:<?= $vc; ?>;"><?= number_format($var); ?></strong></td>
                        <td class="text-muted small"><?= htmlspecialchars($r['retired_on'] ?? '—'); ?></td>
                        <td class="text-end"><a href="<?= PROOT; ?>ServiceRequisitions/view/<?= (int)$r['c0d']; ?>" class="btn btn-sm btn-dark"><i class="fas fa-file-invoice-dollar me-1"></i> Close</a></td>
                    </tr>
                <?php endforeach; ?>
                <?php if (empty($closeRows)): ?>
                    <tr><td colspan="7" class="text-center text-muted py-3">Nothing to close right now.</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<?php else:
    /* PC / originator mode */
    $rows = $this->data['rows'] ?? [];
?>
<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: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><strong><?= htmlspecialchars($r['project_no']); ?></strong><div class="text-muted small"><?= htmlspecialchars($r['project_name']); ?></div></td>
                        <td><?= htmlspecialchars($r['title']); ?></td>
                        <td class="text-end"><strong><?= number_format((float)$r['total_amount']); ?></strong></td>
                        <td><span class="badge" style="background:<?= $sp[0]; ?>;color:#fff;"><?= htmlspecialchars($sp[1]); ?></span></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="6" class="text-center text-muted py-4">You have not raised any requisitions yet.</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
<?php endif; ?>
<?php $this->end(); ?>
