<?php
$order      = $order ?? [];
$companies  = $companies ?? [];
$quotations = $quotations ?? [];
$lines      = $lines ?? [['item_code' => '', 'item_name' => '', 'quantity' => '1', 'unit' => 'pcs', 'unit_price' => '']];
$mode       = 'priced';
$taxAmount  = old('tax_amount', $order['tax_amount'] ?? '0');
?>
<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', $order['company_id'] ?? '') === (string) $c['id'] ? 'selected' : '' ?>>
                    <?= esc($c['name']) ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Quotation</label>
        <select name="quotation_id" class="form-select">
            <option value="">— None —</option>
            <?php foreach ($quotations as $q): ?>
                <option value="<?= $q['id'] ?>" <?= (string) old('quotation_id', $order['quotation_id'] ?? '') === (string) $q['id'] ? 'selected' : '' ?>>
                    <?= esc($q['number']) ?><?= ! empty($q['company_name']) ? ' · ' . esc($q['company_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">Required Date</label>
        <input type="date" name="required_date" class="form-control"
               value="<?= esc(old('required_date', $order['required_date'] ?? '')) ?>">
    </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', $order['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'      => 'priced',
        'taxAmount' => $taxAmount,
    ]) ?>
</div>
