@php
    $jsonPermissionLists = json_encode($permissionLists ?? []);
@endphp

<div class="grid grid-cols-1 gap-4 sm:gap-5 lg:gap-6" x-data="{
    permissionListAlpine: {!! $jsonPermissionLists !!},
    submit() {
        @this.call('submitEditForm', this.permissionListAlpine);
    }
}">

    <form wire:submit.prevent="">
        @csrf
        <div class="card pb-4">
            <div class="my-3 flex h-8 items-center justify-between px-4 sm:px-5">
                <h2 class="font-medium tracking-wide text-slate-700 line-clamp-1 dark:text-navy-100 lg:text-base">
                    <i aria-hidden="true" class="fa-solid fa-edit text-primary"></i> Create
                </h2>
            </div>
            <div class="w-3/4 px-3 space-y-3 m-auto mb-4">
                <div class="grid grid-cols-1">
                    <div class="flex items-center mb-2 ml-4">
                        <label for="name" class="w-3/12 mr-2">
                            <span id='name'>Name</span>
                        </label>
                        <span class="relative flex w-full">
                            <input
                                class="form-input w-3/4 rounded-lg border border-slate-300 bg-transparent px-3 py-2 text-xs placeholder-text-slate-400/70 hover:border-slate-400 focus:border-primary dark:border-navy-450 dark:hover:border-navy-400 dark:focus:border-accent"
                                placeholder="" type="text" wire:model.defer="name" />
                            @error('name')
                                <div class="ml-3 text-red-500 text-sm self-center">
                                    {{ $message }}</div>
                            @enderror
                        </span>
                    </div>
                    <div class="flex items-center mb-2 ml-4">
                        <label for='description' class="w-3/12 mr-2">
                            <span>Description</span>
                        </label>
                        <span class="relative flex w-full">
                            <input
                                class="form-input w-3/4 rounded-lg border border-slate-300 bg-transparent px-3 py-2 text-xs pl-4 placeholder:text-slate-400/70 hover:border-slate-400 focus:border-primary dark:border-navy-450 dark:hover:border-navy-400 dark:focus:border-accent"
                                placeholder="" wire:model.defer="description" type="text" />
                        </span>
                    </div>
                </div>
                <div class="tabs flex flex-col">
                    <div x-data="{ activeTab: 'Import' }" class="tabs flex flex-col">

                        <div class="tab-content pt-4  px-2 mt-2 pb-2">
                            <div x-transition:enter="transition-all duration-500 ease-in-out"
                                x-transition:enter-start="opacity-0 [transform:translate3d(1rem,0,0)]"
                                x-transition:enter-end="opacity-100 [transform:translate3d(0,0,0)]">
                                <div
                                    class="grid grid-cols-1 gap-4 sm:grid-cols-2 sm:gap-5 lg:grid-cols-3 lg:gap-6 xl:grid-cols-3">
                                    @php
                                        $currentGroup = '';
                                        $tabGroup = [
                                            'User Management',
                                            'Role Management',
                                            'Group Management',
                                            'Company Management',
                                            'Company Type Management',
                                            'Server License Management',
                                            'Patch Management',
                                            'Send Patch Management',
                                            'Parameter Management',
                                            'Patch Exchange Rate Management',
                                            'Patch Discharge Port Management',
                                        ];
                                    @endphp

                                    @foreach ($permissions as $permission)
                                        @if (!in_array($permission['permission_group_name'], $tabGroup))
                                            @continue
                                        @endif
                                        @if ($permission['permission_group_name'] !== $currentGroup)
                                            @if ($currentGroup !== '')
                                </div>
                            </div>
                            @endif

                            <div x-data="{
                                allChecked: false,
                                permissionList: @entangle('permissionLists'),
                            
                                toggleAll(group) {
                                    this.allChecked = !this.allChecked;
                                    document.querySelectorAll(`.permissionGroup[data-group='${group}'] input[type='checkbox']`).forEach(checkbox => {
                                        checkbox.checked = this.allChecked;
                                        if (this.allChecked) {
                                            if (!this.permissionListAlpine.includes(checkbox.value)) {
                                                this.permissionListAlpine.push(checkbox.value);
                                            }
                                        } else {
                                            this.permissionListAlpine = this.permissionListAlpine.filter(id => id != checkbox.value);
                                        }
                                    });
                                },
                                toggleIndividual(event) {
                                    event.stopPropagation();
                                    this.allChecked = this.permissionListAlpine.length === @json($permissions->count());
                                }
                            }"
                                class="card grow items-center p-4 sm:p-5 cursor-pointer permissionGroup"
                                data-group="{{ $permission['permission_group_name'] }}"
                                @click="toggleAll('{{ $permission['permission_group_name'] }}')">
                                <h3 class="pt-3 text-lg font-medium text-slate-700 dark:text-navy-100">
                                    {{ $permission['permission_group_name'] }}
                                </h3>
                                <div class="my-4 h-px w-full bg-slate-200 dark:bg-navy-500"></div>
                                <div class="grow space-y-4 permission-list">
                                    @endif

                                    <div class="flex items-center space-x-4">
                                        <label class="inline-flex items-center space-x-2"
                                            @click="toggleIndividual($event)">
                                            <input
                                                class="form-switch h-5 w-10 rounded-full bg-slate-300 before:rounded-full before:bg-slate-50 checked:!bg-primary checked:before:bg-white dark:bg-navy-900 dark:before:bg-navy-300 dark:checked:before:bg-white"
                                                type="checkbox" x-model="permissionListAlpine"
                                                value="{{ $permission['id'] }}" />
                                            <span>{{ $permission['name'] }}</span>
                                        </label>
                                    </div>
                                    @php $currentGroup = $permission['permission_group_name']; @endphp
                                    @endforeach
                                    @if ($currentGroup !== '')
                                </div>
                                @endif
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </form>
    <div class="grid grid-cols-6 justify-center space-x-2 pt-4">
        <span wire:click="goBack"
            class="col-start-3 btn border border-slate-300 font-medium text-slate-800 hover:bg-slate-150 focus:bg-slate-150 active:bg-slate-150/80 dark:border-navy-450 dark:text-navy-50 dark:hover:bg-navy-500 dark:focus:bg-navy-500 dark:active:bg-navy-500/90">
            Cancel
        </span>
        <button type="button" @click="submit"
            class="btn ml-3 bg-primary font-medium text-white hover:bg-primary-focus focus:bg-primary-focus active:bg-primary-focus/90 dark:bg-accent dark:hover:bg-accent-focus dark:focus:bg-accent-focus dark:active:bg-accent/90">
            Save
        </button>
    </div>
</div>
