@php
$children = $gruppo->children()->with(['diocesi', 'individui'])->orderBy('nome')->get();
$hasChildren = $children->count() > 0;
$canWriteGruppi = Auth::user()->canManage('gruppi');
$responsabili = $gruppo->getResponsabili();
@endphp

<div class="tree-item" style="margin-left: {{ $gruppo->depth ?? 0 }}px;">
    <div class="tree-label">
        @if($hasChildren)
            <span class="tree-toggle" onclick="toggleItem({{ $gruppo->id }})">
                <i id="icon-{{ $gruppo->id }}" class="fas fa-chevron-right text-muted"></i>
            </span>
        @else
            <span style="width: 22px; display: inline-block;"></span>
        @endif
        <i class="fas fa-{{ $hasChildren ? 'folder' : 'folder-open' }} text-{{ $gruppo->depth == 0 ? 'warning' : 'secondary' }} mr-1"></i>
        <a href="{{ route('gruppi.show', $gruppo->id) }}" class="{{ ($gruppo->depth ?? 0) == 0 ? 'font-weight-bold' : '' }}">
            {{ $gruppo->nome }}
        </a>
        <span class="badge badge-{{ ($gruppo->depth ?? 0) == 0 ? 'primary' : 'secondary' }} badge-level">{{ $gruppo->depth ?? 0 }}</span>
        <small class="text-muted ml-2">
            {{ $gruppo->diocesi?->nome ?? '' }}
            @if($responsabili->count() > 0)
                · <i class="fas fa-user text-info"></i> {{ $responsabili->pluck('cognome')->implode(', ') }}
            @endif
        </small>
        <span class="ml-auto">
            @if($gruppo->individui_count > 0)
                <span class="badge badge-info">{{ $gruppo->individui_count }} membri</span>
            @endif
            @if($hasChildren)
                <span class="badge badge-warning">{{ $children->count() }} figli</span>
            @endif
        </span>
        <span class="ml-2">
            <a href="{{ route('gruppi.show', $gruppo->id) }}" class="btn btn-xs btn-info" title="Visualizza">
                <i class="fas fa-eye"></i>
            </a>
            @if($canWriteGruppi)
            <a href="{{ url('/gruppi/' . $gruppo->id . '/edit') }}" class="btn btn-xs btn-warning" title="Modifica">
                <i class="fas fa-edit"></i>
            </a>
            @php
                $isParent = $hasChildren;
                $isSuperAdmin = Auth::user()->isSuperAdmin();
                $canDelete = !$isParent || $isSuperAdmin;
            @endphp
            @if($canDelete)
            <form action="{{ url('/gruppi/' . $gruppo->id) }}" method="POST" class="d-inline">
                @csrf @method('DELETE')
                <button type="submit" class="btn btn-xs btn-danger" onclick="return confirm('Confermi l\'eliminazione? Verranno eliminati anche tutti i sottogruppi.')" title="Elimina">
                    <i class="fas fa-trash"></i>
                </button>
            </form>
            @else
            <button type="button" class="btn btn-xs btn-danger disabled" title="Solo il superamministratore può eliminare gruppi con sottogruppi">
                <i class="fas fa-trash"></i>
            </button>
            @endif
            @endif
        </span>
    </div>
</div>

@if($hasChildren)
    <div id="group-{{ $gruppo->id }}" class="tree-children" style="display: none;">
        @foreach($children as $child)
            @include('gruppi.partials.tree-item', ['gruppo' => $child])
        @endforeach
    </div>
@endif