@extends('layouts.default')
@section('title', 'Mail Gönderim Logları')
@section('content')
<div class="container-fluid">
    @include('admin.partials.page-header', ['actions' => new \Illuminate\Support\HtmlString('<a href="'.route('admin.mail.settings').'" class="btn btn-primary">SMTP Ayarları</a>')])

    <div class="card">
        <div class="card-body">
            <form method="GET" class="row g-3 align-items-end mb-4">
                <div class="col-md-4"><label class="form-label">Müşteri</label><select name="client_id" class="form-control default-select"><option value="">Tümü</option>@foreach($clients as $client)<option value="{{ $client->id }}" @selected(request('client_id') == $client->id)>{{ $client->company_name }}</option>@endforeach</select></div>
                <div class="col-md-3"><label class="form-label">Durum</label><select name="status" class="form-control default-select"><option value="">Tümü</option><option value="sent" @selected(request('status')==='sent')>Gönderildi</option><option value="failed" @selected(request('status')==='failed')>Hatalı</option><option value="queued" @selected(request('status')==='queued')>Kuyrukta</option></select></div>
                <div class="col-md-3"><button class="btn btn-primary">Filtrele</button><a href="{{ route('admin.mail.logs') }}" class="btn btn-light">Temizle</a></div>
            </form>

            <div class="table-responsive">
                <table class="table table-responsive-md align-middle">
                    <thead><tr><th>Tarih</th><th>Müşteri</th><th>Konu</th><th>Alıcılar</th><th>Ekler</th><th>Durum</th><th>Hata</th></tr></thead>
                    <tbody>
                    @forelse($logs as $log)
                        <tr>
                            <td>{{ $log->sent_at?->format('d.m.Y H:i') ?? $log->created_at?->format('d.m.Y H:i') }}</td>
                            <td>{{ $log->client?->company_name ?? '-' }} @if($log->report)<a class="d-block small" href="{{ route('admin.reports.show', $log->report) }}">Rapor #{{ $log->report->id }}</a>@endif</td>
                            <td><strong>{{ $log->subject }}</strong></td>
                            <td>{{ implode(', ', $log->recipients ?? []) }}</td>
                            <td>{{ count($log->attachment_paths ?? []) }}</td>
                            <td>@include('admin.partials.status-badge', ['status' => $log->status])</td>
                            <td class="text-danger small">{{ \Illuminate\Support\Str::limit($log->error_message, 90) }}</td>
                        </tr>
                    @empty
                        <tr><td colspan="7" class="text-center text-muted py-5">Henüz mail logu yok.</td></tr>
                    @endforelse
                    </tbody>
                </table>
            </div>
            {{ $logs->links() }}
        </div>
    </div>
</div>
@endsection
