<?php $this->start('body');
    $grn   = $this->data['grn'];
    $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-loading me-2 text-muted"></i><?= htmlspecialchars($grn['grn_no']); ?></h5>
        <div class="text-muted" style="font-size:.72rem;">
            <?= htmlspecialchars($grn['received_on']); ?> ·
            LPO <a href="<?= PROOT; ?>Lpos/view/<?= (int)$grn['lpo_id']; ?>"><?= htmlspecialchars($grn['lpo_no']); ?></a>
            · <?= htmlspecialchars($grn['payee_name']); ?>
            <?php if (!empty($grn['project_no'])): ?> · <?= htmlspecialchars($grn['project_no']); ?><?php endif; ?>
        </div>
    </div>
    <a href="<?= PROOT; ?>GoodsGrn" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-left me-1"></i> All GRNs</a>
</div>

<div class="row g-3 mb-3">
    <div class="col-md-4"><div class="card border-0 shadow-sm"><div class="card-body p-2"><div class="text-muted small">Supplier delivery ref</div><div style="font-weight:600;"><?= htmlspecialchars($grn['supplier_delivery_note_ref'] ?: '—'); ?></div></div></div></div>
    <div class="col-md-4"><div class="card border-0 shadow-sm"><div class="card-body p-2"><div class="text-muted small">Received by</div><div style="font-weight:600;"><?= htmlspecialchars($grn['received_by_name']); ?></div></div></div></div>
    <div class="col-md-4"><div class="card border-0 shadow-sm"><div class="card-body p-2"><div class="text-muted small">Condition</div><div>
        <?php if ((int)$grn['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 ($grn['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($grn['notes'])); ?></div>
</div>
<?php endif; ?>

<div class="card border-0 shadow-sm">
    <div class="card-header bg-white"><strong>Lines received</strong></div>
    <div class="table-responsive">
        <table class="table table-sm mb-0 align-middle">
            <thead class="table-light">
                <tr><th>Description</th><th class="text-end">LPO qty</th><th class="text-end">Received</th><th class="text-end">Rejected</th><th>Condition notes</th></tr>
            </thead>
            <tbody>
                <?php foreach ($items as $it): ?>
                    <tr>
                        <td><?= htmlspecialchars($it['description']); ?></td>
                        <td class="text-end text-muted"><?= number_format((float)$it['lpo_qty'], (fmod((float)$it['lpo_qty'],1)==0?0:2)); ?></td>
                        <td class="text-end" style="font-weight:600;"><?= number_format((float)$it['qty_received'], (fmod((float)$it['qty_received'],1)==0?0:2)); ?></td>
                        <td class="text-end" style="color:<?= (float)$it['qty_rejected']>0?'#ef4444':'#94a3b8'; ?>;"><?= number_format((float)$it['qty_rejected'], (fmod((float)$it['qty_rejected'],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(); ?>
