<?php
$order     = $order ?? [];
$suppliers = $suppliers ?? [];
$isEdit    = ! empty($order['id']);
$taxAmount = $taxAmount ?? ($order['tax_amount'] ?? 0);
?>
<div class="enterprise-settings-fields">
    <div class="enterprise-field">
        <label class="enterprise-field-label">Supplier <span class="text-danger">*</span></label>
        <select name="supplier_id" class="form-select" required>
            <option value="">— Select supplier —</option>
            <?php foreach ($suppliers as $s): ?>
                <option value="<?= $s['id'] ?>" <?= (string) old('supplier_id', $order['supplier_id'] ?? '') === (string) $s['id'] ? 'selected' : '' ?>>
                    <?= esc($s['name']) ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Order Date <span class="text-danger">*</span></label>
        <input type="date" name="order_date" class="form-control" required
               value="<?= esc(old('order_date', $order['order_date'] ?? date('Y-m-d'))) ?>">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Expected Date</label>
        <input type="date" name="expected_date" class="form-control"
               value="<?= esc(old('expected_date', $order['expected_date'] ?? '')) ?>">
    </div>
    <?php if (! $isEdit): ?>
        <div class="enterprise-field">
            <label class="enterprise-field-label">Status</label>
            <select name="status" class="form-select">
                <?php foreach (['draft' => 'Draft', 'pending_approval' => 'Pending Approval'] as $val => $label): ?>
                    <option value="<?= $val ?>" <?= old('status', 'draft') === $val ? 'selected' : '' ?>><?= $label ?></option>
                <?php endforeach; ?>
            </select>
        </div>
    <?php endif; ?>
    <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', $order['notes'] ?? '')) ?></textarea>
    </div>
    <?= $this->include('Modules\Sale\Views\partials\document_lines', [
        'lines'     => $lines ?? [],
        'mode'      => 'priced',
        'taxAmount' => $taxAmount,
    ]) ?>
</div>
