<?php $this->start('body');
    $dn    = $this->data['dn'];
    $items = $this->data['items'] ?? [];
?>

<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-truck me-2 text-muted"></i><?= htmlspecialchars($dn['dn_no']); ?></h5>
        <div class="text-muted" style="font-size:.72rem;">
            <?= htmlspecialchars($dn['delivery_date']); ?> ·
            Client PO <a href="<?= PROOT; ?>GoodsClientPos/view/<?= (int)$dn['client_po_id']; ?>"><?= htmlspecialchars($dn['client_po_no']); ?></a>
            · <?= htmlspecialchars($dn['customer_name']); ?>
            <?php if (!empty($dn['project_no'])): ?> · <?= htmlspecialchars($dn['project_no']); ?><?php endif; ?>
        </div>
    </div>
    <a href="<?= PROOT; ?>GoodsCustomerDeliveries" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-left me-1"></i> All DNs</a>
</div>

<div class="row g-3 mb-3">
    <div class="col-md-3"><div class="card border-0 shadow-sm"><div class="card-body p-2"><div class="text-muted small">Delivered to</div><div style="font-weight:600;"><?= htmlspecialchars($dn['delivered_to']); ?></div><div class="text-muted small"><?= htmlspecialchars($dn['delivered_to_phone'] ?: '—'); ?></div></div></div></div>
    <div class="col-md-3"><div class="card border-0 shadow-sm"><div class="card-body p-2"><div class="text-muted small">Delivered by</div><div style="font-weight:600;"><?= htmlspecialchars($dn['delivered_by_name']); ?></div></div></div></div>
    <div class="col-md-3"><div class="card border-0 shadow-sm"><div class="card-body p-2"><div class="text-muted small">Signature / photo ref</div><div style="font-weight:600;"><?= htmlspecialchars($dn['delivered_to_signature'] ?: '—'); ?></div></div></div></div>
    <div class="col-md-3"><div class="card border-0 shadow-sm"><div class="card-body p-2"><div class="text-muted small">Condition</div><div>
        <?php if ((int)$dn['condition_ok']===1): ?>
            <span class="badge bg-success"><i class="fas fa-check me-1"></i>OK</span>
        <?php else: ?>
            <span class="badge bg-warning text-dark"><i class="fas fa-exclamation-triangle me-1"></i>Issues noted</span>
        <?php endif; ?>
    </div></div></div></div>
</div>

<?php if ($dn['notes']): ?>
<div class="card border-0 shadow-sm mb-3">
    <div class="card-header bg-white"><strong>Notes</strong></div>
    <div class="card-body p-3 text-muted"><?= nl2br(htmlspecialchars($dn['notes'])); ?></div>
</div>
<?php endif; ?>

<div class="card border-0 shadow-sm">
    <div class="card-header bg-white"><strong>Lines delivered</strong></div>
    <div class="table-responsive">
        <table class="table table-sm mb-0 align-middle">
            <thead class="table-light">
                <tr><th>#</th><th>Description</th><th class="text-end">PO qty</th><th class="text-end">Delivered</th><th>Condition notes</th></tr>
            </thead>
            <tbody>
                <?php foreach ($items as $it): ?>
                    <tr>
                        <td class="text-muted"><?= (int)($it['item_no'] ?? 0); ?></td>
                        <td><?= htmlspecialchars($it['description']); ?></td>
                        <td class="text-end text-muted"><?= number_format((float)$it['ordered_qty'], (fmod((float)$it['ordered_qty'],1)==0?0:2)); ?></td>
                        <td class="text-end" style="font-weight:600;"><?= number_format((float)$it['qty_delivered'], (fmod((float)$it['qty_delivered'],1)==0?0:2)); ?></td>
                        <td class="text-muted small"><?= htmlspecialchars($it['condition_notes'] ?: '—'); ?></td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>

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