<?php
$refund    = $refund ?? [];
$companies = $companies ?? [];
$invoices  = $invoices ?? [];
$returns   = $returns ?? [];
$isEdit    = ! empty($refund['id']);
?>
<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', $refund['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', $refund['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 Return</label>
        <select name="sales_return_id" class="form-select">
            <option value="">— None —</option>
            <?php foreach ($returns as $r): ?>
                <option value="<?= $r['id'] ?>" <?= (string) old('sales_return_id', $refund['sales_return_id'] ?? '') === (string) $r['id'] ? 'selected' : '' ?>>
                    <?= esc($r['number']) ?><?= ! empty($r['company_name']) ? ' · ' . esc($r['company_name']) : '' ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Refund Date <span class="text-danger">*</span></label>
        <input type="date" name="refund_date" class="form-control" required
               value="<?= esc(old('refund_date', $refund['refund_date'] ?? date('Y-m-d'))) ?>">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Amount <span class="text-danger">*</span></label>
        <input type="number" name="amount" class="form-control" step="0.01" min="0" required
               value="<?= esc(old('amount', $refund['amount'] ?? '')) ?>">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Method</label>
        <input type="text" name="method" class="form-control" placeholder="e.g. bank transfer, cash, credit note"
               value="<?= esc(old('method', $refund['method'] ?? '')) ?>">
    </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', $refund['notes'] ?? '')) ?></textarea>
    </div>
</div>
