@extends('layouts.portal')
@section('title', 'Rapor Detayı')
@section('content')
<div class="d-flex align-items-start justify-content-between flex-wrap gap-3 mb-4">
    <div>
        <h2 class="mb-1">{{ $report->client?->company_name }}</h2>
        <p class="portal-muted mb-0">{{ ucfirst($report->report_type) }} raporu · {{ optional($report->date_from)->format('d.m.Y') }} - {{ optional($report->date_to)->format('d.m.Y') }}</p>
    </div>
    <a href="{{ route('portal.reports', ['client_id' => $report->client_id]) }}" class="btn btn-light">Raporlara Dön</a>
</div>

<div class="row">
    <div class="col-xl-4 mb-4">
        <div class="card soft-panel h-100">
            <div class="card-body">
                <h4 class="mb-3">Dosyalar</h4>
                <div class="d-grid gap-2">
                    @foreach(['pdf' => 'PDF İndir', 'excel' => 'Excel İndir', 'csv' => 'CSV İndir', 'html' => 'HTML İndir'] as $type => $label)
                        @php
                            $path = match($type) { 'pdf' => $report->pdf_path, 'excel' => $report->excel_path, 'csv' => $report->csv_path, 'html' => $report->html_path };
                        @endphp
                        @if($report->fileExists($path))
                            <a href="{{ route('portal.reports.download', [$report, $type]) }}" class="btn btn-primary">{{ $label }}</a>
                        @else
                            <button class="btn btn-light" disabled>{{ $label }} Yok</button>
                        @endif
                    @endforeach
                </div>
            </div>
        </div>
    </div>
    <div class="col-xl-8 mb-4">
        <div class="card soft-panel h-100">
            <div class="card-body">
                <h4 class="mb-3">Rapor Özeti</h4>
                @if(is_array($report->summary) && count($report->summary))
                    <div class="row">
                        @foreach($report->summary as $key => $value)
                            <div class="col-md-6 mb-3">
                                <div class="border rounded p-3">
                                    <div class="portal-muted small">{{ ucwords(str_replace('_', ' ', $key)) }}</div>
                                    <strong>{{ is_numeric($value) ? number_format((float) $value, 2, ',', '.') : $value }}</strong>
                                </div>
                            </div>
                        @endforeach
                    </div>
                @else
                    <p class="text-muted mb-0">Bu raporda özet verisi bulunmuyor.</p>
                @endif
            </div>
        </div>
    </div>
</div>

<div class="card soft-panel">
    <div class="card-header border-0 pb-0"><h4 class="card-title mb-0">Gönderim Geçmişi</h4></div>
    <div class="card-body">
        <div class="table-responsive">
            <table class="table">
                <thead><tr><th>Konu</th><th>Alıcı</th><th>Durum</th><th>Tarih</th></tr></thead>
                <tbody>
                @forelse($report->emailLogs as $log)
                    <tr>
                        <td>{{ $log->subject }}</td>
                        <td>{{ implode(', ', $log->recipients ?? []) }}</td>
                        <td><span class="badge badge-light">{{ $log->status }}</span></td>
                        <td>{{ $log->sent_at ? $log->sent_at->format('d.m.Y H:i') : $log->created_at->format('d.m.Y H:i') }}</td>
                    </tr>
                @empty
                    <tr><td colspan="4" class="text-center text-muted py-4">Bu rapor için mail gönderim kaydı yok.</td></tr>
                @endforelse
                </tbody>
            </table>
        </div>
    </div>
</div>
@endsection
