<?php
$return    = $return ?? [];
$suppliers = $suppliers ?? [];
$orders    = $orders ?? [];
$receipts  = $receipts ?? [];
$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">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', $return['supplier_id'] ?? '') === (string) $s['id'] ? 'selected' : '' ?>>
                    <?= esc($s['name']) ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Purchase Order</label>
        <select name="purchase_order_id" class="form-select">
            <option value="">— None —</option>
            <?php foreach ($orders as $o): ?>
                <option value="<?= $o['id'] ?>" <?= (string) old('purchase_order_id', $return['purchase_order_id'] ?? '') === (string) $o['id'] ? 'selected' : '' ?>>
                    <?= esc($o['number']) ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Goods Receipt</label>
        <select name="goods_receipt_id" class="form-select">
            <option value="">— None —</option>
            <?php foreach ($receipts as $gr): ?>
                <option value="<?= $gr['id'] ?>" <?= (string) old('goods_receipt_id', $return['goods_receipt_id'] ?? '') === (string) $gr['id'] ? 'selected' : '' ?>>
                    <?= esc($gr['number']) ?>
                </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>
