<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ă Meniu: {{ $menu->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-menus.index') }}" class="text-blue-600 dark:text-blue-400 hover:underline">Meniu Landing</a>
                    <span class="mx-2">/</span>
                    <span>Editează</span>
                </p>
            </div>
        </div>
    </x-slot>

    <div class="py-6">
        <div class="max-w-3xl mx-auto px-4 sm:px-6 lg:px-8">
            <form action="{{ route('admin.landing-menus.update', $menu) }}" 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 Meniu
                    </h3>

                    <div class="space-y-4">
                        <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" value="{{ old('title', $menu->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>

                        <div>
                            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
                                Tip Meniu <span class="text-red-500">*</span>
                            </label>
                            <select name="menu_type" id="menu_type" 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($menuTypes as $key => $label)
                                    <option value="{{ $key }}" {{ old('menu_type', $menu->menu_type) == $key ? 'selected' : '' }}>{{ $label }}</option>
                                @endforeach
                            </select>
                        </div>

                        <div>
                            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
                                Meniu Părinte
                            </label>
                            <select name="parent_id"
                                    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">
                                <option value="">-- Meniu Principal --</option>
                                @foreach($parentMenus as $parent)
                                    <option value="{{ $parent->id }}" {{ old('parent_id', $menu->parent_id) == $parent->id ? 'selected' : '' }}>
                                        {{ $parent->title }}
                                    </option>
                                @endforeach
                            </select>
                        </div>

                        <div id="page_field" class="{{ $menu->menu_type != 'page' ? 'hidden' : '' }}">
                            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
                                Selectează Pagina
                            </label>
                            <select name="page_id"
                                    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">
                                <option value="">-- Selectează --</option>
                                @foreach($pages as $page)
                                    <option value="{{ $page->id }}" {{ old('page_id', $menu->page_id) == $page->id ? 'selected' : '' }}>
                                        {{ $page->title }} (/pagina/{{ $page->slug }})
                                    </option>
                                @endforeach
                            </select>
                        </div>

                        <div id="external_url_field" class="{{ $menu->menu_type != 'external' ? 'hidden' : '' }}">
                            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
                                URL Extern
                            </label>
                            <input type="url" name="external_url" value="{{ old('external_url', $menu->external_url) }}"
                                   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="https://example.com">
                        </div>

                        <div id="anchor_field" class="{{ $menu->menu_type != 'anchor' ? 'hidden' : '' }}">
                            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
                                Anchor
                            </label>
                            <div class="flex items-center">
                                <span class="px-3 py-2 bg-gray-100 dark:bg-gray-600 border border-r-0 border-gray-300 dark:border-gray-600 rounded-l-lg text-gray-500 dark:text-gray-400">#</span>
                                <input type="text" name="anchor" value="{{ old('anchor', ltrim($menu->anchor, '#')) }}"
                                       class="flex-1 rounded-r-lg rounded-l-none border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-white focus:ring-indigo-500 focus:border-indigo-500">
                            </div>
                        </div>

                        <div>
                            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
                                Deschide în
                            </label>
                            <select name="target"
                                    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">
                                <option value="_self" {{ old('target', $menu->target) == '_self' ? 'selected' : '' }}>Aceeași fereastră</option>
                                <option value="_blank" {{ old('target', $menu->target) == '_blank' ? 'selected' : '' }}>Fereastră nouă</option>
                            </select>
                        </div>
                    </div>
                </div>

                {{-- Aspect --}}
                <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-palette mr-2 text-indigo-500"></i>Aspect
                    </h3>

                    <div class="space-y-4">
                        <div>
                            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
                                Icon
                            </label>
                            <select name="icon" id="icon_select"
                                    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">
                                <option value="">-- Fără icon --</option>
                                @foreach($icons as $class => $label)
                                    <option value="{{ $class }}" {{ old('icon', $menu->icon) == $class ? 'selected' : '' }}>{{ $label }}</option>
                                @endforeach
                            </select>
                            <div class="mt-2 flex items-center gap-2">
                                <span class="text-sm text-gray-500">Preview:</span>
                                <span id="icon_preview" class="w-8 h-8 bg-gray-100 dark:bg-gray-700 rounded flex items-center justify-center">
                                    <i class="{{ old('icon', $menu->icon) ?: 'fas fa-link' }} {{ $menu->icon_color ?: 'text-gray-500' }}"></i>
                                </span>
                            </div>
                        </div>

                        <div>
                            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
                                Culoare Icon
                            </label>
                            <div class="flex flex-wrap gap-2">
                                @foreach($iconColors as $class => $label)
                                    <label class="cursor-pointer">
                                        <input type="radio" name="icon_color" value="{{ $class }}" class="sr-only peer" {{ old('icon_color', $menu->icon_color) == $class ? 'checked' : '' }}>
                                        <span class="block w-8 h-8 rounded-lg border-2 border-transparent peer-checked:border-indigo-500 peer-checked:ring-2 peer-checked:ring-indigo-200 transition {{ str_replace('text-', 'bg-', $class) }}"
                                              title="{{ $label }}"></span>
                                    </label>
                                @endforeach
                            </div>
                        </div>

                        <div class="grid grid-cols-2 gap-4">
                            <div>
                                <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
                                    Badge
                                </label>
                                <input type="text" name="badge" value="{{ old('badge', $menu->badge) }}"
                                       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: Nou">
                            </div>
                            <div>
                                <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
                                    Culoare Badge
                                </label>
                                <select name="badge_color"
                                        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($badgeColors as $key => $label)
                                        <option value="{{ $key }}" {{ old('badge_color', $menu->badge_color) == $key ? 'selected' : '' }}>{{ $label }}</option>
                                    @endforeach
                                </select>
                            </div>
                        </div>
                    </div>
                </div>

                {{-- Setări --}}
                <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
                    </h3>

                    <div class="space-y-4">
                        <div class="flex items-center">
                            <label class="relative inline-flex items-center cursor-pointer">
                                <input type="checkbox" name="is_active" value="1" {{ old('is_active', $menu->is_active) ? '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 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 peer-checked:bg-indigo-600"></div>
                                <span class="ml-3 text-sm font-medium text-gray-700 dark:text-gray-300">Activ</span>
                            </label>
                        </div>

                        <div class="flex items-center">
                            <label class="relative inline-flex items-center cursor-pointer">
                                <input type="checkbox" name="show_in_mobile" value="1" {{ old('show_in_mobile', $menu->show_in_mobile) ? '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 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 peer-checked:bg-indigo-600"></div>
                                <span class="ml-3 text-sm font-medium text-gray-700 dark:text-gray-300">Afișează pe mobil</span>
                            </label>
                        </div>

                        <div>
                            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
                                Ordine
                            </label>
                            <input type="number" name="sort_order" value="{{ old('sort_order', $menu->sort_order) }}"
                                   class="w-24 rounded-lg border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-white focus:ring-indigo-500 focus:border-indigo-500">
                        </div>
                    </div>
                </div>

                {{-- Butoane --}}
                <div class="flex items-center justify-end gap-4">
                    <a href="{{ route('admin.landing-menus.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>
            </form>
        </div>
    </div>

    @push('scripts')
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const menuType = document.getElementById('menu_type');
            const pageField = document.getElementById('page_field');
            const externalField = document.getElementById('external_url_field');
            const anchorField = document.getElementById('anchor_field');

            function toggleFields() {
                pageField.classList.add('hidden');
                externalField.classList.add('hidden');
                anchorField.classList.add('hidden');

                switch(menuType.value) {
                    case 'page':
                        pageField.classList.remove('hidden');
                        break;
                    case 'external':
                        externalField.classList.remove('hidden');
                        break;
                    case 'anchor':
                        anchorField.classList.remove('hidden');
                        break;
                }
            }

            menuType.addEventListener('change', toggleFields);
        });
    </script>
    @endpush
</x-admin-layout>
