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

$payFromType = old('pay_from_type', $expense['pay_from_type'] ?? 'bank');
$payFromId   = (string) old('pay_from_id', $expense['pay_from_id'] ?? '');
$accountId   = (string) old('expense_account_id', $expense['expense_account_id'] ?? '');
$ccId        = (string) old('cost_center_id', $expense['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">Expense Date <span class="text-danger">*</span></label>
        <input type="date" name="expense_date" class="form-control" required
               value="<?= esc(old('expense_date', $expense['expense_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', $expense['amount'] ?? '')) ?>">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Expense Account <span class="text-danger">*</span></label>
        <select name="expense_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">Pay From Type <span class="text-danger">*</span></label>
        <select name="pay_from_type" id="pay_from_type" class="form-select" required>
            <option value="bank" <?= $payFromType === 'bank' ? 'selected' : '' ?>>Bank</option>
            <option value="cash" <?= $payFromType === 'cash' ? 'selected' : '' ?>>Cash</option>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Pay From Account <span class="text-danger">*</span></label>
        <select name="pay_from_id" id="pay_from_id" class="form-select" required>
            <option value="">— Select —</option>
            <?php foreach ($optionsFor($payFromType) as $a): ?>
                <option value="<?= $a['id'] ?>" <?= $payFromId === (string) $a['id'] ? 'selected' : '' ?>>
                    <?= esc($a['code']) ?> · <?= esc($a['name']) ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Payee</label>
        <input type="text" name="payee" class="form-control" maxlength="150"
               value="<?= esc(old('payee', $expense['payee'] ?? '')) ?>">
    </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', $expense['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', $expense['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 payFromType = document.getElementById('pay_from_type');
    var payFromId   = document.getElementById('pay_from_id');

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