<div>
    <div class="mb-6 flex items-center justify-between">
        <h1 class="text-2xl font-bold text-gray-900">Registro Assegnazioni</h1>
        @can('settings.manage')
        <button wire:click="openCreate"
                style="background:#4f46e5;color:#fff;border:none;border-radius:8px;padding:8px 18px;font-size:14px;cursor:pointer;display:inline-flex;align-items:center;gap:6px;">
            <svg xmlns="http://www.w3.org/2000/svg" style="width:16px;height:16px;" fill="none" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" d="M12 4v16m8-8H4"/></svg>
            Nuova voce
        </button>
        @endcan
    </div>

    {{-- Filters --}}
    <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-4 mb-4">
        <div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-3">
            <input wire:model.live.debounce.300ms="search" type="text" placeholder="Cerca territorio o proclamatore..." class="rounded-lg border-gray-300 text-sm focus:ring-indigo-500 focus:border-indigo-500">
            <select wire:model.live="filtroAnno" class="rounded-lg border-gray-300 text-sm focus:ring-indigo-500 focus:border-indigo-500">
                <option value="">Tutti gli anni</option>
                @foreach($anni as $anno)
                    <option value="{{ $anno->id }}">{{ $anno->label }}</option>
                @endforeach
            </select>
            <select wire:model.live="filtroZona" class="rounded-lg border-gray-300 text-sm focus:ring-indigo-500 focus:border-indigo-500">
                <option value="">Tutte le zone</option>
                @foreach($zone as $zona)
                    <option value="{{ $zona->id }}">{{ $zona->nome }}</option>
                @endforeach
            </select>
            <select wire:model.live="filtroTipologia" class="rounded-lg border-gray-300 text-sm focus:ring-indigo-500 focus:border-indigo-500">
                <option value="">Tutte le tipologie</option>
                @foreach($tipologie as $tipologia)
                    <option value="{{ $tipologia->id }}">{{ $tipologia->nome }}</option>
                @endforeach
            </select>
            <select wire:model.live="filtroStato" class="rounded-lg border-gray-300 text-sm focus:ring-indigo-500 focus:border-indigo-500">
                <option value="">Tutte</option>
                <option value="aperte">Aperte (in corso)</option>
                <option value="chiuse">Chiuse (rientrate)</option>
            </select>
        </div>
    </div>

    {{-- Table --}}
    <div class="bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden">
        <div class="overflow-x-auto">
            <table class="min-w-full divide-y divide-gray-200 text-sm">
                <thead class="bg-gray-50">
                    <tr>
                        <th wire:click="sortBy('territorio_id')" class="px-3 py-3 text-left text-xs font-medium text-gray-500 uppercase cursor-pointer">Territorio</th>
                        <th class="px-3 py-3 text-left text-xs font-medium text-gray-500 uppercase">Proclamatore</th>
                        <th wire:click="sortBy('assigned_at')" class="px-3 py-3 text-left text-xs font-medium text-gray-500 uppercase cursor-pointer">
                            Assegnato @if($sortField==='assigned_at') {{ $sortDirection==='asc'?'↑':'↓' }} @endif
                        </th>
                        <th wire:click="sortBy('returned_at')" class="px-3 py-3 text-left text-xs font-medium text-gray-500 uppercase cursor-pointer">
                            Rientrato @if($sortField==='returned_at') {{ $sortDirection==='asc'?'↑':'↓' }} @endif
                        </th>
                        <th class="px-3 py-3 text-left text-xs font-medium text-gray-500 uppercase">Giorni</th>
                        <th class="px-3 py-3 text-left text-xs font-medium text-gray-500 uppercase">Anno</th>
                        <th class="px-3 py-3 text-left text-xs font-medium text-gray-500 uppercase">Campagna</th>
                        @can('settings.manage')
                        <th class="px-3 py-3 text-left text-xs font-medium text-gray-500 uppercase">Azioni</th>
                        @endcan
                    </tr>
                </thead>
                <tbody class="divide-y divide-gray-100">
                    @forelse($assegnazioni as $a)
                    <tr class="hover:bg-gray-50">
                        <td class="px-3 py-2">
                            <a href="{{ route('territori.show', $a->territorio_id) }}" class="text-indigo-600 hover:underline font-medium">N° {{ $a->territorio?->numero }}</a>
                            <span class="text-xs text-gray-400 ml-1">{{ $a->territorio?->zona?->nome }}</span>
                        </td>
                        <td class="px-3 py-2">{{ $a->proclamatore?->nome_completo ?? 'N/A' }}</td>
                        <td class="px-3 py-2">{{ $a->assigned_at->format('d/m/Y') }}</td>
                        <td class="px-3 py-2">
                            @if($a->returned_at)
                                {{ $a->returned_at->format('d/m/Y') }}
                            @else
                                <span class="text-amber-600 font-medium text-xs">In corso</span>
                            @endif
                        </td>
                        <td class="px-3 py-2">{{ $a->giorni }}</td>
                        <td class="px-3 py-2 text-xs text-gray-500">{{ $a->annoTeocratico?->label }}</td>
                        <td class="px-3 py-2 text-xs">
                            @if($a->counted_in_campaign)
                                <span class="text-green-600">{{ $a->campagna?->descrizione }}</span>
                            @else
                                <span class="text-gray-300">-</span>
                            @endif
                        </td>
                        @can('settings.manage')
                        <td class="px-3 py-2 whitespace-nowrap">
                            <button wire:click="openEdit({{ $a->id }})"
                                    style="background:#e0e7ff;color:#4338ca;border:none;border-radius:6px;padding:4px 10px;font-size:12px;cursor:pointer;margin-right:4px;">
                                Modifica
                            </button>
                            <button wire:click="askDelete({{ $a->id }})"
                                    style="background:#fee2e2;color:#b91c1c;border:none;border-radius:6px;padding:4px 10px;font-size:12px;cursor:pointer;">
                                Elimina
                            </button>
                        </td>
                        @endcan
                    </tr>
                    @empty
                    <tr>
                        <td colspan="7" class="px-4 py-8 text-center text-gray-500">Nessuna assegnazione trovata.</td>
                    </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        <div class="px-4 py-3 border-t border-gray-200">
            {{ $assegnazioni->links() }}
        </div>
    </div>

    {{-- ─── Modal Crea / Modifica (solo admin) ────────────────── --}}
    @can('settings.manage')
    @if($showModal)
    <div style="position:fixed;inset:0;background:rgba(0,0,0,.45);z-index:50;display:flex;align-items:center;justify-content:center;padding:16px;">
        <div style="background:#fff;border-radius:12px;width:100%;max-width:560px;max-height:90vh;overflow-y:auto;padding:24px;">
            <h2 style="font-size:18px;font-weight:700;color:#111827;margin-bottom:20px;">
                {{ $editingId ? 'Modifica voce' : 'Nuova voce' }}
            </h2>

            <div style="display:grid;grid-template-columns:1fr 1fr;gap:14px;">
                {{-- Territorio --}}
                <div style="grid-column:1/-1;">
                    <label style="font-size:13px;font-weight:600;color:#374151;display:block;margin-bottom:4px;">Territorio *</label>
                    <select wire:model="form_territorio_id" style="width:100%;border:1px solid #d1d5db;border-radius:8px;padding:8px 10px;font-size:14px;">
                        <option value="">-- seleziona --</option>
                        @foreach($territori as $t)
                            <option value="{{ $t->id }}">N° {{ $t->numero }} — {{ $t->zona?->nome }}</option>
                        @endforeach
                    </select>
                    @error('form_territorio_id') <span style="color:#dc2626;font-size:12px;">{{ $message }}</span> @enderror
                </div>

                {{-- Proclamatore --}}
                <div style="grid-column:1/-1;">
                    <label style="font-size:13px;font-weight:600;color:#374151;display:block;margin-bottom:4px;">Proclamatore *</label>
                    <select wire:model="form_proclamatore_id" style="width:100%;border:1px solid #d1d5db;border-radius:8px;padding:8px 10px;font-size:14px;">
                        <option value="">-- seleziona --</option>
                        @foreach($proclamatori as $p)
                            <option value="{{ $p->id }}">{{ $p->cognome }} {{ $p->nome }}</option>
                        @endforeach
                    </select>
                    @error('form_proclamatore_id') <span style="color:#dc2626;font-size:12px;">{{ $message }}</span> @enderror
                </div>

                {{-- Anno teocratico --}}
                <div>
                    <label style="font-size:13px;font-weight:600;color:#374151;display:block;margin-bottom:4px;">Anno teocratico *</label>
                    <select wire:model="form_anno_id" style="width:100%;border:1px solid #d1d5db;border-radius:8px;padding:8px 10px;font-size:14px;">
                        <option value="">-- seleziona --</option>
                        @foreach($anni as $anno)
                            <option value="{{ $anno->id }}">{{ $anno->label }}</option>
                        @endforeach
                    </select>
                    @error('form_anno_id') <span style="color:#dc2626;font-size:12px;">{{ $message }}</span> @enderror
                </div>

                {{-- Campagna --}}
                <div>
                    <label style="font-size:13px;font-weight:600;color:#374151;display:block;margin-bottom:4px;">Campagna</label>
                    <select wire:model="form_campaign_id" style="width:100%;border:1px solid #d1d5db;border-radius:8px;padding:8px 10px;font-size:14px;">
                        <option value="">Nessuna</option>
                        @foreach($campagne as $c)
                            <option value="{{ $c->id }}">{{ $c->descrizione }}</option>
                        @endforeach
                    </select>
                    @error('form_campaign_id') <span style="color:#dc2626;font-size:12px;">{{ $message }}</span> @enderror
                </div>

                {{-- Data assegnazione --}}
                <div>
                    <label style="font-size:13px;font-weight:600;color:#374151;display:block;margin-bottom:4px;">Data assegnazione *</label>
                    <input type="date" wire:model="form_assigned_at" style="width:100%;border:1px solid #d1d5db;border-radius:8px;padding:8px 10px;font-size:14px;">
                    @error('form_assigned_at') <span style="color:#dc2626;font-size:12px;">{{ $message }}</span> @enderror
                </div>

                {{-- Data rientro --}}
                <div>
                    <label style="font-size:13px;font-weight:600;color:#374151;display:block;margin-bottom:4px;">Data rientro</label>
                    <input type="date" wire:model="form_returned_at" style="width:100%;border:1px solid #d1d5db;border-radius:8px;padding:8px 10px;font-size:14px;">
                    @error('form_returned_at') <span style="color:#dc2626;font-size:12px;">{{ $message }}</span> @enderror
                </div>

                {{-- Contata in campagna --}}
                <div style="grid-column:1/-1;display:flex;align-items:center;gap:8px;">
                    <input type="checkbox" wire:model="form_counted_in_campaign" id="ccb" style="width:16px;height:16px;cursor:pointer;">
                    <label for="ccb" style="font-size:14px;color:#374151;cursor:pointer;">Contata in campagna</label>
                </div>

                {{-- Note --}}
                <div style="grid-column:1/-1;">
                    <label style="font-size:13px;font-weight:600;color:#374151;display:block;margin-bottom:4px;">Note</label>
                    <textarea wire:model="form_note" rows="2" style="width:100%;border:1px solid #d1d5db;border-radius:8px;padding:8px 10px;font-size:14px;resize:vertical;"></textarea>
                </div>
            </div>

            <div style="display:flex;justify-content:flex-end;gap:10px;margin-top:20px;">
                <button wire:click="$set('showModal', false)"
                        style="background:#f3f4f6;color:#374151;border:none;border-radius:8px;padding:8px 18px;font-size:14px;cursor:pointer;">
                    Annulla
                </button>
                <button wire:click="save"
                        style="background:#4f46e5;color:#fff;border:none;border-radius:8px;padding:8px 18px;font-size:14px;cursor:pointer;">
                    {{ $editingId ? 'Salva modifiche' : 'Crea voce' }}
                </button>
            </div>
        </div>
    </div>
    @endif

    {{-- ─── Conferma eliminazione (solo admin) ─────────────────── --}}
    @if($showDeleteConfirm)
    <div style="position:fixed;inset:0;background:rgba(0,0,0,.45);z-index:50;display:flex;align-items:center;justify-content:center;padding:16px;">
        <div style="background:#fff;border-radius:12px;width:100%;max-width:360px;padding:24px;">
            <h2 style="font-size:17px;font-weight:700;color:#111827;margin-bottom:10px;">Elimina voce</h2>
            <p style="font-size:14px;color:#6b7280;margin-bottom:20px;">Sei sicuro di voler eliminare questa assegnazione? L'operazione non può essere annullata.</p>
            <div style="display:flex;justify-content:flex-end;gap:10px;">
                <button wire:click="$set('showDeleteConfirm', false)"
                        style="background:#f3f4f6;color:#374151;border:none;border-radius:8px;padding:8px 18px;font-size:14px;cursor:pointer;">
                    Annulla
                </button>
                <button wire:click="deleteConfirmed"
                        style="background:#dc2626;color:#fff;border:none;border-radius:8px;padding:8px 18px;font-size:14px;cursor:pointer;">
                    Elimina
                </button>
            </div>
        </div>
    </div>
    @endif
    @endcan
</div>
