<?php
$category = $category ?? [];
$accounts = $accounts ?? [];
$accountOptions = static function (array $accounts, $selected): string {
    $html = '<option value="">— None —</option>';
    foreach ($accounts as $a) {
        $sel = (string) $selected === (string) $a['id'] ? ' selected' : '';
        $html .= '<option value="' . (int) $a['id'] . '"' . $sel . '>'
            . esc($a['code'] . ' — ' . $a['name']) . '</option>';
    }
    return $html;
};
?>
<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', $category['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', $category['name'] ?? '')) ?>" required>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Useful Life (months) <span class="text-danger">*</span></label>
        <input type="number" name="useful_life_months" class="form-control" min="1" required
               value="<?= esc(old('useful_life_months', $category['useful_life_months'] ?? '60')) ?>">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Salvage Percent</label>
        <input type="number" name="salvage_percent" class="form-control" step="0.01" min="0" max="100"
               value="<?= esc(old('salvage_percent', $category['salvage_percent'] ?? '0')) ?>">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Asset Account</label>
        <select name="asset_account_id" class="form-select">
            <?= $accountOptions($accounts, old('asset_account_id', $category['asset_account_id'] ?? '')) ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Accum. Depreciation Account</label>
        <select name="accum_depr_account_id" class="form-select">
            <?= $accountOptions($accounts, old('accum_depr_account_id', $category['accum_depr_account_id'] ?? '')) ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Depreciation Expense Account</label>
        <select name="depr_expense_account_id" class="form-select">
            <?= $accountOptions($accounts, old('depr_expense_account_id', $category['depr_expense_account_id'] ?? '')) ?>
        </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', $category['status'] ?? 'active') === $s ? 'selected' : '' ?>><?= ucfirst($s) ?></option>
            <?php endforeach; ?>
        </select>
    </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', $category['notes'] ?? '')) ?></textarea>
    </div>
</div>
