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

<div x-data="editUser" x-cloak x-show="show" id="editUserModal" tabindex="-1"
    x-on:show-edit-user-modal.window="showModal($event)"
    x-on:user-modal-initialized.window="onModalInitialized($event)"
    x-on:user-saved.window="onSaved($event)"
    x-on:user-password-reset-sent.window="onPasswordResetSent($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">
                    <h3 class="text-lg font-semibold text-gray-900 dark:text-white">
                        @lang('globals::users.modal.edit.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 clip-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" fill-rule="evenodd" />
                    </svg>
                    <span class="sr-only">
                        @lang('globals::global.action.close')
                    </span>
                </button>
            </div>

            <!-- Tab navigation -->
            <div class="border-b border-gray-200 dark:border-gray-700 mb-4">
                <ul class="flex flex-wrap -mb-px text-sm font-medium text-center">
                    <li class="me-2">
                        <button x-on:click="activeTab = 'base'" type="button"
                            :class="activeTab === 'base'
                                ? 'inline-flex items-center justify-center p-4 text-primary-600 border-b-2 border-primary-600 rounded-t-lg active dark:text-primary-500 dark:border-primary-500'
                                : 'inline-flex items-center justify-center p-4 border-b-2 border-transparent rounded-t-lg hover:text-gray-600 hover:border-gray-300 dark:hover:text-gray-300 text-gray-500 dark:text-gray-400'">
                            @lang('globals::users.tab.base')
                        </button>
                    </li>
                    <li class="me-2">
                        <button x-on:click="activeTab = 'user-data'" type="button"
                            :class="activeTab === 'user-data'
                                ? 'inline-flex items-center justify-center p-4 text-primary-600 border-b-2 border-primary-600 rounded-t-lg active dark:text-primary-500 dark:border-primary-500'
                                : 'inline-flex items-center justify-center p-4 border-b-2 border-transparent rounded-t-lg hover:text-gray-600 hover:border-gray-300 dark:hover:text-gray-300 text-gray-500 dark:text-gray-400'">
                            @lang('globals::users.tab.user-data')
                        </button>
                    </li>
                    <li class="me-2">
                        <button x-on:click="if ({{ $restricted ? 'true' : 'false' }}) activeTab = 'tenant-access'" type="button"
                            :class="activeTab === 'tenant-access'
                                ? 'inline-flex items-center justify-center p-4 text-primary-600 border-b-2 border-primary-600 rounded-t-lg active dark:text-primary-500 dark:border-primary-500'
                                : 'inline-flex items-center justify-center p-4 border-b-2 border-transparent rounded-t-lg hover:text-gray-600 hover:border-gray-300 dark:hover:text-gray-300 {{ $restricted ? "text-gray-500 dark:text-gray-400" : "text-gray-300 dark:text-gray-600 cursor-not-allowed" }}'">
                            @lang('globals::users.tab.tenant-access')
                        </button>
                    </li>
                </ul>
            </div>

            <!-- Modal body -->
            <div>

                <!-- Tab 1: Basisdaten -->
                <div x-show="activeTab === 'base'">

                    <div class="grid gap-4 mb-4 sm:grid-cols-2">

                        <!-- Zeile 1: Mandant (disabled) | Aktiv -->
                        <div>
                            <label for="edit-user-tenant" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">
                                @lang('globals::users.label.tenant')
                            </label>
                            <select id="edit-user-tenant" disabled
                                class="bg-gray-100 border border-gray-300 text-gray-500 text-sm rounded-lg block w-full p-2.5 cursor-not-allowed dark:bg-gray-700 dark:border-gray-600 dark:text-gray-400">
                                @if ($tenantSelect !== null)
                                    <option selected>{{ current($tenantSelect) }}</option>
                                @endif
                            </select>
                        </div>

                        <div class="flex items-end pb-1">
                            <label class="inline-flex items-center cursor-pointer">
                                <input type="checkbox" wire:model="active" class="sr-only peer" />
                                <div class="relative w-11 h-6 bg-gray-200 peer-focus:outline-none peer-focus:ring-4 peer-focus:ring-primary-300 dark:peer-focus:ring-primary-800 rounded-full peer dark:bg-gray-700 peer-checked:after:translate-x-full rtl:peer-checked:after:-translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:start-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all dark:border-gray-600 peer-checked:bg-primary-600"></div>
                                <span class="ms-3 text-sm font-medium text-gray-900 dark:text-gray-300">
                                    @lang('globals::global.label.active')
                                </span>
                            </label>
                        </div>

                        <!-- Zeile 2: Username | Restricted | Internal -->
                        <div>
                            <label for="edit-user-name" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">
                                @lang('globals::users.label.name') <span class="text-red-500">*</span>
                            </label>
                            <input type="text" id="edit-user-name" wire:model="name"
                                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" />
                            @error('name') <p class="mt-1 text-xs text-red-600 dark:text-red-400">{{ $message }}</p> @enderror
                        </div>

                        <div class="flex items-end gap-6 pb-1">

@if ($editorRestricted === false)

                            <label class="inline-flex items-center cursor-pointer">
                                <input type="checkbox" wire:model.live="restricted" class="sr-only peer" />
                                <div class="relative w-11 h-6 bg-gray-200 peer-focus:outline-none peer-focus:ring-4 peer-focus:ring-primary-300 dark:peer-focus:ring-primary-800 rounded-full peer dark:bg-gray-700 peer-checked:after:translate-x-full rtl:peer-checked:after:-translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:start-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all dark:border-gray-600 peer-checked:bg-primary-600"></div>
                                <span class="ms-3 text-sm font-medium text-gray-900 dark:text-gray-300">
                                    @lang('globals::users.label.restricted')
                                </span>
                            </label>

@endif

                            <label class="inline-flex items-center cursor-pointer">
                                <input type="checkbox" wire:model="internal" class="sr-only peer" />
                                <div class="relative w-11 h-6 bg-gray-200 peer-focus:outline-none peer-focus:ring-4 peer-focus:ring-primary-300 dark:peer-focus:ring-primary-800 rounded-full peer dark:bg-gray-700 peer-checked:after:translate-x-full rtl:peer-checked:after:-translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:start-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all dark:border-gray-600 peer-checked:bg-primary-600"></div>
                                <span class="ms-3 text-sm font-medium text-gray-900 dark:text-gray-300">
                                    @lang('globals::users.label.internal')
                                </span>
                            </label>
                        </div>

                        <!-- Zeile 3: E-Mail (disabled) | AccessLevel -->
                        <div>
                            <label for="edit-user-email" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">
                                @lang('globals::users.label.email')
                            </label>
                            <input type="email" id="edit-user-email" disabled
                                value="{{ $email }}"
                                class="bg-gray-100 border border-gray-300 text-gray-500 text-sm rounded-lg block w-full p-2.5 cursor-not-allowed dark:bg-gray-700 dark:border-gray-600 dark:text-gray-400" />
                        </div>

                        <div>
                            @if ($accessLevels !== null)
                                <label for="edit-user-access-level" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">
                                    @lang('globals::users.label.access_level') <span class="text-red-500">*</span>
                                </label>
                                <select id="edit-user-access-level" wire:model="accessLevel"
                                    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">
                                    <option value="">—</option>
                                    @foreach ($accessLevels as $key => $label)
                                        <option value="{{ $key }}" @selected($key === $accessLevel)>{{ $label }}</option>
                                    @endforeach
                                </select>
                                @error('accessLevel') <p class="mt-1 text-xs text-red-600 dark:text-red-400">{{ $message }}</p> @enderror
                            @endif
                        </div>

@if ($canSetPassword === true)

                        <!-- Zeile 4: Passwort | Passwort-Bestätigung (nur Developer+) -->
                        <div>
                            <label for="edit-user-password" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">
                                @lang('globals::users.label.password')
                            </label>
                            <input type="password" id="edit-user-password" wire:model="password"
                                placeholder="{{ __('globals::users.placeholder.password-leave-empty') }}"
                                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" />
                            @error('password') <p class="mt-1 text-xs text-red-600 dark:text-red-400">{{ $message }}</p> @enderror
                        </div>

                        <div>
                            <label for="edit-user-password-confirmation" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">
                                @lang('globals::users.label.password_confirmation')
                            </label>
                            <input type="password" id="edit-user-password-confirmation" wire:model="passwordConfirmation"
                                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" />
                            @error('passwordConfirmation') <p class="mt-1 text-xs text-red-600 dark:text-red-400">{{ $message }}</p> @enderror
                        </div>

@endif

                        <!-- Zeile 5: Passwort-Reset-Button -->
                        <div class="sm:col-span-2">
                            <button wire:loading.remove wire:target="sendPasswordReset" wire:click="sendPasswordReset" type="button"
                                class="text-gray-700 inline-flex items-center border border-gray-300 hover:bg-gray-100 focus:ring-4 focus:outline-none focus:ring-gray-200 font-medium rounded-lg text-sm px-4 py-2 text-center dark:text-gray-300 dark:border-gray-600 dark:hover:bg-gray-700 dark:focus:ring-gray-700">
                                <svg class="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 8l7.89 5.26a2 2 0 002.22 0L21 8M5 19h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v10a2 2 0 002 2z" />
                                </svg>
                                @lang('globals::users.action.reset-password')
                            </button>
                            <x-globals::global.loading-spinner wire:loading wire:target="sendPasswordReset" />
                            <p x-cloak x-show="passwordResetSent" class="mt-2 text-xs text-green-600 dark:text-green-400">
                                @lang('globals::users.info.password-reset')
                            </p>
                        </div>

                    </div>

                </div>

                <!-- Tab 2: Benutzerdaten (UserRecord) -->
                <div x-show="activeTab === 'user-data'">

                    <div class="grid gap-4 mb-4 sm:grid-cols-2">

                        <!-- Zeile 1: Vorname | Nachname -->
                        <div>
                            <label for="edit-user-first-name" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">
                                @lang('globals::users.label.first_name')
                            </label>
                            <input type="text" id="edit-user-first-name" wire:model="firstName"
                                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" />
                            @error('firstName') <p class="mt-1 text-xs text-red-600 dark:text-red-400">{{ $message }}</p> @enderror
                        </div>

                        <div>
                            <label for="edit-user-last-name" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">
                                @lang('globals::users.label.last_name')
                            </label>
                            <input type="text" id="edit-user-last-name" wire:model="lastName"
                                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" />
                            @error('lastName') <p class="mt-1 text-xs text-red-600 dark:text-red-400">{{ $message }}</p> @enderror
                        </div>

                    </div>

                </div>

                <!-- Tab 3: Mandantenzugriff -->
                <div x-show="activeTab === 'tenant-access'">

@if ($restricted === true)

                    <div class="py-2 px-1 space-y-4">

@foreach ($tenantAccess as $tenantId => $item)

                        <div class="grid gap-4 sm:grid-cols-3 items-end border-b border-gray-100 dark:border-gray-700 pb-4 last:border-0 last:pb-0">

                            <div>
                                <span class="block text-sm font-medium text-gray-700 dark:text-gray-300">{{ $item['identifier'] }}</span>
                            </div>

                            <div class="flex items-center">
                                <label class="inline-flex items-center cursor-pointer">
                                    <input type="checkbox" wire:model.live="tenantAccess.{{ $tenantId }}.active" class="sr-only peer" />
                                    <div class="relative w-11 h-6 bg-gray-200 peer-focus:outline-none peer-focus:ring-4 peer-focus:ring-primary-300 dark:peer-focus:ring-primary-800 rounded-full peer dark:bg-gray-700 peer-checked:after:translate-x-full rtl:peer-checked:after:-translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:start-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all dark:border-gray-600 peer-checked:bg-primary-600"></div>
                                    <span class="ms-3 text-sm font-medium text-gray-900 dark:text-gray-300">
                                        @lang('globals::global.label.active')
                                    </span>
                                </label>
                            </div>

                            <div>
                                @if ($item['access_level'] !== false)
                                    <label for="edit-user-tenant-{{ $tenantId }}-access-level" class="block mb-1 text-xs font-medium text-gray-700 dark:text-gray-400">
                                        @lang('globals::users.label.tenant-access.access-level')
                                    </label>
                                    <select id="edit-user-tenant-{{ $tenantId }}-access-level"
                                        wire:model.live="tenantAccess.{{ $tenantId }}.access_level"
                                        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">
                                        <option value="">—</option>
                                        @foreach ($item['accessLevels'] as $lvlKey => $lvlLabel)
                                            <option value="{{ $lvlKey }}" @selected($lvlKey === $item['access_level'])>{{ $lvlLabel }}</option>
                                        @endforeach
                                    </select>
                                @endif
                            </div>

                        </div>

@endforeach

                    </div>

@else

                    <div class="py-6 px-4 text-sm text-gray-500 dark:text-gray-400">
                        @lang('globals::users.info.tenant-access-disabled')
                    </div>

@endif

                </div>

                <!-- Erfolgsmeldung -->
                <div x-show="saveSucceeded" class="flex justify-end mb-4">
                    <p x-cloak class="mt-2 text-xs text-green-600 dark:text-green-400">
                        @lang('globals::users.info.updated')
                    </p>
                </div>

                <!-- Speichern-Button -->
                <div x-cloak x-show="!saveSucceeded" class="flex justify-end">
                    <button wire:loading.remove wire:target="save" wire:click="save" 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-5 h-5 mr-1.5" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg">
                            <path d="M17.414 2.586a2 2 0 00-2.828 0L7 10.172V13h2.828l7.586-7.586a2 2 0 000-2.828z" />
                            <path clip-rule="evenodd" d="M2 6a2 2 0 012-2h4a1 1 0 010 2H4v10h10v-4a1 1 0 112 0v4a2 2 0 01-2 2H4a2 2 0 01-2-2V6z" fill-rule="evenodd" />
                        </svg>
                        @lang('globals::users.action.edit')
                    </button>
                    <x-globals::global.loading-spinner wire:loading wire:target="save" />
                </div>

            </div>
        </div>
    </div>

    <script>

        document.addEventListener('alpine:init', () => {
            Alpine.data('editUser', () => ({
                show: false,
                saveSucceeded: false,
                passwordResetSent: false,
                activeTab: 'base',

                onModalInitialized(event) {
                    this.activeTab = event.detail.openTab ?? 'base';
                },

                onPasswordResetSent(event) {
                    this.passwordResetSent = true;
                },

                onSaved(event) {
                    this.saveSucceeded = true;

                    setTimeout(() => {
                        this.hideModal();
                    }, 2000);
                },

                hideModal() {
                    this.show = false;
                    this.saveSucceeded = false;
                    this.passwordResetSent = false;
                    this.activeTab = 'base';
                    @this.resetModal();
                },

                showModal(event) {
                    this.show = true;
                }
            }));
        });

    </script>
</div>
