<?php
$transfer     = $transfer ?? [];
$bankAccounts = $bankAccounts ?? [];
$cashAccounts = $cashAccounts ?? [];

$fromType = old('from_type', $transfer['from_type'] ?? 'bank');
$toType   = old('to_type', $transfer['to_type'] ?? 'bank');
$fromId   = (string) old('from_id', $transfer['from_id'] ?? '');
$toId     = (string) old('to_id', $transfer['to_id'] ?? '');

$optionsFor = static function (string $type) use ($bankAccounts, $cashAccounts): array {
    return $type === 'cash' ? $cashAccounts : $bankAccounts;
};
?>
<div class="enterprise-settings-fields">
    <div class="enterprise-field">
        <label class="enterprise-field-label">From Type <span class="text-danger">*</span></label>
        <select name="from_type" id="from_type" class="form-select" required>
            <option value="bank" <?= $fromType === 'bank' ? 'selected' : '' ?>>Bank</option>
            <option value="cash" <?= $fromType === 'cash' ? 'selected' : '' ?>>Cash</option>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">From Account <span class="text-danger">*</span></label>
        <select name="from_id" id="from_id" class="form-select" required>
            <option value="">— Select —</option>
            <?php foreach ($optionsFor($fromType) as $a): ?>
                <option value="<?= $a['id'] ?>" <?= $fromId === (string) $a['id'] ? 'selected' : '' ?>>
                    <?= esc($a['code']) ?> · <?= esc($a['name']) ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">To Type <span class="text-danger">*</span></label>
        <select name="to_type" id="to_type" class="form-select" required>
            <option value="bank" <?= $toType === 'bank' ? 'selected' : '' ?>>Bank</option>
            <option value="cash" <?= $toType === 'cash' ? 'selected' : '' ?>>Cash</option>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">To Account <span class="text-danger">*</span></label>
        <select name="to_id" id="to_id" class="form-select" required>
            <option value="">— Select —</option>
            <?php foreach ($optionsFor($toType) as $a): ?>
                <option value="<?= $a['id'] ?>" <?= $toId === (string) $a['id'] ? 'selected' : '' ?>>
                    <?= esc($a['code']) ?> · <?= esc($a['name']) ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Transfer Date <span class="text-danger">*</span></label>
        <input type="date" name="transfer_date" class="form-control" required
               value="<?= esc(old('transfer_date', $transfer['transfer_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.01" required
               value="<?= esc(old('amount', $transfer['amount'] ?? '')) ?>">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Reference</label>
        <input type="text" name="reference" class="form-control" maxlength="100"
               value="<?= esc(old('reference', $transfer['reference'] ?? '')) ?>">
    </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', $transfer['notes'] ?? '')) ?></textarea>
    </div>
</div>

<script>
(function () {
    var banks = <?= json_encode(array_map(static fn ($a) => ['id' => (int) $a['id'], 'label' => $a['code'] . ' · ' . $a['name']], $bankAccounts)) ?>;
    var cash  = <?= json_encode(array_map(static fn ($a) => ['id' => (int) $a['id'], 'label' => $a['code'] . ' · ' . $a['name']], $cashAccounts)) ?>;

    function fillSelect(selectEl, type, selected) {
        var list = type === 'cash' ? cash : banks;
        selectEl.innerHTML = '<option value="">— Select —</option>';
        list.forEach(function (item) {
            var opt = document.createElement('option');
            opt.value = item.id;
            opt.textContent = item.label;
            if (String(selected) === String(item.id)) opt.selected = true;
            selectEl.appendChild(opt);
        });
    }

    var fromType = document.getElementById('from_type');
    var toType   = document.getElementById('to_type');
    var fromId   = document.getElementById('from_id');
    var toId     = document.getElementById('to_id');

    fromType.addEventListener('change', function () { fillSelect(fromId, fromType.value, ''); });
    toType.addEventListener('change', function () { fillSelect(toId, toType.value, ''); });
})();
</script>
