@extends('layouts.default')
@section('title', 'Rapor Oluştur')
@section('content')
<div class="container-fluid">
    @include('admin.partials.page-header')

    <form action="{{ route('admin.reports.generate') }}" method="POST">
        @csrf
        <div class="row">
            <div class="col-xl-8">
                <div class="card">
                    <div class="card-header border-0 pb-0">
                        <h4 class="card-title mb-0">Rapor Bilgileri</h4>
                    </div>
                    <div class="card-body">
                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <label class="form-label">Müşteri *</label>
                                <select name="client_id" class="form-control default-select" required>
                                    <option value="">Seçiniz</option>
                                    @foreach($clients as $client)
                                        <option value="{{ $client->id }}" @selected(old('client_id') == $client->id)>{{ $client->company_name }}</option>
                                    @endforeach
                                </select>
                                @error('client_id')<small class="text-danger">{{ $message }}</small>@enderror
                            </div>
                            <div class="col-md-6 mb-3">
                                <label class="form-label">Rapor Şablonu</label>
                                <select name="report_template_id" class="form-control default-select">
                                    <option value="">Şablonsuz / varsayılan görünüm</option>
                                    @foreach($templates as $template)
                                        <option value="{{ $template->id }}" @selected(old('report_template_id') == $template->id)>{{ $template->name }} {{ $template->client ? '· '.$template->client->company_name : '· Genel' }}</option>
                                    @endforeach
                                </select>
                                <small class="text-muted">Seçilen şablon PDF/Excel kapak, renk ve bölüm düzenini belirler.</small>
                            </div>
                            <div class="col-md-6 mb-3">
                                <label class="form-label">Rapor Tipi *</label>
                                <select name="report_type" class="form-control default-select" required>
                                    <option value="daily" @selected(old('report_type') === 'daily')>Günlük</option>
                                    <option value="weekly" @selected(old('report_type') === 'weekly')>Haftalık</option>
                                    <option value="monthly" @selected(old('report_type', 'monthly') === 'monthly')>Aylık</option>
                                    <option value="custom" @selected(old('report_type') === 'custom')>Özel Tarih</option>
                                </select>
                            </div>
                            <div class="col-md-3 mb-3">
                                <label class="form-label">Başlangıç *</label>
                                <input type="date" name="date_from" value="{{ old('date_from', now()->startOfMonth()->toDateString()) }}" class="form-control" required>
                                @error('date_from')<small class="text-danger">{{ $message }}</small>@enderror
                            </div>
                            <div class="col-md-3 mb-3">
                                <label class="form-label">Bitiş *</label>
                                <input type="date" name="date_to" value="{{ old('date_to', now()->toDateString()) }}" class="form-control" required>
                                @error('date_to')<small class="text-danger">{{ $message }}</small>@enderror
                            </div>
                        </div>
                    </div>
                </div>

                <div class="card">
                    <div class="card-header border-0 pb-0">
                        <h4 class="card-title mb-0">Kanallar</h4>
                    </div>
                    <div class="card-body">
                        @php($selectedChannels = old('channels', ['google_ads', 'meta_ads', 'search_console', 'ga4']))
                        <div class="row">
                            @foreach([
                                'google_ads' => ['Google Ads', 'Harcama, tıklama, kampanya, anahtar kelime, arama terimi'],
                                'meta_ads' => ['Meta Ads', 'Harcama, erişim, reklam seti, reklam, lead/dönüşüm'],
                                'search_console' => ['Search Console', 'Organik tıklama, gösterim, sorgu ve sayfa performansı'],
                                'ga4' => ['GA4', 'Oturum, kullanıcı, sayfa görüntüleme ve dönüşüm'],
                            ] as $key => $info)
                                <div class="col-md-6 mb-3">
                                    <label class="border rounded p-3 d-block h-100" style="cursor:pointer;">
                                        <input type="checkbox" name="channels[]" value="{{ $key }}" class="form-check-input me-2" @checked(in_array($key, $selectedChannels, true))>
                                        <strong>{{ $info[0] }}</strong>
                                        <span class="d-block text-muted mt-1">{{ $info[1] }}</span>
                                    </label>
                                </div>
                            @endforeach
                        </div>
                        @error('channels')<small class="text-danger">{{ $message }}</small>@enderror
                    </div>
                </div>
            </div>

            <div class="col-xl-4">
                <div class="card">
                    <div class="card-body">
                        <button class="btn btn-primary w-100 mb-2">Raporu Şimdi Oluştur</button>
                        <a href="{{ route('admin.reports.index') }}" class="btn btn-light w-100">Vazgeç</a>
                        <a href="{{ route('admin.report-templates.index') }}" class="btn btn-outline-primary w-100 mt-2">Şablonları Yönet</a>
                        <p class="text-muted mt-3 mb-0">Şablon seçildiğinde raporun kapak rengi, ajans adı, bölüm seçimi ve tablo limitleri o şablondan gelir.</p>
                    </div>
                </div>
            </div>
        </div>
    </form>
</div>
@endsection
