<?php
$budget      = $budget ?? [];
$accounts    = $accounts ?? [];
$costCenters = $costCenters ?? [];
$isEdit      = ! empty($budget['id']);
?>
<div class="enterprise-settings-fields">
    <div class="enterprise-field">
        <label class="enterprise-field-label">Name <span class="text-danger">*</span></label>
        <input type="text" name="name" class="form-control" maxlength="150" required
               value="<?= esc(old('name', $budget['name'] ?? '')) ?>">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Fiscal Year <span class="text-danger">*</span></label>
        <input type="number" name="fiscal_year" class="form-control" min="2000" max="2100" required
               value="<?= esc(old('fiscal_year', $budget['fiscal_year'] ?? date('Y'))) ?>">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Period From <span class="text-danger">*</span></label>
        <input type="date" name="period_from" class="form-control" required
               value="<?= esc(old('period_from', $budget['period_from'] ?? date('Y-01-01'))) ?>">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Period To <span class="text-danger">*</span></label>
        <input type="date" name="period_to" class="form-control" required
               value="<?= esc(old('period_to', $budget['period_to'] ?? date('Y-12-31'))) ?>">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Account <span class="text-danger">*</span></label>
        <select name="account_id" class="form-select" required>
            <option value="">— Select —</option>
            <?php foreach ($accounts as $a): ?>
                <option value="<?= $a['id'] ?>" <?= (string) old('account_id', $budget['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">Cost Center</label>
        <select name="cost_center_id" class="form-select">
            <option value="">— None —</option>
            <?php foreach ($costCenters as $c): ?>
                <option value="<?= $c['id'] ?>" <?= (string) old('cost_center_id', $budget['cost_center_id'] ?? '') === (string) $c['id'] ? 'selected' : '' ?>>
                    <?= esc($c['code'] . ' — ' . $c['name']) ?>
                </option>
            <?php endforeach; ?>
        </select>
    </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', $budget['amount'] ?? '')) ?>">
    </div>
    <?php if ($isEdit): ?>
        <div class="enterprise-field">
            <label class="enterprise-field-label">Status <span class="text-danger">*</span></label>
            <select name="status" class="form-select" required>
                <?php foreach (['draft', 'active', 'closed'] as $s): ?>
                    <option value="<?= $s ?>" <?= old('status', $budget['status'] ?? 'draft') === $s ? 'selected' : '' ?>><?= ucfirst($s) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
    <?php endif; ?>
    <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', $budget['notes'] ?? '')) ?></textarea>
    </div>
</div>
