<?php $this->start('body');
    $types = $this->data['types'] ?? [];
    $stat  = $this->v('status');
    $statuses = ['DRAFT','SUBMITTED','APPROVED','ISSUED','PAID','RETIRED','CLOSED','DECLINED'];
?>

<div class="d-flex align-items-center justify-content-between mb-3">
    <h5 class="mb-0" style="font-weight:600;"><i class="fas fa-file-invoice me-2 text-muted"></i>LPOs · unified queue</h5>
    <a href="<?= PROOT; ?>PettyCash/raise" class="btn btn-sm btn-primary" style="background:<?= BRAND_COLOR; ?>;border-color:<?= BRAND_COLOR; ?>;">
        <i class="fas fa-plus me-1"></i> Raise Petty Cash
    </a>
</div>

<!-- Status filter chips -->
<div class="mb-2 d-flex flex-wrap gap-1">
    <a href="<?= PROOT; ?>Lpos" class="badge <?= $stat===''?'bg-dark':'bg-light text-muted'; ?>" style="text-decoration:none;padding:.35em .6em;">All</a>
    <?php foreach ($statuses as $s):
        $active = ($stat === $s);
        $cls    = $active ? 'bg-dark' : 'bg-light text-muted';
    ?>
        <a href="<?= PROOT; ?>Lpos?status=<?= $s; ?>" class="badge <?= $cls; ?>" style="text-decoration:none;padding:.35em .6em;text-transform:capitalize;">
            <?= htmlspecialchars(strtolower(str_replace('_',' ',$s))); ?>
        </a>
    <?php endforeach; ?>
</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>LPO #</th><th>Type</th><th>Project</th><th>Payee</th>
                    <th class="text-end">Amount</th><th>Purpose</th><th>Status</th><th>Requested by</th><th></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($this->data['rows'] as $r):
                    $t   = $types[$r['lpo_type']] ?? ['label'=>$r['lpo_type'], 'icon'=>'fas fa-file'];
                    $sc  = [
                        'DRAFT'     => 'bg-light text-muted',
                        'SUBMITTED' => 'bg-info text-white',
                        'APPROVED'  => 'bg-success',
                        'ISSUED'    => 'bg-primary',
                        'PAID'      => 'bg-success',
                        'RETIRED'   => 'bg-secondary',
                        'CLOSED'    => 'bg-dark',
                        'DECLINED'  => 'bg-danger',
                    ][$r['cash_out_status']] ?? 'bg-light text-muted';
                ?>
                    <tr>
                        <td><a href="<?= PROOT; ?>Lpos/view/<?= (int)$r['c0d']; ?>" class="text-decoration-none"><strong><?= htmlspecialchars($r['lpo_no']); ?></strong></a></td>
                        <td><i class="<?= $t['icon']; ?> me-1 text-muted"></i><?= htmlspecialchars($t['label']); ?></td>
                        <td class="text-muted small"><?= htmlspecialchars($r['project_no'] ?: '(central)'); ?></td>
                        <td><?= htmlspecialchars($r['payee_name'] ?: '—'); ?></td>
                        <td class="text-end"><?= htmlspecialchars($r['currency']); ?> <?= number_format((float)$r['amount']); ?></td>
                        <td class="text-muted small" style="max-width:280px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;" title="<?= htmlspecialchars($r['purpose']); ?>"><?= htmlspecialchars($r['purpose']); ?></td>
                        <td><span class="badge <?= $sc; ?>" style="text-transform:capitalize;"><?= htmlspecialchars(strtolower(str_replace('_',' ',$r['cash_out_status']))); ?></span></td>
                        <td class="text-muted small"><?= htmlspecialchars($r['requested_by_name']); ?></td>
                        <td class="text-end"><a href="<?= PROOT; ?>Lpos/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($this->data['rows'])): ?>
                    <tr><td colspan="9" class="text-center text-muted py-4">No LPOs match this filter. <a href="<?= PROOT; ?>PettyCash/raise">Raise a Petty Cash</a> to start.</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

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