<!DOCTYPE html>
<html lang="it">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Report Individui Multipli - {{ now()->format('d/m/Y') }}</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            font-size: 12px;
            line-height: 1.4;
            color: #333;
            padding: 20px;
        }
        .header {
            text-align: center;
            margin-bottom: 20px;
            padding-bottom: 15px;
            border-bottom: 2px solid #2c3e50;
        }
        .header h1 {
            font-size: 24px;
            color: #2c3e50;
            margin-bottom: 5px;
        }
        .header .subtitle {
            font-size: 14px;
            color: #7f8c8d;
        }
        .individuo-block {
            margin-bottom: 30px;
            page-break-after: always;
        }
        .individuo-block:last-child {
            page-break-after: avoid;
        }
        .section {
            margin-bottom: 15px;
            page-break-inside: avoid;
        }
        .section-title {
            font-size: 14px;
            font-weight: bold;
            color: #fff;
            background: #2c3e50;
            padding: 8px 12px;
            margin-bottom: 8px;
            border-radius: 3px;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 8px;
        }
        th, td {
            border: 1px solid #ddd;
            padding: 6px 8px;
            text-align: left;
        }
        th {
            background: #f8f9fa;
            font-weight: bold;
            width: 150px;
        }
        .badge {
            display: inline-block;
            padding: 3px 8px;
            background: #6c757d;
            color: #fff;
            border-radius: 3px;
            font-size: 11px;
        }
        .badge-success { background: #28a745; }
        .badge-warning { background: #ffc107; color: #333; }
        .badge-danger { background: #dc3545; }
        .badge-info { background: #17a2b8; }
        .info-grid {
            display: grid;
            grid-template-columns: repeat(2, 1fr);
            gap: 8px;
        }
        .info-item {
            border: 1px solid #ddd;
            padding: 8px;
        }
        .info-item label {
            display: block;
            font-weight: bold;
            color: #666;
            font-size: 10px;
            text-transform: uppercase;
            margin-bottom: 2px;
        }
        .empty {
            color: #999;
            font-style: italic;
        }
        .footer {
            margin-top: 30px;
            padding-top: 15px;
            border-top: 1px solid #ddd;
            text-align: center;
            font-size: 10px;
            color: #999;
        }
        .totale {
            background: #f8f9fa;
            padding: 10px;
            text-align: center;
            margin-bottom: 20px;
            border: 2px solid #2c3e50;
            font-weight: bold;
        }
        @media print {
            body { padding: 0; }
            .individuo-block { page-break-after: always; }
            .individuo-block:last-child { page-break-after: avoid; }
        }
    </style>
</head>
<body>
    <div class="header">
        <h1>Report Individui Multipli</h1>
        <div class="subtitle">Totale: {{ $individui->count() }} individui</div>
    </div>

    <div class="totale">
        Report generato il {{ now()->format('d/m/Y H:i') }}
    </div>

    @foreach($individui as $ind)
    <div class="individuo-block">
        <div class="section">
            <div class="section-title">
                @if($ind->avatar)
                <img src="{{ url('/individui/' . $ind->id . '/avatar') }}"
                     alt="Avatar"
                     style="width: 30px; height: 30px; object-fit: cover; border-radius: 50%; margin-right: 8px; vertical-align: middle;">
                @endif
                {{ $ind->cognome }} {{ $ind->nome }} - Codice: {{ $ind->codice_id }}
            </div>
            <table>
                <tr>
                    <th>Codice</th>
                    <td><span class="badge">{{ $ind->codice_id }}</span></td>
                    <th>Genere</th>
                    <td>{{ $ind->genere === 'M' ? 'Maschile' : ($ind->genere === 'F' ? 'Femminile' : '-') }}</td>
                </tr>
                <tr>
                    <th>Data di nascita</th>
                    <td>{{ $ind->data_nascita ? $ind->data_nascita->format('d/m/Y') : '-' }}</td>
                    <th>Luogo di nascita</th>
                    <td>{{ $ind->città ?: '-' }}</td>
                </tr>
            </table>
        </div>

        <div class="section">
            <div class="section-title">Residenza</div>
            <div class="info-grid">
                <div class="info-item">
                    <label>Indirizzo</label>
                    {{ $ind->indirizzo ?: '-' }}
                </div>
                <div class="info-item">
                    <label>CAP</label>
                    {{ $ind->cap ?: '-' }}
                </div>
                <div class="info-item">
                    <label>Città</label>
                    {{ $ind->città ?: '-' }}
                </div>
                <div class="info-item">
                    <label>Provincia</label>
                    {{ $ind->sigla_provincia ?: '-' }}
                </div>
            </div>
        </div>

        <div class="section">
            <div class="section-title">Documento di Identità</div>
            @if($ind->tipo_documento || $ind->numero_documento)
            <table>
                <tr>
                    <th>Tipo</th>
                    <td>
                        @if($ind->tipo_documento === 'carta_identita')
                            <span class="badge badge-info">Carta d'Identità</span>
                        @elseif($ind->tipo_documento === 'patente')
                            <span class="badge badge-warning">Patente</span>
                        @else
                            {{ $ind->tipo_documento }}
                        @endif
                    </td>
                    <th>Numero</th>
                    <td>{{ $ind->numero_documento }}</td>
                </tr>
                <tr>
                    <th>Scadenza</th>
                    <td>
                        @if($ind->scadenza_documento)
                            @if($ind->scadenza_documento->isPast())
                                <span class="badge badge-danger">SCADUTO</span>
                            @else
                                {{ $ind->scadenza_documento->format('d/m/Y') }}
                            @endif
                        @else
                            -
                        @endif
                    </td>
                    <th></th>
                    <td></td>
                </tr>
            </table>
            @else
            <p class="empty">Nessun documento registrato</p>
            @endif
        </div>

        <div class="section">
            <div class="section-title">Contatti ({{ $ind->contatti->count() }})</div>
            @if($ind->contatti->count() > 0)
            <table>
                <thead>
                    <tr>
                        <th>Tipo</th>
                        <th>Valore</th>
                        <th>Etichetta</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach($ind->contatti as $contatto)
                    <tr>
                        <td>
                            @switch($contatto->tipo)
                                @case('email')
                                    <span class="badge badge-info">Email</span>
                                    @break
                                @case('telefono')
                                    <span class="badge">Telefono</span>
                                    @break
                                @case('cellulare')
                                    <span class="badge badge-success">Cellulare</span>
                                    @break
                                @default
                                    {{ $contatto->tipo }}
                            @endswitch
                        </td>
                        <td>{{ $contatto->valore }}</td>
                        <td>{{ $contatto->etichetta ?: '-' }}</td>
                    </tr>
                    @endforeach
                </tbody>
            </table>
            @else
            <p class="empty">Nessun contatto registrato</p>
            @endif
        </div>

        <div class="section">
            <div class="section-title">Gruppi di Appartenenza ({{ $ind->gruppi->count() }})</div>
            @if($ind->gruppi->count() > 0)
            <table>
                <thead>
                    <tr>
                        <th>Gruppo</th>
                        <th>Ruolo</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach($ind->gruppi as $gruppo)
                    <tr>
                        <td>{{ $gruppo->nome }}</td>
                        <td>{{ $gruppo->pivot->ruolo_nel_gruppo ?: '-' }}</td>
                    </tr>
                    @endforeach
                </tbody>
            </table>
            @else
            <p class="empty">Nessun gruppo associato</p>
            @endif
        </div>
    </div>
    @endforeach

    <div class="footer">
        <p>Glastree - Report generato automaticamente</p>
    </div>

    <script>
        window.onload = function() {
            window.print();
        }
    </script>
</body>
</html>