@extends('layouts.admin')

@section('title', 'İlanı Düzenle')
@section('page-title', 'İlanı Düzenle')

@section('content')

<div class="max-w-3xl mt-2">

    {{-- Breadcrumb --}}
    <div class="flex items-center gap-2 mb-5 text-sm">
        <a href="{{ route('admin.ilanlar.index') }}" class="text-gray-400 hover:text-primary transition-colors 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="M15 19l-7-7 7-7"/></svg>
            İlanlara Geri Dön
        </a>
        <span class="text-gray-300">/</span>
        <a href="{{ route('admin.ilanlar.show', $job) }}" class="text-gray-400 hover:text-primary transition-colors truncate max-w-xs">{{ $job->title }}</a>
        <span class="text-gray-300">/</span>
        <span class="text-gray-600 font-medium">Düzenle</span>
    </div>

    {{-- Durum Bilgisi --}}
    <div class="flex items-center gap-3 mb-5">
        @php
            $statusBadge = match($job->status) {
                'published' => 'bg-green-100 text-green-700',
                'draft'     => 'bg-gray-100 text-gray-600',
                'closed'    => 'bg-red-100 text-red-600',
                default     => 'bg-gray-100 text-gray-500',
            };
            $statusLabel = match($job->status) {
                'published' => 'Yayında',
                'draft'     => 'Taslak',
                'closed'    => 'Kapalı',
                default     => $job->status,
            };
        @endphp
        <span class="text-xs px-2.5 py-1 rounded-full font-medium {{ $statusBadge }}">{{ $statusLabel }}</span>
        <span class="text-xs text-gray-400">Son güncelleme: {{ $job->updated_at->format('d.m.Y H:i') }}</span>
    </div>

    @if($errors->any())
    <div class="bg-red-50 border border-red-200 text-red-700 px-4 py-3 rounded-lg text-sm mb-4">
        <p class="font-medium mb-1">Lütfen aşağıdaki hataları düzeltin:</p>
        <ul class="list-disc list-inside space-y-0.5">
            @foreach($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
    @endif

    <form method="POST" action="{{ route('admin.ilanlar.update', $job) }}" enctype="multipart/form-data" class="space-y-6">
        @csrf
        @method('PUT')

        <div class="bg-white rounded-xl shadow-sm border border-gray-100 p-6 space-y-5">
            <h2 class="font-bold text-gray-800 border-b border-gray-100 pb-3">Temel Bilgiler</h2>

            <div>
                <label class="block text-sm font-medium text-gray-700 mb-1">İlan Başlığı <span class="text-red-500">*</span></label>
                <input type="text" name="title" value="{{ old('title', $job->title) }}"
                       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 @error('title') border-red-400 @enderror">
                @error('title')<p class="text-red-500 text-xs mt-1">{{ $message }}</p>@enderror
            </div>

            <div class="grid grid-cols-2 gap-4">
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">Departman <span class="text-red-500">*</span></label>
                    <input type="text" name="department" value="{{ old('department', $job->department) }}"
                           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 @error('department') border-red-400 @enderror">
                    @error('department')<p class="text-red-500 text-xs mt-1">{{ $message }}</p>@enderror
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">Lokasyon <span class="text-red-500">*</span></label>
                    <input type="text" name="location" value="{{ old('location', $job->location) }}"
                           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 @error('location') border-red-400 @enderror">
                    @error('location')<p class="text-red-500 text-xs mt-1">{{ $message }}</p>@enderror
                </div>
            </div>

            <div class="grid grid-cols-2 gap-4">
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">Çalışma Şekli <span class="text-red-500">*</span></label>
                    <select name="employment_type"
                            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 @error('employment_type') border-red-400 @enderror">
                        @foreach(['Tam Zamanlı', 'Yarı Zamanlı', 'Staj', 'Uzaktan', 'Hibrit', 'Ofis'] as $type)
                        <option value="{{ $type }}" {{ old('employment_type', $job->employment_type) == $type ? 'selected' : '' }}>{{ $type }}</option>
                        @endforeach
                    </select>
                    @error('employment_type')<p class="text-red-500 text-xs mt-1">{{ $message }}</p>@enderror
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">Çalışma Saatleri</label>
                    <input type="text" name="working_hours" value="{{ old('working_hours', $job->working_hours) }}"
                           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>
                <label class="block text-sm font-medium text-gray-700 mb-1">Maaş Aralığı</label>
                <input type="text" name="salary_range" value="{{ old('salary_range', $job->salary_range) }}"
                       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">Son Başvuru Tarihi</label>
                <input type="date" name="closes_at" value="{{ old('closes_at', $job->closes_at?->format('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="bg-white rounded-xl shadow-sm border border-gray-100 p-6 space-y-5">
            <h2 class="font-bold text-gray-800 border-b border-gray-100 pb-3">İlan İçeriği</h2>

            <div>
                <label class="block text-sm font-medium text-gray-700 mb-1">Pozisyon Açıklaması <span class="text-red-500">*</span></label>
                <textarea name="description" rows="8"
                          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 @error('description') border-red-400 @enderror">{{ old('description', $job->description) }}</textarea>
                @error('description')<p class="text-red-500 text-xs mt-1">{{ $message }}</p>@enderror
            </div>

            <div>
                <label class="block text-sm font-medium text-gray-700 mb-1">Aranan Nitelikler <span class="text-red-500">*</span></label>
                <textarea name="requirements" rows="8"
                          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 @error('requirements') border-red-400 @enderror">{{ old('requirements', $job->requirements) }}</textarea>
                @error('requirements')<p class="text-red-500 text-xs mt-1">{{ $message }}</p>@enderror
            </div>
        </div>

        {{-- Ek Belgeler & Özel Form --}}
        <div class="bg-white rounded-xl shadow-sm border border-gray-100 p-6 space-y-5">
            <h2 class="font-bold text-gray-800 border-b border-gray-100 pb-3">Başvuru & Belgeler</h2>

            <div>
                <label class="block text-sm font-medium text-gray-700 mb-1">Görüşmeye Gelirken Getirilmesi Gereken Belgeler</label>
                <p class="text-xs text-gray-400 mb-2">Görüşmeye davet edilen adayların getirmesi gereken belgeleri yazın. Her satıra bir belge yazabilirsiniz.</p>
                <textarea name="required_documents" rows="5"
                          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"
                          placeholder="Örn:&#10;Güncel CV (2 adet)&#10;Diploma veya mezuniyet belgesi fotokopisi&#10;Nüfus cüzdanı fotokopisi&#10;Referans mektubu (varsa)&#10;Sertifikalar (varsa)">{{ old('required_documents', $job->required_documents) }}</textarea>
            </div>

            <div>
                <label class="block text-sm font-medium text-gray-700 mb-1">Özel Başvuru Formu <span class="text-gray-400 font-normal">(İsteğe bağlı)</span></label>
                <p class="text-xs text-gray-400 mb-2">Kendi hazırladığınız başvuru formunu yükleyin. Yüklenmezse sistem otomatik form oluşturur. PDF, DOC veya DOCX — maks. 10MB.</p>

                @if($job->custom_application_form_path)
                <div class="flex items-center gap-3 mb-3 p-3 bg-green-50 border border-green-200 rounded-xl">
                    <svg class="w-5 h-5 text-green-600 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"/></svg>
                    <div class="flex-1 min-w-0">
                        <p class="text-sm font-medium text-green-800 truncate">{{ $job->custom_application_form_name }}</p>
                        <p class="text-xs text-green-600">Mevcut özel form</p>
                    </div>
                    <label class="flex items-center gap-1.5 text-xs text-rose-600 cursor-pointer hover:text-rose-700">
                        <input type="checkbox" name="remove_custom_form" value="1" class="rounded border-rose-300">
                        Kaldır
                    </label>
                </div>
                @endif

                <label class="flex items-center gap-3 w-full px-4 py-3 border-2 border-dashed border-gray-200 rounded-xl cursor-pointer hover:border-orange-400 hover:bg-orange-50 transition-colors group">
                    <svg class="w-5 h-5 text-gray-400 group-hover:text-orange-500 flex-shrink-0" 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-8l-4-4m0 0L8 8m4-4v12"/></svg>
                    <span class="text-sm text-gray-500 group-hover:text-orange-600" id="edit-form-label">{{ $job->custom_application_form_path ? 'Yeni dosya seçin (mevcut değiştirilir)' : 'Dosya seçin (PDF, DOC, DOCX)' }}</span>
                    <input type="file" name="custom_application_form" accept=".pdf,.doc,.docx" class="hidden"
                           onchange="document.getElementById('edit-form-label').textContent = this.files[0]?.name || '{{ $job->custom_application_form_path ? 'Yeni dosya seçin' : 'Dosya seçin (PDF, DOC, DOCX)' }}'">
                </label>
                @error('custom_application_form')<p class="text-red-500 text-xs mt-1">{{ $message }}</p>@enderror
            </div>

            {{-- Şartlar / Koşullar PDF --}}
            <div>
                <label class="block text-sm font-medium text-gray-700 mb-1">Şartlar / Koşullar PDF <span class="text-gray-400 font-normal">(İsteğe bağlı)</span></label>
                <p class="text-xs text-gray-400 mb-2">İş başvurusu şartlarını veya koşullarını içeren PDF dosyası. Maks. 10MB.</p>

                @if($job->conditions_pdf_path)
                <div class="flex items-center gap-3 mb-3 p-3 bg-orange-50 border border-orange-200 rounded-xl">
                    <svg class="w-8 h-8 text-red-500 flex-shrink-0" fill="currentColor" viewBox="0 0 24 24"><path d="M14 2H6a2 2 0 00-2 2v16a2 2 0 002 2h12a2 2 0 002-2V8l-6-6zm-1 1.5L18.5 9H13V3.5z"/></svg>
                    <div class="flex-1 min-w-0">
                        <p class="text-sm font-medium text-orange-800 truncate">{{ $job->conditions_pdf_name ?? 'sartlar.pdf' }}</p>
                        <p class="text-xs text-orange-600">Mevcut şartlar PDF</p>
                    </div>
                    <label class="flex items-center gap-1.5 text-xs text-rose-600 cursor-pointer hover:text-rose-700">
                        <input type="checkbox" name="remove_conditions_pdf" value="1" class="rounded border-rose-300">
                        Kaldır
                    </label>
                </div>
                @endif

                <label class="flex items-center gap-3 px-4 py-3 border-2 border-dashed border-gray-300 rounded-xl cursor-pointer hover:border-orange-400 hover:bg-orange-50 transition-colors group">
                    <svg class="w-5 h-5 text-gray-400 group-hover:text-orange-500 flex-shrink-0" 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-8l-4-4m0 0L8 8m4-4v12"/></svg>
                    <span class="text-sm text-gray-500 group-hover:text-orange-600" id="edit-conditions-label">{{ $job->conditions_pdf_path ? 'Yeni PDF seçin (mevcut değiştirilir)' : 'PDF dosyası seçin' }}</span>
                    <svg class="w-5 h-5 text-gray-400 group-hover:text-orange-500 flex-shrink-0 ml-auto" 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-8l-4-4m0 0L8 8m4-4v12"/></svg>
                    <input type="file" name="conditions_pdf" accept=".pdf" class="hidden"
                           onchange="document.getElementById('edit-conditions-label').textContent = this.files[0]?.name || '{{ $job->conditions_pdf_path ? 'Yeni PDF seçin' : 'PDF dosyası seçin' }}'">
                </label>
                @error('conditions_pdf')<p class="text-red-500 text-xs mt-1">{{ $message }}</p>@enderror
            </div>
        </div>

        {{-- İlana Özel Form Ayarları --}}
        <div class="bg-white rounded-xl shadow-sm border border-gray-100 p-6 space-y-4 mb-6">
            <div class="flex items-center justify-between border-b border-gray-100 pb-3">
                <div>
                    <h2 class="font-bold text-gray-800">İlana Özel Başvuru Formu Ayarları</h2>
                    <p class="text-xs text-gray-400 mt-0.5">Bu ilan için global form ayarlarını geçersiz kılmak istiyorsanız etkinleştirin.</p>
                </div>
                <label class="relative inline-flex items-center cursor-pointer">
                    <input type="checkbox" name="use_custom_form_settings" id="use_custom_form_settings_edit" value="1"
                           {{ old('use_custom_form_settings', $job->use_custom_form_settings) ? 'checked' : '' }}
                           onchange="document.getElementById('job-form-settings-panel-edit').style.display = this.checked ? '' : 'none'"
                           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>

            @php
                $jfs = $job->job_form_settings ?? [];
            @endphp
            <div id="job-form-settings-panel-edit" style="{{ old('use_custom_form_settings', $job->use_custom_form_settings) ? '' : 'display:none' }}" class="space-y-4">
                <div class="grid grid-cols-2 gap-4">
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1">Şirket Adı</label>
                        <input type="text" name="jfs_company_name"
                               value="{{ old('jfs_company_name', $jfs['company_name'] ?? '') }}"
                               placeholder="Markamama"
                               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">Form Başlığı</label>
                        <input type="text" name="jfs_form_title"
                               value="{{ old('jfs_form_title', $jfs['form_title'] ?? '') }}"
                               placeholder="Başvuru Formu"
                               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>
                    <label class="block text-sm font-medium text-gray-700 mb-1">Form Şablonu</label>
                    <select name="jfs_form_template" 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">
                        @php $currentTpl = old('jfs_form_template', $jfs['form_template'] ?? 'classic'); @endphp
                        <option value="classic" {{ $currentTpl === 'classic' ? 'selected' : '' }}>Klasik</option>
                        <option value="modern"  {{ $currentTpl === 'modern'  ? 'selected' : '' }}>Modern</option>
                        <option value="minimal" {{ $currentTpl === 'minimal' ? 'selected' : '' }}>Minimal</option>
                    </select>
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-2">Gösterilecek Alanlar</label>
                    <div class="grid grid-cols-2 gap-2">
                        <label class="flex items-center gap-2 text-sm text-gray-700 cursor-pointer">
                            <input type="checkbox" name="jfs_show_phone" value="1"
                                   {{ old('jfs_show_phone', $jfs['show_phone'] ?? true) ? 'checked' : '' }}
                                   class="rounded border-gray-300 text-orange-500">
                            Telefon
                        </label>
                        <label class="flex items-center gap-2 text-sm text-gray-700 cursor-pointer">
                            <input type="checkbox" name="jfs_show_linkedin" value="1"
                                   {{ old('jfs_show_linkedin', $jfs['show_linkedin'] ?? true) ? 'checked' : '' }}
                                   class="rounded border-gray-300 text-orange-500">
                            LinkedIn
                        </label>
                        <label class="flex items-center gap-2 text-sm text-gray-700 cursor-pointer">
                            <input type="checkbox" name="jfs_show_cover_letter" value="1"
                                   {{ old('jfs_show_cover_letter', $jfs['show_cover_letter'] ?? true) ? 'checked' : '' }}
                                   class="rounded border-gray-300 text-orange-500">
                            Ön Yazı
                        </label>
                        <label class="flex items-center gap-2 text-sm text-gray-700 cursor-pointer">
                            <input type="checkbox" name="jfs_show_source" value="1"
                                   {{ old('jfs_show_source', $jfs['show_source'] ?? true) ? 'checked' : '' }}
                                   class="rounded border-gray-300 text-orange-500">
                            Bizi Nereden Duydunuz
                        </label>
                    </div>
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">Alt Bilgi Metni</label>
                    <input type="text" name="jfs_footer_text"
                           value="{{ old('jfs_footer_text', $jfs['footer_text'] ?? '') }}"
                           placeholder="Başvurunuz için teşekkür ederiz."
                           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>

        <div class="flex items-center gap-3 flex-wrap">
            <button type="submit" name="action" value="save"
                    class="bg-primary text-white px-6 py-2.5 rounded-lg text-sm font-medium hover:bg-orange-600 transition-colors">
                Değişiklikleri Kaydet
            </button>
            @can('publish job postings')
            @if($job->status === 'published')
            <button type="submit" name="action" value="draft"
                    class="bg-white border border-gray-300 text-gray-700 px-5 py-2.5 rounded-lg text-sm font-medium hover:bg-gray-50 transition-colors">
                Taslağa Al
            </button>
            <button type="submit" name="action" value="close"
                    class="bg-white border border-red-300 text-red-600 px-5 py-2.5 rounded-lg text-sm font-medium hover:bg-red-50 transition-colors">
                Kapat
            </button>
            @elseif($job->status === 'draft')
            <button type="submit" name="action" value="publish"
                    class="bg-green-600 text-white px-5 py-2.5 rounded-lg text-sm font-medium hover:bg-green-700 transition-colors">
                Yayınla
            </button>
            @elseif($job->status === 'closed')
            <button type="submit" name="action" value="publish"
                    class="bg-green-600 text-white px-5 py-2.5 rounded-lg text-sm font-medium hover:bg-green-700 transition-colors">
                Yeniden Yayınla
            </button>
            <button type="submit" name="action" value="draft"
                    class="bg-white border border-gray-300 text-gray-700 px-5 py-2.5 rounded-lg text-sm font-medium hover:bg-gray-50 transition-colors">
                Taslağa Al
            </button>
            @endif
            @endcan
            <a href="{{ route('admin.ilanlar.show', $job) }}" class="text-gray-500 hover:text-gray-700 text-sm ml-auto">İptal</a>
        </div>
    </form>
</div>

@endsection