@extends('layouts.admin')

@section('title', 'Historique des Emails')
@section('page-title', 'Historique des Emails')

@section('content')
<div class="card">
    <div class="card-header bg-white d-flex justify-content-between align-items-center">
        <h5 class="mb-0">Historique des Emails Envoyés</h5>
        <div class="d-flex gap-2">
            <a href="{{ route('admin.dashboard') }}" class="btn btn-outline-secondary">
                <i class="bi bi-arrow-left"></i> Retour
            </a>
        </div>
    </div>
    <div class="card-body">
        
        <!-- Statistiques -->
        <div class="row mb-4">
            <div class="col-md-3">
                <div class="card bg-primary text-white">
                    <div class="card-body text-center py-3">
                        <h3 class="mb-1">{{ $stats['total'] }}</h3>
                        <p class="mb-0 small">Total Emails</p>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="card bg-success text-white">
                    <div class="card-body text-center py-3">
                        <h3 class="mb-1">{{ $stats['sent'] }}</h3>
                        <p class="mb-0 small">Envoyés</p>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="card bg-danger text-white">
                    <div class="card-body text-center py-3">
                        <h3 class="mb-1">{{ $stats['failed'] }}</h3>
                        <p class="mb-0 small">Échecs</p>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="card bg-warning text-white">
                    <div class="card-body text-center py-3">
                        <h3 class="mb-1">{{ $stats['pending'] }}</h3>
                        <p class="mb-0 small">En Attente</p>
                    </div>
                </div>
            </div>
        </div>

        <!-- Filtres -->
        <div class="card mb-4">
            <div class="card-body">
                <form action="{{ route('admin.email-logs.index') }}" method="GET" class="row g-3 align-items-end">
                    <div class="col-md-5">
                        <label class="form-label small text-muted mb-1">Recherche</label>
                        <input type="text" name="search" value="{{ request('search') }}" 
                               class="form-control" placeholder="Email, nom ou sujet...">
                    </div>
                    <div class="col-md-4">
                        <label class="form-label small text-muted mb-1">Statut</label>
                        <select name="status" class="form-select">
                            <option value="">Tous les statuts</option>
                            <option value="sent" {{ request('status') == 'sent' ? 'selected' : '' }}>Envoyé</option>
                            <option value="failed" {{ request('status') == 'failed' ? 'selected' : '' }}>Échec</option>
                            <option value="pending" {{ request('status') == 'pending' ? 'selected' : '' }}>En attente</option>
                        </select>
                    </div>
                    <div class="col-md-3">
                        <button type="submit" class="btn btn-primary w-100">
                            <i class="bi bi-funnel"></i> Filtrer
                        </button>
                        @if(request()->hasAny(['search', 'status']))
                            <a href="{{ route('admin.email-logs.index') }}" class="btn btn-outline-secondary w-100 mt-2">
                                <i class="bi bi-arrow-clockwise"></i> Réinitialiser
                            </a>
                        @endif
                    </div>
                </form>
            </div>
        </div>

        <!-- Tableau des logs -->
        @if($logs->count() > 0)
            <div class="table-responsive">
                <table class="table table-hover">
                    <thead class="table-light">
                        <tr>
                            <th class="border-0">Date</th>
                            <th class="border-0">Destinataire</th>
                            <th class="border-0">Sujet</th>
                            <th class="border-0">Template</th>
                            <th class="border-0">Statut</th>
                            <th class="border-0 text-end">Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($logs as $log)
                        <tr class="border-bottom">
                            <td class="py-3">
                                <div class="text-muted small">
                                    <div>{{ $log->created_at->format('d/m/Y') }}</div>
                                    <div class="text-primary">{{ $log->created_at->format('H:i') }}</div>
                                </div>
                            </td>
                            <td class="py-3">
                                <div class="fw-semibold text-dark">{{ $log->recipient_name ?? 'Non spécifié' }}</div>
                                <div class="text-muted small">{{ $log->recipient_email }}</div>
                            </td>
                            <td class="py-3">
                                <span class="text-dark" title="{{ $log->subject }}">
                                    {{ Str::limit($log->subject, 60) }}
                                </span>
                            </td>
                            <td class="py-3">
                                @if($log->template_slug)
                                    <code class="bg-light border px-2 py-1 rounded small text-primary">
                                        {{ $log->template_slug }}
                                    </code>
                                @else
                                    <span class="text-muted small">-</span>
                                @endif
                            </td>
                            <td class="py-3">
                                @if($log->status === 'sent')
                                    <span class="badge bg-success bg-opacity-10 text-success border border-success border-opacity-25">
                                        <i class="bi bi-check-circle me-1"></i>Envoyé
                                    </span>
                                @elseif($log->status === 'failed')
                                    <span class="badge bg-danger bg-opacity-10 text-danger border border-danger border-opacity-25">
                                        <i class="bi bi-x-circle me-1"></i>Échec
                                    </span>
                                @else
                                    <span class="badge bg-warning bg-opacity-10 text-warning border border-warning border-opacity-25">
                                        <i class="bi bi-clock me-1"></i>En attente
                                    </span>
                                @endif
                            </td>
                            <td class="py-3 text-end">
                                <div class="btn-group btn-group-sm">
                                    <a href="{{ route('admin.email-logs.show', $log) }}" 
                                       class="btn btn-outline-primary border" 
                                       title="Voir détails"
                                       data-bs-toggle="tooltip">
                                        <i class="bi bi-eye"></i>
                                    </a>
                                    <form action="{{ route('admin.email-logs.destroy', $log) }}" 
                                          method="POST" class="d-inline">
                                        @csrf
                                        @method('DELETE')
                                        <button type="submit" 
                                                class="btn btn-outline-danger border" 
                                                title="Supprimer"
                                                data-bs-toggle="tooltip"
                                                onclick="return confirm('Supprimer cet historique d\\'email ?')">
                                            <i class="bi bi-trash"></i>
                                        </button>
                                    </form>
                                </div>
                            </td>
                        </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>

            <!-- Pagination -->
            @if($logs->hasPages())
                <div class="mt-4">
                    {{ $logs->links() }}
                </div>
            @endif
        @else
            <div class="text-center py-5">
                <div class="mb-4">
                    <i class="bi bi-envelope-x display-1 text-muted opacity-50"></i>
                </div>
                <h4 class="text-muted mb-3">Aucun email trouvé</h4>
                <p class="text-muted mb-4">
                    @if(request()->hasAny(['search', 'status']))
                        Aucun résultat ne correspond à vos critères de recherche.
                    @else
                        Aucun email n'a été enregistré dans l'historique pour le moment.
                    @endif
                </p>
                @if(request()->hasAny(['search', 'status']))
                    <a href="{{ route('admin.email-logs.index') }}" class="btn btn-primary">
                        <i class="bi bi-arrow-clockwise me-2"></i>Afficher tous les emails
                    </a>
                @endif
            </div>
        @endif
    </div>
