@csrf
@php
    $selectedChannels = old('channels', $template->channels ?: array_keys($channelOptions));
    $selectedSections = old('sections', $templateSections);
    $settings = array_merge($defaultSettings, $templateSettings ?? []);
@endphp
<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">Şablon Bilgileri</h4></div>
            <div class="card-body">
                <div class="row">
                    <div class="col-md-8 mb-3"><label class="form-label">Şablon Adı *</label><input type="text" name="name" class="form-control" value="{{ old('name', $template->name) }}" required>@error('name')<small class="text-danger">{{ $message }}</small>@enderror</div>
                    <div class="col-md-4 mb-3"><label class="form-label">Rapor Tipi *</label><select name="type" class="form-control default-select" required>@foreach(['daily'=>'Günlük','weekly'=>'Haftalık','monthly'=>'Aylık','custom'=>'Özel'] as $key => $label)<option value="{{ $key }}" @selected(old('type', $template->type)===$key)>{{ $label }}</option>@endforeach</select></div>
                    <div class="col-md-6 mb-3"><label class="form-label">Müşteri Kapsamı</label><select name="client_id" class="form-control default-select"><option value="">Genel şablon</option>@foreach($clients as $client)<option value="{{ $client->id }}" @selected(old('client_id', $template->client_id)==$client->id)>{{ $client->company_name }}</option>@endforeach</select><small class="text-muted">Boş kalırsa tüm müşterilerde kullanılabilir.</small></div>
                    <div class="col-md-6 mb-3"><label class="form-label">Marka Kiti</label><select name="brand_kit_id" class="form-control default-select"><option value="">Varsayılan marka kitini otomatik kullan</option>@foreach($brandKits as $kit)<option value="{{ $kit->id }}" @selected(old('brand_kit_id', $template->brand_kit_id)==$kit->id)>{{ $kit->name }} @if($kit->client) — {{ $kit->client->company_name }} @else — Genel @endif</option>@endforeach</select><small class="text-muted">Seçilirse logo, renk ve footer bilgileri bu kitten gelir.</small></div>
                    <div class="col-md-3 mb-3"><label class="form-label">Sıra</label><input type="number" name="sort_order" class="form-control" value="{{ old('sort_order', $template->sort_order ?: 100) }}" min="0" max="9999"></div>
                    <div class="col-md-3 mb-3"><label class="form-label">Durum</label><div class="border rounded p-2"><label class="me-3"><input type="checkbox" name="is_active" value="1" class="form-check-input me-1" @checked(old('is_active', $template->is_active ?? true))> Aktif</label><label><input type="checkbox" name="is_default" value="1" class="form-check-input me-1" @checked(old('is_default', $template->is_default))> Varsayılan</label></div></div>
                    <div class="col-12 mb-3"><label class="form-label">Açıklama</label><textarea name="description" class="form-control" rows="3">{{ old('description', $template->description) }}</textarea></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"><div class="row">@foreach($channelOptions as $key => $label)<div class="col-md-3 mb-2"><label class="border rounded p-3 d-block"><input type="checkbox" name="channels[]" value="{{ $key }}" class="form-check-input me-2" @checked(in_array($key, $selectedChannels, true))>{{ $label }}</label></div>@endforeach</div></div>
        </div>

        <div class="card">
            <div class="card-header border-0 pb-0"><h4 class="card-title mb-0">Rapor Bölümleri</h4></div>
            <div class="card-body">
                @foreach($sectionGroups as $group => $sections)
                    <h6 class="mb-2 mt-3">{{ $group }}</h6>
                    <div class="row">
                        @foreach($sections as $key => $label)
                            <div class="col-md-4 mb-2"><label class="border rounded p-2 d-block"><input type="checkbox" name="sections[{{ $key }}]" value="1" class="form-check-input me-2" @checked((bool)($selectedSections[$key] ?? false))>{{ $label }}</label></div>
                        @endforeach
                    </div>
                @endforeach
            </div>
        </div>
    </div>

    <div class="col-xl-4">
        <div class="card">
            <div class="card-header border-0 pb-0"><h4 class="card-title mb-0">Marka / PDF Ayarları</h4></div>
            <div class="card-body">
                <div class="alert alert-light border small">Marka kiti seçerseniz logo, renk ve iletişim alanları otomatik dolar. Buradaki alanlar şablona özel ezme/override içindir.</div>
                <div class="mb-3"><label class="form-label">Ajans Adı</label><input type="text" name="settings[agency_name]" class="form-control" value="{{ old('settings.agency_name', $settings['agency_name']) }}"></div>
                <div class="mb-3"><label class="form-label">Ajans Logo URL</label><input type="text" name="settings[agency_logo_url]" class="form-control" value="{{ old('settings.agency_logo_url', $settings['agency_logo_url']) }}"></div>
                <div class="mb-3"><label class="form-label">Müşteri Logo URL</label><input type="text" name="settings[client_logo_url]" class="form-control" value="{{ old('settings.client_logo_url', $settings['client_logo_url'] ?? '') }}"></div>
                <div class="row"><div class="col-md-6 mb-3"><label class="form-label">Ana Renk</label><input type="color" name="settings[primary_color]" class="form-control form-control-color w-100" value="{{ old('settings.primary_color', $settings['primary_color']) }}"></div><div class="col-md-6 mb-3"><label class="form-label">Vurgu Rengi</label><input type="color" name="settings[accent_color]" class="form-control form-control-color w-100" value="{{ old('settings.accent_color', $settings['accent_color']) }}"></div></div>
                <div class="row"><div class="col-md-6 mb-3"><label class="form-label">Arka Plan</label><input type="color" name="settings[secondary_color]" class="form-control form-control-color w-100" value="{{ old('settings.secondary_color', $settings['secondary_color'] ?? '#f8fafc') }}"></div><div class="col-md-6 mb-3"><label class="form-label">Metin Rengi</label><input type="color" name="settings[text_color]" class="form-control form-control-color w-100" value="{{ old('settings.text_color', $settings['text_color'] ?? '#111827') }}"></div></div>
                <div class="mb-3"><label class="form-label">Kapak Tipi</label><select name="settings[cover_style]" class="form-control default-select">@foreach(['gradient'=>'Gradient','minimal'=>'Minimal','image'=>'Görsel','dark'=>'Koyu Kurumsal'] as $key=>$label)<option value="{{ $key }}" @selected(old('settings.cover_style', $settings['cover_style'] ?? 'gradient')===$key)>{{ $label }}</option>@endforeach</select></div>
                <div class="mb-3"><label class="form-label">Kapak Görsel URL</label><input type="text" name="settings[cover_background_url]" class="form-control" value="{{ old('settings.cover_background_url', $settings['cover_background_url'] ?? '') }}"></div>
                <div class="mb-3"><label class="form-label">Kapak Başlığı</label><input type="text" name="settings[cover_title]" class="form-control" value="{{ old('settings.cover_title', $settings['cover_title']) }}"></div>
                <div class="mb-3"><label class="form-label">Kapak Alt Başlığı</label><textarea name="settings[cover_subtitle]" class="form-control" rows="2">{{ old('settings.cover_subtitle', $settings['cover_subtitle']) }}</textarea></div>
                <div class="mb-3"><label class="form-label">Giriş Metni</label><textarea name="settings[intro_text]" class="form-control" rows="3">{{ old('settings.intro_text', $settings['intro_text']) }}</textarea></div>
                <div class="mb-3"><label class="form-label">Alt Bilgi</label><textarea name="settings[footer_note]" class="form-control" rows="2">{{ old('settings.footer_note', $settings['footer_note']) }}</textarea></div>
                <div class="mb-3"><label class="form-label">Tablo Satır Limiti</label><input type="number" name="settings[table_limit]" min="5" max="100" class="form-control" value="{{ old('settings.table_limit', $settings['table_limit']) }}"></div>
                @foreach(['show_agency_logo'=>'Ajans logosu göster','show_client_logo'=>'Müşteri logosu göster','show_data_timestamp'=>'Veri zamanını göster','compact_mode'=>'Kompakt mod','include_pdf_cover'=>'PDF kapak alanı','include_excel_summary'=>'Excel özet alanı'] as $key => $label)
                    <div class="form-check mb-2"><input type="checkbox" class="form-check-input" name="settings[{{ $key }}]" value="1" id="{{ $key }}" @checked(old('settings.'.$key, $settings[$key]))><label for="{{ $key }}" class="form-check-label">{{ $label }}</label></div>
                @endforeach
                <hr>
                <button class="btn btn-primary w-100 mb-2">Kaydet</button>
                <a href="{{ route('admin.report-templates.index') }}" class="btn btn-light w-100">Vazgeç</a>
                @if($template->exists)
                    <a href="{{ route('admin.report-templates.preview', $template) }}" class="btn btn-outline-primary w-100 mt-2">Önizle</a>
                @endif
            </div>
        </div>
    </div>
</div>
