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

<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>Receive against <?= htmlspecialchars($lpo['lpo_no']); ?></h5>
        <div class="text-muted" style="font-size:.72rem;">Supplier: <?= htmlspecialchars($lpo['payee_name']); ?> · Total: <?= htmlspecialchars($lpo['currency']); ?> <?= number_format((float)$lpo['amount']); ?></div>
    </div>
    <a href="<?= PROOT; ?>Lpos/view/<?= (int)$lpo['c0d']; ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-left me-1"></i> Back to LPO</a>
</div>

<?= $this->raw('fb_msg'); ?>

<form method="post" action="<?= PROOT; ?>GoodsGrn/save" autocomplete="off">
    <input type="hidden" name="lpo_id" value="<?= (int)$lpo['c0d']; ?>">

    <div class="card border-0 shadow-sm mb-3">
        <div class="card-header bg-white"><strong>Delivery header</strong></div>
        <div class="card-body p-3">
            <div class="row g-3">
                <div class="col-md-4">
                    <label class="form-label">Received date *</label>
                    <input type="date" name="received_on" class="form-control" required value="<?= htmlspecialchars($f['received_on'] ?? date('Y-m-d')); ?>">
                </div>
                <div class="col-md-4">
                    <label class="form-label">Supplier delivery note ref</label>
                    <input type="text" name="supplier_delivery_note_ref" class="form-control" placeholder="e.g. SDN-2026-4472" value="<?= htmlspecialchars($f['supplier_delivery_note_ref'] ?? ''); ?>">
                </div>
                <div class="col-md-4">
                    <label class="form-label">Overall notes</label>
                    <input type="text" name="notes" class="form-control" value="<?= htmlspecialchars($f['notes'] ?? ''); ?>">
                </div>
            </div>
        </div>
    </div>

    <div class="card border-0 shadow-sm">
        <div class="card-header bg-white d-flex align-items-center justify-content-between">
            <strong>Line items · qty received</strong>
            <div>
                <button type="button" class="btn btn-sm btn-outline-secondary" onclick="rsReceiveAll()"><i class="fas fa-check-double me-1"></i> Receive all outstanding</button>
            </div>
        </div>
        <div class="table-responsive">
            <table class="table table-sm mb-0 align-middle">
                <thead class="table-light">
                    <tr>
                        <th style="width:40px;">#</th>
                        <th>Description</th>
                        <th class="text-end">Ordered</th>
                        <th class="text-end">Previously received</th>
                        <th class="text-end">Outstanding</th>
                        <th style="width:130px;">Qty received now</th>
                        <th style="width:130px;">Qty rejected</th>
                        <th style="min-width:180px;">Condition notes</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($items as $i => $it):
                        $ordered = (float)$it['qty'];
                        $received = (float)$it['qty_received'];
                        $outstanding = round($ordered - $received, 4);
                    ?>
                        <tr>
                            <td class="text-muted"><?= (int)$it['item_no']; ?></td>
                            <td>
                                <?= htmlspecialchars($it['description']); ?>
                                <input type="hidden" name="line_id[]" value="<?= (int)$it['c0d']; ?>">
                            </td>
                            <td class="text-end"><?= number_format($ordered, (fmod($ordered,1)==0?0:2)); ?> <span class="text-muted small"><?= htmlspecialchars($it['unit']); ?></span></td>
                            <td class="text-end text-muted"><?= number_format($received, (fmod($received,1)==0?0:2)); ?></td>
                            <td class="text-end" style="font-weight:600;color:<?= $outstanding>0?'#f59e0b':'#22c55e'; ?>;"><?= number_format($outstanding, (fmod($outstanding,1)==0?0:2)); ?></td>
                            <td>
                                <input type="number" step="0.0001" min="0" max="<?= $outstanding; ?>"
                                       name="qty_received[]" class="form-control form-control-sm rs-qr"
                                       data-outstanding="<?= $outstanding; ?>"
                                       value="<?= htmlspecialchars($f['qty_received'][$i] ?? ''); ?>">
                            </td>
                            <td><input type="number" step="0.0001" min="0" name="qty_rejected[]" class="form-control form-control-sm" value="<?= htmlspecialchars($f['qty_rejected'][$i] ?? ''); ?>"></td>
                            <td><input type="text" name="condition_notes[]" class="form-control form-control-sm" value="<?= htmlspecialchars($f['condition_notes'][$i] ?? ''); ?>"></td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
        <div class="card-footer bg-white text-end">
            <a href="<?= PROOT; ?>Lpos/view/<?= (int)$lpo['c0d']; ?>" class="btn btn-sm btn-outline-secondary">Cancel</a>
            <button type="submit" class="btn btn-sm btn-primary" style="background:<?= BRAND_COLOR; ?>;border-color:<?= BRAND_COLOR; ?>;">
                <i class="fas fa-save me-1"></i> Post GRN
            </button>
        </div>
    </div>
</form>

<script>
function rsReceiveAll() {
    document.querySelectorAll('.rs-qr').forEach(inp => {
        const out = parseFloat(inp.dataset.outstanding || '0');
        if (out > 0) inp.value = out;
    });
}
</script>

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