<?php $this->start('body');
    $items         = $this->data['items'] ?? [];
    $total         = (int)($this->data['total'] ?? 0);
    $page          = (int)($this->data['page'] ?? 1);
    $perPage       = (int)($this->data['per_page'] ?? 25);
    $pages         = (int)($this->data['pages'] ?? 1);
    $chainKey      = (string)($this->data['chain_key']   ?? '');
    $entityType    = (string)($this->data['entity_type'] ?? '');
    $qSearch       = (string)($this->data['q']           ?? '');
    $chainOptions  = $this->data['chain_options']  ?? [];
    $entityOptions = $this->data['entity_options'] ?? [];

    // Nice labels for entity_type chips
    $entityLabel = [
        'lpo'                      => 'Cash-out LPO',
        'payment_schedule'         => 'Payment Schedule',
        'payee_account'            => 'Payee Account',
        'crm_project_onboarding'   => 'CRM Onboarding',
        'goods_project_onboarding' => 'Goods Onboarding',
        'goods_rfq'                => 'RFQ',
        'TENDER_CONTRACT'          => 'Tender Contract',
        'TENDER_PRICING'           => 'Tender Pricing',
    ];

    function rsQueueLink(string $chainKey, string $entityType, string $q, int $page): string {
        $qs = [];
        if ($chainKey !== '')   $qs['chain_key']   = $chainKey;
        if ($entityType !== '') $qs['entity_type'] = $entityType;
        if ($q !== '')          $qs['q']           = $q;
        if ($page > 1)          $qs['page']        = $page;
        return PROOT . 'FinChains/myQueue' . (empty($qs) ? '' : '?' . http_build_query($qs));
    }
?>

<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-2 text-muted"></i>My Approval Queue</h5>
        <div class="text-muted small mt-1" style="font-size:.72rem;">
            <?= $total; ?> item<?= $total === 1 ? '' : 's'; ?> awaiting your decision
            <?php if ($chainKey !== '' || $entityType !== ''): ?> · filtered<?php endif; ?>
        </div>
    </div>
    <a href="<?= PROOT; ?>FinChains/inFlight" class="btn btn-sm btn-outline-secondary"><i class="fas fa-stream me-1"></i>All in-flight</a>
</div>

<!-- Search + filter chips (chain + entity) — single row, wraps on narrow viewports -->
<?php
    $chipBase = 'display:inline-block;padding:.2rem .55rem;border:1px solid #E2E8F0;border-radius:3px;font-size:.66rem;line-height:1.2;text-decoration:none;white-space:nowrap;margin:2px 2px 2px 0;';
    $chipOn   = 'background:#334155;color:#fff;font-weight:600;';
    $chipOff  = 'background:#fff;color:#334155;font-weight:400;';
    $lblStyle = 'color:#64748B;font-size:.62rem;font-weight:600;text-transform:capitalize;margin-right:.3rem;';
?>
<div class="card border-0 shadow-sm mb-3">
    <div class="card-body p-2 d-flex align-items-center flex-wrap gap-2" style="background:#F8FAFC;">
        <!-- Search -->
        <form method="get" action="<?= PROOT; ?>FinChains/myQueue" class="d-flex align-items-center gap-1 mb-0" style="flex:0 0 auto;">
            <input type="hidden" name="chain_key"   value="<?= htmlspecialchars($chainKey); ?>">
            <input type="hidden" name="entity_type" value="<?= htmlspecialchars($entityType); ?>">
            <div class="input-group input-group-sm" style="width:320px;">
                <span class="input-group-text" style="background:#fff;border:1px solid #E2E8F0;color:#7A8BA1;"><i class="fas fa-search"></i></span>
                <input type="text" name="q" value="<?= htmlspecialchars($qSearch); ?>" placeholder="Search reference, chain, submitter…" class="form-control form-control-sm" style="border:1px solid #E2E8F0;">
                <button type="submit" class="btn btn-sm" style="background:#7A8BA1;border-color:#7A8BA1;color:#fff;font-weight:600;">Go</button>
                <?php if ($qSearch !== ''): ?>
                    <a href="<?= rsQueueLink($chainKey, $entityType, '', 1); ?>" class="btn btn-sm" style="background:#F1F5F9;border:1px solid #E2E8F0;color:#334155;" title="Clear search"><i class="fas fa-times"></i></a>
                <?php endif; ?>
            </div>
        </form>

        <!-- Chain group -->
        <?php if (!empty($chainOptions)): ?>
            <div class="d-flex align-items-center flex-wrap" style="flex:0 1 auto;">
                <span style="<?= $lblStyle; ?>">Chain:</span>
                <a href="<?= rsQueueLink('', $entityType, $qSearch, 1); ?>" style="<?= $chipBase . ($chainKey === '' ? $chipOn : $chipOff); ?>">All</a>
                <?php foreach ($chainOptions as $c):
                    $active = ($c['chain_key'] === $chainKey);
                ?>
                    <a href="<?= rsQueueLink($c['chain_key'], $entityType, $qSearch, 1); ?>"
                       style="<?= $chipBase . ($active ? $chipOn : $chipOff); ?>"
                       title="<?= htmlspecialchars($c['chain_key']); ?>"><?= htmlspecialchars($c['title']); ?></a>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>

        <!-- Type group -->
        <?php if (!empty($entityOptions)): ?>
            <div class="d-flex align-items-center flex-wrap" style="flex:0 1 auto;">
                <span style="<?= $lblStyle; ?>">Type:</span>
                <a href="<?= rsQueueLink($chainKey, '', $qSearch, 1); ?>" style="<?= $chipBase . ($entityType === '' ? $chipOn : $chipOff); ?>">All</a>
                <?php foreach ($entityOptions as $e):
                    $et = (string)$e['entity_type'];
                    $active = ($et === $entityType);
                    $lbl = $entityLabel[$et] ?? $et;
                ?>
                    <a href="<?= rsQueueLink($chainKey, $et, $qSearch, 1); ?>"
                       style="<?= $chipBase . ($active ? $chipOn : $chipOff); ?>"><?= htmlspecialchars($lbl); ?></a>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>

        <!-- Reset (pushed to right) -->
        <?php if ($qSearch !== '' || $chainKey !== '' || $entityType !== ''): ?>
            <a href="<?= PROOT; ?>FinChains/myQueue" class="ms-auto" style="color:#64748B;font-size:.72rem;text-decoration:none;flex:0 0 auto;"><i class="fas fa-undo me-1"></i>Reset all</a>
        <?php endif; ?>
    </div>
