<x-admin-layout>
    <x-slot name="header">
        <div class="flex justify-between items-center">
            <div>
                <h2 class="font-semibold text-lg text-gray-800 dark:text-gray-200 leading-tight">
                    <i class="fas fa-plus mr-2"></i>Adaugă Pagină Nouă
                </h2>
                <p class="mt-1 text-sm text-gray-600 dark:text-gray-400">
                    <a href="{{ route('dashboard') }}" class="text-blue-600 dark:text-blue-400 hover:underline">Dashboard</a>
                    <span class="mx-2">/</span>
                    <a href="{{ route('admin.dashboard') }}" class="text-blue-600 dark:text-blue-400 hover:underline">Admin</a>
                    <span class="mx-2">/</span>
                    <a href="{{ route('admin.landing-pages.index') }}" class="text-blue-600 dark:text-blue-400 hover:underline">Pagini Landing</a>
                    <span class="mx-2">/</span>
                    <span>Adaugă</span>
                </p>
            </div>
        </div>
    </x-slot>

    <div class="py-6">
        <div class="max-w-4xl mx-auto px-4 sm:px-6 lg:px-8">
            <form action="{{ route('admin.landing-pages.store') }}" method="POST" class="space-y-6">
                @csrf

                {{-- Info principale --}}
                <div class="bg-white dark:bg-gray-800 rounded-xl shadow-sm border border-gray-200 dark:border-gray-700 p-6">
                    <h3 class="text-lg font-medium text-gray-900 dark:text-white mb-4">
                        <i class="fas fa-info-circle mr-2 text-indigo-500"></i>Informații Pagină
                    </h3>

                    <div class="space-y-4">
                        {{-- Titlu --}}
                        <div>
                            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
                                Titlu <span class="text-red-500">*</span>
                            </label>
                            <input type="text" name="title" id="title" value="{{ old('title') }}" required
                                   class="w-full rounded-lg border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-white focus:ring-indigo-500 focus:border-indigo-500"
                                   placeholder="Ex: DNS Standard">
                            @error('title')
                                <p class="mt-1 text-sm text-red-600 dark:text-red-400">{{ $message }}</p>
                            @enderror
                        </div>

                        {{-- Slug --}}
                        <div>
                            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
                                Slug (URL)
                                <button type="button" id="toggleSlugEdit" class="ml-2 text-xs text-indigo-600 dark:text-indigo-400 hover:underline">
                                    <i class="fas fa-lock" id="slugLockIcon"></i> Editează
                                </button>
                            </label>
                            <div class="flex items-center gap-2">
                                <span class="text-gray-500 dark:text-gray-400">/pagina/</span>
                                <input type="text" name="slug" id="slug" value="{{ old('slug') }}" readonly
                                       class="flex-1 rounded-lg border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-white focus:ring-indigo-500 focus:border-indigo-500 bg-gray-100 dark:bg-gray-600"
                                       placeholder="dns-standard">
                                <button type="button" id="regenerateSlug" class="px-3 py-2 bg-gray-200 dark:bg-gray-600 rounded-lg hover:bg-gray-300 dark:hover:bg-gray-500 transition" title="Regenerează">
                                    <i class="fas fa-sync-alt text-gray-600 dark:text-gray-300"></i>
                                </button>
                            </div>
                            <p class="mt-1 text-xs text-gray-500 dark:text-gray-400">
                                Se generează automat din titlu. Click pe "Editează" pentru modificare manuală.
                            </p>
                            @error('slug')
                                <p class="mt-1 text-sm text-red-600 dark:text-red-400">{{ $message }}</p>
                            @enderror
                        </div>

                        {{-- Template --}}
                        <div>
                            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
                                Template <span class="text-red-500">*</span>
                            </label>
                            <select name="template" required
                                    class="w-full rounded-lg border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-white focus:ring-indigo-500 focus:border-indigo-500">
                                @foreach($templates as $key => $label)
                                    <option value="{{ $key }}" {{ old('template', 'default') == $key ? 'selected' : '' }}>{{ $label }}</option>
                                @endforeach
                            </select>
                        </div>
                    </div>
                </div>

                {{-- Conținut --}}
                <div class="bg-white dark:bg-gray-800 rounded-xl shadow-sm border border-gray-200 dark:border-gray-700 p-6">
                    <h3 class="text-lg font-medium text-gray-900 dark:text-white mb-4">
                        <i class="fas fa-align-left mr-2 text-indigo-500"></i>Conținut Pagină
                    </h3>

                    <div>
                        <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">Conținut (HTML)</label>
                        <textarea name="content" id="content" rows="15"
                                  class="w-full rounded-lg border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-white focus:ring-indigo-500 focus:border-indigo-500 font-mono text-sm"
                                  placeholder="<h1>Titlu</h1><p>Conținut pagină...</p>">{{ old('content') }}</textarea>
                        <p class="mt-1 text-xs text-gray-500 dark:text-gray-400">
                            Poți folosi HTML pentru formatare. În viitor va fi disponibil un editor vizual.
                        </p>
                    </div>
                </div>

                {{-- SEO --}}
                <div class="bg-white dark:bg-gray-800 rounded-xl shadow-sm border border-gray-200 dark:border-gray-700 p-6">
                    <h3 class="text-lg font-medium text-gray-900 dark:text-white mb-4">
                        <i class="fas fa-search mr-2 text-indigo-500"></i>SEO (Opțional)
                    </h3>

                    <div class="space-y-4">
                        <div>
                            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">Meta Title</label>
                            <input type="text" name="meta_title" value="{{ old('meta_title') }}"
                                   class="w-full rounded-lg border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-white focus:ring-indigo-500 focus:border-indigo-500"
                                   placeholder="Titlu pentru motoarele de căutare" maxlength="255">
                            <p class="mt-1 text-xs text-gray-500 dark:text-gray-400">Lasă gol pentru a folosi titlul paginii.</p>
                        </div>

                        <div>
                            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">Meta Description</label>
                            <textarea name="meta_description" rows="2"
                                      class="w-full rounded-lg border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-white focus:ring-indigo-500 focus:border-indigo-500"
                                      placeholder="Descriere scurtă pentru motoarele de căutare" maxlength="500">{{ old('meta_description') }}</textarea>
                        </div>

                        <div>
                            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">Meta Keywords</label>
                            <input type="text" name="meta_keywords" value="{{ old('meta_keywords') }}"
                                   class="w-full rounded-lg border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-white focus:ring-indigo-500 focus:border-indigo-500"
                                   placeholder="cuvânt1, cuvânt2, cuvânt3" maxlength="255">
                        </div>
                    </div>
                </div>

                {{-- Publicare --}}
                <div class="bg-white dark:bg-gray-800 rounded-xl shadow-sm border border-gray-200 dark:border-gray-700 p-6">
                    <h3 class="text-lg font-medium text-gray-900 dark:text-white mb-4">
                        <i class="fas fa-cog mr-2 text-indigo-500"></i>Setări Publicare
                    </h3>

                    <div class="flex items-center">
                        <label class="relative inline-flex items-center cursor-pointer">
                            <input type="checkbox" name="is_published" value="1" {{ old('is_published') ? 'checked' : '' }} class="sr-only peer">
                            <div class="w-11 h-6 bg-gray-200 peer-focus:outline-none peer-focus:ring-4 peer-focus:ring-indigo-300 dark:peer-focus:ring-indigo-800 rounded-full peer dark:bg-gray-700 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 dark:border-gray-600 peer-checked:bg-indigo-600"></div>
                            <span class="ml-3 text-sm font-medium text-gray-700 dark:text-gray-300">Publică imediat</span>
                        </label>
                    </div>
                    <p class="mt-2 text-xs text-gray-500 dark:text-gray-400">
                        Dacă nu bifezi, pagina va fi salvată ca draft și nu va fi vizibilă public.
                    </p>
                </div>

                {{-- Butoane --}}
                <div class="flex items-center justify-end gap-4">
                    <a href="{{ route('admin.landing-pages.index') }}" class="px-6 py-2 border border-gray-300 dark:border-gray-600 rounded-lg text-gray-700 dark:text-gray-300 hover:bg-gray-100 dark:hover:bg-gray-700 transition">
                        Anulează
                    </a>
                    <button type="submit" class="px-6 py-2 bg-indigo-600 hover:bg-indigo-700 text-white font-bold rounded-lg transition">
                        <i class="fas fa-save mr-2"></i>Salvează Pagina
                    </button>
                </div>
            </form>
        </div>
    </div>

    @push('scripts')
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const titleInput = document.getElementById('title');
            const slugInput = document.getElementById('slug');
            const toggleBtn = document.getElementById('toggleSlugEdit');
            const lockIcon = document.getElementById('slugLockIcon');
            const regenerateBtn = document.getElementById('regenerateSlug');
            let slugLocked = true;
            let slugManuallyEdited = false;

            // Generează slug din titlu
            function generateSlug(title) {
                return title.toLowerCase()
                    .normalize('NFD')
                    .replace(/[\u0300-\u036f]/g, '') // remove diacritics
                    .replace(/[^a-z0-9\s-]/g, '')
                    .replace(/\s+/g, '-')
                    .replace(/-+/g, '-')
                    .replace(/^-|-$/g, '');
            }

            // Auto-generate slug când se schimbă titlul
            titleInput.addEventListener('input', function() {
                if (slugLocked && !slugManuallyEdited) {
                    slugInput.value = generateSlug(this.value);
                }
            });

            // Toggle slug edit
            toggleBtn.addEventListener('click', function() {
                slugLocked = !slugLocked;
                slugInput.readOnly = slugLocked;
                slugInput.classList.toggle('bg-gray-100', slugLocked);
                slugInput.classList.toggle('dark:bg-gray-600', slugLocked);
                lockIcon.classList.toggle('fa-lock', slugLocked);
                lockIcon.classList.toggle('fa-unlock', !slugLocked);

                if (!slugLocked) {
                    slugManuallyEdited = true;
                    slugInput.focus();
                }
            });

            // Regenerate slug
            regenerateBtn.addEventListener('click', function() {
                slugInput.value = generateSlug(titleInput.value);
                slugManuallyEdited = false;
            });

            // Detectează editare manuală
            slugInput.addEventListener('input', function() {
                if (!slugLocked) {
                    slugManuallyEdited = true;
                }
            });
        });
    </script>
    @endpush
</x-admin-layout>
