@extends('layouts.default')
@section('title', 'Rapor Şablonları')
@section('content')
<div class="container-fluid">
    @include('admin.partials.page-header', ['actions' => new \Illuminate\Support\HtmlString('<div class="d-flex gap-2"><form method="POST" action="'.route('admin.report-templates.seed-defaults').'">'.csrf_field().'<button class="btn btn-outline-primary">Varsayılanları Kur</button></form><a href="'.route('admin.report-templates.create').'" class="btn btn-primary">Şablon Ekle</a></div>')])

    <div class="card">
        <div class="card-body">
            <form method="GET" class="row g-2 mb-4">
                <div class="col-md-4"><select name="client_id" class="form-control default-select"><option value="">Tüm müşteriler / genel şablonlar</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"><select name="type" class="form-control default-select"><option value="">Tüm rapor tipleri</option>@foreach(['daily'=>'Günlük','weekly'=>'Haftalık','monthly'=>'Aylık','custom'=>'Özel'] as $key => $label)<option value="{{ $key }}" @selected(request('type')===$key)>{{ $label }}</option>@endforeach</select></div>
                <div class="col-md-3"><select name="status" class="form-control default-select"><option value="">Tüm durumlar</option><option value="active" @selected(request('status')==='active')>Aktif</option><option value="passive" @selected(request('status')==='passive')>Pasif</option></select></div>
                <div class="col-md-2"><button class="btn btn-primary w-100">Filtrele</button></div>
            </form>

            <div class="table-responsive">
                <table class="table table-responsive-md align-middle">
                    <thead><tr><th>Şablon</th><th>Kapsam</th><th>Tip</th><th>Kanallar</th><th>Kullanım</th><th>Durum</th><th class="text-end">İşlem</th></tr></thead>
                    <tbody>
                    @forelse($templates as $template)
                        <tr>
                            <td><strong>{{ $template->name }}</strong>@if($template->description)<span class="d-block small text-muted">{{ \Illuminate\Support\Str::limit($template->description, 90) }}</span>@endif</td>
                            <td>{{ $template->client?->company_name ?? 'Genel' }} @if($template->is_default)<span class="badge badge-primary ms-1">Varsayılan</span>@endif</td>
                            <td>{{ ['daily'=>'Günlük','weekly'=>'Haftalık','monthly'=>'Aylık','custom'=>'Özel'][$template->type] ?? $template->type }}</td>
                            <td>@foreach($template->effectiveChannels() as $channel)<span class="badge badge-light me-1">{{ str_replace('_',' ', strtoupper($channel)) }}</span>@endforeach</td>
                            <td><span class="small d-block">Plan: {{ $template->schedules_count }}</span><span class="small d-block">Rapor: {{ $template->generated_reports_count }}</span></td>
                            <td>@include('admin.partials.status-badge', ['status' => $template->is_active ? 'active' : 'passive'])</td>
                            <td class="text-end">
                                <div class="d-flex justify-content-end gap-1 flex-wrap">
                                    <a href="{{ route('admin.report-templates.preview', $template) }}" class="btn btn-xs btn-outline-primary">Önizle</a>
                                    <a href="{{ route('admin.report-templates.show', $template) }}" class="btn btn-xs btn-primary">Detay</a>
                                    <a href="{{ route('admin.report-templates.edit', $template) }}" class="btn btn-xs btn-outline-secondary">Düzenle</a>
                                    <form method="POST" action="{{ route('admin.report-templates.duplicate', $template) }}">@csrf<button class="btn btn-xs btn-outline-info">Kopyala</button></form>
                                </div>
                            </td>
                        </tr>
                    @empty
                        <tr><td colspan="7" class="text-center text-muted py-5">Henüz rapor şablonu yok. Varsayılanları kurarak başlayabilirsiniz.</td></tr>
                    @endforelse
                    </tbody>
                </table>
            </div>
            {{ $templates->links() }}
        </div>
    </div>
</div>
@endsection
