<?php $this->start('body');
    $p      = $this->data['project'];
    $ipcs   = $this->data['ipcs']   ?? [];
    $totals = $this->data['totals'] ?? ['gross'=>0,'held'=>0,'net'=>0,'invoiced'=>0,'count'=>0];
    $received = $this->data['received']  ?? ['received'=>0,'count'=>0];
    $arByIpc  = $this->data['ar_by_ipc'] ?? [];
    $totOutstanding = 0.0;
    foreach ($arByIpc as $a) $totOutstanding += (float)$a['outstanding'];
    $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>
            IPCs · <?= htmlspecialchars($p['project_no']); ?>
        </h5>
        <div class="text-muted" style="font-size:.72rem;">
            <?= htmlspecialchars($p['project_customer']); ?> · <?= htmlspecialchars($p['project_name']); ?>
        </div>
    </div>
    <div>
        <a href="<?= PROOT; ?>WorksIpcs/create/<?= (int)$p['c0d']; ?>" class="btn btn-sm btn-primary" style="background:#7A9E7E;border-color:#7A9E7E;">
            <i class="fas fa-plus me-1"></i> New IPC
        </a>
        <a href="<?= PROOT; ?>WorksCustomerRetention/pool/<?= (int)$p['c0d']; ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-hourglass-half me-1"></i> Customer retention</a>
        <a href="<?= PROOT; ?>WorksProjectReport/view/<?= (int)$p['c0d']; ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-chart-line me-1"></i> Project report</a>
    </div>
</div>

<div class="row g-2 mb-3">
    <div class="col-md-3">
        <div class="p-3 border rounded" style="background:#fff;">
            <div class="text-muted small" style="text-transform:uppercase;letter-spacing:.4px;font-size:.6rem;"><i class="fas fa-file-invoice me-1"></i> IPCs approved</div>
            <div style="font-weight:700;font-size:1.15rem;color:#334155;"><?= (int)$totals['count']; ?></div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="p-3 border rounded" style="background:#fff;">
            <div class="text-muted small" style="text-transform:uppercase;letter-spacing:.4px;font-size:.6rem;"><i class="fas fa-check me-1"></i> Certified (gross)</div>
            <div style="font-weight:700;font-size:1.15rem;color:#7A9E7E;"><?= number_format((float)$totals['gross']); ?></div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="p-3 border rounded" style="background:#F3E5D9;">
            <div class="text-muted small" style="text-transform:uppercase;letter-spacing:.4px;font-size:.6rem;"><i class="fas fa-lock me-1"></i> Held by customer (retention)</div>
            <div style="font-weight:700;font-size:1.15rem;color:#6B3F2A;"><?= number_format((float)$totals['held']); ?></div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="p-3 border rounded" style="background:#EAF2E8;">
            <div class="text-muted small" style="text-transform:uppercase;letter-spacing:.4px;font-size:.6rem;"><i class="fas fa-hand-holding-usd me-1"></i> Net invoiced (net + VAT)</div>
            <div style="font-weight:700;font-size:1.15rem;color:#3D5B44;"><?= number_format((float)$totals['invoiced']); ?></div>
        </div>
    </div>
</div>

<div class="row g-2 mb-3">
    <div class="col-md-6">
        <div class="p-3 border rounded" style="background:#EAF2E8;">
            <div class="text-muted small" style="text-transform:uppercase;letter-spacing:.4px;font-size:.6rem;"><i class="fas fa-check-circle me-1"></i> Received from customer</div>
            <div style="font-weight:700;font-size:1.15rem;color:#3D5B44;"><?= number_format((float)$received['received']); ?></div>
            <div class="small text-muted">across <?= (int)$received['count']; ?> receipt<?= (int)$received['count'] === 1 ? '' : 's'; ?> allocated to IPC-sourced sales</div>
        </div>
    </div>
    <div class="col-md-6">
        <?php $outBand = $totOutstanding > 0 ? ['#B87A5A','#F3E5D9','Chase'] : ['#3D5B44','#EAF2E8','All settled']; ?>
        <div class="p-3 border rounded" style="background:<?= $outBand[1]; ?>;">
            <div class="text-muted small" style="text-transform:uppercase;letter-spacing:.4px;font-size:.6rem;"><i class="fas fa-hourglass-half me-1"></i> Outstanding across all IPCs</div>
            <div style="font-weight:700;font-size:1.15rem;color:<?= $outBand[0]; ?>;"><?= number_format($totOutstanding); ?></div>
            <div class="small" style="color:<?= $outBand[0]; ?>;text-transform:capitalize;"><?= strtolower($outBand[2]); ?></div>
        </div>
    </div>
</div>

<div class="card border-0 shadow-sm">
    <div class="card-header bg-white"><strong>IPCs · <?= count($ipcs); ?></strong></div>
    <?php if (empty($ipcs)): ?>
        <div class="card-body p-3 text-muted small">No IPCs raised yet. Click <em>New IPC</em> to create the first one.</div>
    <?php else: ?>
    <div class="card-body p-0">
        <table class="table table-sm mb-0 align-middle">
            <thead class="table-light">
                <tr>
                    <th>IPC</th>
                    <th style="width:60px;">Seq</th>
                    <th style="width:110px;">Date</th>
                    <th style="width:180px;">Period</th>
                    <th class="text-end">Gross</th>
                    <th class="text-end">Net</th>
                    <th class="text-end">Invoice total</th>
                    <th class="text-end">Received</th>
                    <th class="text-end">Outstanding</th>
                    <th style="width:110px;">Status</th>
                    <th style="width:100px;">Sale</th>
                    <th style="width:80px;" class="text-end">Open</th>
                </tr>
            </thead>
            <tbody>
            <?php foreach ($ipcs as $r):
                $sp = $statusPill[$r['status']] ?? ['#94a3b8', $r['status']];
            ?>
                <tr>
                    <td><code><?= htmlspecialchars($r['ipc_no']); ?></code></td>
                    <td class="text-muted"><?= (int)$r['ipc_seq']; ?></td>
                    <td class="small text-muted"><?= htmlspecialchars($r['ipc_date']); ?></td>
                    <td class="small text-muted"><?= htmlspecialchars((string)$r['period_from']); ?> → <?= htmlspecialchars((string)$r['period_to']); ?></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>
                    <?php $arr = $arByIpc[(int)$r['c0d']] ?? ['received' => 0, 'outstanding' => 0]; ?>
                    <td class="text-end small" style="color:#3D5B44;"><?= number_format((float)$arr['received']); ?></td>
                    <td class="text-end small" style="color:<?= (float)$arr['outstanding'] > 0 ? '#6B3F2A' : '#7A8BA1'; ?>;"><?= number_format((float)$arr['outstanding']); ?></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; ?>
            </tbody>
        </table>
    </div>
    <?php endif; ?>
</div>
<?php $this->end(); ?>
