<div class="p-6 space-y-6">
    <div class="flex flex-wrap items-center justify-between gap-4">
        <div>
            <h1 class="text-2xl font-bold">{{ __('قوائم التنقل') }}</h1>
            <p class="text-sm text-gray-500 mt-1">{{ __('تحكّم كامل في روابط الشريط العلوي والتذييل، بما في ذلك القوائم المنسدلة') }}</p>
        </div>
        @can('settings.manage')
            <button type="button" wire:click="addItem"
                    class="inline-flex items-center gap-2 rounded-lg bg-primary-600 px-4 py-2.5 text-white font-medium">
                <x-ui.icon name="plus" class="w-5 h-5" /> {{ __('عنصر جديد') }}
            </button>
        @endcan
    </div>

    @if (session('success'))
        <div class="rounded-lg bg-green-50 border border-green-200 text-green-800 px-4 py-3">{{ session('success') }}</div>
    @endif

    <div class="flex flex-wrap gap-2" role="tablist">
        @foreach ($menuKeys as $key)
            <button type="button" wire:click="$set('menuKey', '{{ $key }}')" role="tab"
                    aria-selected="{{ $menuKey === $key ? 'true' : 'false' }}"
                    class="rounded-lg px-4 py-2 text-sm font-medium {{ $menuKey === $key ? 'bg-primary-600 text-white' : 'border hover:bg-gray-50' }}">
                {{ \App\Domain\Website\Models\WebsiteMenu::labelFor($key) }}
            </button>
        @endforeach
    </div>

    @if ($showForm)
        <div class="rounded-xl border bg-white p-6 space-y-4">
            <h2 class="font-semibold">{{ $editingId ? __('تعديل العنصر') : __('عنصر جديد') }}</h2>

            <div class="grid md:grid-cols-2 gap-4">
                <div>
                    <label for="mi-label" class="block text-sm font-medium mb-1.5">{{ __('النص (عربي)') }} *</label>
                    <input id="mi-label" type="text" wire:model="label" class="w-full rounded-lg border px-3 py-2">
                    @error('label') <p class="text-sm text-red-600 mt-1">{{ $message }}</p> @enderror
                </div>
                <div>
                    <label for="mi-label-en" class="block text-sm font-medium mb-1.5">{{ __('النص (إنجليزي)') }}</label>
                    <input id="mi-label-en" type="text" wire:model="label_en" dir="ltr" class="w-full rounded-lg border px-3 py-2">
                </div>
                <div>
                    <label for="mi-type" class="block text-sm font-medium mb-1.5">{{ __('نوع الرابط') }}</label>
                    <select id="mi-type" wire:model.live="link_type" class="w-full rounded-lg border px-3 py-2">
                        <option value="page">{{ __('صفحة من الموقع') }}</option>
                        <option value="url">{{ __('رابط خارجي') }}</option>
                        <option value="anchor">{{ __('قسم داخل الصفحة') }}</option>
                        <option value="none">{{ __('قائمة منسدلة (بدون رابط)') }}</option>
                    </select>
                    @error('link_type') <p class="text-sm text-red-600 mt-1">{{ $message }}</p> @enderror
                </div>

                @if ($link_type === 'page')
                    <div>
                        <label for="mi-page" class="block text-sm font-medium mb-1.5">{{ __('الصفحة') }}</label>
                        <select id="mi-page" wire:model="website_page_id" class="w-full rounded-lg border px-3 py-2">
                            <option value="">{{ __('— اختر —') }}</option>
                            @foreach ($pages as $p)
                                <option value="{{ $p->id }}">{{ $p->title ?: $p->slug }}</option>
                            @endforeach
                        </select>
                        @error('website_page_id') <p class="text-sm text-red-600 mt-1">{{ $message }}</p> @enderror
                    </div>
                @elseif ($link_type === 'url')
                    <div>
                        <label for="mi-url" class="block text-sm font-medium mb-1.5">{{ __('الرابط') }}</label>
                        <input id="mi-url" type="url" wire:model="url" dir="ltr" placeholder="https://..." class="w-full rounded-lg border px-3 py-2">
                        @error('url') <p class="text-sm text-red-600 mt-1">{{ $message }}</p> @enderror
                    </div>
                @elseif ($link_type === 'anchor')
                    <div>
                        <label for="mi-anchor" class="block text-sm font-medium mb-1.5">{{ __('معرّف القسم') }}</label>
                        <input id="mi-anchor" type="text" wire:model="anchor" dir="ltr" placeholder="about" class="w-full rounded-lg border px-3 py-2 font-mono text-sm">
                    </div>
                @endif

                <div>
                    <label for="mi-icon" class="block text-sm font-medium mb-1.5">{{ __('أيقونة') }}</label>
                    <input id="mi-icon" type="text" wire:model="icon" dir="ltr" list="ec-icon-names" class="w-full rounded-lg border px-3 py-2 font-mono text-sm">
                </div>
            </div>

            <div class="flex flex-wrap gap-6">
                <label class="inline-flex items-center gap-2"><input type="checkbox" wire:model="is_visible" class="rounded"> <span>{{ __('ظاهر') }}</span></label>
                <label class="inline-flex items-center gap-2"><input type="checkbox" wire:model="highlight" class="rounded"> <span>{{ __('إبراز كزر') }}</span></label>
                <label class="inline-flex items-center gap-2"><input type="checkbox" wire:model="open_in_new_tab" class="rounded"> <span>{{ __('فتح في تبويب جديد') }}</span></label>
            </div>

            <div class="flex gap-3">
                <button type="button" wire:click="save" wire:loading.attr="disabled" wire:target="save" class="rounded-lg bg-primary-600 px-5 py-2.5 text-white font-medium">
                    <span wire:loading.remove wire:target="save">{{ __('حفظ') }}</span>
                    <span wire:loading wire:target="save">{{ __('جارٍ الحفظ...') }}</span>
                </button>
                <button type="button" wire:click="$set('showForm', false)" class="rounded-lg border px-5 py-2.5">{{ __('إلغاء') }}</button>
            </div>
        </div>
    @endif

    <div class="rounded-xl border bg-white p-4" wire:loading.class="opacity-50">
        @if ($items->isEmpty())
            <p class="text-center text-gray-500 py-10">{{ __('لا توجد عناصر في هذه القائمة بعد') }}</p>
        @else
            <ul class="space-y-1">
                @foreach ($items as $item)
                    @include('website.builder.menu-row', ['item' => $item, 'depth' => 0])
                @endforeach
            </ul>
        @endif
    </div>

    <datalist id="ec-icon-names">
        @foreach (['home','users','calendar','trophy','phone','envelope','map-pin','academic-cap','building-office','star'] as $n)
            <option value="{{ $n }}"></option>
        @endforeach
    </datalist>
</div>
