<x-layout.universal pageTitle="Anagrafiche" showBreadcrumb="true" showSidebar="true">
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h3 class="card-title mb-0">
                        <i class="fa-solid fa-user-friends me-2"></i>
                        Gestione Anagrafiche
                    </h3>
                    <div class="d-flex gap-2">
                        <form method="GET" action="{{ route('admin.anagrafiche.index') }}" class="d-flex">
                            <input type="text" name="search" placeholder="Cerca anagrafica..." value="{{ request('search') }}" class="form-control form-control-sm me-2"/>
                            <button type="submit" class="btn btn-outline-primary btn-sm">Cerca</button>
                        </form>
                        <a href="{{ route('admin.anagrafiche.create') }}" class="btn btn-primary btn-sm">
                            <i class="fa-solid fa-plus me-1"></i>
                            Nuova Anagrafica
                        </a>
                    </div>
                </div>
                <div class="card-body">
                    @if (session('success'))
                        <div class="alert alert-success" role="alert">
                            {{ session('success') }}
                        </div>
                    @endif

                    <div class="table-responsive">
                        <table class="table table-striped">
                            <thead>
                                <tr>
                                    <th>Nominativo/Rag. Sociale</th>
                                    <th>CF/P.IVA</th>
                                    <th>Email</th>
                                    <th>Tipo</th>
                                    <th>Azioni</th>
                                </tr>
                            </thead>
                            <tbody>
                                @forelse ($anagrafiche as $anagrafica)
                                    <tr>
                                        <td>
                                            <a href="{{ route('admin.anagrafiche.show', $anagrafica) }}" class="text-primary text-decoration-none">
                                                {{ $anagrafica->ragione_sociale ?: ($anagrafica->cognome . ' ' . $anagrafica->nome) }}
                                            </a>
                                        </td>
                                        <td>{{ $anagrafica->codice_fiscale ?: $anagrafica->partita_iva ?: '-' }}</td>
                                        <td>{{ $anagrafica->email ?: '-' }}</td>
                                        <td>{{ ucfirst($anagrafica->tipo) }}</td>
                                        <td>
                                            <a href="{{ route('admin.anagrafiche.edit', $anagrafica) }}" class="btn btn-sm btn-outline-primary me-1">Modifica</a>
                                            <form action="{{ route('admin.anagrafiche.destroy', $anagrafica) }}" method="POST" class="d-inline" onsubmit="return confirm('Sei sicuro di voler eliminare questa anagrafica?');">
                                                @csrf
                                                @method('DELETE')
                                                <button type="submit" class="btn btn-sm btn-outline-danger">Elimina</button>
                                            </form>
                                        </td>
                                    </tr>
                                @empty
                                    <tr>
                                        <td colspan="5" class="text-center text-muted">Nessuna anagrafica trovata.</td>
                                    </tr>
                                @endforelse
                            </tbody>
                        </table>
                    </div>
                    
                    @if($anagrafiche->hasPages())
                        <div class="d-flex justify-content-center mt-3">
                            {{ $anagrafiche->appends(request()->query())->links() }}
                        </div>
                    @endif
                </div>
            </div>
        </div>
    </div>

</x-layout.universal>