<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-edit mr-2"></i>Editează Pagina: {{ $page->title }}
                </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>Editează</span>
                </p>
            </div>
            <div class="flex items-center gap-3">
                @if($page->is_published)
                    <a href="{{ url('/pagina/' . $page->slug) }}" target="_blank" class="bg-blue-600 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded-lg transition">
                        <i class="fas fa-external-link-alt mr-2"></i>Vezi Pagina
                    </a>
                @endif
            </div>
        </div>
    </x-slot>

    <div class="py-6">
        <div class="max-w-4xl mx-auto px-4 sm:px-6 lg:px-8">
            @if(session('success'))
                <div class="mb-4 p-4 bg-green-100 dark:bg-green-900/50 border border-green-400 dark:border-green-600 text-green-700 dark:text-green-300 rounded-lg">
                    <i class="fas fa-check-circle mr-2"></i>{{ session('success') }}
                </div>
            @endif

            <form action="{{ route('admin.landing-pages.update', $page) }}" method="POST" class="space-y-6">
                @csrf
                @method('PUT')

                {{-- 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', $page->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">
                            @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', $page->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">
                                <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>
                            @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', $page->template) == $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>
                        <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">{{ old('content', $page->content) }}</textarea>
                    </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', $page->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" maxlength="255">
                        </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" maxlength="500">{{ old('meta_description', $page->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', $page->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" 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 justify-between">
                        <label class="relative inline-flex items-center cursor-pointer">
                            <input type="checkbox" name="is_published" value="1" {{ old('is_published', $page->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">Pagină publicată</span>
                        </label>

                        @if($page->published_at)
                            <span class="text-sm text-gray-500 dark:text-gray-400">
                                <i class="fas fa-clock mr-1"></i>Publicat: {{ $page->published_at->format('d.m.Y H:i') }}
                            </span>
                        @endif
                    </div>
                </div>

                {{-- Butoane --}}
                <div class="flex items-center justify-between">
                    <div class="text-sm text-gray-500 dark:text-gray-400">
                        <i class="fas fa-info-circle mr-1"></i>
                        Ultima modificare: {{ $page->updated_at->format('d.m.Y H:i') }}
                    </div>
                    <div class="flex items-center 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ă Modificările
                        </button>
                    </div>
                </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;

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

            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) {
                    slugInput.focus();
                }
            });

            regenerateBtn.addEventListener('click', function() {
                if (confirm('Sigur vrei să regenerezi slug-ul? Link-urile vechi nu vor mai funcționa.')) {
                    slugInput.value = generateSlug(titleInput.value);
                }
            });
        });
    </script>
    @endpush
</x-admin-layout>
