<?php
$rfq                  = $rfq ?? [];
$suppliers            = $suppliers ?? [];
$purchaseRequests     = $purchaseRequests ?? [];
$selectedSupplierIds  = $selectedSupplierIds ?? [];
$isEdit               = ! empty($rfq['id']);
$taxAmount            = $taxAmount ?? ($rfq['tax_amount'] ?? 0);
$oldSupplierIds       = old('supplier_ids');
if (is_array($oldSupplierIds)) {
    $selectedSupplierIds = array_map('intval', $oldSupplierIds);
} else {
    $selectedSupplierIds = array_map('intval', (array) $selectedSupplierIds);
}
?>
<div class="enterprise-settings-fields">
    <div class="enterprise-field">
        <label class="enterprise-field-label">RFQ Date <span class="text-danger">*</span></label>
        <input type="date" name="rfq_date" class="form-control" required
               value="<?= esc(old('rfq_date', $rfq['rfq_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', $rfq['valid_until'] ?? '')) ?>">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Purchase Request</label>
        <select name="purchase_request_id" class="form-select">
            <option value="">— None —</option>
            <?php foreach ($purchaseRequests as $pr): ?>
                <option value="<?= $pr['id'] ?>" <?= (string) old('purchase_request_id', $rfq['purchase_request_id'] ?? '') === (string) $pr['id'] ? 'selected' : '' ?>>
                    <?= esc($pr['number']) ?><?= ! empty($pr['department']) ? ' — ' . esc($pr['department']) : '' ?>
                </option>
            <?php endforeach; ?>
        </select>
    </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">Suppliers</label>
        <?php if (count($suppliers) === 0): ?>
            <p class="text-muted small mb-0">No active suppliers available.</p>
        <?php else: ?>
            <div class="d-flex flex-wrap gap-3 pt-1">
                <?php foreach ($suppliers as $s): ?>
                    <label class="form-check">
                        <input type="checkbox" class="form-check-input" name="supplier_ids[]" value="<?= (int) $s['id'] ?>"
                            <?= in_array((int) $s['id'], $selectedSupplierIds, true) ? 'checked' : '' ?>>
                        <span class="form-check-label"><?= esc($s['name']) ?><?= ! empty($s['code']) ? ' (' . esc($s['code']) . ')' : '' ?></span>
                    </label>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>
    </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', $rfq['notes'] ?? '')) ?></textarea>
    </div>
    <?= $this->include('Modules\Sale\Views\partials\document_lines', [
        'lines'     => $lines ?? [],
        'mode'      => 'priced',
        'taxAmount' => $taxAmount,
    ]) ?>
</div>
