<!DOCTYPE html>
<html lang="it">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Elenco Documenti - {{ $stabile->denominazione }}</title>
    <style>
        @media print {
            @page {
                margin: 1.5cm;
                size: A4 portrait;
            }
            
            .no-print {
                display: none !important;
            }
            
            .page-break {
                page-break-before: always;
            }
        }
        
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            font-size: 11px;
            line-height: 1.4;
            color: #333;
            margin: 0;
            padding: 20px;
        }
        
        .header {
            text-align: center;
            border-bottom: 3px solid #007bff;
            padding-bottom: 20px;
            margin-bottom: 30px;
        }
        
        .header h1 {
            margin: 0;
            color: #007bff;
            font-size: 24px;
            font-weight: bold;
        }
        
        .header h2 {
            margin: 5px 0 0 0;
            color: #666;
            font-size: 16px;
            font-weight: normal;
        }
        
        .info-stabile {
            background: #f8f9fa;
            padding: 15px;
            border-radius: 8px;
            margin-bottom: 30px;
            border-left: 4px solid #007bff;
        }
        
        .info-stabile h3 {
            margin: 0 0 10px 0;
            color: #007bff;
            font-size: 14px;
        }
        
        .info-grid {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 15px;
        }
        
        .info-item {
            display: flex;
        }
        
        .info-label {
            font-weight: bold;
            width: 120px;
            color: #555;
        }
        
        .info-value {
            color: #333;
        }
        
        .categoria-section {
            margin-bottom: 40px;
            break-inside: avoid;
        }
        
        .categoria-header {
            background: #007bff;
            color: white;
            padding: 10px 15px;
            margin-bottom: 15px;
            border-radius: 5px;
            font-weight: bold;
            font-size: 12px;
            text-transform: uppercase;
        }
        
        .documenti-table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 20px;
            background: white;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
        }
        
        .documenti-table th,
        .documenti-table td {
            padding: 8px 10px;
            border: 1px solid #dee2e6;
            text-align: left;
            vertical-align: top;
        }
        
        .documenti-table th {
            background: #f8f9fa;
            font-weight: bold;
            color: #495057;
            font-size: 10px;
            text-transform: uppercase;
        }
        
        .documenti-table td {
            font-size: 10px;
        }
        
        .documento-nome {
            font-weight: bold;
            color: #007bff;
        }
        
        .documento-dimensione {
            color: #6c757d;
            font-size: 9px;
        }
        
        .documento-data {
            color: #6c757d;
            white-space: nowrap;
        }
        
        .documento-descrizione {
            color: #666;
            font-style: italic;
            max-width: 200px;
            word-wrap: break-word;
        }
        
        .scadenza-warning {
            color: #dc3545;
            font-weight: bold;
        }
        
        .scadenza-danger {
            color: #dc3545;
            font-weight: bold;
            background: #f8d7da;
            padding: 2px 5px;
            border-radius: 3px;
        }
        
        .totali-section {
            background: #e9ecef;
            padding: 15px;
            border-radius: 8px;
            margin-top: 30px;
        }
        
        .totali-grid {
            display: grid;
            grid-template-columns: repeat(3, 1fr);
            gap: 20px;
        }
        
        .totale-item {
            text-align: center;
        }
        
        .totale-numero {
            font-size: 20px;
            font-weight: bold;
            color: #007bff;
        }
        
        .totale-label {
            font-size: 10px;
            color: #666;
            text-transform: uppercase;
        }
        
        .footer {
            margin-top: 40px;
            padding-top: 20px;
            border-top: 1px solid #dee2e6;
            text-align: center;
            color: #6c757d;
            font-size: 9px;
        }
        
        .no-documents {
            text-align: center;
            color: #6c757d;
            font-style: italic;
            padding: 30px;
        }
    </style>
