@extends('layouts.adminlte')
@section('title', $mailingList->nome)
@section('page_title', $mailingList->nome)

@section('content')
<div class="row">
    <div class="col-md-4">
        <div class="card">
            <div class="card-header">
                <h3 class="card-title"><i class="fas fa-info-circle mr-2"></i>Dettagli Lista</h3>
            </div>
            <div class="card-body">
                <dl class="row">
                    <dt class="col-sm-4">Nome</dt>
                    <dd class="col-sm-8">{{ $mailingList->nome }}</dd>

                    <dt class="col-sm-4">Descrizione</dt>
                    <dd class="col-sm-8">{{ $mailingList->descrizione ?: '-' }}</dd>

                    <dt class="col-sm-4">Stato</dt>
                    <dd class="col-sm-8">
                        @if($mailingList->attiva)
                        <span class="badge badge-success">Attiva</span>
                        @else
                        <span class="badge badge-secondary">Disattiva</span>
                        @endif
                    </dd>

                    <dt class="col-sm-4">Creata</dt>
                    <dd class="col-sm-8">{{ $mailingList->created_at ? $mailingList->created_at->format('d/m/Y H:i') : '-' }}</dd>

                    <dt class="col-sm-4">Creato da</dt>
                    <dd class="col-sm-8">{{ $mailingList->user?->name ?: '-' }}</dd>

                    <dt class="col-sm-4">Contatti</dt>
                    <dd class="col-sm-8">
                        <span class="badge badge-info">{{ $mailingList->contatti->count() }}</span>
                    </dd>
                </dl>

                <a href="{{ route('mailing-liste.edit', $mailingList->id) }}" class="btn btn-warning btn-sm">
                    <i class="fas fa-edit mr-1"></i> Modifica
                </a>
                <form action="{{ route('mailing-liste.destroy', $mailingList->id) }}" method="POST" class="d-inline">
                    @csrf @method('DELETE')
                    <button type="submit" class="btn btn-danger btn-sm" onclick="return confirm('Eliminare questa lista?')">
                        <i class="fas fa-trash mr-1"></i> Elimina
                    </button>
                </form>
            </div>
        </div>
    </div>

    <div class="col-md-8">
        <div class="card">
            <div class="card-header">
                <h3 class="card-title"><i class="fas fa-users mr-2"></i>Contatti ({{ $mailingList->contatti->count() }})</h3>
            </div>
            <div class="card-body p-0">
                @if($mailingList->contatti->count() > 0)
                <table class="table table-bordered table-hover mb-0">
                    <thead class="thead-light">
                        <tr>
                            <th>Codice</th>
                            <th>Nome</th>
                            <th>Email</th>
                            <th>Stato</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($mailingList->contatti as $contact)
                        @if($contact->individuo)
                        <tr>
                            <td><span class="badge badge-secondary">{{ $contact->individuo->codice_id }}</span></td>
                            <td>{{ $contact->individuo->cognome }} {{ $contact->individuo->nome }}</td>
                            <td>
                                @php
                                $email = $contact->individuo->contatti->where('tipo', 'email')->first()?->valore;
                                @endphp
                                {{ $email ?: '-' }}
                            </td>
                            <td>
                                @if($contact->opt_in)
                                <span class="badge badge-success">Iscritto</span>
                                @else
                                <span class="badge badge-danger">Disiscritto</span>
                                @endif
                            </td>
                        </tr>
                        @endif
                        @endforeach
                    </tbody>
                </table>
                @else
                <div class="text-center text-muted py-4">
                    <p>Nessun contatto in questa lista</p>
                </div>
                @endif
            </div>
        </div>
    </div>
</div>
@endsection