<div class="mt-4 max-w-md" wire:init="preSelected" x-data="{ open: false, selected: @entangle('selectedAccount') }">

    @isset($label)
        <x-jetstream.label :value="$label" for="account" />
    @else
        <x-jetstream.label for="account" value="{{ __('From account') }}" />
    @endisset

    <div class="relative mt-1">
        <button @click="open = !open"
                class="block w-full rounded-md border border-theme-primary bg-theme-background px-3 py-2 text-left shadow-sm transition duration-150 ease-in-out focus:border-theme-accent focus:ring-theme-accent focus:outline-none sm:text-sm"
                type="button">
            <div :class="selected ? 'text-theme-primary' : 'text-theme-light'" class="flex items-center justify-between">
                @if (!$selectedAccount)
                    <span class="cursor-default text-red-600" x-text="'{{ __('No account available') }}'"></span>
                @else
                    <span class="flex w-full items-center">
                    <span x-text="selected ? selected.name : '{{ __('Select an account') }}'"></span>
                    <span x-show="selected" class="ml-auto" x-text="selected ? selected.balanceTbFormat : ''"></span>
                    </span>
                @endif
                <svg class="text-secondary-400 invalidated:text-negative-400 invalidated:dark:text-negative-600 ml-2 h-5 w-5"
                        fill="none" height="24" stroke="currentColor" viewBox="0 0 24 24" width="24"
                        xmlns="http://www.w3.org/2000/svg">
                    <path d="M8.25 15L12 18.75L15.75 15M8.25 9L12 5.25L15.75 9" stroke-linecap="round"
                            stroke-linejoin="round" stroke-width="1.5"></path>
                </svg>
            </div>
        </button>
        <ul @click.away="open = false"
            class="absolute z-10 mt-1 w-full rounded-md border border-theme-primary bg-theme-background shadow-lg" x-show="open">
            @foreach ($profileAccounts as $index => $profileAccount)
            <li
                @click="selected = { id: {{ $profileAccount['id'] }}, name: '{{ __(ucfirst(strtolower($profileAccount['name']))) }}{{ $profileAccount['inactive'] === true ? ' (' . strtolower(__('Inactive')) . ')' : '' }}', balance: '{{ tbFormat($profileAccount['balance']) }}' }; open = false; $wire.fromAccountSelected({{ $profileAccount['id'] }})"
                class="flex cursor-pointer justify-between px-3 py-2 hover:bg-theme-surface"
                wire:key="{{ $index }}">
                <span>
                    {{ __(ucfirst(strtolower($profileAccount['name']))) }}
                    @if($profileAccount['inactive'])
                        ({{ strtolower(__('Inactive')) }})
                    @endif
                </span>
                <span class="ml-auto">{{ tbFormat($profileAccount['balance']) }}</span>
                <span class="ml-2 w-5"></span>
            </li>
            @endforeach
        </ul>
    </div>
</div>