<?php
$asset      = $asset ?? [];
$categories = $categories ?? [];
$accounts   = $accounts ?? [];
$accountOptions = static function (array $accounts, $selected): string {
    $html = '<option value="">— Use category default —</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">Name <span class="text-danger">*</span></label>
        <input type="text" name="name" class="form-control" value="<?= esc(old('name', $asset['name'] ?? '')) ?>" required>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Category <span class="text-danger">*</span></label>
        <select name="category_id" class="form-select" required>
            <option value="">— Select category —</option>
            <?php foreach ($categories as $c): ?>
                <option value="<?= $c['id'] ?>" <?= (string) old('category_id', $asset['category_id'] ?? '') === (string) $c['id'] ? 'selected' : '' ?>>
                    <?= esc($c['code'] . ' — ' . $c['name']) ?>
                </option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Purchase Date <span class="text-danger">*</span></label>
        <input type="date" name="purchase_date" class="form-control" required
               value="<?= esc(old('purchase_date', $asset['purchase_date'] ?? date('Y-m-d'))) ?>">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">In-Service Date</label>
        <input type="date" name="in_service_date" class="form-control"
               value="<?= esc(old('in_service_date', $asset['in_service_date'] ?? '')) ?>">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Cost <span class="text-danger">*</span></label>
        <input type="number" name="cost" class="form-control" step="0.01" min="0" required
               value="<?= esc(old('cost', $asset['cost'] ?? '')) ?>">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Salvage Value</label>
        <input type="number" name="salvage_value" class="form-control" step="0.01" min="0"
               value="<?= esc(old('salvage_value', $asset['salvage_value'] ?? '')) ?>"
               placeholder="Blank = category salvage %">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Useful Life (months)</label>
        <input type="number" name="useful_life_months" class="form-control" min="1"
               value="<?= esc(old('useful_life_months', $asset['useful_life_months'] ?? '')) ?>"
               placeholder="Blank = category default">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Location</label>
        <input type="text" name="location" class="form-control"
               value="<?= esc(old('location', $asset['location'] ?? '')) ?>">
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Asset Account Override</label>
        <select name="asset_account_id" class="form-select">
            <?= $accountOptions($accounts, old('asset_account_id', $asset['asset_account_id'] ?? '')) ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Accum. Depr. Account Override</label>
        <select name="accum_depr_account_id" class="form-select">
            <?= $accountOptions($accounts, old('accum_depr_account_id', $asset['accum_depr_account_id'] ?? '')) ?>
        </select>
    </div>
    <div class="enterprise-field">
        <label class="enterprise-field-label">Depr. Expense Account Override</label>
        <select name="depr_expense_account_id" class="form-select">
            <?= $accountOptions($accounts, old('depr_expense_account_id', $asset['depr_expense_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', $asset['notes'] ?? '')) ?></textarea>
    </div>
</div>
