<?php
$voucher      = $voucher ?? [];
$coaAccounts  = $coaAccounts ?? [];
$bankAccounts = $bankAccounts ?? [];
$cashAccounts = $cashAccounts ?? [];
$costCenters  = $costCenters ?? [];

$receiveToType = old('receive_to_type', $voucher['receive_to_type'] ?? 'bank');
$receiveToId   = (string) old('receive_to_id', $voucher['receive_to_id'] ?? '');
$accountId     = (string) old('account_id', $voucher['account_id'] ?? '');
$ccId          = (string) old('cost_center_id', $voucher['cost_center_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">Voucher Date <span class="text-danger">*</span></label>
        <input type="date" name="voucher_date" class="form-control" required
               value="<?= esc(old('voucher_date', $voucher['voucher_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', $voucher['amount'] ?? '')) ?>">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Credit Account <span class="text-danger">*</span></label>
        <select name="account_id" class="form-select" required>
            <option value="">— Select —</option>
            <?php foreach ($coaAccounts as $a): ?>
                <option value="<?= $a['id'] ?>" <?= $accountId === (string) $a['id'] ? 'selected' : '' ?>>
                    <?= esc($a['code']) ?> · <?= esc($a['name']) ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Cost Center</label>
        <select name="cost_center_id" class="form-select">
            <option value="">— None —</option>
            <?php foreach ($costCenters as $cc): ?>
                <option value="<?= $cc['id'] ?>" <?= $ccId === (string) $cc['id'] ? 'selected' : '' ?>>
                    <?= esc($cc['code']) ?> · <?= esc($cc['name']) ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Receive To Type <span class="text-danger">*</span></label>
        <select name="receive_to_type" id="receive_to_type" class="form-select" required>
            <option value="bank" <?= $receiveToType === 'bank' ? 'selected' : '' ?>>Bank</option>
            <option value="cash" <?= $receiveToType === 'cash' ? 'selected' : '' ?>>Cash</option>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Receive To Account <span class="text-danger">*</span></label>
        <select name="receive_to_id" id="receive_to_id" class="form-select" required>
            <option value="">— Select —</option>
            <?php foreach ($optionsFor($receiveToType) as $a): ?>
                <option value="<?= $a['id'] ?>" <?= $receiveToId === (string) $a['id'] ? 'selected' : '' ?>>
                    <?= esc($a['code']) ?> · <?= esc($a['name']) ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Payer</label>
        <input type="text" name="payer" class="form-control" maxlength="150"
               value="<?= esc(old('payer', $voucher['payer'] ?? '')) ?>">
    </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', $voucher['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', $voucher['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 receiveToType = document.getElementById('receive_to_type');
    var receiveToId   = document.getElementById('receive_to_id');

    receiveToType.addEventListener('change', function () { fillSelect(receiveToId, receiveToType.value, ''); });
})();
</script>