</head>
<body>
    <!-- Header -->
    <div class="header">
        <h1>Elenco Documenti</h1>
        <h2>{{ $stabile->denominazione }}</h2>
    </div>

    <!-- Informazioni Stabile -->
    <div class="info-stabile">
        <h3>Informazioni Stabile</h3>
        <div class="info-grid">
            <div class="info-item">
                <span class="info-label">Codice:</span>
                <span class="info-value">{{ $stabile->codice_stabile ?? 'N/D' }}</span>
            </div>
            <div class="info-item">
                <span class="info-label">Indirizzo:</span>
                <span class="info-value">{{ $stabile->indirizzo ?? 'N/D' }}</span>
            </div>
            <div class="info-item">
                <span class="info-label">Città:</span>
                <span class="info-value">{{ $stabile->citta ?? 'N/D' }} ({{ $stabile->provincia ?? 'N/D' }})</span>
            </div>
            <div class="info-item">
                <span class="info-label">Amministratore:</span>
                <span class="info-value">{{ $stabile->amministratore_nome ?? 'N/D' }}</span>
            </div>
        </div>
    </div>

    @php
        $totalDocuments = $documenti->flatten()->count();
        $totalSize = $documenti->flatten()->sum('dimensione');
        $categorieAttive = $documenti->count();
    @endphp

    @if($totalDocuments > 0)
        <!-- Sezioni per Categoria -->
        @foreach(\App\Models\DocumentoStabile::categorie() as $codiceCategoria => $nomeCategoria)
            @if(isset($documenti[$codiceCategoria]) && $documenti[$codiceCategoria]->count() > 0)
                <div class="categoria-section">
                    <div class="categoria-header">
                        {{ $nomeCategoria }} ({{ $documenti[$codiceCategoria]->count() }} documenti)
                    </div>
                    
                    <table class="documenti-table">
                        <thead>
                            <tr>
                                <th style="width: 35%">Nome Documento</th>
                                <th style="width: 25%">Descrizione</th>
                                <th style="width: 10%">Dimensione</th>
                                <th style="width: 12%">Data Caricamento</th>
                                <th style="width: 12%">Scadenza</th>
                                <th style="width: 6%">Downloads</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach($documenti[$codiceCategoria] as $documento)
                                <tr>
                                    <td>
                                        <div class="documento-nome">{{ $documento->nome_originale }}</div>
                                        <div class="documento-dimensione">{{ $documento->nome_file }}</div>
                                    </td>
                                    <td>
                                        <div class="documento-descrizione">
                                            {{ $documento->descrizione ?: 'Nessuna descrizione' }}
                                        </div>
                                        @if($documento->tags)
                                            <div style="font-size: 8px; color: #007bff; margin-top: 3px;">
                                                Tags: {{ $documento->tags }}
                                            </div>
                                        @endif
                                    </td>
                                    <td class="documento-dimensione">
                                        {{ $documento->dimensione_formattata }}
                                    </td>
                                    <td class="documento-data">
                                        {{ $documento->created_at->format('d/m/Y H:i') }}
                                        @if($documento->caricatore)
                                            <br><small>da {{ $documento->caricatore->name }}</small>
                                        @endif
                                    </td>
                                    <td>
                                        @if($documento->data_scadenza)
                                            @php
                                                $statoScadenza = $documento->stato_scadenza;
                                            @endphp
                                            <span class="@if($statoScadenza['tipo'] === 'scaduto' || $statoScadenza['tipo'] === 'in_scadenza_critica') scadenza-danger @elseif($statoScadenza['tipo'] === 'in_scadenza') scadenza-warning @endif">
                                                {{ $documento->data_scadenza->format('d/m/Y') }}
                                                @if($statoScadenza['tipo'] === 'scaduto')
                                                    <br><small>SCADUTO ({{ $statoScadenza['giorni'] }}gg fa)</small>
                                                @elseif($statoScadenza['tipo'] === 'in_scadenza_critica')
                                                    <br><small>CRITICO ({{ $statoScadenza['giorni'] }}gg)</small>
                                                @elseif($statoScadenza['tipo'] === 'in_scadenza')
                                                    <br><small>{{ $statoScadenza['giorni'] }} giorni</small>
                                                @endif
                                            </span>
                                        @else
                                            <span style="color: #6c757d;">-</span>
                                        @endif
                                    </td>
                                    <td style="text-align: center;">
                                        {{ $documento->downloads }}
                                    </td>
                                </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>
            @endif
        @endforeach

        <!-- Totali -->
        <div class="totali-section">
            <div class="totali-grid">
                <div class="totale-item">
                    <div class="totale-numero">{{ $totalDocuments }}</div>
                    <div class="totale-label">Documenti Totali</div>
                </div>
                <div class="totale-item">
                    <div class="totale-numero">{{ $categorieAttive }}</div>
                    <div class="totale-label">Categorie Attive</div>
                </div>
                <div class="totale-item">
                    <div class="totale-numero">
                        @if($totalSize > 1024*1024*1024)
                            {{ number_format($totalSize / (1024*1024*1024), 2) }} GB
                        @elseif($totalSize > 1024*1024)
                            {{ number_format($totalSize / (1024*1024), 2) }} MB
                        @else
                            {{ number_format($totalSize / 1024, 2) }} KB
                        @endif
                    </div>
                    <div class="totale-label">Spazio Utilizzato</div>
                </div>
            </div>
        </div>
    @else
        <div class="no-documents">
            <h3>Nessun documento caricato</h3>
            <p>Non sono stati caricati documenti per questo stabile.</p>
        </div>
    @endif

    <!-- Footer -->
    <div class="footer">
        <p>
            Elenco generato il {{ now()->format('d/m/Y H:i') }} - NetGesCon Unified Platform<br>
            {{ $totalDocuments }} documento/i totale/i
        </p>
    </div>

    <!-- Pulsante Stampa -->
    <div class="no-print" style="position: fixed; top: 20px; right: 20px;">
        <button onclick="window.print()" style="padding: 10px 20px; background: #007bff; color: white; border: none; border-radius: 5px; cursor: pointer;">
            🖨️ Stampa
        </button>
        <button onclick="window.close()" style="padding: 10px 20px; background: #6c757d; color: white; border: none; border-radius: 5px; cursor: pointer; margin-left: 10px;">
            ✖️ Chiudi
        </button>
    </div>

    <script>
        // Auto-print quando la pagina è caricata (opzionale)
        // window.onload = function() { window.print(); };
    </script>
</body>
</html>
