<?php $this->start('body');
    $rows  = $this->data['rows']  ?? [];
    $types = $this->data['types'] ?? [];
    $statusChip = [
        'DRAFT'     => ['#F1F5F9','#334155','Draft'],
        'SUBMITTED' => ['#EEF2F6','#7A8BA1','Submitted'],
        'APPROVED'  => ['#EAF2E8','#3F5C43','Approved'],
        'REJECTED'  => ['#F5EAE1','#C48579','Bounced back'],
    ];
?>

<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-list me-2 text-muted"></i>My retirements</h5>
        <div class="text-muted small mt-1" style="font-size:.72rem;">
            Retirements you've drafted or submitted — bounced-back ones can be edited and re-submitted.
        </div>
    </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>LPO #</th>
                    <th>Type</th>
                    <th>Project</th>
                    <th class="text-end">Advance</th>
                    <th class="text-end">Spent</th>
                    <th class="text-end">Returned</th>
                    <th class="text-end">Variance</th>
                    <th>Status</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($rows as $r):
                    $lbl = $types[$r['lpo_type']]['label'] ?? $r['lpo_type'];
                    $ico = $types[$r['lpo_type']]['icon']  ?? 'fas fa-wallet';
                    $sc  = $statusChip[$r['status']] ?? ['#F1F5F9','#334155',$r['status']];
                    $bal = abs((float)$r['variance']) < 0.005;
                    $canEdit = in_array($r['status'], ['DRAFT','REJECTED'], true);
                ?>
                    <tr>
                        <td><strong style="color:#7A8BA1;"><?= htmlspecialchars($r['lpo_no']); ?></strong></td>
                        <td class="small"><i class="<?= $ico; ?> me-1" style="color:#94A3B8;"></i><?= htmlspecialchars($lbl); ?></td>
                        <td class="small text-muted"><?= !empty($r['project_no']) ? htmlspecialchars($r['project_no']) : '<em>Central</em>'; ?></td>
                        <td class="text-end small" style="font-weight:600;color:#334155;"><?= htmlspecialchars($r['currency']); ?> <?= number_format((float)$r['lpo_amount']); ?></td>
                        <td class="text-end small"><?= number_format((float)$r['total_spent']); ?></td>
                        <td class="text-end small"><?= number_format((float)$r['cash_returned']); ?></td>
                        <td class="text-end small">
                            <span class="badge" style="background:<?= $bal ? '#EAF2E8' : '#F5EAE1'; ?>;color:<?= $bal ? '#3F5C43' : '#C48579'; ?>;border:1px solid <?= $bal ? '#3F5C43' : '#C48579'; ?>;font-size:.62rem;">
                                <?= number_format((float)$r['variance']); ?>
                            </span>
                        </td>
                        <td><span class="badge" style="background:<?= $sc[0]; ?>;color:<?= $sc[1]; ?>;border:1px solid <?= $sc[1]; ?>;font-size:.62rem;text-transform:capitalize;"><?= htmlspecialchars($sc[2]); ?></span></td>
                        <td class="text-end">
                            <?php if ($canEdit): ?>
                                <a href="<?= PROOT; ?>Retirements/new/<?= (int)$r['lpo_id']; ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-pen"></i></a>
                            <?php else: ?>
                                <a href="<?= PROOT; ?>Retirements/view/<?= (int)$r['c0d']; ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-right"></i></a>
                            <?php endif; ?>
                        </td>
                    </tr>
                <?php endforeach; ?>
                <?php if (empty($rows)): ?>
                    <tr><td colspan="9" class="text-center text-muted py-4" style="font-size:.72rem;">You have no retirements yet. Open a paid cash advance from <a href="<?= PROOT; ?>CashRequests/my">My cash requests</a> to start one.</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

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