@extends('admin.layout')

@section('title', 'Newsletter - Campagnes')
@section('header', 'Newsletter')

@section('content')
<nav class="mb-6 flex flex-wrap gap-2">
    <a href="{{ route('admin.dashboard') }}" class="text-slate-500 hover:text-slate-900 text-sm">← Tableau de bord</a>
    <span class="text-slate-400">/</span>
    <a href="{{ route('admin.newsletter.index') }}" class="text-slate-500 hover:text-slate-900 text-sm">Abonnés</a>
    <span class="text-slate-400">/</span>
    <span class="text-slate-900 font-medium text-sm">Campagnes</span>
</nav>

<div class="flex gap-2 mb-6">
    <a href="{{ route('admin.newsletter.index') }}" class="px-4 py-2 rounded-xl text-slate-600 hover:bg-slate-100 text-sm">Abonnés</a>
    <a href="{{ route('admin.newsletter.campaigns') }}" class="px-4 py-2 rounded-xl font-medium text-sm bg-primary-500 text-white">Campagnes</a>
    <a href="{{ route('admin.newsletter.settings') }}" class="px-4 py-2 rounded-xl text-slate-600 hover:bg-slate-100 text-sm">Popup</a>
</div>

@if(session('success'))
    <div class="mb-6 p-4 bg-green-50 border border-green-200 text-green-800 rounded-xl">
        <p>{{ session('success') }}</p>
    </div>
@endif
@if(session('error'))
    <div class="mb-6 p-4 bg-red-50 border border-red-200 text-red-800 rounded-xl">
        <p>{{ session('error') }}</p>
    </div>
@endif

<div class="flex justify-between items-center mb-6">
    <p class="text-slate-600 text-sm">Créez des newsletters et envoyez-les à tous vos abonnés. Utilisez le HTML pour le contenu.</p>
    <a href="{{ route('admin.newsletter.campaigns.create') }}" class="inline-flex items-center gap-2 px-4 py-2 bg-primary-500 text-white rounded-xl font-medium hover:bg-primary-600 transition text-sm">
        <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"/></svg>
        Nouvelle campagne
    </a>
</div>

<div class="bg-white rounded-2xl border border-slate-200 overflow-hidden">
    @if($campaigns->isEmpty())
        <div class="p-12 text-center text-slate-500">
            <svg class="w-16 h-16 mx-auto mb-4 text-slate-300" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 8l7.89 5.26a2 2 0 002.22 0L21 8M5 19h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v10a2 2 0 002 2z"/></svg>
            <p class="font-medium">Aucune campagne</p>
            <p class="text-sm mt-1">Créez votre première newsletter pour envoyer des emails à vos abonnés.</p>
            <a href="{{ route('admin.newsletter.campaigns.create') }}" class="inline-block mt-4 px-4 py-2 bg-primary-500 text-white rounded-xl text-sm font-medium">Créer une campagne</a>
        </div>
    @else
        <div class="overflow-x-auto">
            <table class="w-full">
                <thead class="bg-slate-50 border-b border-slate-200">
                    <tr>
                        <th class="text-left px-6 py-4 text-sm font-semibold text-slate-700">Sujet</th>
                        <th class="text-left px-6 py-4 text-sm font-semibold text-slate-700">Statut</th>
                        <th class="text-left px-6 py-4 text-sm font-semibold text-slate-700">Destinataires</th>
                        <th class="text-left px-6 py-4 text-sm font-semibold text-slate-700">Date</th>
                        <th class="text-right px-6 py-4 text-sm font-semibold text-slate-700">Actions</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-slate-100">
                    @foreach($campaigns as $c)
                        <tr class="hover:bg-slate-50/80 transition">
                            <td class="px-6 py-4 font-medium text-slate-900">{{ $c->subject }}</td>
                            <td class="px-6 py-4">
                                @if($c->isSent())
                                    <span class="px-2 py-0.5 bg-emerald-100 text-emerald-700 text-xs font-medium rounded-full">Envoyée</span>
                                @elseif($c->isScheduled())
                                    <span class="px-2 py-0.5 bg-blue-100 text-blue-700 text-xs font-medium rounded-full">Programmée {{ $c->scheduled_at->format('d/m H:i') }}</span>
                                @else
                                    <span class="px-2 py-0.5 bg-amber-100 text-amber-700 text-xs font-medium rounded-full">Brouillon</span>
                                @endif
                            </td>
                            <td class="px-6 py-4 text-slate-600">{{ $c->recipients_count }}</td>
                            <td class="px-6 py-4 text-slate-500 text-sm">{{ $c->created_at->format('d/m/Y H:i') }}</td>
                            <td class="px-6 py-4 text-right">
                                @if(!$c->isSent())
                                    <span class="inline-flex gap-2">
                                        <a href="{{ route('admin.newsletter.campaigns.edit', $c) }}" class="text-slate-600 hover:underline text-sm">Modifier</a>
                                        <form method="POST" action="{{ route('admin.newsletter.campaigns.send', $c) }}" class="inline" onsubmit="return confirm('Envoyer cette newsletter à tous les abonnés ?');">
                                            @csrf
                                            <button type="submit" class="text-primary-600 hover:underline text-sm font-medium">Envoyer maintenant</button>
                                        </form>
                                    </span>
                                @else
                                    <span class="text-slate-400 text-sm">—</span>
                                @endif
                            </td>
                        </tr>
                    @endforeach
                </tbody>
            </table>
        </div>
        <div class="px-6 py-4 border-t border-slate-200">
            {{ $campaigns->links() }}
        </div>
    @endif
</div>
@endsection
