<?php $this->start('body'); ?>

<div class="d-flex align-items-center justify-content-between mb-3">
    <h5 class="mb-0" style="font-weight:600;"><i class="fas fa-truck-loading me-2 text-muted"></i>Goods Received Notes</h5>
    <a href="<?= PROOT; ?>Lpos?status=APPROVED" class="btn btn-sm btn-outline-secondary"><i class="fas fa-search me-1"></i> Find approved LPO to receive</a>
</div>

<div class="card border-0 shadow-sm">
    <div class="table-responsive">
        <table class="table table-sm mb-0 align-middle">
            <thead class="table-light">
                <tr>
                    <th>GRN #</th><th>Date</th><th>LPO</th><th>Supplier</th><th>Delivery ref</th>
                    <th class="text-end">Lines</th><th class="text-end">Qty received</th>
                    <th>Condition</th><th>Receipt status</th><th></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($this->data['rows'] as $r):
                    $rs = ['NOT_RECEIVED'=>'bg-light text-muted','PARTIALLY_RECEIVED'=>'bg-warning text-dark','FULLY_RECEIVED'=>'bg-success','OVER_RECEIVED'=>'bg-danger'][$r['receipt_status']] ?? 'bg-light text-muted';
                ?>
                    <tr>
                        <td><a href="<?= PROOT; ?>GoodsGrn/view/<?= (int)$r['c0d']; ?>" class="text-decoration-none"><strong><?= htmlspecialchars($r['grn_no']); ?></strong></a></td>
                        <td class="text-muted small"><?= htmlspecialchars($r['received_on']); ?></td>
                        <td><a href="<?= PROOT; ?>Lpos/view/<?= (int)($r['c0d']); ?>" class="text-decoration-none text-body"><?= htmlspecialchars($r['lpo_no']); ?></a></td>
                        <td><?= htmlspecialchars($r['payee_name']); ?></td>
                        <td class="text-muted small"><?= htmlspecialchars($r['supplier_delivery_note_ref'] ?: '—'); ?></td>
                        <td class="text-end text-muted"><?= (int)$r['line_count']; ?></td>
                        <td class="text-end"><?= number_format((float)$r['qty_received_total'], 0); ?></td>
                        <td>
                            <?php if ((int)$r['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</span>
                            <?php endif; ?>
                        </td>
                        <td><span class="badge <?= $rs; ?>" style="text-transform:capitalize;"><?= htmlspecialchars(strtolower(str_replace('_',' ',$r['receipt_status']))); ?></span></td>
                        <td class="text-end"><a href="<?= PROOT; ?>GoodsGrn/view/<?= (int)$r['c0d']; ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-right"></i></a></td>
                    </tr>
                <?php endforeach; ?>
                <?php if (empty($this->data['rows'])): ?>
                    <tr><td colspan="10" class="text-center text-muted py-4">No GRNs yet. Open an APPROVED supplier LPO and click "Record GRN".</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

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