<?php
$delivery = $delivery ?? [];
$companies = $companies ?? [];
$orders   = $orders ?? [];
$lines    = $lines ?? [['item_code' => '', 'item_name' => '', 'quantity' => '1', 'unit' => 'pcs']];
$mode     = 'delivery';
?>
<div class="enterprise-settings-fields">
    <div class="enterprise-field">
        <label class="enterprise-field-label">Company <span class="text-danger">*</span></label>
        <select name="company_id" class="form-select" required>
            <option value="">— Select company —</option>
            <?php foreach ($companies as $c): ?>
                <option value="<?= $c['id'] ?>" <?= (string) old('company_id', $delivery['company_id'] ?? '') === (string) $c['id'] ? 'selected' : '' ?>>
                    <?= esc($c['name']) ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Sales Order</label>
        <select name="sales_order_id" class="form-select">
            <option value="">— None —</option>
            <?php foreach ($orders as $o): ?>
                <option value="<?= $o['id'] ?>" <?= (string) old('sales_order_id', $delivery['sales_order_id'] ?? '') === (string) $o['id'] ? 'selected' : '' ?>>
                    <?= esc($o['number']) ?><?= ! empty($o['company_name']) ? ' · ' . esc($o['company_name']) : '' ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Delivery Date <span class="text-danger">*</span></label>
        <input type="date" name="delivery_date" class="form-control" required
               value="<?= esc(old('delivery_date', $delivery['delivery_date'] ?? date('Y-m-d'))) ?>">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Status</label>
        <select name="status" class="form-select">
            <?php foreach (['draft', 'ready'] as $s): ?>
                <option value="<?= $s ?>" <?= old('status', $delivery['status'] ?? 'draft') === $s ? 'selected' : '' ?>>
                    <?= esc(ucfirst($s)) ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Carrier</label>
        <input type="text" name="carrier" class="form-control"
               value="<?= esc(old('carrier', $delivery['carrier'] ?? '')) ?>">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Tracking Number</label>
        <input type="text" name="tracking_number" class="form-control"
               value="<?= esc(old('tracking_number', $delivery['tracking_number'] ?? '')) ?>">
    </div>
    <div class="enterprise-field" style="grid-column: 1 / -1;">
        <label class="enterprise-field-label">Shipping Address</label>
        <textarea name="shipping_address" class="form-control" rows="2"><?= esc(old('shipping_address', $delivery['shipping_address'] ?? '')) ?></textarea>
    </div>
    <div class="enterprise-field" style="grid-column: 1 / -1;">
        <label class="enterprise-field-label">Notes</label>
        <textarea name="notes" class="form-control" rows="3"><?= esc(old('notes', $delivery['notes'] ?? '')) ?></textarea>
    </div>
</div>

<div class="mt-4">
    <h3 class="h6 mb-3">Line Items</h3>
    <?= $this->include('Modules\Sale\Views\partials\document_lines', [
        'lines' => $lines,
        'mode'  => 'delivery',
    ]) ?>
</div>
