<?php
$account = $account ?? [];
$parents = $parents ?? [];
?>
<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', $account['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', $account['name'] ?? '')) ?>" required>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Type <span class="text-danger">*</span></label>
        <select name="type" class="form-select" required>
            <?php foreach (['asset', 'liability', 'equity', 'revenue', 'expense'] as $t): ?>
                <option value="<?= $t ?>" <?= old('type', $account['type'] ?? 'asset') === $t ? 'selected' : '' ?>><?= ucfirst($t) ?></option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Parent Account</label>
        <select name="parent_id" class="form-select">
            <option value="">— None —</option>
            <?php foreach ($parents as $p): ?>
                <option value="<?= $p['id'] ?>" <?= (string) old('parent_id', $account['parent_id'] ?? '') === (string) $p['id'] ? 'selected' : '' ?>>
                    <?= esc($p['code'] . ' — ' . $p['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', $account['status'] ?? 'active') === $s ? 'selected' : '' ?>><?= ucfirst($s) ?></option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Postable</label>
        <?php $postable = old('is_postable', isset($account['is_postable']) ? (string) (int) $account['is_postable'] : '1'); ?>
        <div class="form-check mt-2">
            <input type="hidden" name="is_postable" value="0">
            <input class="form-check-input" type="checkbox" name="is_postable" value="1" id="is_postable"
                   <?= $postable === '1' || $postable === 'on' ? 'checked' : '' ?>>
            <label class="form-check-label" for="is_postable">Allow journal postings to this account</label>
        </div>
    </div>
    <div class="enterprise-field" style="grid-column: 1 / -1;">
        <label class="enterprise-field-label">Description</label>
        <textarea name="description" class="form-control" rows="3"><?= esc(old('description', $account['description'] ?? '')) ?></textarea>
    </div>
</div>
