<?php $this->start('body');
    $p      = $this->data['project'];
    $boq    = $this->data['boq'];
    $items  = $this->data['items']  ?? [];
    $totals = $this->data['totals'] ?? [];

    // Group items by section for readability
    $sections = [];
    foreach ($items as $it) {
        $sec = $it['section'] ?: 'Unclassified';
        $sections[$sec][] = $it;
    }
?>
<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-invoice me-1 text-muted"></i>
            Contract BOQ · <?= 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>
        </div>
    </div>
    <div>
        <a href="<?= PROOT; ?>WorksProjectReport/view/<?= (int)$p['c0d']; ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-chart-line me-1"></i> Report</a>
        <a href="<?= PROOT; ?>WorksIpcs/list/<?= (int)$p['c0d']; ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-file-signature me-1"></i> IPCs</a>
        <a href="<?= PROOT; ?>WorksPhases/plan/<?= (int)$p['c0d']; ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-tasks me-1"></i> Phases</a>
        <a href="<?= PROOT; ?>ProjectStarter/index/<?= (int)$p['c0d']; ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-left me-1"></i> Checklist</a>
    </div>
</div>

<?php if (!$boq): ?>
    <div class="alert py-3" style="background:#F8FAFC;border:1px solid #E2E8F0;color:#334155;">
        <i class="fas fa-info-circle me-1" style="color:#7A8BA1;"></i>
        <strong>No BOQ snapshot.</strong> The contract BOQ is auto-created from the approved tender pricing at project provisioning. If this project was migrated or has no pricing version, no BOQ exists.
    </div>
<?php else: ?>

<!-- Totals bar -->
<div class="card border-0 shadow-sm mb-3">
    <div class="card-body p-3 d-flex align-items-center justify-content-between">
        <div>
            <div style="text-transform:uppercase;letter-spacing:.4px;font-size:.66rem;color:#64748B;">Contract value</div>
            <div style="color:#334155;font-size:1.2rem;font-weight:700;">
                <?= htmlspecialchars($boq['currency']); ?> <?= number_format((float)$boq['contract_value']); ?>
            </div>
            <?php if ((float)$boq['variation_value'] > 0): ?>
            <div class="text-muted small">including variations · <?= number_format((float)$boq['variation_value']); ?></div>
            <?php endif; ?>
        </div>
        <div>
            <div style="text-transform:uppercase;letter-spacing:.4px;font-size:.66rem;color:#64748B;">Items</div>
            <div style="color:#334155;font-size:1.2rem;font-weight:700;"><?= (int)($totals['item_count'] ?? 0); ?></div>
            <div class="text-muted small"><?= count($sections); ?> section<?= count($sections) === 1 ? '' : 's' ?></div>
        </div>
        <div>
            <div style="text-transform:uppercase;letter-spacing:.4px;font-size:.66rem;color:#64748B;">Source</div>
            <div class="text-muted small"><?= htmlspecialchars($boq['snapshot_source']); ?></div>
            <?php if ((int)$boq['snapshot_pricing_version_id'] > 0): ?>
                <div class="text-muted small">Pricing v#<?= (int)$boq['snapshot_pricing_version_id']; ?></div>
            <?php endif; ?>
        </div>
    </div>
</div>

<!-- Section-grouped items -->
<?php foreach ($sections as $sec => $rows): ?>
<div class="card border-0 shadow-sm mb-3">
    <div class="card-header bg-white d-flex justify-content-between align-items-center">
        <strong><?= htmlspecialchars($sec); ?></strong>
        <span class="text-muted small"><?= count($rows); ?> line<?= count($rows) === 1 ? '' : 's' ?></span>
    </div>
    <div class="card-body p-0">
        <table class="table table-sm mb-0 align-middle">
            <thead class="table-light">
                <tr>
                    <th style="width:60px;">#</th>
                    <th>Description</th>
                    <th style="width:80px;">Unit</th>
                    <th style="width:110px;" class="text-end">Qty</th>
                    <th style="width:120px;" class="text-end">Rate</th>
                    <th style="width:140px;" class="text-end">Amount</th>
                    <th style="width:100px;" class="text-end">Origin</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($rows as $it):
                    $isVar = $it['origin'] === 'VARIATION';
                ?>
                    <tr>
                        <td class="text-muted"><?= htmlspecialchars($it['item_no']); ?></td>
                        <td><?= htmlspecialchars($it['description']); ?></td>
                        <td class="small"><?= htmlspecialchars($it['unit']); ?></td>
                        <td class="text-end small"><?= number_format((float)$it['contract_qty'], 2); ?></td>
                        <td class="text-end small"><?= number_format((float)$it['contract_rate'], 2); ?></td>
                        <td class="text-end"><strong><?= number_format((float)$it['contract_amount']); ?></strong></td>
                        <td class="text-end">
                            <?php if ($isVar): ?>
                                <span class="badge" style="background:#f59e0b;color:#fff;">Variation</span>
                            <?php else: ?>
                                <span class="text-muted small">Contract</span>
                            <?php endif; ?>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
<?php endforeach; ?>

<?php endif; ?>

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