<?php $this->start('body');
    $cpo   = $this->data['cpo'];
    $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 me-2 text-muted"></i>Deliver against <?= htmlspecialchars($cpo['client_po_no']); ?></h5>
        <div class="text-muted" style="font-size:.72rem;">Customer: <?= htmlspecialchars($cpo['customer_name']); ?> · Contract: <?= htmlspecialchars($cpo['currency']); ?> <?= number_format((float)$cpo['total']); ?></div>
    </div>
    <a href="<?= PROOT; ?>GoodsClientPos/view/<?= (int)$cpo['c0d']; ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-left me-1"></i> Back to PO</a>
</div>

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

<form method="post" action="<?= PROOT; ?>GoodsCustomerDeliveries/save" autocomplete="off">
    <input type="hidden" name="client_po_id" value="<?= (int)$cpo['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-3">
                    <label class="form-label">Delivery date *</label>
                    <input type="date" name="delivery_date" class="form-control" required value="<?= htmlspecialchars($f['delivery_date'] ?? date('Y-m-d')); ?>">
                </div>
                <div class="col-md-3">
                    <label class="form-label">Delivered to (customer POC) *</label>
                    <input type="text" name="delivered_to" class="form-control" required
                           value="<?= htmlspecialchars($f['delivered_to'] ?? ($cpo['delivery_poc'] ?? '')); ?>">
                </div>
                <div class="col-md-3">
                    <label class="form-label">POC phone</label>
                    <input type="text" name="delivered_to_phone" class="form-control"
                           value="<?= htmlspecialchars($f['delivered_to_phone'] ?? ($cpo['delivery_poc_phone'] ?? '')); ?>">
                </div>
                <div class="col-md-3">
                    <label class="form-label">Signature / photo ref</label>
                    <input type="text" name="delivered_to_signature" class="form-control" placeholder="Sig ref or photo ID" value="<?= htmlspecialchars($f['delivered_to_signature'] ?? ''); ?>">
                </div>
                <div class="col-12">
                    <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 delivered</strong>
            <button type="button" class="btn btn-sm btn-outline-secondary" onclick="rsDeliverAll()"><i class="fas fa-check-double me-1"></i> Deliver everything available</button>
        </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">In warehouse</th>
                        <th class="text-end">Already delivered</th>
                        <th class="text-end">Outstanding</th>
                        <th style="width:130px;">Qty delivering now</th>
                        <th style="min-width:180px;">Notes</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($items as $i => $it):
                        $ord     = (float)$it['qty'];
                        $rec     = (float)$it['qty_received'];
                        $del     = (float)$it['qty_delivered'];
                        $inWh    = round($rec - $del, 4);           // in our warehouse but not yet delivered to customer
                        $outstd  = round($ord - $del, 4);           // still owed to customer
                        $canDeliverNow = min($inWh, $outstd);
                    ?>
                        <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($ord, (fmod($ord,1)==0?0:2)); ?> <span class="text-muted small"><?= htmlspecialchars($it['unit']); ?></span></td>
                            <td class="text-end text-muted"><?= number_format($rec, (fmod($rec,1)==0?0:2)); ?></td>
                            <td class="text-end text-muted"><?= number_format($del, (fmod($del,1)==0?0:2)); ?></td>
                            <td class="text-end" style="font-weight:600;color:<?= $outstd>0?'#f59e0b':'#22c55e'; ?>;"><?= number_format($outstd, (fmod($outstd,1)==0?0:2)); ?></td>
                            <td>
                                <input type="number" step="0.0001" min="0" max="<?= $canDeliverNow; ?>"
                                       name="qty_delivered[]" class="form-control form-control-sm rs-qd"
                                       data-can-deliver="<?= $canDeliverNow; ?>"
                                       value="<?= htmlspecialchars($f['qty_delivered'][$i] ?? ''); ?>"
                                       <?= $canDeliverNow <= 0 ? 'disabled placeholder="none in stock"' : ''; ?>>
                            </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; ?>GoodsClientPos/view/<?= (int)$cpo['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 DN
            </button>
        </div>
    </div>
</form>

<script>
function rsDeliverAll() {
    document.querySelectorAll('.rs-qd').forEach(inp => {
        const cd = parseFloat(inp.dataset.canDeliver || '0');
        if (cd > 0 && !inp.disabled) inp.value = cd;
    });
}
</script>

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