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

<div>
    <div x-data="editRole" x-cloak x-show="show" id="editRoleModal" tabindex="-1"
        x-on:show-edit-role-modal.window="showModal($event.detail)"
        x-on:role-saved.window="saved($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-4xl 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">
                        {{-- shield-check --}}
                        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" class="size-6">
                            <path fill-rule="evenodd" d="M12.516 2.17a.75.75 0 0 0-1.032 0 11.209 11.209 0 0 1-7.877 3.08.75.75 0 0 0-.722.515A12.74 12.74 0 0 0 2.25 9.75c0 5.942 4.064 10.933 9.563 12.348a.749.749 0 0 0 .374 0c5.499-1.415 9.563-6.406 9.563-12.348 0-1.39-.223-2.73-.635-3.985a.75.75 0 0 0-.722-.516l-.143.001c-2.996 0-5.717-1.17-7.704-3.08Zm3.22 7.658a.75.75 0 0 0-1.22-.872l-3.236 4.53L9.53 12.22a.75.75 0 0 0-1.06 1.06l2.25 2.25a.75.75 0 0 0 1.14-.094l3.75-5.25Z" clip-rule="evenodd" />
                        </svg>

                        <h3 class="text-lg font-semibold text-gray-900 dark:text-white">
                            @lang('globals::roles.modal.edit.heading')
                        </h3>

                        @if ($internal)
                            <span class="ml-2 inline-flex items-center rounded-md bg-yellow-100 px-2 py-1 text-xs font-medium text-yellow-800 dark:bg-yellow-900 dark:text-yellow-300">
                                @lang('globals::roles.label.internal')
                            </span>
                        @endif
                    </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="mb-4 space-y-4">

                        {{-- Row 1: Name --}}
                        <div>
                            <label for="edit-role-name" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">
                                @lang('globals::roles.label.name')
                            </label>
                            <input type="text" wire:model="name" id="edit-role-name"
                                @if ($internal) disabled @endif
                                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 disabled:opacity-50 disabled:cursor-not-allowed"
                                placeholder="@lang('globals::roles.placeholder.name')" />
                            <x-input-error for="name" />
                        </div>

                        {{-- Row 2: Labels --}}
                        @if (!$internal)
                        <div>
                            <label class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">
                                @lang('globals::roles.label.labels')
                            </label>
                            <x-globals::global.input-multi-language-json :labelsJson="$labelsJson" target="edit" />
                            <x-input-error for="labelsJson" />
                        </div>
                        @endif

                        {{-- Row 3: Active --}}
                        <div class="flex items-center">
                            <label class="inline-flex items-center {{ $internal ? 'opacity-50 cursor-not-allowed' : 'cursor-pointer' }}">
                                <input id="edit-role-active" wire:model="active" type="checkbox"
                                    @if ($internal) disabled @endif
                                    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::roles.label.active')
                                </span>
                            </label>
                        </div>

                        {{-- Permission Matrix --}}
                        @if (count($permissionGroups) > 0)
                        <div>
                            <h4 class="mb-3 text-sm font-semibold text-gray-900 dark:text-white">
                                @lang('globals::roles.label.permissions')
                            </h4>

                            <div class="overflow-x-auto rounded-lg border border-gray-200 dark:border-gray-700">
                                <table class="w-full text-sm text-left text-gray-700 dark:text-gray-300">
                                    <thead class="text-xs text-gray-700 uppercase bg-gray-50 dark:bg-gray-700 dark:text-gray-400">
                                        <tr>
                                            <th class="px-4 py-2 w-1/3">@lang('globals::roles.label.permission-resource')</th>
                                            @foreach ($permissionActions as $action)
                                                <th class="px-4 py-2 text-center">{{ $action }}</th>
                                            @endforeach
                                        </tr>
                                    </thead>
                                    @foreach ($permissionGroups as $package => $resources)
                                        @php
                                            $packageOpen = false;
                                            foreach ($resources as $resource => $resourceActions) {
                                                foreach (array_keys($resourceActions) as $action) {
                                                    if (($permissionMatrix[$resource][$action] ?? false) === true) {
                                                        $packageOpen = true;
                                                        break 2;
                                                    }
                                                }
                                            }
                                        @endphp
                                        <tbody x-data="{ open: {{ $packageOpen ? 'true' : 'false' }} }">
                                            <tr @click="open = !open"
                                                class="cursor-pointer bg-gray-100 dark:bg-gray-700 hover:bg-gray-200 dark:hover:bg-gray-600 select-none">
                                                <td colspan="{{ count($permissionActions) + 1 }}"
                                                    class="px-4 py-1 text-xs font-semibold text-gray-500 dark:text-gray-400 uppercase tracking-wider">
                                                    <span class="flex items-center gap-1">
                                                        <svg x-show="!open" class="w-3 h-3 shrink-0" fill="currentColor" viewBox="0 0 20 20"><path fill-rule="evenodd" d="M7.293 14.707a1 1 0 010-1.414L10.586 10 7.293 6.707a1 1 0 011.414-1.414l4 4a1 1 0 010 1.414l-4 4a1 1 0 01-1.414 0z" clip-rule="evenodd"/></svg>
                                                        <svg x-show="open" class="w-3 h-3 shrink-0" fill="currentColor" viewBox="0 0 20 20"><path fill-rule="evenodd" d="M5.293 7.293a1 1 0 011.414 0L10 10.586l3.293-3.293a1 1 0 111.414 1.414l-4 4a1 1 0 01-1.414 0l-4-4a1 1 0 010-1.414z" clip-rule="evenodd"/></svg>
                                                        {{ $package }}
                                                    </span>
                                                </td>
                                            </tr>
                                            @foreach ($resources as $resource => $actions)
                                                <tr x-show="open"
                                                    class="border-t border-gray-100 dark:border-gray-700 hover:bg-gray-50 dark:hover:bg-gray-800">
                                                    <td class="px-4 py-2 font-medium">{{ $resource }}</td>
                                                    @foreach ($permissionActions as $action)
                                                        <td class="px-4 py-2 text-center">
                                                            @if (isset($actions[$action]))
                                                                <input type="checkbox"
                                                                    wire:model="permissionMatrix.{{ $resource }}.{{ $action }}"
                                                                    @if ($internal) disabled @endif
                                                                    class="w-4 h-4 text-primary-600 bg-gray-100 border-gray-300 rounded focus:ring-primary-500 dark:focus:ring-primary-600 dark:ring-offset-gray-800 dark:focus:ring-offset-gray-800 focus:ring-2 dark:bg-gray-700 dark:border-gray-600 disabled:opacity-50 disabled:cursor-not-allowed">
                                                            @else
                                                                <span class="text-gray-300 dark:text-gray-600">—</span>
                                                            @endif
                                                        </td>
                                                    @endforeach
                                                </tr>
                                            @endforeach
                                        </tbody>
                                    @endforeach
                                </table>
                            </div>
                        </div>
                        @endif

                    </div>

                    @if (!$internal)
                    <div class="flex justify-end">
                        <button wire:loading.remove wire:target="editRole" wire:click="editRole"
                            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 -ml-1" 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="M5 13l4 4L19 7"></path>
                            </svg>
                            @lang('globals::roles.action.edit')
                        </button>
                        <x-globals::global.loading-spinner wire:loading wire:target="editRole" />
                    </div>
                    @endif
                </div>
                <!-- /Modal body -->

            </div>
        </div>

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

                    saved(event) {
                        this.hideModal();
                    },
                    resetModal() {
                        @this.resetModal()
                    },
                    hideModal() {
                        this.show = false;
                        this.resetModal();
                    },
                    showModal(detail) {
                        this.show = true;
                    }
                }));
            });
        </script>

    </div>
</div>
