<?php
$return    = $return ?? [];
$companies = $companies ?? [];
$invoices  = $invoices ?? [];
$orders    = $orders ?? [];
$lines     = $lines ?? [];
$isEdit    = ! empty($return['id']);
$taxAmount = $taxAmount ?? ($return['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', $return['company_id'] ?? '') === (string) $c['id'] ? 'selected' : '' ?>>
                    <?= esc($c['name']) ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Invoice</label>
        <select name="invoice_id" class="form-select">
            <option value="">— None —</option>
            <?php foreach ($invoices as $inv): ?>
                <option value="<?= $inv['id'] ?>" <?= (string) old('invoice_id', $return['invoice_id'] ?? '') === (string) $inv['id'] ? 'selected' : '' ?>>
                    <?= esc($inv['number']) ?><?= ! empty($inv['company_name']) ? ' · ' . esc($inv['company_name']) : '' ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Sales Order</label>
        <select name="sales_order_id" class="form-select">
            <option value="">— None —</option>
            <?php foreach ($orders as $o): ?>
                <option value="<?= $o['id'] ?>" <?= (string) old('sales_order_id', $return['sales_order_id'] ?? '') === (string) $o['id'] ? 'selected' : '' ?>>
                    <?= esc($o['number']) ?><?= ! empty($o['company_name']) ? ' · ' . esc($o['company_name']) : '' ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Return Date <span class="text-danger">*</span></label>
        <input type="date" name="return_date" class="form-control" required
               value="<?= esc(old('return_date', $return['return_date'] ?? date('Y-m-d'))) ?>">
    </div>
    <div class="enterprise-field" style="grid-column: 1 / -1;">
        <label class="enterprise-field-label">Reason</label>
        <textarea name="reason" class="form-control" rows="2"><?= esc(old('reason', $return['reason'] ?? '')) ?></textarea>
    </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', $return['notes'] ?? '')) ?></textarea>
    </div>
    <?= $this->include('Modules\Sale\Views\partials\document_lines', ['lines' => $lines, 'mode' => 'priced', 'taxAmount' => $taxAmount]) ?>
</div>
