<?php $this->start('body');
    $rows   = $this->data['rows']   ?? [];
    $status = (string)($this->data['status'] ?? '');
    $statuses = ['DRAFT','SUBMITTED','PC_CONFIRMED','PM_REVIEWED','SCHEDULED','PAID','REJECTED','CANCELLED'];
    $statusChip = [
        'DRAFT'        => ['#F1F5F9','#334155','Draft'],
        'SUBMITTED'    => ['#EEF2F6','#7A8BA1','Submitted'],
        'PC_CONFIRMED' => ['#EEF2F6','#7A8BA1','PC confirmed'],
        'PM_REVIEWED'  => ['#EEF2F6','#7A8BA1','PM reviewed'],
        'SCHEDULED'    => ['#FEF3E7','#8B6914','Scheduled'],
        'PAID'         => ['#EAF2E8','#3F5C43','Paid'],
        'REJECTED'     => ['#F5EAE1','#C48579','Rejected'],
        'CANCELLED'    => ['#F1F5F9','#334155','Cancelled'],
    ];
    // Totals
    $paidTot = 0.0; $pendingTot = 0.0; $rejTot = 0.0;
    foreach ($rows as $r) {
        $amt = (float)$r['requested_amount'];
        if ($r['status'] === 'PAID')                             $paidTot    += $amt;
        elseif (in_array($r['status'], ['REJECTED','CANCELLED'], true)) $rejTot += $amt;
        else                                                     $pendingTot += $amt;
    }
?>

<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-money-check-alt me-2 text-muted"></i>My Payment Requests</h5>
        <div class="text-muted small mt-1" style="font-size:.72rem;"><?= count($rows); ?> total &middot; raised by you</div>
    </div>
</div>

<div class="row g-2 mb-3">
    <div class="col-md-4"><div class="card border-0 shadow-sm"><div class="card-body p-2">
        <div class="text-muted small" style="font-size:.62rem;text-transform:capitalize;">In flight</div>
        <div style="font-weight:700;font-size:1.05rem;color:#8B6914;"><?= number_format($pendingTot); ?></div>
    </div></div></div>
    <div class="col-md-4"><div class="card border-0 shadow-sm"><div class="card-body p-2">
        <div class="text-muted small" style="font-size:.62rem;text-transform:capitalize;">Paid</div>
        <div style="font-weight:700;font-size:1.05rem;color:#3F5C43;"><?= number_format($paidTot); ?></div>
    </div></div></div>
    <div class="col-md-4"><div class="card border-0 shadow-sm"><div class="card-body p-2">
        <div class="text-muted small" style="font-size:.62rem;text-transform:capitalize;">Rejected / cancelled</div>
        <div style="font-weight:700;font-size:1.05rem;color:#C48579;"><?= number_format($rejTot); ?></div>
    </div></div></div>
</div>

<div class="mb-2 d-flex flex-wrap gap-1">
    <a href="<?= PROOT; ?>PaymentRequests/my" class="badge <?= $status===''?'bg-dark':'bg-light text-muted'; ?>" style="text-decoration:none;padding:.35em .6em;">All</a>
    <?php foreach ($statuses as $s): ?>
        <a href="<?= PROOT; ?>PaymentRequests/my?status=<?= $s; ?>" class="badge <?= $status===$s?'bg-dark':'bg-light text-muted'; ?>" 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>PR #</th>
                    <th>LPO</th>
                    <th>Project</th>
                    <th>Payee</th>
                    <th class="text-end">Requested</th>
                    <th>Raised on</th>
                    <th>Status</th>
                    <th>Next / Outcome</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($rows as $r):
                    $sc = $statusChip[$r['status']] ?? ['#F1F5F9','#334155',$r['status']];
                    $outcome = '';
                    if ($r['status'] === 'PAID') {
                        $outcome = 'Ref <code>' . htmlspecialchars((string)($r['acct_disburse_ref'] ?? '')) . '</code>';
                    } elseif ($r['status'] === 'REJECTED') {
                        $outcome = '<span class="text-muted" style="font-size:.62rem;">' . htmlspecialchars(mb_strimwidth((string)($r['rejected_reason'] ?? ''), 0, 40, '…')) . '</span>';
                    } elseif (!empty($r['next_check_status'])) {
                        $nextLbl = ['PC_CONFIRM'=>'PC confirm','PM_REVIEW'=>'PM review','FM_SCHEDULE'=>'FM schedule','ACCT_RECORD'=>'Accountant record'][$r['next_check_status']] ?? $r['next_check_status'];
                        $outcome = '<span class="text-muted" style="font-size:.65rem;">' . htmlspecialchars($nextLbl) . '</span>';
                    }
                ?>
                    <tr>
                        <td><a href="<?= PROOT; ?>PaymentRequests/view/<?= (int)$r['c0d']; ?>" class="text-decoration-none"><strong><?= htmlspecialchars($r['pr_no']); ?></strong></a>
                            <?php if (!empty($r['supplier_invoice_no'])): ?><div class="text-muted" style="font-size:.6rem;">Inv <?= htmlspecialchars((string)$r['supplier_invoice_no']); ?></div><?php endif; ?>
                        </td>
                        <td class="small"><?= htmlspecialchars((string)$r['lpo_no']); ?>
                            <?php if (!empty($r['cash_out_status'])): ?><div class="text-muted" style="font-size:.6rem;text-transform:capitalize;"><?= htmlspecialchars(strtolower(str_replace('_',' ', (string)$r['cash_out_status']))); ?></div><?php endif; ?>
                        </td>
                        <td class="small text-muted"><?= htmlspecialchars((string)($r['project_no'] ?? '—')); ?></td>
                        <td class="small"><?= htmlspecialchars((string)($r['payee_name'] ?? '')); ?></td>
                        <td class="text-end small" style="font-weight:600;color:#334155;"><?= htmlspecialchars((string)$r['currency']); ?> <?= number_format((float)$r['requested_amount']); ?></td>
                        <td class="small text-muted"><?= htmlspecialchars((string)$r['raised_on']); ?></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="small"><?= $outcome; ?></td>
                        <td class="text-end"><a href="<?= PROOT; ?>PaymentRequests/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="9" class="text-center text-muted py-4">
                        <?php if ($status !== ''): ?>
                            No PRs with status <em><?= htmlspecialchars(strtolower(str_replace('_',' ', $status))); ?></em>.
                        <?php else: ?>
                            You haven't raised any Payment Requests yet. Open a SUPPLIER LPO with payment terms and click <strong>Raise Payment Request</strong>.
                        <?php endif; ?>
                    </td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

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