<?php $this->start('body');
    $p   = $this->data['project'];
    $pos = $this->data['pos'] ?? [];
    $statusPill = [
        'DRAFT'      => ['#f59e0b', 'Draft'],
        'CONFIRMED'  => ['#22c55e', 'Confirmed'],
        'SOURCING'   => ['#0ea5e9', 'Sourcing'],
        'INVOICING'  => ['#a78bfa', 'Invoicing'],
        'CLOSED'     => ['#94a3b8', 'Closed'],
        'CANCELLED'  => ['#dc2626', '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-broom me-1 text-muted"></i>
            PC Scrub · <?= htmlspecialchars($p['project_no']); ?> · <?= htmlspecialchars($p['project_name']); ?>
        </h5>
        <div class="text-muted" style="font-size:.72rem;">
            <?= htmlspecialchars($p['project_customer']); ?> ·
            <span style="text-transform:capitalize;"><?= htmlspecialchars($p['project_type']); ?></span> ·
            <?= htmlspecialchars($p['currency']); ?> <?= number_format((float)$p['contract_value']); ?>
        </div>
    </div>
    <a href="<?= PROOT; ?>ProjectStarter/index/<?= (int)$p['c0d']; ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-left me-1"></i> Back to checklist</a>
</div>

<div class="card border-0 shadow-sm">
    <div class="card-header bg-white"><strong>Client POs on this project</strong></div>
    <div class="card-body p-0">
        <table class="table table-sm mb-0 align-middle">
            <thead class="table-light">
                <tr>
                    <th>PO #</th>
                    <th>Customer</th>
                    <th class="text-end">Sell (net)</th>
                    <th class="text-end">Total</th>
                    <th>Status</th>
                    <th>Scrubbed</th>
                    <th class="text-end">Action</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($pos as $po):
                    $st = $statusPill[$po['status']] ?? ['#94a3b8', $po['status']];
                    $isScrubbed = !empty($po['scrubbed_at']);
                ?>
                    <tr>
                        <td><strong><?= htmlspecialchars($po['client_po_no']); ?></strong></td>
                        <td class="text-muted small"><?= htmlspecialchars($po['customer_name']); ?></td>
                        <td class="text-end"><?= number_format((float)$po['subtotal_sell']); ?></td>
                        <td class="text-end"><strong><?= number_format((float)$po['total']); ?></strong></td>
                        <td><span class="badge" style="background:<?= $st[0]; ?>;color:#fff;text-transform:capitalize;"><?= htmlspecialchars($st[1]); ?></span></td>
                        <td>
                            <?php if ($isScrubbed): ?>
                                <div class="small">by <?= htmlspecialchars($po['scrubbed_by_name']); ?></div>
                                <div class="text-muted small"><?= htmlspecialchars($po['scrubbed_at']); ?></div>
                            <?php else: ?>
                                <span class="text-muted small">—</span>
                            <?php endif; ?>
                        </td>
                        <td class="text-end">
                            <a href="<?= PROOT; ?>GoodsScrub/scrubItem/<?= (int)$po['c0d']; ?>"
                               class="btn btn-sm <?= $isScrubbed ? 'btn-outline-secondary' : 'btn-primary'; ?>">
                                <i class="fas <?= $isScrubbed ? 'fa-eye' : 'fa-broom'; ?> me-1"></i>
                                <?= $isScrubbed ? 'Review' : 'Scrub items'; ?>
                            </a>
                        </td>
                    </tr>
                <?php endforeach; ?>
                <?php if (empty($pos)): ?>
                    <tr><td colspan="7" class="text-center text-muted py-4">No client POs on this project — check tender pricing snapshot ran, or add one manually.</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
<?php $this->end(); ?>
