<?php
$quotation    = $quotation ?? [];
$companies    = $companies ?? [];
$templates    = $templates ?? [];
$priceLists   = $priceLists ?? [];
$opportunities = $opportunities ?? [];
$isEdit       = ! empty($quotation['id']);
$taxAmount    = $taxAmount ?? ($quotation['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', $quotation['company_id'] ?? '') === (string) $c['id'] ? 'selected' : '' ?>>
                    <?= esc($c['name']) ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Template</label>
        <select name="template_id" class="form-select">
            <option value="">— None —</option>
            <?php foreach ($templates as $t): ?>
                <option value="<?= $t['id'] ?>" <?= (string) old('template_id', $quotation['template_id'] ?? '') === (string) $t['id'] ? 'selected' : '' ?>>
                    <?= esc($t['name']) ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Price List</label>
        <select name="price_list_id" class="form-select">
            <option value="">— None —</option>
            <?php foreach ($priceLists as $pl): ?>
                <option value="<?= $pl['id'] ?>" <?= (string) old('price_list_id', $quotation['price_list_id'] ?? '') === (string) $pl['id'] ? 'selected' : '' ?>>
                    <?= esc($pl['name']) ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Opportunity</label>
        <select name="opportunity_id" class="form-select">
            <option value="">— None —</option>
            <?php foreach ($opportunities as $o): ?>
                <option value="<?= $o['id'] ?>" <?= (string) old('opportunity_id', $quotation['opportunity_id'] ?? '') === (string) $o['id'] ? 'selected' : '' ?>>
                    <?= esc($o['name']) ?>
                </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>