</div>

<div class="card border-0 shadow-sm">
    <div class="table-responsive">
        <table class="table table-sm mb-0 align-middle">
            <thead class="table-light">
                <tr>
                    <th>Chain</th><th>Reference</th><th>Step</th><th>Role gate</th>
                    <th>Submitted by</th><th>Submitted</th><th class="text-end" style="width:100px;"></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($items as $it): ?>
                    <tr>
                        <td class="text-muted small"><?= htmlspecialchars($it['chain_title']); ?></td>
                        <td class="small"><strong><?= htmlspecialchars($it['ref_label']); ?></strong>
                            <?php if (!empty($it['entity_type'])): $lbl = $entityLabel[$it['entity_type']] ?? $it['entity_type']; ?>
                                <div class="text-muted" style="font-size:.6rem;text-transform:capitalize;"><?= htmlspecialchars($lbl); ?></div>
                            <?php endif; ?>
                        </td>
                        <td><span class="badge" style="background:#EEF2F6;color:#7A8BA1;border:1px solid #7A8BA1;font-size:.62rem;"><?= htmlspecialchars($it['step_label']); ?></span></td>
                        <td class="text-muted small"><code><?= htmlspecialchars($it['role_required']); ?></code></td>
                        <td class="text-muted small"><?= htmlspecialchars($it['submitted_by_name']); ?></td>
                        <td class="text-muted small" style="font-size:.62rem;"><?= htmlspecialchars(date('d M · H:i', strtotime((string)$it['submitted_on']))); ?></td>
                        <td class="text-end">
                            <?php
                                $detailUrl = (string)($it['detail_url'] ?? (PROOT . 'FinChains/view/' . (int)$it['c0d']));
                                $isNativeDetail = strpos($detailUrl, PROOT . 'FinChains/view/') !== 0;
                            ?>
                            <a href="<?= htmlspecialchars($detailUrl); ?>" class="btn btn-sm" style="background:#7A9E7E;border-color:#7A9E7E;color:#fff;font-weight:600;" title="Open the entity detail page — the approval strip is embedded there">
                                <i class="fas fa-arrow-right me-1"></i>Open
                            </a>
                            <?php if ($isNativeDetail): ?>
                                <a href="<?= PROOT; ?>FinChains/view/<?= (int)$it['c0d']; ?>" class="btn btn-sm ms-1" style="background:transparent;border:1px solid #E2E8F0;color:#7A8BA1;font-weight:500;font-size:.66rem;" title="Open the bare approval strip (audit / step-history view)">
                                    <i class="fas fa-clipboard-check"></i>
                                </a>
                            <?php endif; ?>
                        </td>
                    </tr>
                <?php endforeach; ?>
                <?php if (empty($items)): ?>
                    <tr><td colspan="7" class="text-center text-muted py-4" style="font-size:.72rem;">Nothing in your queue right now<?= ($chainKey !== '' || $entityType !== '') ? ' matching this filter' : ''; ?>. <i class="fas fa-check-circle" style="color:#7A9E7E;"></i></td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<!-- Pagination -->
<?php if ($pages > 1): ?>
<div class="d-flex align-items-center justify-content-between mt-3 small text-muted" style="font-size:.72rem;">
    <div>Page <?= $page; ?> of <?= $pages; ?> · <?= $total; ?> total</div>
    <div>
        <?php if ($page > 1): ?>
            <a href="<?= rsQueueLink($chainKey, $entityType, $qSearch, $page - 1); ?>" class="btn btn-sm btn-outline-secondary me-1"><i class="fas fa-chevron-left me-1"></i>Prev</a>
        <?php else: ?>
            <button type="button" class="btn btn-sm btn-outline-secondary me-1" disabled><i class="fas fa-chevron-left me-1"></i>Prev</button>
        <?php endif; ?>
        <?php if ($page < $pages): ?>
            <a href="<?= rsQueueLink($chainKey, $entityType, $qSearch, $page + 1); ?>" class="btn btn-sm btn-outline-secondary">Next<i class="fas fa-chevron-right ms-1"></i></a>
        <?php else: ?>
            <button type="button" class="btn btn-sm btn-outline-secondary" disabled>Next<i class="fas fa-chevron-right ms-1"></i></button>
        <?php endif; ?>
    </div>
</div>
<?php endif; ?>

<?php $this->end(); ?>
