<?php
$disposal = $disposal ?? [];
$assets   = $assets ?? [];
$accounts = $accounts ?? [];
$accountOptions = static function (array $accounts, $selected): string {
    $html = '<option value="">— Select account —</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">Fixed Asset <span class="text-danger">*</span></label>
        <select name="fixed_asset_id" class="form-select" required>
            <option value="">— Select asset —</option>
            <?php foreach ($assets as $a): ?>
                <option value="<?= $a['id'] ?>" <?= (string) old('fixed_asset_id', $disposal['fixed_asset_id'] ?? '') === (string) $a['id'] ? 'selected' : '' ?>>
                    <?= esc(($a['number'] ?? '') . ' — ' . ($a['name'] ?? '')) ?>
                    · BV <?= number_format((float) ($a['book_value'] ?? 0), 2) ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Disposal Date <span class="text-danger">*</span></label>
        <input type="date" name="disposal_date" class="form-control" required
               value="<?= esc(old('disposal_date', $disposal['disposal_date'] ?? date('Y-m-d'))) ?>">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Proceeds</label>
        <input type="number" name="proceeds" class="form-control" step="0.01" min="0"
               value="<?= esc(old('proceeds', $disposal['proceeds'] ?? '0')) ?>">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Proceeds Account</label>
        <select name="proceeds_account_id" class="form-select">
            <?= $accountOptions($accounts, old('proceeds_account_id', $disposal['proceeds_account_id'] ?? '')) ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Gain / Loss Account</label>
        <select name="gain_loss_account_id" class="form-select">
            <?= $accountOptions($accounts, old('gain_loss_account_id', $disposal['gain_loss_account_id'] ?? '')) ?>
        </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', $disposal['notes'] ?? '')) ?></textarea>
    </div>
</div>
