<!-- Tab Palazzine -->
<div class="space-y-6">
    <!-- Header con statistiche -->
    <div class="grid grid-cols-1 md:grid-cols-4 gap-6">
        <div class="bg-blue-50 rounded-lg p-4">
            <div class="flex items-center">
                <div class="p-2 bg-blue-100 rounded-lg">
                    <i class="fas fa-building text-blue-600"></i>
                </div>
                <div class="ml-3">
                    <p class="text-sm font-medium text-blue-600">Palazzine Totali</p>
                    <p class="text-2xl font-bold text-blue-900">{{ $stabile->palazzine()->count() }}</p>
                </div>
            </div>
        </div>
        
        <div class="bg-green-50 rounded-lg p-4">
            <div class="flex items-center">
                <div class="p-2 bg-green-100 rounded-lg">
                    <i class="fas fa-home text-green-600"></i>
                </div>
                <div class="ml-3">
                    <p class="text-sm font-medium text-green-600">Unità Teoriche</p>
                    <p class="text-2xl font-bold text-green-900">
                        {{ $stabile->palazzine->sum(function($p) { return $p->numero_teorico_unita; }) }}
                    </p>
                </div>
            </div>
        </div>
        
        <div class="bg-yellow-50 rounded-lg p-4">
            <div class="flex items-center">
                <div class="p-2 bg-yellow-100 rounded-lg">
                    <i class="fas fa-check-circle text-yellow-600"></i>
                </div>
                <div class="ml-3">
                    <p class="text-sm font-medium text-yellow-600">Unità Generate</p>
                    <p class="text-2xl font-bold text-yellow-900">
                        {{ $stabile->unitaImmobiliari()->count() }}
                    </p>
                </div>
            </div>
        </div>
        
        <div class="bg-purple-50 rounded-lg p-4">
            <div class="flex items-center">
                <div class="p-2 bg-purple-100 rounded-lg">
                    <i class="fas fa-chart-line text-purple-600"></i>
                </div>
                <div class="ml-3">
                    <p class="text-sm font-medium text-purple-600">Completamento</p>
                    <p class="text-2xl font-bold text-purple-900">
                        @php 
                            $teoriche = $stabile->palazzine->sum(function($p) { return $p->numero_teorico_unita; });
                            $generate = $stabile->unitaImmobiliari()->count();
                            $percentuale = $teoriche > 0 ? round(($generate / $teoriche) * 100) : 0;
                        @endphp
                        {{ $percentuale }}%
                    </p>
                </div>
            </div>
        </div>
    </div>

    <!-- Azioni rapide -->
    <div class="flex justify-between items-center">
        <h3 class="text-lg font-semibold text-gray-900">Gestione Palazzine</h3>
        <div class="flex space-x-3">
            <a href="{{ route('admin.palazzine.create', ['stabile_id' => $stabile->id]) }}" 
               class="netgescon-btn netgescon-btn-primary">
                <i class="fas fa-plus mr-2"></i>
                Nuova Palazzina
            </a>
            @if($stabile->palazzine()->count() > 0)
                <button class="netgescon-btn netgescon-btn-success" 
                        onclick="generaTutteLeUnita()">
                    <i class="fas fa-magic mr-2"></i>
                    Genera Tutte le Unità
                </button>
            @endif
        </div>
    </div>

    <!-- Elenco Palazzine -->
    @if($stabile->palazzine()->count() > 0)
        <div class="grid grid-cols-1 lg:grid-cols-2 gap-6">
            @foreach($stabile->palazzine as $palazzina)
                <div class="bg-white border border-gray-200 rounded-lg shadow-sm hover:shadow-md transition-shadow">
                    <div class="p-6">
                        <!-- Header palazzina -->
                        <div class="flex justify-between items-start mb-4">
                            <div>
                                <h4 class="text-lg font-semibold text-gray-900">
                                    Palazzina {{ $palazzina->codice_palazzina }}
                                </h4>
                                @if($palazzina->denominazione)
                                    <p class="text-sm text-gray-600">{{ $palazzina->denominazione }}</p>
                                @endif
                            </div>
                            <div class="flex space-x-2">
                                <a href="{{ route('admin.palazzine.show', $palazzina) }}" 
                                   class="text-blue-600 hover:text-blue-900">
                                    <i class="fas fa-eye"></i>
                                </a>
                                <a href="{{ route('admin.palazzine.edit', $palazzina) }}" 
                                   class="text-green-600 hover:text-green-900">
                                    <i class="fas fa-edit"></i>
                                </a>
                            </div>
                        </div>

                        <!-- Caratteristiche strutturali -->
                        <div class="grid grid-cols-2 gap-4 mb-4">
                            <div class="text-sm">
                                <span class="text-gray-500">Scale:</span>
                                <span class="font-medium ml-1">{{ $palazzina->numero_scale }}</span>
                            </div>
                            <div class="text-sm">
                                <span class="text-gray-500">Piani:</span>
                                <span class="font-medium ml-1">{{ $palazzina->numero_totale_piani }}</span>
                            </div>
                            <div class="text-sm">
                                <span class="text-gray-500">App/Piano:</span>
                                <span class="font-medium ml-1">{{ $palazzina->appartamenti_per_piano }}</span>
                            </div>
                            <div class="text-sm">
                                <span class="text-gray-500">Anno:</span>
                                <span class="font-medium ml-1">{{ $palazzina->anno_costruzione ?? 'N.D.' }}</span>
                            </div>
                        </div>

                        <!-- Servizi -->
                        <div class="flex flex-wrap gap-2 mb-4">
                            @if($palazzina->ha_ascensore)
                                <span class="inline-flex items-center px-2 py-1 rounded-full text-xs font-medium bg-green-100 text-green-800">
                                    <i class="fas fa-elevator mr-1"></i>
                                    Ascensore
                                </span>
                            @endif
                            @if($palazzina->ha_videocitofono)
                                <span class="inline-flex items-center px-2 py-1 rounded-full text-xs font-medium bg-blue-100 text-blue-800">
                                    <i class="fas fa-video mr-1"></i>
                                    Videocitofono
                                </span>
                            @endif
                            @if($palazzina->ha_giardino_comune)
                                <span class="inline-flex items-center px-2 py-1 rounded-full text-xs font-medium bg-green-100 text-green-800">
                                    <i class="fas fa-leaf mr-1"></i>
                                    Giardino
                                </span>
                            @endif
                            @if($palazzina->accessibile_disabili)
                                <span class="inline-flex items-center px-2 py-1 rounded-full text-xs font-medium bg-purple-100 text-purple-800">
                                    <i class="fas fa-wheelchair mr-1"></i>
                                    Accessibile
                                </span>
                            @endif
                        </div>

                        <!-- Statistiche unità -->
                        <div class="bg-gray-50 rounded-lg p-3">
                            <div class="flex justify-between items-center">
                                <div class="text-sm">
                                    <span class="text-gray-500">Unità:</span>
                                    <span class="font-bold text-blue-600">
                                        {{ $palazzina->numero_effettivo_unita }}/{{ $palazzina->numero_teorico_unita }}
                                    </span>
                                </div>
                                @if($palazzina->numero_effettivo_unita < $palazzina->numero_teorico_unita)
                                    <a href="{{ route('admin.palazzine.genera-unita', $palazzina) }}" 
                                       class="text-xs bg-orange-100 text-orange-700 px-2 py-1 rounded hover:bg-orange-200">
                                        <i class="fas fa-plus mr-1"></i>
                                        Genera
                                    </a>
                                @else
                                    <span class="text-xs bg-green-100 text-green-700 px-2 py-1 rounded">
                                        <i class="fas fa-check mr-1"></i>
                                        Completa
                                    </span>
                                @endif
                            </div>
                        </div>

                        <!-- Azioni -->
                        <div class="mt-4 flex justify-between">
                            <a href="{{ route('admin.palazzine.show', $palazzina) }}" 
                               class="text-sm text-blue-600 hover:text-blue-900 font-medium">
                                <i class="fas fa-arrow-right mr-1"></i>
                                Visualizza Dettagli
                            </a>
                            @if($palazzina->numero_effettivo_unita < $palazzina->numero_teorico_unita)
                                <a href="{{ route('admin.palazzine.anteprima-generazione', $palazzina) }}" 
                                   class="text-sm text-green-600 hover:text-green-900 font-medium">
                                    <i class="fas fa-eye mr-1"></i>
                                    Anteprima Generazione
                                </a>
                            @endif
                        </div>
                    </div>
                </div>
            @endforeach
        </div>
    @else
        <!-- Stato vuoto -->
        <div class="text-center py-12">
            <div class="mx-auto w-24 h-24 bg-gray-100 rounded-full flex items-center justify-center mb-4">
                <i class="fas fa-building text-gray-400 text-3xl"></i>
            </div>
            <h3 class="text-lg font-medium text-gray-900 mb-2">Nessuna palazzina configurata</h3>
            <p class="text-gray-500 mb-6">
                Inizia creando la prima palazzina per questo stabile.
            </p>
            <a href="{{ route('admin.palazzine.create', ['stabile_id' => $stabile->id]) }}" 
               class="netgescon-btn netgescon-btn-primary">
                <i class="fas fa-plus mr-2"></i>
                Crea Prima Palazzina
            </a>
        </div>
    @endif
</div>

<script>
function generaTutteLeUnita() {
    if (confirm('Vuoi generare automaticamente tutte le unità immobiliari mancanti per tutte le palazzine? Questa operazione non è reversibile.')) {
        // Implementa logica per generazione massiva
        alert('Funzionalità in sviluppo');
    }
}</script>
