
<div class="modal-header">
    <h5 class="modal-title">Modifica permessi del ruolo {{ isset($role) ? $role->display_name : '' }}</h5>
    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Chiudi"></button>
</div>
<form id="permessiRoleForm" method="POST" action="{{ route('role.updatePermission') }}">
    @csrf
    <input type="hidden" name="role_id" value="{{ isset($role) ? $role->id : '' }}">
    <!-- <input type="hidden" name="display_name" value="{{ isset($role) ? $role->display_name : '' }}">
    <input type="hidden" name="modalRoleName" value="{{ isset($role) ? $role->id : '' }}"> -->
    <div class="modal-body">
        <label class="form-label">Permessi</label>
        <div class="accordion mb-2" id="permessiAccordion">
            @php
                $allPermissions = \App\Models\Permission::all();
                $rolePermissions = isset($role) ? $role->permissions->pluck('id')->toArray() : [];
                // Raggruppa per 'group' del model Permission, fallback 'Altro'
                $permissionsByGroup = [];
                foreach($allPermissions as $perm) {
                    $group = $perm->group ?? 'Altro';
                    $permissionsByGroup[$group][] = $perm;
                }
                ksort($permissionsByGroup);

                // Conta i permessi accordati per ciascun gruppo
                $checkedCountsByGroup = [];
                foreach($permissionsByGroup as $group => $permessi) {
                    $count = 0;
                    foreach($permessi as $perm) {
                        if (in_array($perm->id, $rolePermissions)) {
                            $count++;
                        }
                    }
                    $checkedCountsByGroup[$group] = [
                        'checked' => $count,
                        'total' => count($permessi),
                    ];
                }
            @endphp

            @foreach($permissionsByGroup as $group => $permessi)
                <div class="accordion-item">
                    <h2 class="accordion-header" id="heading_{{ \Illuminate\Support\Str::slug($group) }}">
                        <button class="accordion-button collapsed py-2" type="button" data-bs-toggle="collapse" data-bs-target="#collapse_{{ \Illuminate\Support\Str::slug($group) }}" aria-expanded="false" aria-controls="collapse_{{ \Illuminate\Support\Str::slug($group) }}">
                            {{ $group }}
                            <span class="badge bg-info ms-2 align-middle">{{ $checkedCountsByGroup[$group]['checked'] }}/{{ $checkedCountsByGroup[$group]['total'] }}</span>
                        </button>
                    </h2>
                    <div id="collapse_{{ \Illuminate\Support\Str::slug($group) }}" class="accordion-collapse collapse" aria-labelledby="heading_{{ \Illuminate\Support\Str::slug($group) }}" data-bs-parent="#permessiAccordion">
                        <div class="accordion-body">
                            <div class="row">
                                @foreach($permessi as $perm)
                                <div class="col-12 col-md-6 mb-2">
                                    <div class="form-check">
                                        <input
                                            class="form-check-input"
                                            type="checkbox"
                                            name="permesso[]"
                                            id="perm_{{ $perm->id }}"
                                            value="{{ $perm->id }}"
                                            {{ in_array($perm->id, $rolePermissions) ? 'checked' : '' }}
                                        >
                                        <label class="form-check-label" for="perm_{{ $perm->id }}">
                                            {{ $perm->display_name ?? $perm->name }}
                                        </label>
                                    </div>
                                </div>
                                @endforeach
                            </div>
                        </div>
                    </div>
                </div>
            @endforeach

        </div>
    </div>
    <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Annulla</button>
        <button type="submit" class="btn btn-primary">Salva modifiche</button>
    </div>
</form>
<script>
    // Optional: gestire l'invio via AJAX o la validazione qui
    $('#permessiRoleForm').on('submit', function(e){
        // Se vuoi inviare via AJAX, decommenta queste righe e implementa la chiamata AJAX
        /*
        e.preventDefault();
        $.ajax({
            method: "POST",
            url: $(this).attr('action'),
            data: $(this).serialize(),
            success: function(data){
                // gestisci successo (es: chiudi modal, mostra notifica, ricarica tabella ruoli, ecc)
            },
            error: function(xhr){
                // gestisci errore
            }
        });
        */
    });
</script>
