@extends('layouts.admin')

@section('title', 'Site Ayarları')
@section('page-title', 'Site Ayarları')
@section('page-subtitle', 'VirusTotal, Google Calendar ve genel ayarlar')

@section('content')
@php
    $canManageGeneral   = auth()->user()->can('manage general settings');
    $canManageGoogle    = auth()->user()->can('manage google calendar settings');
    $canManageVirustotal= auth()->user()->can('manage virustotal settings');
    $canManageSystem    = auth()->user()->can('manage system settings');
    $canManageKvkk      = auth()->user()->can('manage kvkk settings');
    // Herhangi bir sekmeyi yönetme izni yoksa genel uyarı göster
    $canManageAny = $canManageGeneral || $canManageGoogle || $canManageVirustotal || $canManageSystem || $canManageKvkk;
@endphp
<div class="mt-4">

    {{-- Hiçbir sekmeyi yönetme izni yoksa bilgi bandı --}}
    @if(!$canManageAny)
    <div class="mb-4 bg-amber-50 border border-amber-200 rounded-xl px-4 py-3 flex items-center gap-3 text-sm text-amber-800">
        <svg class="w-5 h-5 flex-shrink-0 text-amber-500" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"/></svg>
        <span>Bu sayfayı yalnızca görüntüleme izniniz var. Değişiklik yapmak için yöneticinizle iletişime geçin.</span>
    </div>
    @endif

    {{-- Sekme Navigasyonu --}}
    <div class="bg-white rounded-xl shadow-sm border border-gray-100 px-4 py-3 mb-5 flex items-center gap-1" id="tab-nav">
        <button data-tab="genel" id="tab-btn-genel"
                class="tab-btn px-4 py-2 rounded-lg text-sm font-medium transition-colors bg-primary text-white">
            <span class="flex items-center gap-1.5">
                <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.065 2.572c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.572 1.065c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.065-2.572c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z"/><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"/></svg>
                Genel Ayarlar
            </span>
        </button>
        <button data-tab="google" id="tab-btn-google"
                class="tab-btn px-4 py-2 rounded-lg text-sm font-medium transition-colors text-gray-600 hover:bg-gray-100">
            <span class="flex items-center gap-1.5">
                <svg class="w-4 h-4" viewBox="0 0 24 24"><path d="M22.56 12.25c0-.78-.07-1.53-.2-2.25H12v4.26h5.92c-.26 1.37-1.04 2.53-2.21 3.31v2.77h3.57c2.08-1.92 3.28-4.74 3.28-8.09z" fill="#4285F4"/><path d="M12 23c2.97 0 5.46-.98 7.28-2.66l-3.57-2.77c-.98.66-2.23 1.06-3.71 1.06-2.86 0-5.29-1.93-6.16-4.53H2.18v2.84C3.99 20.53 7.7 23 12 23z" fill="#34A853"/><path d="M5.84 14.09c-.22-.66-.35-1.36-.35-2.09s.13-1.43.35-2.09V7.07H2.18C1.43 8.55 1 10.22 1 12s.43 3.45 1.18 4.93l2.85-2.22.81-.62z" fill="#FBBC05"/><path d="M12 5.38c1.62 0 3.06.56 4.21 1.64l3.15-3.15C17.45 2.09 14.97 1 12 1 7.7 1 3.99 3.47 2.18 7.07l3.66 2.84c.87-2.6 3.3-4.53 6.16-4.53z" fill="#EA4335"/></svg>
                Google Calendar
            </span>
        </button>
        <button data-tab="virustotal" id="tab-btn-virustotal"
                class="tab-btn px-4 py-2 rounded-lg text-sm font-medium transition-colors text-gray-600 hover:bg-gray-100">
            <span class="flex items-center gap-1.5">
                <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m5.618-4.016A11.955 11.955 0 0112 2.944a11.955 11.955 0 01-8.618 3.04A12.02 12.02 0 003 9c0 5.591 3.824 10.29 9 11.622 5.176-1.332 9-6.03 9-11.622 0-1.042-.133-2.052-.382-3.016z"/></svg>
                VirusTotal
            </span>
        </button>
        <button data-tab="sistem" id="tab-btn-sistem"
                class="tab-btn px-4 py-2 rounded-lg text-sm font-medium transition-colors text-gray-600 hover:bg-gray-100">
            <span class="flex items-center gap-1.5">
                <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 12h14M5 12a2 2 0 01-2-2V6a2 2 0 012-2h14a2 2 0 012 2v4a2 2 0 01-2 2M5 12a2 2 0 00-2 2v4a2 2 0 002 2h14a2 2 0 002-2v-4a2 2 0 00-2-2m-2-4h.01M17 16h.01"/></svg>
                Sistem
            </span>
        </button>
        <button data-tab="kvkk" id="tab-btn-kvkk"
                class="tab-btn px-4 py-2 rounded-lg text-sm font-medium transition-colors text-gray-600 hover:bg-gray-100">
            <span class="flex items-center gap-1.5">
                <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m5.618-4.016A11.955 11.955 0 0112 2.944a11.955 11.955 0 01-8.618 3.04A12.02 12.02 0 003 9c0 5.591 3.824 10.29 9 11.622 5.176-1.332 9-6.03 9-11.622 0-1.042-.133-2.052-.382-3.016z"/></svg>
                KVKK
            </span>
        </button>
    </div>

    {{-- ======================================================= --}}
    {{-- TAB: Genel Ayarlar + Banner                            --}}
    {{-- ======================================================= --}}
    <div id="tab-genel" class="tab-panel space-y-5">

        {{-- Logo & Favicon Yükleme (ayrı form — multipart) --}}
        @php
            $currentLogo    = \App\Models\SiteSetting::get('site_logo_path');
            $currentFavicon = \App\Models\SiteSetting::get('site_favicon_path');
        @endphp
        <form method="POST" action="{{ route('admin.ayarlar.branding') }}" enctype="multipart/form-data">
            @csrf
            <div class="bg-white rounded-xl shadow-sm border border-gray-100 p-6">
                <h2 class="font-bold text-gray-800 mb-5 flex items-center gap-2">
                    <span class="w-2 h-2 bg-primary rounded-full"></span>
                    Logo & Favicon
                </h2>
                <div class="grid grid-cols-1 sm:grid-cols-2 gap-6">

                    {{-- Logo --}}
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-2">Site Logosu</label>
                        <div class="flex items-center gap-4 mb-3">
                            <div class="w-20 h-14 bg-gray-50 border border-gray-200 rounded-lg flex items-center justify-center overflow-hidden flex-shrink-0">
                                @if($currentLogo)
                                <img id="logo_preview_img" src="{{ Storage::url($currentLogo) }}" alt="Logo" class="max-h-12 max-w-full object-contain">
                                @else
                                <img id="logo_preview_img" src="{{ asset('images/markamama-M-kucuk.jpg') }}" alt="Logo" class="max-h-12 max-w-full object-contain">
                                @endif
                            </div>
                            <div class="text-xs text-gray-400">
                                <p>PNG, JPG, SVG, WEBP</p>
                                <p>Maks. 2MB</p>
                                <p>Önerilen: şeffaf PNG</p>
                            </div>
                        </div>
                        <input type="file" name="site_logo" id="site_logo" accept=".png,.jpg,.jpeg,.svg,.webp"
                               class="w-full border border-gray-300 rounded-lg px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-orange-400">
                        @if($currentLogo)
                        <div class="mt-2 flex items-center gap-2">
                            <input type="checkbox" name="delete_site_logo" id="delete_site_logo" value="1" class="rounded border-gray-300 text-red-500">
                            <label for="delete_site_logo" class="text-xs text-red-500 cursor-pointer">Mevcut logoyu sil (varsayılana dön)</label>
                        </div>
                        @endif
                    </div>

                    {{-- Favicon --}}
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-2">Favicon (Tarayıcı Sekmesi İkonu)</label>
                        <div class="flex items-center gap-4 mb-3">
                            <div class="w-14 h-14 bg-gray-50 border border-gray-200 rounded-lg flex items-center justify-center overflow-hidden flex-shrink-0">
                                @if($currentFavicon)
                                <img id="favicon_preview_img" src="{{ Storage::url($currentFavicon) }}" alt="Favicon" class="max-h-10 max-w-full object-contain">
                                @else
                                <img id="favicon_preview_img" src="{{ asset('images/markamama-M-kucuk.jpg') }}" alt="Favicon" class="max-h-10 max-w-full object-contain">
                                @endif
                            </div>
                            <div class="text-xs text-gray-400">
                                <p>ICO, PNG, JPG</p>
                                <p>Maks. 1MB</p>
                                <p>Önerilen: 32×32 veya 64×64 px</p>
                            </div>
                        </div>
                        <input type="file" name="site_favicon" id="site_favicon" accept=".ico,.png,.jpg,.jpeg"
                               class="w-full border border-gray-300 rounded-lg px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-orange-400">
                        @if($currentFavicon)
                        <div class="mt-2 flex items-center gap-2">
                            <input type="checkbox" name="delete_site_favicon" id="delete_site_favicon" value="1" class="rounded border-gray-300 text-red-500">
                            <label for="delete_site_favicon" class="text-xs text-red-500 cursor-pointer">Mevcut favicon'u sil (varsayılana dön)</label>
                        </div>
                        @endif
                    </div>

                </div>
                <div class="mt-5 flex justify-end">
                    <button type="submit" class="bg-primary text-white px-5 py-2 rounded-lg text-sm font-medium hover:bg-orange-600 transition-colors">
                        Logo & Favicon Kaydet
                    </button>
                </div>
            </div>
        </form>

        <form method="POST" action="{{ route('admin.ayarlar.update') }}">
            @csrf

            <div class="grid grid-cols-1 lg:grid-cols-2 gap-5">

                {{-- Genel Ayarlar --}}
                <div class="bg-white rounded-xl shadow-sm border border-gray-100 p-6">
                    <h2 class="font-bold text-gray-800 mb-4 flex items-center gap-2">
                        <span class="w-2 h-2 bg-gray-500 rounded-full"></span>
                        Genel Ayarlar
                    </h2>
                    <div class="space-y-4">
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">Site Başlığı</label>
                            <input type="text" name="settings[site_title]"
                                   value="{{ \App\Models\SiteSetting::get('site_title', 'Markamama Kariyer') }}"
                                   class="w-full border border-gray-300 rounded-lg px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-orange-400">
                        </div>
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">İletişim E-postası</label>
                            <input type="email" name="settings[contact_email]"
                                   value="{{ \App\Models\SiteSetting::get('contact_email') }}"
                                   placeholder="kariyer@markamama.com"
                                   class="w-full border border-gray-300 rounded-lg px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-orange-400">
                        </div>
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">Başvuru Bildirimi E-postası</label>
                            <input type="email" name="settings[notification_email]"
                                   value="{{ \App\Models\SiteSetting::get('notification_email') }}"
                                   placeholder="Yeni başvuru bildirimlerinin gönderileceği e-posta"
                                   class="w-full border border-gray-300 rounded-lg px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-orange-400">
                        </div>

                        {{-- Görüşme Durum Bildirimi --}}
                        <div class="pt-3 border-t border-gray-100">
                            <div class="flex items-center justify-between">
                                <div>
                                    <label class="text-sm font-medium text-gray-700">Görüşme Durum E-posta Bildirimi</label>
                                    <p class="text-xs text-gray-400 mt-0.5">Görüşme iptal/tamamlandı/yeniden planlandığında başvurana e-posta gönderilsin</p>
                                </div>
                                <label class="relative inline-flex items-center cursor-pointer ml-4 flex-shrink-0">
                                    <input type="hidden" name="settings[interview_email_notifications]" value="0">
                                    <input type="checkbox" name="settings[interview_email_notifications]" value="1"
                                           {{ \App\Models\SiteSetting::isEnabled('interview_email_notifications') ? 'checked' : '' }}
                                           class="sr-only peer">
                                    <div class="w-11 h-6 bg-gray-200 peer-focus:outline-none rounded-full peer peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-orange-500"></div>
                                </label>
                            </div>
                        </div>
                    </div>
                </div>

                {{-- Banner Yönetimi --}}
                <div class="bg-white rounded-xl shadow-sm border border-gray-100 p-6">
                    <h2 class="font-bold text-gray-800 mb-4 flex items-center gap-2">
                        <span class="w-2 h-2 bg-yellow-500 rounded-full"></span>
                        Banner Yönetimi
                    </h2>
                    <div class="space-y-4">
                        <div class="flex items-center justify-between">
                            <label class="text-sm font-medium text-gray-700">Banner'ı Etkinleştir</label>
                            <label class="relative inline-flex items-center cursor-pointer">
                                <input type="hidden" name="settings[banner_enabled]" value="0">
                                <input type="checkbox" name="settings[banner_enabled]" value="1"
                                       {{ \App\Models\SiteSetting::isEnabled('banner_enabled') ? 'checked' : '' }}
                                       class="sr-only peer">
                                <div class="w-11 h-6 bg-gray-200 peer-focus:outline-none rounded-full peer peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-orange-500"></div>
                            </label>
                        </div>
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">Banner Başlığı</label>
                            <input type="text" name="settings[banner_title]"
                                   value="{{ \App\Models\SiteSetting::get('banner_title') }}"
                                   placeholder="örn: Markamama Ailesine Katıl!"
                                   class="w-full border border-gray-300 rounded-lg px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-orange-400">
                        </div>
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">Alt Başlık</label>
                            <input type="text" name="settings[banner_subtitle]"
                                   value="{{ \App\Models\SiteSetting::get('banner_subtitle') }}"
                                   placeholder="örn: Açık pozisyonlarımızı keşfet"
                                   class="w-full border border-gray-300 rounded-lg px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-orange-400">
                        </div>
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">Buton Metni</label>
                            <input type="text" name="settings[banner_button_text]"
                                   value="{{ \App\Models\SiteSetting::get('banner_button_text', 'Pozisyonları Gör') }}"
                                   class="w-full border border-gray-300 rounded-lg px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-orange-400">
                        </div>
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">Buton Linki</label>
                            <input type="text" name="settings[banner_button_url]"
                                   value="{{ \App\Models\SiteSetting::get('banner_button_url', '#ilanlar') }}"
                                   placeholder="#ilanlar veya /pozisyon/slug"
                                   class="w-full border border-gray-300 rounded-lg px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-orange-400">
                        </div>
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">Arka Plan Rengi</label>
                            <div class="flex items-center gap-2">
                                <input type="color" id="bannerColorPicker" name="settings[banner_bg_color]"
                                       value="{{ \App\Models\SiteSetting::get('banner_bg_color', '#ff7b00') }}"
                                       class="w-10 h-10 rounded border border-gray-300 cursor-pointer">
                                <input type="text" id="bannerColorText"
                                       value="{{ \App\Models\SiteSetting::get('banner_bg_color', '#ff7b00') }}"
                                       placeholder="#ff7b00"
                                       class="flex-1 border border-gray-300 rounded-lg px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-orange-400"
                                       readonly>
                            </div>
                        </div>
                    </div>
                </div>

            </div>

            <div class="mt-4 flex justify-end">
                <button type="submit"
                        class="bg-primary text-white px-6 py-2.5 rounded-lg text-sm font-medium hover:bg-orange-600 transition-colors">
                    Ayarları Kaydet
                </button>
            </div>
        </form>

    </div>{{-- /tab-genel --}}

    {{-- ======================================================= --}}
    {{-- TAB: Google Calendar                                    --}}
    {{-- ======================================================= --}}
    <div id="tab-google" class="tab-panel space-y-5" style="display:none">

        @php
            $hasToken = !empty(\App\Models\SiteSetting::get('google_calendar_access_token'));
            $calendarService = app(\App\Services\GoogleCalendarService::class);
        @endphp

        <div class="bg-white rounded-xl shadow-sm border border-gray-100 p-6">
            <h2 class="font-bold text-gray-800 mb-4 flex items-center gap-2">
                <span class="w-2 h-2 bg-blue-500 rounded-full"></span>
                Google Calendar Entegrasyonu
            </h2>

            {{-- Bağlantı Durumu Bandı --}}
            @if($hasToken)
            <div class="flex items-center justify-between bg-green-50 border border-green-200 rounded-lg px-4 py-2.5 mb-5">
                <div class="flex items-center gap-2 text-sm text-green-700 font-medium">
                    <svg class="w-4 h-4" fill="currentColor" viewBox="0 0 20 20"><path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zm3.707-9.293a1 1 0 00-1.414-1.414L9 10.586 7.707 9.293a1 1 0 00-1.414 1.414l2 2a1 1 0 001.414 0l4-4z" clip-rule="evenodd"/></svg>
                    Google hesabı bağlı
                </div>
                <div class="flex items-center gap-2">
                    <button type="button" id="googleTestBtn"
                            class="text-xs bg-blue-100 text-blue-700 px-3 py-1.5 rounded-lg hover:bg-blue-200 transition-colors font-medium flex items-center gap-1">
                        <svg class="w-3.5 h-3.5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"/></svg>
                        Bağlantıyı Test Et
                    </button>
                    <form method="POST" action="{{ route('admin.ayarlar.google-disconnect') }}"
                          data-confirm="Google Calendar bağlantısını kesmek istediğinizden emin misiniz?">
                        @csrf
                        <button type="submit"
                                class="text-xs bg-red-100 text-red-700 px-3 py-1.5 rounded-lg hover:bg-red-200 transition-colors font-medium flex items-center gap-1">
                            <svg class="w-3.5 h-3.5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"/></svg>
                            Bağlantıyı Kes
                        </button>
                    </form>
                </div>
            </div>
            <div id="googleTestResult" class="hidden mb-4 px-3 py-2 rounded-lg text-sm font-medium"></div>
            @else
            <div class="flex items-center gap-2 bg-yellow-50 border border-yellow-200 rounded-lg px-4 py-2.5 mb-5 text-sm text-yellow-700">
                <svg class="w-4 h-4 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z"/></svg>
                Google hesabı henüz bağlanmamış. Bilgileri girin, kaydedin, ardından "Google ile Bağlan" butonuna tıklayın.
            </div>
            @endif

            <form id="googleSaveForm" class="space-y-0">
                <div class="grid grid-cols-1 lg:grid-cols-2 gap-5">
                    <div class="space-y-4">
                        <div class="flex items-center justify-between">
                            <label class="text-sm font-medium text-gray-700">Entegrasyonu Etkinleştir</label>
                            <label class="relative inline-flex items-center cursor-pointer">
                                <input type="checkbox" id="gcal_enabled" name="google_calendar_enabled" value="1"
                                       {{ \App\Models\SiteSetting::isEnabled('google_calendar_enabled') ? 'checked' : '' }}
                                       class="sr-only peer">
                                <div class="w-11 h-6 bg-gray-200 peer-focus:outline-none rounded-full peer peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-orange-500"></div>
                            </label>
                        </div>
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">Client ID</label>
                            <input type="text" id="gcal_client_id" name="google_calendar_client_id"
                                   value="{{ \App\Models\SiteSetting::get('google_calendar_client_id') }}"
                                   placeholder="Google OAuth Client ID"
                                   class="w-full border border-gray-300 rounded-lg px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-orange-400">
                        </div>
                    </div>
                    <div class="space-y-4">
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">Client Secret</label>
                            <input type="password" id="gcal_client_secret" name="google_calendar_client_secret"
                                   value="{{ \App\Models\SiteSetting::get('google_calendar_client_secret') }}"
                                   placeholder="Google OAuth Client Secret"
                                   class="w-full border border-gray-300 rounded-lg px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-orange-400">
                        </div>
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">Calendar ID</label>
                            <input type="text" id="gcal_calendar_id" name="google_calendar_id"
                                   value="{{ \App\Models\SiteSetting::get('google_calendar_id', 'primary') }}"
                                   placeholder="primary veya calendar@group.calendar.google.com"
                                   class="w-full border border-gray-300 rounded-lg px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-orange-400">
                        </div>
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">Redirect URI <span class="text-gray-400 font-normal">(Google Console'a ekleyin)</span></label>
                            <div class="flex items-center gap-2">
                                <input type="text" readonly
                                       value="{{ url('/admin/profil/google-callback') }}"
                                       class="flex-1 border border-gray-200 bg-gray-50 rounded-lg px-3 py-2 text-sm text-gray-600 select-all">
                                <button type="button" id="redirectUriCopyBtn"
                                        data-uri="{{ url('/admin/profil/google-callback') }}"
                                        class="text-xs bg-gray-100 text-gray-600 px-3 py-2 rounded-lg hover:bg-gray-200 transition-colors whitespace-nowrap">
                                    Kopyala
                                </button>
                            </div>
                            <p class="text-xs text-gray-400 mt-1">Google Cloud Console → OAuth 2.0 → Authorized redirect URIs bölümüne bu URL'yi ekleyin.</p>
                        </div>
                    </div>
                </div>

                <div id="googleSaveResult" class="hidden mt-3 px-3 py-2 rounded-lg text-sm font-medium"></div>

                <div class="flex items-center gap-3 mt-5 pt-4 border-t border-gray-100">
                    <button type="button" id="googleSaveBtn"
                            class="bg-primary text-white px-5 py-2 rounded-lg text-sm font-medium hover:bg-orange-600 transition-colors flex items-center gap-2">
                        <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"/></svg>
                        Google Ayarlarını Kaydet
                    </button>
                    <a href="{{ route('admin.profil.show') }}#google"
                       class="text-sm text-blue-600 hover:underline flex items-center gap-1">
                        <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13.828 10.172a4 4 0 00-5.656 0l-4 4a4 4 0 105.656 5.656l1.102-1.101m-.758-4.899a4 4 0 005.656 0l4-4a4 4 0 00-5.656-5.656l-1.1 1.1"/></svg>
                        Google hesabı bağlamak için Profil sayfasına gidin
                    </a>
                </div>
            </form>
        </div>

    </div>{{-- /tab-google --}}

    {{-- ======================================================= --}}
    {{-- TAB: VirusTotal                                         --}}
    {{-- ======================================================= --}}
    <div id="tab-virustotal" class="tab-panel space-y-5" style="display:none">

        <form method="POST" action="{{ route('admin.ayarlar.update') }}">
            @csrf
            <div class="bg-white rounded-xl shadow-sm border border-gray-100 p-6">
                <h2 class="font-bold text-gray-800 mb-4 flex items-center gap-2">
                    <span class="w-2 h-2 bg-green-500 rounded-full"></span>
                    VirusTotal CV Tarama
                </h2>
                <div class="space-y-4 max-w-lg">
                    <div class="flex items-center justify-between">
                        <label class="text-sm font-medium text-gray-700">Taramayı Etkinleştir</label>
                        <label class="relative inline-flex items-center cursor-pointer">
                            <input type="hidden" name="settings[virustotal_enabled]" value="0">
                            <input type="checkbox" name="settings[virustotal_enabled]" value="1"
                                   {{ \App\Models\SiteSetting::isEnabled('virustotal_enabled') ? 'checked' : '' }}
                                   class="sr-only peer">
                            <div class="w-11 h-6 bg-gray-200 peer-focus:outline-none rounded-full peer peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-orange-500"></div>
                        </label>
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1">API Key</label>
                        <input type="password" name="settings[virustotal_api_key]"
                               value="{{ \App\Models\SiteSetting::get('virustotal_api_key') }}"
                               placeholder="VirusTotal API anahtarınız"
                               class="w-full border border-gray-300 rounded-lg px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-orange-400">
                        <p class="text-xs text-gray-400 mt-1">
                            <a href="https://www.virustotal.com/gui/my-apikey" target="_blank" class="text-primary hover:underline">virustotal.com</a>'dan ücretsiz API key alabilirsiniz. Günlük 500 istek limiti vardır.
                        </p>

                        {{-- CV Tarama Modu --}}
                        <div class="mt-4 pt-4 border-t border-gray-100">
                            <label class="block text-sm font-medium text-gray-700 mb-2">CV Tarama Modu</label>
                            <p class="text-xs text-gray-400 mb-3">Yeni başvuru geldiğinde CV taraması nasıl başlatılsın?</p>
                            @php $cvScanMode = \App\Models\SiteSetting::get('cv_scan_mode', 'otomatik'); @endphp
                            <div class="space-y-2">
                                <label class="flex items-start gap-3 p-3 border rounded-lg cursor-pointer hover:bg-orange-50 transition-colors {{ $cvScanMode === 'otomatik' ? 'border-orange-400 bg-orange-50' : 'border-gray-200' }}">
                                    <input type="radio" name="settings[cv_scan_mode]" value="otomatik"
                                           {{ $cvScanMode === 'otomatik' ? 'checked' : '' }}
                                           class="mt-0.5 text-orange-500 focus:ring-orange-400">
                                    <div>
                                        <p class="text-sm font-medium text-gray-800">Otomatik</p>
                                        <p class="text-xs text-gray-500">Başvuru alındığında CV hemen kuyruğa alınır ve arka planda taranır.</p>
                                    </div>
                                </label>
                                <label class="flex items-start gap-3 p-3 border rounded-lg cursor-pointer hover:bg-gray-50 transition-colors {{ $cvScanMode === 'manuel' ? 'border-orange-400 bg-orange-50' : 'border-gray-200' }}">
                                    <input type="radio" name="settings[cv_scan_mode]" value="manuel"
                                           {{ $cvScanMode === 'manuel' ? 'checked' : '' }}
                                           class="mt-0.5 text-orange-500 focus:ring-orange-400">
                                    <div>
                                        <p class="text-sm font-medium text-gray-800">Manuel</p>
                                        <p class="text-xs text-gray-500">CV taraması otomatik başlamaz. Admin başvuru detayından "Tekrar Analiz Yaptır" butonuyla tetikler.</p>
                                    </div>
                                </label>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="mt-5 flex justify-end">
                    <button type="submit"
                            class="bg-primary text-white px-6 py-2.5 rounded-lg text-sm font-medium hover:bg-orange-600 transition-colors">
                        Kaydet
                    </button>
                </div>
            </div>
        </form>

    </div>{{-- /tab-virustotal --}}

    {{-- ======================================================= --}}
    {{-- TAB: Sistem                                             --}}
    {{-- ======================================================= --}}
    <div id="tab-sistem" class="tab-panel space-y-5" style="display:none">

        {{-- Sistem Araçları --}}
        <div class="grid grid-cols-1 lg:grid-cols-3 gap-5">

            {{-- Veritabanı Yedeği --}}
            <div class="bg-white rounded-xl shadow-sm border border-gray-100 p-6 lg:col-span-3">
                <h2 class="font-bold text-gray-800 mb-1 flex items-center gap-2">
                    <span class="w-2 h-2 bg-indigo-500 rounded-full"></span>
                    Veritabanı Yedeği
                </h2>
                <p class="text-xs text-gray-400 mb-4">Yedek modunu ve dosya formatını seçerek indirin</p>

                <form method="POST" action="{{ route('admin.ayarlar.backup') }}" id="backupForm">
                    @csrf

                    <div class="grid grid-cols-1 md:grid-cols-3 gap-5 mb-4">

                        {{-- Mod Seçimi --}}
                        <div>
                            <p class="text-xs font-semibold text-gray-600 mb-2 uppercase tracking-wide">Yedek Modu</p>
                            <div class="space-y-2">
                                <label class="flex items-start gap-2.5 p-2.5 rounded-lg border border-gray-200 hover:border-indigo-300 hover:bg-indigo-50 transition-colors cursor-pointer has-[:checked]:border-indigo-500 has-[:checked]:bg-indigo-50">
                                    <input type="radio" name="mode" value="full" checked class="mt-0.5 accent-indigo-600">
                                    <div>
                                        <p class="text-sm font-medium text-gray-800">Tam Yedek</p>
                                        <p class="text-xs text-gray-400">Tüm tablolar, yapı + veriler</p>
                                    </div>
                                </label>
                                <label class="flex items-start gap-2.5 p-2.5 rounded-lg border border-gray-200 hover:border-indigo-300 hover:bg-indigo-50 transition-colors cursor-pointer has-[:checked]:border-indigo-500 has-[:checked]:bg-indigo-50">
                                    <input type="radio" name="mode" value="schema" class="mt-0.5 accent-indigo-600">
                                    <div>
                                        <p class="text-sm font-medium text-gray-800">Sadece İskelet</p>
                                        <p class="text-xs text-gray-400">Tablo yapıları, veri yok</p>
                                    </div>
                                </label>
                                <label class="flex items-start gap-2.5 p-2.5 rounded-lg border border-gray-200 hover:border-indigo-300 hover:bg-indigo-50 transition-colors cursor-pointer has-[:checked]:border-indigo-500 has-[:checked]:bg-indigo-50">
                                    <input type="radio" name="mode" value="tables" class="mt-0.5 accent-indigo-600">
                                    <div>
                                        <p class="text-sm font-medium text-gray-800">Belirli Tablolar</p>
                                        <p class="text-xs text-gray-400">Seçtiğiniz tablolar, yapı + veriler</p>
                                    </div>
                                </label>
                            </div>
                        </div>

                        {{-- Format Seçimi --}}
                        <div>
                            <p class="text-xs font-semibold text-gray-600 mb-2 uppercase tracking-wide">Dosya Formatı</p>
                            <div class="space-y-2">
                                <label class="flex items-start gap-2.5 p-2.5 rounded-lg border border-gray-200 hover:border-indigo-300 hover:bg-indigo-50 transition-colors cursor-pointer has-[:checked]:border-indigo-500 has-[:checked]:bg-indigo-50">
                                    <input type="radio" name="format" value="sql" checked class="mt-0.5 accent-indigo-600" id="fmt-sql">
                                    <div>
                                        <p class="text-sm font-medium text-gray-800">.sql</p>
                                        <p class="text-xs text-gray-400">Ham SQL dosyası</p>
                                    </div>
                                </label>
                                <label class="flex items-start gap-2.5 p-2.5 rounded-lg border border-gray-200 hover:border-indigo-300 hover:bg-indigo-50 transition-colors cursor-pointer has-[:checked]:border-indigo-500 has-[:checked]:bg-indigo-50">
                                    <input type="radio" name="format" value="gz" class="mt-0.5 accent-indigo-600" id="fmt-gz">
                                    <div>
                                        <p class="text-sm font-medium text-gray-800">.sql.gz</p>
                                        <p class="text-xs text-gray-400">Sıkıştırılmış SQL (daha küçük)</p>
                                    </div>
                                </label>
                                <label class="flex items-start gap-2.5 p-2.5 rounded-lg border border-gray-200 hover:border-indigo-300 hover:bg-indigo-50 transition-colors cursor-pointer has-[:checked]:border-indigo-500 has-[:checked]:bg-indigo-50" id="fmt-json-label">
                                    <input type="radio" name="format" value="json" class="mt-0.5 accent-indigo-600" id="fmt-json">
                                    <div>
                                        <p class="text-sm font-medium text-gray-800">.json</p>
                                        <p class="text-xs text-gray-400">JSON formatı (iskelet modunda kullanılamaz)</p>
                                    </div>
                                </label>
                            </div>
                        </div>

                        {{-- Tablo Seçimi (sadece "Belirli Tablolar" modunda görünür) --}}
                        <div id="tableSelectPanel" style="display:none;">
                            <p class="text-xs font-semibold text-gray-600 mb-2 uppercase tracking-wide">Tablolar</p>
                            <div class="border border-gray-200 rounded-lg p-3 space-y-1.5 max-h-52 overflow-y-auto">
{{-- $backupTables controller'dan (SHOW TABLES) dinamik olarak geliyor --}}
                                <label class="flex items-center gap-2 text-xs text-indigo-600 font-medium cursor-pointer mb-1">
                                    <input type="checkbox" id="selectAllTables" class="accent-indigo-600">
                                    Tümünü Seç / Kaldır
                                </label>
                                <div class="border-t border-gray-100 pt-1.5 space-y-1.5">
                                @foreach($backupTables as $tbl)
                                <label class="flex items-center gap-2 text-xs text-gray-700 cursor-pointer">
                                    <input type="checkbox" name="tables[]" value="{{ $tbl }}" class="accent-indigo-600 backup-table-cb">
                                    <span class="font-mono">{{ $tbl }}</span>
                                </label>
                                @endforeach
                                </div>
                            </div>
                        </div>

                    </div>

                    <button type="submit" id="backupSubmitBtn"
                            class="bg-indigo-600 text-white px-5 py-2 rounded-lg text-sm font-medium hover:bg-indigo-700 transition-colors flex items-center gap-2 disabled:opacity-60 disabled:cursor-not-allowed">
                        <svg id="backupBtnIcon" class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-4l-4 4m0 0l-4-4m4 4V4"/></svg>
                        <span id="backupBtnText">Yedek Al</span>
                    </button>
                </form>
            </div>

            {{-- Bakım Modu --}}
            <div class="bg-white rounded-xl shadow-sm border border-gray-100 p-6">
                <h2 class="font-bold text-gray-800 mb-1 flex items-center gap-2">
                    <span class="w-2 h-2 {{ $isMaintenanceMode ? 'bg-red-500' : 'bg-green-500' }} rounded-full"></span>
                    Bakım Modu
                </h2>
                <p class="text-xs text-gray-400 mb-4">
                    Şu an: <span class="font-medium {{ $isMaintenanceMode ? 'text-red-600' : 'text-green-600' }}">{{ $isMaintenanceMode ? 'Bakım Modunda' : 'Aktif' }}</span>
                </p>
                <form method="POST" action="{{ route('admin.ayarlar.maintenance') }}">
                    @csrf
                    <button type="submit"
                            class="w-full {{ $isMaintenanceMode ? 'bg-green-600 hover:bg-green-700' : 'bg-red-500 hover:bg-red-600' }} text-white py-2 rounded-lg text-sm font-medium transition-colors flex items-center justify-center gap-2">
                        <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.065 2.572c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.572 1.065c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.065-2.572c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z"/><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"/></svg>
                        {{ $isMaintenanceMode ? 'Bakım Modunu Kapat' : 'Bakım Moduna Al' }}
                    </button>
                </form>
            </div>

        </div>

        {{-- Sistem Bilgisi --}}
        <div class="bg-white rounded-xl shadow-sm border border-gray-100 p-6">
            <h2 class="font-bold text-gray-800 mb-4 flex items-center gap-2">
                <span class="w-2 h-2 bg-gray-400 rounded-full"></span>
                Sistem Bilgisi
            </h2>
            <div class="grid grid-cols-2 md:grid-cols-4 gap-4">
                <div class="bg-gray-50 rounded-lg p-3">
                    <p class="text-xs text-gray-400 mb-0.5">PHP Versiyonu</p>
                    <p class="text-sm font-semibold text-gray-800">{{ $systemInfo['php_version'] }}</p>
                </div>
                <div class="bg-gray-50 rounded-lg p-3">
                    <p class="text-xs text-gray-400 mb-0.5">Laravel Versiyonu</p>
                    <p class="text-sm font-semibold text-gray-800">{{ $systemInfo['laravel_version'] }}</p>
                </div>
                <div class="bg-gray-50 rounded-lg p-3">
                    <p class="text-xs text-gray-400 mb-0.5">Ortam</p>
                    <p class="text-sm font-semibold text-gray-800">{{ $systemInfo['environment'] }}</p>
                </div>
                <div class="bg-gray-50 rounded-lg p-3">
                    <p class="text-xs text-gray-400 mb-0.5">Debug Modu</p>
                    <p class="text-sm font-semibold {{ $systemInfo['debug_mode'] === 'Açık' ? 'text-red-600' : 'text-green-600' }}">{{ $systemInfo['debug_mode'] }}</p>
                </div>
                <div class="bg-gray-50 rounded-lg p-3 col-span-2">
                    <p class="text-xs text-gray-400 mb-0.5">Disk Kullanımı</p>
                    <p class="text-sm font-semibold text-gray-800">{{ $systemInfo['disk_used'] }} / {{ $systemInfo['disk_total'] }} ({{ $systemInfo['disk_percent'] }}%)</p>
                    <div class="w-full bg-gray-200 rounded-full h-1.5 mt-1.5">
                        <div class="bg-orange-500 h-1.5 rounded-full" style="width:{{ $systemInfo['disk_percent'] }}%"></div>
                    </div>
                </div>
                <div class="bg-gray-50 rounded-lg p-3">
                    <p class="text-xs text-gray-400 mb-0.5">Queue Driver</p>
                    <p class="text-sm font-semibold text-gray-800">{{ $systemInfo['queue_driver'] }}</p>
                </div>
                <div class="bg-gray-50 rounded-lg p-3">
                    <p class="text-xs text-gray-400 mb-0.5">Cache / Mail</p>
                    <p class="text-sm font-semibold text-gray-800">{{ $systemInfo['cache_driver'] }} / {{ $systemInfo['mail_driver'] }}</p>
                </div>
            </div>
        </div>

    </div>{{-- /tab-sistem --}}

    {{-- ======================================================= --}}
    {{-- TAB: KVKK                                               --}}
    {{-- ======================================================= --}}
    <div id="tab-kvkk" class="tab-panel space-y-5" style="display:none">

        {{-- 1. KVKK Aydınlatma Metni --}}
        <form method="POST" action="{{ route('admin.ayarlar.update') }}" enctype="multipart/form-data">
            @csrf
            <div class="bg-white rounded-xl shadow-sm border border-gray-100 p-6">
                <h2 class="font-bold text-gray-800 mb-1 flex items-center gap-2">
                    <span class="w-2 h-2 bg-orange-500 rounded-full"></span>
                    KVKK Aydınlatma Metni Yönetimi
                </h2>
                <p class="text-xs text-gray-400 mb-5">Bu metin, başvuru formundaki KVKK modalında ve KVKK sayfasında gösterilir.</p>

                <div class="space-y-5">
                    {{-- Saklama Süresi --}}
                    <div class="max-w-xs">
                        <label class="block text-sm font-medium text-gray-700 mb-1">Veri Saklama Süresi (gün)</label>
                        <input type="number" name="settings[kvkk_retention_days]" min="30" max="730"
                               value="{{ \App\Models\SiteSetting::get('kvkk_retention_days', 90) }}"
                               class="w-full border border-gray-300 rounded-lg px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-orange-400">
                        <p class="text-xs text-gray-400 mt-1">Başvuruların kişisel verileri bu süre sonunda otomatik silinir. Varsayılan: 90 gün.</p>
                    </div>

                    {{-- Metin Editörü --}}
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-2">KVKK Aydınlatma Metni</label>
                        <textarea name="settings[kvkk_text]" rows="16"
                                  class="w-full border border-gray-300 rounded-lg px-3 py-2.5 text-sm focus:outline-none focus:ring-2 focus:ring-orange-400 font-mono leading-relaxed resize-y"
                                  placeholder="KVKK aydınlatma metnini buraya girin. HTML etiketleri kullanabilirsiniz.">{{ \App\Models\SiteSetting::get('kvkk_text') }}</textarea>
                        <p class="text-xs text-gray-400 mt-1">Boş bırakırsanız varsayılan statik metin gösterilir. HTML etiketleri desteklenir (&lt;p&gt;, &lt;strong&gt;, &lt;ul&gt;, &lt;li&gt; vb.)</p>
                    </div>

                    {{-- Son Güncelleme Tarihi --}}
                    <div class="max-w-xs">
                        <label class="block text-sm font-medium text-gray-700 mb-1">Son Güncelleme Tarihi</label>
                        <input type="date" name="settings[kvkk_updated_at]"
                               value="{{ \App\Models\SiteSetting::get('kvkk_updated_at', date('Y-m-d')) }}"
                               class="w-full border border-gray-300 rounded-lg px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-orange-400">
                    </div>
                </div>

                <div class="mt-6 flex justify-end">
                    <button type="submit"
                            class="bg-primary text-white px-6 py-2.5 rounded-lg text-sm font-medium hover:bg-orange-600 transition-colors flex items-center gap-2">
                        <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"/></svg>
                        KVKK Ayarlarını Kaydet
                    </button>
                </div>
            </div>
        </form>

        {{-- 2. KVKK Veri Yönetimi (sadece super-admin) --}}
        @if($kvkkData)

        {{-- İstatistik Kartları --}}
        <div class="bg-white rounded-xl shadow-sm border border-gray-100 p-6">
            <h2 class="font-bold text-gray-800 mb-1 flex items-center gap-2">
                <span class="w-2 h-2 bg-red-500 rounded-full"></span>
                KVKK Veri Yönetimi
            </h2>
            <p class="text-xs text-gray-400 mb-5">Saklama süresi dolan başvuruların kişisel verilerini buradan yönetin. Silme işlemi geri alınamaz.</p>

            <div class="grid grid-cols-2 sm:grid-cols-4 gap-3 mb-6">
                <div class="rounded-lg p-3 {{ $kvkkData['pending_count'] > 0 ? 'bg-red-50 border border-red-200' : 'bg-green-50 border border-green-200' }}">
                    <div class="text-2xl font-bold {{ $kvkkData['pending_count'] > 0 ? 'text-red-700' : 'text-green-700' }}">
                        {{ $kvkkData['pending_count'] }}
                    </div>
                    <div class="text-xs {{ $kvkkData['pending_count'] > 0 ? 'text-red-600' : 'text-green-600' }} mt-0.5 font-medium">Silinmeyi Bekliyor</div>
                    @if($kvkkData['pending_count'] > 0)
                    <div class="text-xs text-red-500 mt-1">Süre dolmuş!</div>
                    @endif
                </div>
                <div class="rounded-lg p-3 {{ $kvkkData['expiring_count'] > 0 ? 'bg-orange-50 border border-orange-200' : 'bg-gray-50 border border-gray-200' }}">
                    <div class="text-2xl font-bold {{ $kvkkData['expiring_count'] > 0 ? 'text-orange-700' : 'text-gray-500' }}">
                        {{ $kvkkData['expiring_count'] }}
                    </div>
                    <div class="text-xs {{ $kvkkData['expiring_count'] > 0 ? 'text-orange-600' : 'text-gray-500' }} mt-0.5 font-medium">30 Gün İçinde Dolacak</div>
                </div>
                <div class="rounded-lg p-3 bg-blue-50 border border-blue-200">
                    <div class="text-2xl font-bold text-blue-700">{{ $kvkkData['purged_total'] }}</div>
                    <div class="text-xs text-blue-600 mt-0.5 font-medium">Toplam Anonimleştirilen</div>
                </div>
                <div class="rounded-lg p-3 bg-gray-50 border border-gray-200">
                    <div class="text-sm font-bold text-gray-700">{{ $kvkkData['next_purge_date'] }}</div>
                    <div class="text-xs text-gray-500 mt-0.5 font-medium">Sonraki Otomatik Temizleme</div>
                </div>
            </div>
            {{-- Aktif başvuru sayısı özeti --}}
            <div class="mt-2 mb-4 text-xs text-gray-500 flex items-center gap-1.5">
                <svg class="w-3.5 h-3.5 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"/></svg>
                Saklama süresi içinde <strong class="text-gray-700">{{ $kvkkData['active_count'] }}</strong> aktif başvuru bulunuyor. Bunlar aşağıdaki tabloda listelenmektedir.
            </div>

            {{-- Toplu Silme Butonu --}}
            @if($kvkkData['pending_count'] > 0)
            <form method="POST" action="{{ route('admin.ayarlar.kvkk-delete-all') }}"
                  data-confirm="{{ $kvkkData['pending_count'] }} başvurunun kişisel verileri kalıcı olarak silinecek. Bu işlem geri alınamaz. Devam etmek istiyor musunuz?">
                @csrf
                <button type="submit"
                        class="inline-flex items-center gap-2 bg-red-600 text-white px-5 py-2.5 rounded-lg text-sm font-semibold hover:bg-red-700 transition-colors">
                    <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"/>
                    </svg>
                    Tümünü Sil ({{ $kvkkData['pending_count'] }} başvuru)
                </button>
            </form>
            @else
            <p class="text-xs text-green-600 flex items-center gap-1.5">
                <svg class="w-4 h-4" fill="currentColor" viewBox="0 0 20 20"><path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zm3.707-9.293a1 1 0 00-1.414-1.414L9 10.586 7.707 9.293a1 1 0 00-1.414 1.414l2 2a1 1 0 001.414 0l4-4z" clip-rule="evenodd"/></svg>
                Tüm veriler KVKK saklama süresine uygun. Silinmesi gereken başvuru yok.
            </p>
            @endif
        </div>

        {{-- Süresi Dolmuş Başvurular Tablosu --}}
        @if($kvkkData['pending_count'] > 0)
        <div class="bg-white rounded-xl shadow-sm border border-red-100 overflow-hidden">
            <div class="px-5 py-3 border-b border-red-100 bg-red-50 flex items-center gap-2">
                <svg class="w-4 h-4 text-red-600" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z"/></svg>
                <h3 class="text-sm font-bold text-red-700">Silinmeyi Bekleyen Başvurular ({{ $kvkkData['pending_count'] }})</h3>
            </div>
            <div class="overflow-x-auto">
                <table class="w-full text-sm">
                    <thead class="bg-gray-50 border-b border-gray-100">
                        <tr>
                            <th class="text-left px-4 py-3 font-semibold text-gray-600">Ad Soyad</th>
                            <th class="text-left px-4 py-3 font-semibold text-gray-600">E-posta</th>
                            <th class="text-left px-4 py-3 font-semibold text-gray-600">Pozisyon</th>
                            <th class="text-left px-4 py-3 font-semibold text-gray-600">Başvuru Tarihi</th>
                            <th class="text-left px-4 py-3 font-semibold text-gray-600">Silinmesi Gerekiyordu</th>
                            <th class="text-left px-4 py-3 font-semibold text-gray-600">Gecikme</th>
                            <th class="px-4 py-3"></th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-50">
                        @foreach($kvkkData['pending_applications'] as $app)
                        <tr class="hover:bg-red-50 transition-colors">
                            <td class="px-4 py-3 font-medium text-gray-800">{{ $app['full_name'] }}</td>
                            <td class="px-4 py-3 text-gray-500 text-xs">{{ $app['email'] }}</td>
                            <td class="px-4 py-3 text-gray-600">{{ $app['job_title'] }}</td>
                            <td class="px-4 py-3 text-gray-500 text-xs">{{ $app['applied_at'] }}</td>
                            <td class="px-4 py-3">
                                <span class="text-red-600 font-medium text-xs">{{ $app['delete_date'] }}</span>
                            </td>
                            <td class="px-4 py-3">
                                <span class="inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium bg-red-100 text-red-700">
                                    {{ $app['days_overdue'] }} gün gecikmiş
                                </span>
                            </td>
                            <td class="px-4 py-3">
                                <form method="POST"
                                      action="{{ route('admin.ayarlar.kvkk-delete-single', $app['id']) }}"
                                      data-confirm="{{ $app['full_name'] }} adlı kişinin verileri kalıcı olarak silinecek. Onaylıyor musunuz?">
                                    @csrf
                                    <button type="submit"
                                            class="inline-flex items-center gap-1 text-xs text-red-600 hover:text-red-800 font-medium hover:underline transition-colors">
                                        <svg class="w-3.5 h-3.5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"/></svg>
                                        Sil
                                    </button>
                                </form>
                            </td>
                        </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        </div>
        @endif

        {{-- 30 Gün İçinde Dolacak Başvurular --}}
        @if($kvkkData['expiring_count'] > 0)
        <div class="bg-white rounded-xl shadow-sm border border-orange-100 overflow-hidden">
            <div class="px-5 py-3 border-b border-orange-100 bg-orange-50 flex items-center gap-2">
                <svg class="w-4 h-4 text-orange-600" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z"/></svg>
                <h3 class="text-sm font-bold text-orange-700">30 Gün İçinde Silinecek Başvurular ({{ $kvkkData['expiring_count'] }})</h3>
            </div>
            <div class="overflow-x-auto">
                <table class="w-full text-sm">
                    <thead class="bg-gray-50 border-b border-gray-100">
                        <tr>
                            <th class="text-left px-4 py-3 font-semibold text-gray-600">Ad Soyad</th>
                            <th class="text-left px-4 py-3 font-semibold text-gray-600">E-posta</th>
                            <th class="text-left px-4 py-3 font-semibold text-gray-600">Pozisyon</th>
                            <th class="text-left px-4 py-3 font-semibold text-gray-600">Başvuru Tarihi</th>
                            <th class="text-left px-4 py-3 font-semibold text-gray-600">Silinme Tarihi</th>
                            <th class="text-left px-4 py-3 font-semibold text-gray-600">Kalan Süre</th>
                            <th class="px-4 py-3"></th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-50">
                        @foreach($kvkkData['expiring_applications'] as $app)
                        <tr class="hover:bg-orange-50 transition-colors">
                            <td class="px-4 py-3 font-medium text-gray-800">{{ $app['full_name'] }}</td>
                            <td class="px-4 py-3 text-gray-500 text-xs">{{ $app['email'] }}</td>
                            <td class="px-4 py-3 text-gray-600">{{ $app['job_title'] }}</td>
                            <td class="px-4 py-3 text-gray-500 text-xs">{{ $app['applied_at'] }}</td>
                            <td class="px-4 py-3">
                                <span class="text-orange-600 font-medium text-xs">{{ $app['delete_date'] }}</span>
                            </td>
                            <td class="px-4 py-3">
                                <span class="inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium bg-orange-100 text-orange-700">
                                    {{ $app['days_left'] }} gün kaldı
                                </span>
                            </td>
                            <td class="px-4 py-3">
                                <form method="POST"
                                      action="{{ route('admin.ayarlar.kvkk-delete-single', $app['id']) }}"
                                      data-confirm="{{ $app['full_name'] }} adlı kişinin verileri erken silinecek. Onaylıyor musunuz?">
                                    @csrf
                                    <button type="submit"
                                            class="inline-flex items-center gap-1 text-xs text-orange-600 hover:text-orange-800 font-medium hover:underline transition-colors">
                                        <svg class="w-3.5 h-3.5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"/></svg>
                                        Erken Sil
                                    </button>
                                </form>
                            </td>
                        </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        </div>
        @endif

        {{-- Tüm Aktif Başvurular Tablosu --}}
        @if($kvkkData['active_count'] > 0)
        <div class="bg-white rounded-xl shadow-sm border border-gray-100 overflow-hidden">
            <div class="px-5 py-3 border-b border-gray-100 bg-gray-50 flex items-center gap-2">
                <svg class="w-4 h-4 text-gray-500" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 20h5v-2a3 3 0 00-5.356-1.857M17 20H7m10 0v-2c0-.656-.126-1.283-.356-1.857M7 20H2v-2a3 3 0 015.356-1.857M7 20v-2c0-.656.126-1.283.356-1.857m0 0a5.002 5.002 0 019.288 0M15 7a3 3 0 11-6 0 3 3 0 016 0z"/></svg>
                <h3 class="text-sm font-bold text-gray-700">Tüm Aktif Başvurular — Saklama Süresi İçinde ({{ $kvkkData['active_count'] }})</h3>
            </div>
            <div class="overflow-x-auto">
                <table class="w-full text-sm">
                    <thead class="bg-gray-50 border-b border-gray-100">
                        <tr>
                            <th class="text-left px-4 py-3 font-semibold text-gray-600">Ad Soyad</th>
                            <th class="text-left px-4 py-3 font-semibold text-gray-600">E-posta</th>
                            <th class="text-left px-4 py-3 font-semibold text-gray-600">Pozisyon</th>
                            <th class="text-left px-4 py-3 font-semibold text-gray-600">Başvuru Tarihi</th>
                            <th class="text-left px-4 py-3 font-semibold text-gray-600">Silinme Tarihi</th>
                            <th class="text-left px-4 py-3 font-semibold text-gray-600">Kalan Süre</th>
                            <th class="px-4 py-3"></th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-50">
                        @foreach($kvkkData['active_applications'] as $app)
                        <tr class="hover:bg-gray-50 transition-colors">
                            <td class="px-4 py-3 font-medium text-gray-800">{{ $app['full_name'] }}</td>
                            <td class="px-4 py-3 text-gray-500 text-xs">{{ $app['email'] }}</td>
                            <td class="px-4 py-3 text-gray-600">{{ $app['job_title'] }}</td>
                            <td class="px-4 py-3 text-gray-500 text-xs">{{ $app['applied_at'] }}</td>
                            <td class="px-4 py-3">
                                <span class="text-gray-600 font-medium text-xs">{{ $app['delete_date'] }}</span>
                            </td>
                            <td class="px-4 py-3">
                                @php $daysLeft = $app['days_left']; @endphp
                                <span class="inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium
                                    {{ $daysLeft <= 7 ? 'bg-red-100 text-red-700' : ($daysLeft <= 30 ? 'bg-orange-100 text-orange-700' : 'bg-green-100 text-green-700') }}">
                                    {{ $daysLeft }} gün kaldı
                                </span>
                            </td>
                            <td class="px-4 py-3">
                                <form method="POST"
                                      action="{{ route('admin.ayarlar.kvkk-delete-single', $app['id']) }}"
                                      data-confirm="{{ $app['full_name'] }} adlı kişinin verileri erken silinecek. Onaylıyor musunuz?">
                                    @csrf
                                    <button type="submit"
                                            class="inline-flex items-center gap-1 text-xs text-gray-500 hover:text-red-700 font-medium hover:underline transition-colors">
                                        <svg class="w-3.5 h-3.5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"/></svg>
                                        Erken Sil
                                    </button>
                                </form>
                            </td>
                        </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        </div>
        @endif

        @endif {{-- /kvkkData --}}

    </div>{{-- /tab-kvkk --}}

</div>{{-- /mt-4 --}}

<script nonce="{{ app('csp-nonce') }}">
// Sekme bazında izin kontrolü
var tabPermissions = {
    'genel':      {{ $canManageGeneral    ? 'true' : 'false' }},
    'google':     {{ $canManageGoogle     ? 'true' : 'false' }},
    'virustotal': {{ $canManageVirustotal ? 'true' : 'false' }},
    'sistem':     {{ $canManageSystem     ? 'true' : 'false' }},
    'kvkk':       {{ $canManageKvkk       ? 'true' : 'false' }},
};

function disableTabPanel(tabId) {
    var panel = document.getElementById('tab-' + tabId);
    if (!panel) return;

    // Form submit'lerini engelle
    panel.querySelectorAll('form').forEach(function (form) {
        form.addEventListener('submit', function (e) {
            e.preventDefault();
            alert('Bu sekme için düzenleme yetkiniz bulunmamaktadır.');
        });
    });

    // Input, select, textarea ve sadece submit butonlarını disable et
    // (type="button" olan navigasyon/aksiyon butonlarını etkileme)
    panel.querySelectorAll('input:not([type="hidden"]), select, textarea, button[type="submit"]').forEach(function (el) {
        if (el.hasAttribute('readonly')) return;
        el.disabled = true;
        el.style.cursor = 'not-allowed';
        el.style.opacity = '0.6';
    });
    // type="button" olanları da disable et ama sekme navigasyon butonlarını atla
    panel.querySelectorAll('button[type="button"]').forEach(function (el) {
        if (el.hasAttribute('readonly')) return;
        if (el.id && el.id.startsWith('tab-btn-')) return; // sekme butonlarını atla
        el.disabled = true;
        el.style.cursor = 'not-allowed';
        el.style.opacity = '0.6';
    });

    // Sekme başlığına kilit ikonu ekle
    var btn = document.getElementById('tab-btn-' + tabId);
    if (btn && !btn.querySelector('.lock-icon')) {
        var lock = document.createElement('span');
        lock.className = 'lock-icon ml-1 text-gray-400';
        lock.innerHTML = '<svg class="w-3 h-3 inline" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 15v2m-6 4h12a2 2 0 002-2v-6a2 2 0 00-2-2H6a2 2 0 00-2 2v6a2 2 0 002 2zm10-10V7a4 4 0 00-8 0v4h8z"/></svg>';
        btn.querySelector('span').appendChild(lock);
    }
}

// Sekme navigasyonu
function showTab(tabId) {
    document.querySelectorAll('.tab-panel').forEach(function(p) {
        p.style.display = 'none';
    });
    document.querySelectorAll('.tab-btn').forEach(function(b) {
        b.classList.remove('bg-primary', 'text-white');
        b.classList.add('text-gray-600', 'hover:bg-gray-100');
    });
    var panel = document.getElementById('tab-' + tabId);
    if (panel) panel.style.display = '';
    var btn = document.getElementById('tab-btn-' + tabId);
    if (btn) {
        btn.classList.add('bg-primary', 'text-white');
        btn.classList.remove('text-gray-600', 'hover:bg-gray-100');
    }
    history.replaceState(null, '', '#' + tabId);
}

document.addEventListener('DOMContentLoaded', function() {
    // Sekme butonlarına addEventListener ile bağla (CSP uyumlu — onclick= yok)
    document.querySelectorAll('.tab-btn[data-tab]').forEach(function (btn) {
        btn.addEventListener('click', function () {
            showTab(btn.getAttribute('data-tab'));
        });
    });

    // Google Kaydet butonu
    var googleSaveBtn = document.getElementById('googleSaveBtn');
    if (googleSaveBtn) {
        googleSaveBtn.addEventListener('click', saveGoogleSettings);
    }

    // Google Test butonu
    var googleTestBtn = document.getElementById('googleTestBtn');
    if (googleTestBtn) {
        googleTestBtn.addEventListener('click', testGoogleConnection);
    }

    // data-confirm attribute'u olan formlar için delegated submit listener
    document.addEventListener('submit', function (e) {
        var form = e.target;
        var msg = form.getAttribute('data-confirm');
        if (msg && !confirm(msg)) {
            e.preventDefault();
        }
    });

    // selectAllTables checkbox
    var selectAllCb = document.getElementById('selectAllTables');
    if (selectAllCb) {
        selectAllCb.addEventListener('change', function () {
            toggleAllTables(this);
        });
    }

    // Backup mod radio'ları
    document.querySelectorAll('input[name="mode"]').forEach(function (radio) {
        radio.addEventListener('change', backupModeChanged);
    });

    // Backup form — loading state (çift submit önleme)
    var backupForm = document.getElementById('backupForm');
    if (backupForm) {
        backupForm.addEventListener('submit', function () {
            var btn  = document.getElementById('backupSubmitBtn');
            var icon = document.getElementById('backupBtnIcon');
            var text = document.getElementById('backupBtnText');
            if (btn) {
                btn.disabled = true;
                if (icon) icon.outerHTML = '<svg id="backupBtnIcon" class="w-4 h-4 animate-spin" fill="none" viewBox="0 0 24 24"><circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle><path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4z"></path></svg>';
                if (text) text.textContent = 'Hazırlanıyor...';
                // İndirme tamamlandıktan sonra butonu serbest bırak (tahmini 8 sn)
                setTimeout(function () {
                    var b = document.getElementById('backupSubmitBtn');
                    var t = document.getElementById('backupBtnText');
                    if (b) b.disabled = false;
                    if (t) t.textContent = 'Yedek Al';
                }, 8000);
            }
        });
    }

    // Logo & Favicon file input önizleme
    var siteLogoInput = document.getElementById('site_logo');
    if (siteLogoInput) {
        siteLogoInput.addEventListener('change', function () {
            previewBrandingImage(this, 'logo_preview_img');
        });
    }
    var siteFaviconInput = document.getElementById('site_favicon');
    if (siteFaviconInput) {
        siteFaviconInput.addEventListener('change', function () {
            previewBrandingImage(this, 'favicon_preview_img');
        });
    }

    // Kopyala butonu (Redirect URI)
    var copyBtn = document.getElementById('redirectUriCopyBtn');
    if (copyBtn) {
        copyBtn.addEventListener('click', function () {
            var uri = copyBtn.getAttribute('data-uri');
            navigator.clipboard.writeText(uri).then(function () {
                copyBtn.textContent = 'Kopyalandı!';
                setTimeout(function () { copyBtn.textContent = 'Kopyala'; }, 2000);
            });
        });
    }

    // Önce sekmeyi göster
    var hash = window.location.hash.replace('#', '');
    var validTabs = ['genel', 'google', 'virustotal', 'sistem', 'kvkk'];
    if (hash && validTabs.indexOf(hash) !== -1) {
        showTab(hash);
    } else {
        showTab('genel');
    }

    // Sonra izin kontrolüne göre form elemanlarını disable et
    Object.keys(tabPermissions).forEach(function(tabId) {
        if (!tabPermissions[tabId]) {
            disableTabPanel(tabId);
        }
    });

    // Banner renk picker (null kontrolüyle)
    var colorPicker = document.getElementById('bannerColorPicker');
    if (colorPicker) {
        colorPicker.addEventListener('input', function() {
            var colorText = document.getElementById('bannerColorText');
            if (colorText) colorText.value = this.value;
        });
    }
});

// Logo & Favicon önizleme
function previewBrandingImage(input, imgId) {
    if (input.files && input.files[0]) {
        var reader = new FileReader();
        reader.onload = function(e) {
            document.getElementById(imgId).src = e.target.result;
        };
        reader.readAsDataURL(input.files[0]);
    }
}

function saveGoogleSettings() {
    var btn    = document.getElementById('googleSaveBtn');
    var result = document.getElementById('googleSaveResult');
    var csrf   = document.querySelector('meta[name="csrf-token"]').content;

    var enabled    = document.getElementById('gcal_enabled').checked ? '1' : '0';
    var clientId   = document.getElementById('gcal_client_id').value;
    var clientSec  = document.getElementById('gcal_client_secret').value;
    var calendarId = document.getElementById('gcal_calendar_id').value;

    btn.disabled = true;
    btn.innerHTML = '<svg class="w-4 h-4 animate-spin" fill="none" viewBox="0 0 24 24"><circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle><path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4z"></path></svg> Kaydediliyor...';

    fetch('{{ route("admin.ayarlar.google-save") }}', {
        method: 'POST',
        headers: {
            'X-CSRF-TOKEN': csrf,
            'Accept': 'application/json',
            'Content-Type': 'application/json',
        },
        body: JSON.stringify({
            google_calendar_enabled:       enabled,
            google_calendar_client_id:     clientId,
            google_calendar_client_secret: clientSec,
            google_calendar_id:            calendarId,
        }),
    })
    .then(function(r) { return r.json(); })
    .then(function(data) {
        result.classList.remove('hidden');
        if (data.ok) {
            result.className = 'mt-3 px-3 py-2 rounded-lg text-sm font-medium bg-green-50 text-green-700 border border-green-200';
            result.textContent = '';
            var okSpan = document.createElement('span');
            okSpan.className = 'font-semibold';
            okSpan.textContent = 'Kaydedildi! ';
            result.appendChild(okSpan);
            result.appendChild(document.createTextNode(data.message || ''));
        } else {
            result.className = 'mt-3 px-3 py-2 rounded-lg text-sm font-medium bg-red-50 text-red-700 border border-red-200';
            result.textContent = '';
            var errSpan = document.createElement('span');
            errSpan.className = 'font-semibold';
            errSpan.textContent = 'Hata: ';
            result.appendChild(errSpan);
            result.appendChild(document.createTextNode(data.message || ''));
        }
        setTimeout(function() { result.classList.add('hidden'); }, 4000);
    })
    .catch(function(err) {
        result.classList.remove('hidden');
        result.className = 'mt-3 px-3 py-2 rounded-lg text-sm font-medium bg-red-50 text-red-700 border border-red-200';
        result.textContent = 'İstek gönderilemedi: ' + err.message;
    })
    .finally(function() {
        btn.disabled = false;
        btn.innerHTML = '<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"/></svg> Google Ayarlarını Kaydet';
    });
}

// =============================================
// Backup form JS
// =============================================
function backupModeChanged() {
    var mode = document.querySelector('input[name="mode"]:checked');
    if (!mode) return;

    var tablePanel = document.getElementById('tableSelectPanel');
    var fmtJson    = document.getElementById('fmt-json');
    var fmtJsonLbl = document.getElementById('fmt-json-label');

    // Tablo paneli
    if (tablePanel) tablePanel.style.display = (mode.value === 'tables') ? '' : 'none';

    // İskelet modunda JSON disable
    if (fmtJson && fmtJsonLbl) {
        if (mode.value === 'schema') {
            fmtJson.disabled = true;
            fmtJsonLbl.style.opacity = '0.4';
            fmtJsonLbl.style.pointerEvents = 'none';
            // Eğer JSON seçiliyse .sql'e geç
            if (fmtJson.checked) {
                document.getElementById('fmt-sql').checked = true;
            }
        } else {
            fmtJson.disabled = false;
            fmtJsonLbl.style.opacity = '';
            fmtJsonLbl.style.pointerEvents = '';
        }
    }
}

function toggleAllTables(masterCb) {
    document.querySelectorAll('.backup-table-cb').forEach(function(cb) {
        cb.checked = masterCb.checked;
    });
}

// =============================================
// Google test
// =============================================
function testGoogleConnection() {
    var btn    = document.getElementById('googleTestBtn');
    var result = document.getElementById('googleTestResult');

    btn.disabled = true;
    btn.innerHTML = '<svg class="w-3.5 h-3.5 animate-spin" fill="none" viewBox="0 0 24 24"><circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle><path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4z"></path></svg> Test ediliyor...';

    result.className = 'mb-4 px-3 py-2 rounded-lg text-sm font-medium bg-gray-50 text-gray-600';
    result.textContent = "Google Calendar API'ye bağlanılıyor...";
    result.classList.remove('hidden');

    fetch('{{ route("admin.ayarlar.google-test") }}', {
        method: 'POST',
        headers: {
            'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').content,
            'Accept': 'application/json',
            'Content-Type': 'application/json',
        },
    })
    .then(function(r) { return r.json(); })
    .then(function(data) {
        if (data.ok) {
            result.className = 'mb-4 px-3 py-2 rounded-lg text-sm font-medium bg-green-50 text-green-700 border border-green-200';
            result.textContent = '';
            var okSpan2 = document.createElement('span');
            okSpan2.className = 'font-semibold';
            okSpan2.textContent = 'Başarılı! ';
            result.appendChild(okSpan2);
            result.appendChild(document.createTextNode(data.message || ''));
        } else {
            result.className = 'mb-4 px-3 py-2 rounded-lg text-sm font-medium bg-red-50 text-red-700 border border-red-200';
            result.textContent = '';
            var errSpan2 = document.createElement('span');
            errSpan2.className = 'font-semibold';
            errSpan2.textContent = 'Hata: ';
            result.appendChild(errSpan2);
            result.appendChild(document.createTextNode(data.message || ''));
        }
    })
    .catch(function(err) {
        result.className = 'mb-4 px-3 py-2 rounded-lg text-sm font-medium bg-red-50 text-red-700 border border-red-200';
        result.textContent = 'İstek gönderilemedi: ' + err.message;
    })
    .finally(function() {
        btn.disabled = false;
        btn.innerHTML = '<svg class="w-3.5 h-3.5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"/></svg> Bağlantıyı Test Et';
    });
}
</script>

@endsection