<?php
$quotation = $quotation ?? [];
$suppliers = $suppliers ?? [];
$rfqs      = $rfqs ?? [];
$isEdit    = ! empty($quotation['id']);
$taxAmount = $taxAmount ?? ($quotation['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', $quotation['supplier_id'] ?? '') === (string) $s['id'] ? 'selected' : '' ?>>
                    <?= esc($s['name']) ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">RFQ</label>
        <select name="rfq_id" class="form-select">
            <option value="">— None —</option>
            <?php foreach ($rfqs as $rfq): ?>
                <option value="<?= $rfq['id'] ?>" <?= (string) old('rfq_id', $quotation['rfq_id'] ?? '') === (string) $rfq['id'] ? 'selected' : '' ?>>
                    <?= esc($rfq['number']) ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Quotation Date <span class="text-danger">*</span></label>
        <input type="date" name="quotation_date" class="form-control" required
               value="<?= esc(old('quotation_date', $quotation['quotation_date'] ?? date('Y-m-d'))) ?>">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Valid Until</label>
        <input type="date" name="valid_until" class="form-control"
               value="<?= esc(old('valid_until', $quotation['valid_until'] ?? '')) ?>">
    </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', $quotation['notes'] ?? '')) ?></textarea>
    </div>
    <?= $this->include('Modules\Sale\Views\partials\document_lines', [
        'lines'     => $lines ?? [],
        'mode'      => 'priced',
        'taxAmount' => $taxAmount,
    ]) ?>
</div>