</div>

<style>
.card {
    border: none;
    box-shadow: 0 2px 15px rgba(0,0,0,0.08);
    border-radius: 12px;
}

.card-header {
    border-bottom: 1px solid rgba(0,0,0,0.05);
    padding: 1.25rem 1.5rem;
}

.table th {
    font-weight: 600;
    font-size: 0.85rem;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: #6c757d;
    padding: 1rem 0.75rem;
}

.table td {
    padding: 1rem 0.75rem;
    vertical-align: middle;
}

.badge {
    font-weight: 500;
    padding: 0.35rem 0.75rem;
    font-size: 0.75rem;
}

.btn-group-sm > .btn {
    border-radius: 6px;
    margin: 0 2px;
}

.stats-card {
    transition: transform 0.2s ease;
}

.stats-card:hover {
    transform: translateY(-2px);
}

.form-control, .form-select {
    border-radius: 8px;
    border: 1px solid #e2e8f0;
}

.form-control:focus, .form-select:focus {
    border-color: #667eea;
    box-shadow: 0 0 0 0.2rem rgba(102, 126, 234, 0.1);
}

.pagination {
    margin-bottom: 0;
}

.page-link {
    border-radius: 6px;
    margin: 0 3px;
    border: 1px solid #e2e8f0;
}

.page-item.active .page-link {
    background: linear-gradient(135deg, #667eea, #764ba2);
    border-color: #667eea;
}
</style>

<script>
document.addEventListener('DOMContentLoaded', function() {
    // Activation des tooltips Bootstrap
    var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'))
    var tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) {
        return new bootstrap.Tooltip(tooltipTriggerEl)
    });
});
</script>
@endsection