<?php
$rate     = $rate ?? [];
$accounts = $accounts ?? [];
?>
<div class="enterprise-settings-fields">
    <div class="enterprise-field">
        <label class="enterprise-field-label">Code <span class="text-danger">*</span></label>
        <input type="text" name="code" class="form-control text-uppercase" maxlength="30"
               value="<?= esc(old('code', $rate['code'] ?? '')) ?>" required>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Name <span class="text-danger">*</span></label>
        <input type="text" name="name" class="form-control" value="<?= esc(old('name', $rate['name'] ?? '')) ?>" required>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Rate (%) <span class="text-danger">*</span></label>
        <input type="number" name="rate" class="form-control" step="0.0001" min="0"
               value="<?= esc(old('rate', $rate['rate'] ?? '0')) ?>" required>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Tax Type <span class="text-danger">*</span></label>
        <select name="tax_type" class="form-select" required>
            <?php foreach (['vat', 'sales', 'withholding', 'other'] as $t): ?>
                <option value="<?= $t ?>" <?= old('tax_type', $rate['tax_type'] ?? 'vat') === $t ? 'selected' : '' ?>><?= ucfirst($t) ?></option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">GL Account</label>
        <select name="account_id" class="form-select">
            <option value="">— None —</option>
            <?php foreach ($accounts as $a): ?>
                <option value="<?= $a['id'] ?>" <?= (string) old('account_id', $rate['account_id'] ?? '') === (string) $a['id'] ? 'selected' : '' ?>>
                    <?= esc($a['code'] . ' — ' . $a['name']) ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Status</label>
        <select name="status" class="form-select">
            <?php foreach (['active', 'inactive'] as $s): ?>
                <option value="<?= $s ?>" <?= old('status', $rate['status'] ?? 'active') === $s ? 'selected' : '' ?>><?= ucfirst($s) ?></option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Recoverable</label>
        <?php $recoverable = old('is_recoverable', isset($rate['is_recoverable']) ? (string) (int) $rate['is_recoverable'] : '1'); ?>
        <div class="form-check mt-2">
            <input type="hidden" name="is_recoverable" value="0">
            <input class="form-check-input" type="checkbox" name="is_recoverable" value="1" id="is_recoverable"
                   <?= $recoverable === '1' || $recoverable === 'on' ? 'checked' : '' ?>>
            <label class="form-check-label" for="is_recoverable">Input tax is recoverable</label>
        </div>
    </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', $rate['notes'] ?? '')) ?></textarea>
    </div>
</div>
