<?php $this->start('body');
    $rows = $this->data['rows'] ?? [];
    $statusPill = [
        'DRAFT'      => ['#f59e0b', 'Draft'],
        'SUBMITTED'  => ['#0ea5e9', 'Submitted'],
        'APPROVED'   => ['#22c55e', 'Approved'],
        '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-file-signature me-1 text-muted"></i>
            Interim Payment Certificates · <?= count($rows); ?>
        </h5>
        <div class="text-muted" style="font-size:.72rem;">Customer-side IPCs — DRAFT + SUBMITTED surface first.</div>
    </div>
</div>

<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>IPC</th>
                    <th>Project</th>
                    <th style="width:110px;">Date</th>
                    <th class="text-end">Certified gross</th>
                    <th class="text-end">Net</th>
                    <th class="text-end">Invoice total</th>
                    <th style="width:110px;">Status</th>
                    <th style="width:110px;">Sale</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['ipc_no']); ?></code></td>
                    <td class="small">
                        <?= htmlspecialchars($r['project_no']); ?>
                        <div class="text-muted"><?= htmlspecialchars($r['project_name']); ?></div>
                    </td>
                    <td class="small text-muted"><?= htmlspecialchars($r['ipc_date']); ?></td>
                    <td class="text-end"><?= number_format((float)$r['certified_gross']); ?></td>
                    <td class="text-end"><strong><?= number_format((float)$r['net_amount']); ?></strong></td>
                    <td class="text-end"><?= number_format((float)$r['invoice_total']); ?></td>
                    <td><span class="badge" style="background:<?= $sp[0]; ?>;color:#fff;text-transform:capitalize;"><?= htmlspecialchars($sp[1]); ?></span></td>
                    <td class="small">
                        <?php if ((int)$r['sale_id'] > 0): ?>
                            <a href="<?= PROOT; ?>Sales/view/<?= (int)$r['sale_id']; ?>">#<?= (int)$r['sale_id']; ?></a>
                        <?php else: ?>
                            <span class="text-muted">—</span>
                        <?php endif; ?>
                    </td>
                    <td class="text-end"><a href="<?= PROOT; ?>WorksIpcs/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">No IPCs raised yet.</td></tr>
            <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
<?php $this->end(); ?>
