@props([
    'locations',
    'currentId' => null,
    'action' => null,
    'name' => 'location_id',
])

<form
    method="post"
    @if ($action) action="{{ $action }}" @endif
    {{ $attributes->merge(['class' => 'flex items-center gap-2']) }}
>
    @csrf

    <label for="pos-location-switcher" class="sr-only">Location</label>
    <select
        id="pos-location-switcher"
        name="{{ $name }}"
        class="tw-input"
        onchange="this.form.submit()"
    >
        @foreach ($locations as $location)
            @php
                $id = is_array($location) ? ($location['id'] ?? null) : ($location->id ?? null);
                $label = is_array($location) ? ($location['name'] ?? $id) : ($location->name ?? $id);
            @endphp
            <option value="{{ $id }}" @selected((string) $id === (string) $currentId)>
                {{ $label }}
            </option>
        @endforeach
    </select>
</form>
