@props([
    'name',
    'label' => null,
    'options' => [],
    'selected' => null,
    'placeholder' => null,
    'required' => false,
    'id' => null,
])

{{-- Use x-ui.form.search-select for relational / FK fields. Keep native select for small fixed enums. --}}

@php
    $id = $id ?? $name;
    $current = old($name, $selected);
    $errorBag = $errors ?? new \Illuminate\Support\ViewErrorBag();
    $hasError = $errorBag->has($name);
@endphp

<x-ui.form.field :name="$name" :label="$label" :required="$required" :id="$id">
    <select
        id="{{ $id }}"
        name="{{ $name }}"
        @if ($required) required @endif
        {{ $attributes->class(['form-select', 'is-invalid' => $hasError]) }}
    >
        @if ($placeholder)
            <option value="" @selected($current === null || $current === '')>{{ $placeholder }}</option>
        @endif

        @foreach ($options as $optValue => $optLabel)
            <option value="{{ $optValue }}" @selected((string) $current === (string) $optValue)>
                {{ $optLabel }}
            </option>
        @endforeach
    </select>
</x-ui.form.field>
