{{--
 *
 * @version     2.0.$Revision:$
 * @version     SVN: $Id:$
 * @package     bplan-base/globals
 * @subpackage  Views
 * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
 * @copyright   Copyright (C) 2025 bplan-solutions GmbH & Co. KG <https://www.bplan-solutions.de/>
 * /Δ\
 --}}

<div x-data="createLocation" x-cloak x-show="show" id="createLocationModal" tabindex="-1"
    x-on:show-create-location-modal.window="showModal()" x-on:location-created.window="created($event)"
    class="fixed top-0 left-0 right-0 z-50 flex items-center justify-center w-full h-screen overflow-x-hidden overflow-y-auto md:inset-0 md:h-full">
    <div class="fixed inset-0 transition-opacity bg-gray-500 bg-opacity-75"></div>
    <div class="relative w-full h-full max-w-2xl p-4 md:h-auto">
        <!-- Modal content -->
        <div class="relative p-4 bg-white rounded-lg shadow dark:bg-gray-800 sm:p-5">
            <!-- Modal header -->
            <div class="flex items-center justify-between pb-4 mb-4 border-b rounded-t sm:mb-5 dark:border-gray-600">

                <div class="flex items-center space-x-2 text-gray-900 dark:text-white">
                    {{-- map-pin --}}
                    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" class="size-6">
                        <path fill-rule="evenodd"
                            d="m11.54 22.351.07.04.028.016a.76.76 0 0 0 .723 0l.028-.015.071-.041a16.975 16.975 0 0 0 1.144-.742 19.58 19.58 0 0 0 2.683-2.282c1.944-1.99 3.963-4.98 3.963-8.827a8.25 8.25 0 0 0-16.5 0c0 3.846 2.02 6.837 3.963 8.827a19.58 19.58 0 0 0 2.682 2.282 16.975 16.975 0 0 0 1.145.742ZM12 13.5a3 3 0 1 0 0-6 3 3 0 0 0 0 6Z"
                            clip-rule="evenodd" />
                    </svg>



                    <h3 class="text-lg font-semibold text-gray-900 dark:text-white">
                        @lang('globals::locations.modal.create.heading')
                    </h3>
                </div>
                <button x-on:click="hideModal()" type="button"
                    class="text-gray-400 bg-transparent hover:bg-gray-200 hover:text-gray-900 rounded-lg text-sm p-1.5 ml-auto inline-flex items-center dark:hover:bg-gray-600 dark:hover:text-white">
                    <svg class="w-5 h-5" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg">
                        <path fill-rule="evenodd"
                            d="M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z"
                            clip-rule="evenodd"></path>
                    </svg>
                    <span class="sr-only">
                        @lang('globals::global.action.close')
                    </span>
                </button>
            </div>
            <!-- Modal body -->
            <div>
                <div class="grid gap-4 mb-4 sm:grid-cols-2">
                    <div>
                        <div class="flex items-center mb-2 space-x-1">
                            <label for="label" class="block text-sm font-medium text-gray-900 dark:text-white">
                                @lang('globals::global.label.label')
                                <x-globals::global.input-required-hint />
                            </label>
                        </div>
                        <x-globals::global.input-multi-language-json :labelsJson="$labelsJson" target="create" />
                        <x-input-error for="labelsJson" />
                    </div>
                    <div>
                        <label for="location_type_id"
                            class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">
                            @lang('globals::locations.label.location-type')
                            <x-globals::global.input-required-hint />
                        </label>
                        <select :disabled="createSucceeded" wire:model="location_type_id" id="location_type_id"
                            class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-primary-500 focus:border-primary-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-primary-500 dark:focus:border-primary-500"
                            placeholder="@lang('globals::locations.label.location-type')" required="" />
                            <option>
                                @lang('globals::global.select-option.please-select')
                            </option>
                            @foreach ($locationTypes as $locationType)
                                <option value="{{ $locationType->id }}">
                                    {{ $locationType->identifier }}
                                </option>
                            @endforeach
                        </select>
                        <x-input-error for="location_type_id" />
                    </div>
                    <div>
                        <label for="master_location_id"
                            class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">
                            @lang('globals::locations.label.parent-location')
                            <x-globals::global.input-required-hint />
                        </label>
                        <select :disabled="createSucceeded" wire:model="master_location_id" id="master_location_id"
                            class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-primary-500 focus:border-primary-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-primary-500 dark:focus:border-primary-500"
                            placeholder="@lang('globals::locations.label.parent-location')" required="" />
                            <option>
                                @lang('globals::global.select-option.empty')
                            </option>
                            @foreach ($locations as $location)
                                <option value="{{ $location->id }}">
                                    {{ $location->labels->{app()->getLocale()} }}
                                </option>
                            @endforeach
                        </select>
                        <x-input-error for="master_location_id" />
                    </div>
                    <div>
                        <div class="flex items-center mb-2 space-x-1">
                            <label for="city" class="block text-sm font-medium text-gray-900 dark:text-white">
                                @lang('globals::locations.label.city')
                            </label>
                        </div>
                        <input :disabled="createSucceeded" @keyup.enter="$wire.createLocation" type="text"
                            wire:model="city" id="city"
                            class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-primary-600 focus:border-primary-600 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-primary-500 dark:focus:border-primary-500"
                            placeholder="@lang('globals::locations.label.city')" required="" />
                        <x-input-error for="city" />
                    </div>
                    <div>
                        <div class="flex items-center mb-2 space-x-1">
                            <label for="postcode" class="block text-sm font-medium text-gray-900 dark:text-white">
                                @lang('globals::locations.label.zip-code')
                            </label>
                        </div>
                        <input :disabled="createSucceeded" @keyup.enter="$wire.createLocation" type="text"
                            wire:model="postcode" id="postcode"
                            class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-primary-600 focus:border-primary-600 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-primary-500 dark:focus:border-primary-500"
                            placeholder="@lang('globals::locations.label.zip-code')" required="" />
                        <x-input-error for="postcode" />
                    </div>
                    <div>
                        <div class="flex items-center mb-2 space-x-1">
                            <label for="street" class="block text-sm font-medium text-gray-900 dark:text-white">
                                @lang('globals::locations.label.street')
                            </label>
                        </div>
                        <input :disabled="createSucceeded" @keyup.enter="$wire.createLocation" type="text"
                            wire:model="street" id="street"
                            class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-primary-600 focus:border-primary-600 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-primary-500 dark:focus:border-primary-500"
                            placeholder="@lang('globals::locations.label.street')" required="" />
                        <x-input-error for="street" />
                    </div>
                    <div>
                        <div class="flex items-center mb-2 space-x-1">
                            <label for="housenumber" class="block text-sm font-medium text-gray-900 dark:text-white">
                                @lang('globals::locations.label.house-number')
                            </label>
                        </div>
                        <input :disabled="createSucceeded" @keyup.enter="$wire.createLocation" type="text"
                            wire:model="housenumber" id="housenumber"
                            class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-primary-600 focus:border-primary-600 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-primary-500 dark:focus:border-primary-500"
                            placeholder="@lang('globals::locations.label.house-number')" required="" />
                        <x-input-error for="housenumber" />
                    </div>
                    <div>
                        <div class="flex items-center mb-2 space-x-1">
                            <label for="email" class="block text-sm font-medium text-gray-900 dark:text-white">
                                @lang('globals::global.label.email')
                            </label>
                        </div>
                        <input :disabled="createSucceeded" @keyup.enter="$wire.createLocation" type="text"
                            wire:model="email" id="email"
                            class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-primary-600 focus:border-primary-600 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-primary-500 dark:focus:border-primary-500"
                            placeholder="@lang('globals::global.label.email')" required="" />
                        <x-input-error for="email" />
                    </div>
                    <div>
                        <div class="flex items-center mb-2 space-x-1">
                            <label for="phonenumber" class="block text-sm font-medium text-gray-900 dark:text-white">
                                @lang('globals::locations.label.phone-number')
                            </label>
                        </div>
                        <input :disabled="createSucceeded" @keyup.enter="$wire.createLocation" type="text"
                            wire:model="phonenumber" id="phonenumber"
                            class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-primary-600 focus:border-primary-600 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-primary-500 dark:focus:border-primary-500"
                            placeholder="@lang('globals::locations.label.phone-number')" required="" />
                        <x-input-error for="phonenumber" />
                    </div>
                </div>
                <div class="flex justify-end">
                    <button wire:loading.remove wire:target="createLocation" wire:click="createLocation"
                        type="button"
                        class="text-white inline-flex items-center bg-primary-700 hover:bg-primary-800 focus:ring-4 focus:outline-none focus:ring-primary-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center dark:bg-primary-600 dark:hover:bg-primary-700 dark:focus:ring-primary-800">
                        <svg class="w-6 h-6 mr-1 -ml-1" fill="currentColor" viewBox="0 0 20 20"
                            xmlns="http://www.w3.org/2000/svg">
                            <path fill-rule="evenodd"
                                d="M10 5a1 1 0 011 1v3h3a1 1 0 110 2h-3v3a1 1 0 11-2 0v-3H6a1 1 0 110-2h3V6a1 1 0 011-1z"
                                clip-rule="evenodd"></path>
                        </svg>
                        @lang('globals::locations.action.create')
                    </button>
                    <x-globals::global.loading-spinner wire:loading wire:target="createLocation" />
                </div>
            </div>
        </div>
    </div>

    <script>
        document.addEventListener('alpine:init', () => {
            Alpine.data('createLocation', () => ({
                show: false,
                createSucceeded: false,

                // CREATE
                created(event) {
                    this.createSucceeded = true;
                    this.hideModal();
                },
                resetModal() {
                    this.createSucceeded = false;

                    @this.resetModal()
                },
                hideModal() {
                    this.show = false;
                    this.resetModal();
                },
                showModal(id) {
                    this.show = true;

                    setTimeout(() => {
                        // document.getElementById('label').focus();
                    }, 100);
                }
            }));
        });
    </script>
</div>
