@extends('admin.layouts.app')
@section('title', 'Edit Slider: ' . $slider->name)
@section('page-title', 'Edit Slider: ' . $slider->name)

@section('content')
<div x-data="sliderEditor({{ json_encode($slider->slides->toArray()) }})">
    <form method="POST" action="{{ route('admin.sliders.update', $slider->id) }}"
          x-ref="sliderForm" x-on:submit.prevent="save($event)">
        @csrf
        <input type="hidden" name="_method" value="PUT">

        <div class="flex flex-col md:flex-row gap-6">
            {{-- Settings panel --}}
            <div class="w-full md:w-64 md:flex-shrink-0 space-y-4">
                <div class="bg-white dark:bg-gray-800 rounded-xl shadow-sm p-4 space-y-3">
                    <h3 class="font-semibold text-sm text-gray-900 dark:text-white">Settings</h3>
                    <div>
                        <label class="block text-xs font-medium text-gray-600 dark:text-gray-400 mb-1">Name</label>
                        <input type="text" name="name" value="{{ $slider->name }}" required
                               class="w-full border border-gray-300 dark:border-gray-600 rounded-lg px-3 py-2 text-sm bg-white dark:bg-gray-700 text-gray-900 dark:text-white">
                    </div>
                    <div>
                        <label class="block text-xs font-medium text-gray-600 dark:text-gray-400 mb-1">Slug</label>
                        <input type="text" name="slug" value="{{ $slider->slug }}"
                               class="w-full border border-gray-300 dark:border-gray-600 rounded-lg px-3 py-2 text-sm bg-white dark:bg-gray-700 text-gray-900 dark:text-white font-mono text-xs">
                    </div>
                    <div>
                        <label class="block text-xs font-medium text-gray-600 dark:text-gray-400 mb-1">Autoplay</label>
                        <div class="flex gap-1 p-1 bg-gray-100 dark:bg-gray-700 rounded-lg">
                            <button type="button" x-on:click="autoplay = true"
                                    :class="autoplay ? 'bg-white dark:bg-gray-600 text-gray-900 dark:text-white shadow-sm' : 'text-gray-500 hover:text-gray-700'"
                                    class="flex-1 py-1 rounded-md text-xs font-medium transition-all">On</button>
                            <button type="button" x-on:click="autoplay = false"
                                    :class="!autoplay ? 'bg-white dark:bg-gray-600 text-gray-900 dark:text-white shadow-sm' : 'text-gray-500 hover:text-gray-700'"
                                    class="flex-1 py-1 rounded-md text-xs font-medium transition-all">Off</button>
                        </div>
                        <input type="hidden" name="autoplay" :value="autoplay ? '1' : '0'">
                    </div>
                    <div x-show="autoplay">
                        <label class="block text-xs font-medium text-gray-600 dark:text-gray-400 mb-1">Interval (ms)</label>
                        <input type="number" name="interval" x-model="interval" min="1000" max="30000" step="500"
                               class="w-full border border-gray-300 dark:border-gray-600 rounded-lg px-3 py-2 text-sm bg-white dark:bg-gray-700 text-gray-900 dark:text-white">
                    </div>
                </div>

                {{-- Add Slide --}}
                <div class="bg-white dark:bg-gray-800 rounded-xl shadow-sm p-4 space-y-2">
                    <h3 class="font-semibold text-sm text-gray-900 dark:text-white mb-2">Add Slide</h3>
                    <input type="text" x-model="newSlide.title" placeholder="Title"
                           class="w-full border border-gray-300 dark:border-gray-600 rounded-lg px-3 py-2 text-sm bg-white dark:bg-gray-700 text-gray-900 dark:text-white">
                    <input type="text" x-model="newSlide.subtitle" placeholder="Subtitle"
                           class="w-full border border-gray-300 dark:border-gray-600 rounded-lg px-3 py-2 text-sm bg-white dark:bg-gray-700 text-gray-900 dark:text-white">

                    {{-- Image picker for new slide --}}
                    <div>
                        <div class="flex gap-1">
                            <input type="text" x-model="newSlide.image_url" placeholder="Image URL"
                                   class="flex-1 min-w-0 border border-gray-300 dark:border-gray-600 rounded-l-lg px-3 py-2 text-sm bg-white dark:bg-gray-700 text-gray-900 dark:text-white font-mono text-xs">
                            <button type="button" x-on:click="openPicker(url => newSlide.image_url = url)"
                                    class="px-2 py-2 bg-gray-100 dark:bg-gray-700 border border-gray-300 dark:border-gray-600 border-l-0 rounded-r-lg text-gray-500 hover:text-indigo-600 hover:bg-indigo-50 dark:hover:bg-indigo-900/20 transition-colors" title="Pick from Media">
                                <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="M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 14m-6-6h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z"/></svg>
                            </button>
                        </div>
                        <div x-show="newSlide.image_url" class="mt-1 rounded overflow-hidden h-16 bg-gray-100 dark:bg-gray-700">
                            <img :src="newSlide.image_url" class="w-full h-full object-cover" x-on:error="newSlide.image_url = ''">
                        </div>
                        <input type="text" x-model="newSlide.image_alt" placeholder="Image alt / caption (optional)"
                               class="mt-1 w-full border border-gray-300 dark:border-gray-600 rounded-lg px-3 py-2 text-sm bg-white dark:bg-gray-700 text-gray-900 dark:text-white">
                    </div>

                    <input type="text" x-model="newSlide.button_text" placeholder="Button text (optional)"
                           class="w-full border border-gray-300 dark:border-gray-600 rounded-lg px-3 py-2 text-sm bg-white dark:bg-gray-700 text-gray-900 dark:text-white">
                    <input type="text" x-model="newSlide.button_url" placeholder="Button URL"
                           class="w-full border border-gray-300 dark:border-gray-600 rounded-lg px-3 py-2 text-sm bg-white dark:bg-gray-700 text-gray-900 dark:text-white font-mono text-xs">
                    <div class="flex gap-1 p-1 bg-gray-100 dark:bg-gray-700 rounded-lg">
                        <button type="button" x-on:click="newSlide.button_target = '_self'"
                                :class="newSlide.button_target === '_self' ? 'bg-white dark:bg-gray-600 text-gray-900 dark:text-white shadow-sm' : 'text-gray-500 hover:text-gray-700'"
                                class="flex-1 py-1 rounded-md text-xs font-medium transition-all">Same Tab</button>
                        <button type="button" x-on:click="newSlide.button_target = '_blank'"
                                :class="newSlide.button_target === '_blank' ? 'bg-white dark:bg-gray-600 text-gray-900 dark:text-white shadow-sm' : 'text-gray-500 hover:text-gray-700'"
                                class="flex-1 py-1 rounded-md text-xs font-medium transition-all">New Tab</button>
                    </div>
                    <button type="button" x-on:click="addSlide"
                            class="w-full py-2 bg-indigo-600 hover:bg-indigo-700 text-white rounded-lg text-sm font-medium">Add Slide</button>
                </div>
            </div>

            {{-- Slides list --}}
            <div class="flex-1">
                <div class="bg-white dark:bg-gray-800 rounded-xl shadow-sm p-6">
                    <div class="flex items-center justify-between mb-4">
                        <h3 class="font-semibold text-sm text-gray-900 dark:text-white">Slides</h3>
                        <p class="text-xs text-gray-400">Drag to reorder</p>
                    </div>

                    <div class="space-y-2 min-h-[60px]" x-ref="slidesList">
                        <template x-for="(slide, i) in slides" :key="slide._id">
                            <div class="border border-gray-200 dark:border-gray-700 rounded-lg overflow-hidden">
                                <div class="flex items-center gap-3 px-3 py-3 bg-gray-50 dark:bg-gray-700/50">
                                    <span class="drag-handle cursor-move text-gray-300 hover:text-gray-500 select-none px-1">⠿</span>
                                    <div class="w-12 h-8 rounded bg-gray-200 dark:bg-gray-600 flex-shrink-0 overflow-hidden">
                                        <img x-show="slide.image_url" :src="slide.image_url" class="w-full h-full object-cover" x-on:error="slide.image_url = ''">
                                    </div>
                                    <div class="flex-1 min-w-0">
                                        <p class="text-sm font-medium text-gray-800 dark:text-gray-200 truncate" x-text="slide.title || '(No title)'"></p>
                                        <p class="text-xs text-gray-400 truncate" x-text="slide.subtitle || slide.image_url || '—'"></p>
                                    </div>
                                    <button type="button" x-on:click="slide.is_active = !slide.is_active"
                                            :class="slide.is_active ? 'text-green-500' : 'text-gray-300'"
                                            class="p-1 rounded transition-colors" title="Toggle active">
                                        <svg class="w-4 h-4" fill="currentColor" viewBox="0 0 20 20"><circle cx="10" cy="10" r="5"/></svg>
                                    </button>
                                    <button type="button" x-on:click="slide._open = !slide._open"
                                            class="p-1 text-gray-400 hover:text-indigo-600 rounded transition-colors">
                                        <svg class="w-4 h-4 transition-transform" :class="slide._open ? 'rotate-180' : ''" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7"/></svg>
                                    </button>
                                    <button type="button" x-on:click="removeSlide(i)"
                                            class="p-1 text-gray-400 hover:text-red-600 rounded transition-colors">
                                        <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="M6 18L18 6M6 6l12 12"/></svg>
                                    </button>
                                </div>
                                {{-- Expanded edit --}}
                                <div x-show="slide._open" class="border-t border-gray-100 dark:border-gray-700 p-4 grid grid-cols-1 sm:grid-cols-2 gap-3 bg-white dark:bg-gray-800">
                                    <div>
                                        <label class="block text-xs font-medium text-gray-500 mb-1">Title</label>
                                        <input type="text" x-model="slide.title"
                                               class="w-full border border-gray-300 dark:border-gray-600 rounded-lg px-3 py-2 text-sm bg-white dark:bg-gray-700 text-gray-900 dark:text-white">
                                    </div>
                                    <div>
                                        <label class="block text-xs font-medium text-gray-500 mb-1">Subtitle</label>
                                        <input type="text" x-model="slide.subtitle"
                                               class="w-full border border-gray-300 dark:border-gray-600 rounded-lg px-3 py-2 text-sm bg-white dark:bg-gray-700 text-gray-900 dark:text-white">
                                    </div>
                                    <div class="col-span-full">
                                        <label class="block text-xs font-medium text-gray-500 mb-1">Image</label>
                                        <div class="flex gap-1">
                                            <input type="text" x-model="slide.image_url"
                                                   class="flex-1 min-w-0 border border-gray-300 dark:border-gray-600 rounded-l-lg px-3 py-2 text-sm bg-white dark:bg-gray-700 text-gray-900 dark:text-white font-mono text-xs">
                                            <button type="button" x-on:click="openPicker(url => slide.image_url = url)"
                                                    class="px-3 py-2 bg-gray-100 dark:bg-gray-700 border border-gray-300 dark:border-gray-600 border-l-0 rounded-r-lg text-gray-500 hover:text-indigo-600 hover:bg-indigo-50 dark:hover:bg-indigo-900/20 transition-colors text-xs font-medium whitespace-nowrap">
                                                Pick
                                            </button>
                                        </div>
                                        <div x-show="slide.image_url" class="mt-1 rounded overflow-hidden h-24 bg-gray-100 dark:bg-gray-700">
                                            <img :src="slide.image_url" class="w-full h-full object-cover" x-on:error="slide.image_url = ''">
                                        </div>
                                        <input type="text" x-model="slide.image_alt" placeholder="Image alt / caption (optional)"
                                               class="mt-1 w-full border border-gray-300 dark:border-gray-600 rounded-lg px-3 py-2 text-sm bg-white dark:bg-gray-700 text-gray-900 dark:text-white">
                                    </div>
                                    <div>
                                        <label class="block text-xs font-medium text-gray-500 mb-1">Button Text</label>
                                        <input type="text" x-model="slide.button_text"
                                               class="w-full border border-gray-300 dark:border-gray-600 rounded-lg px-3 py-2 text-sm bg-white dark:bg-gray-700 text-gray-900 dark:text-white">
                                    </div>
                                    <div>
                                        <label class="block text-xs font-medium text-gray-500 mb-1">Button URL</label>
                                        <input type="text" x-model="slide.button_url"
                                               class="w-full border border-gray-300 dark:border-gray-600 rounded-lg px-3 py-2 text-sm bg-white dark:bg-gray-700 text-gray-900 dark:text-white font-mono text-xs">
                                    </div>
                                    <div>
                                        <label class="block text-xs font-medium text-gray-500 mb-1">Button Target</label>
                                        <div class="flex gap-1 p-1 bg-gray-100 dark:bg-gray-700 rounded-lg">
                                            <button type="button" x-on:click="slide.button_target = '_self'"
                                                    :class="slide.button_target === '_self' ? 'bg-white dark:bg-gray-600 text-gray-900 dark:text-white shadow-sm' : 'text-gray-500 hover:text-gray-700'"
                                                    class="flex-1 py-1 rounded-md text-xs font-medium transition-all">Same Tab</button>
                                            <button type="button" x-on:click="slide.button_target = '_blank'"
                                                    :class="slide.button_target === '_blank' ? 'bg-white dark:bg-gray-600 text-gray-900 dark:text-white shadow-sm' : 'text-gray-500 hover:text-gray-700'"
                                                    class="flex-1 py-1 rounded-md text-xs font-medium transition-all">New Tab</button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </template>
                        <p x-show="slides.length === 0" class="text-sm text-gray-400 text-center py-8">No slides yet. Add from the panel above.</p>
                    </div>

                    <input type="hidden" name="slides" :value="JSON.stringify(slides)">

                    <div x-show="saveMessage" x-text="saveMessage"
                         :class="saveDone ? 'bg-green-50 text-green-700 dark:bg-green-900/20 dark:text-green-300' : 'bg-red-50 text-red-700 dark:bg-red-900/20 dark:text-red-300'"
                         class="mt-3 text-xs p-2 rounded border" style="display:none"></div>

                    <div class="mt-6 pt-4 border-t border-gray-200 dark:border-gray-700 flex gap-3">
                        <button type="submit" :disabled="saving"
                                class="px-4 py-2 bg-indigo-600 hover:bg-indigo-700 disabled:opacity-60 text-white rounded-lg text-sm font-medium flex items-center gap-2">
                            <svg x-show="saving" class="w-4 h-4 animate-spin" fill="none" viewBox="0 0 24 24">
                                <circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"/>
                                <path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4z"/>
                            </svg>
                            <span x-text="saving ? 'Saving...' : 'Save Slider'"></span>
                        </button>
                        <a href="{{ route('admin.sliders.index') }}" class="px-4 py-2 bg-gray-100 dark:bg-gray-700 text-gray-700 dark:text-gray-300 rounded-lg text-sm font-medium">Cancel</a>
                    </div>
                </div>
            </div>
        </div>
    </form>

    {{-- Media Picker Modal --}}
    <div x-show="pickerOpen" x-cloak
         class="fixed inset-0 z-50 flex items-center justify-center p-4"
         style="display:none">
        <div class="absolute inset-0 bg-black/60" x-on:click="closePicker()"></div>
        <div class="relative bg-white dark:bg-gray-800 rounded-2xl shadow-2xl w-full max-w-3xl max-h-[80vh] flex flex-col">
            {{-- Header --}}
            <div class="flex items-center justify-between p-4 border-b border-gray-200 dark:border-gray-700">
                <h3 class="font-semibold text-gray-900 dark:text-white">Pick Image</h3>
                <div class="flex items-center gap-3">
                    {{-- Upload button --}}
                    <input type="file" accept="image/*" class="hidden" x-ref="pickerFileInput" x-on:change="pickerUpload($event)">
                    <button type="button" x-on:click="$refs.pickerFileInput.click()" :disabled="pickerUploading"
                            class="flex items-center gap-1.5 px-3 py-1.5 bg-indigo-600 hover:bg-indigo-700 disabled:opacity-60 text-white rounded-lg text-xs font-medium">
                        <svg x-show="!pickerUploading" class="w-3.5 h-3.5" 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>
                        <svg x-show="pickerUploading" class="w-3.5 h-3.5 animate-spin" fill="none" viewBox="0 0 24 24"><circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"/><path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4z"/></svg>
                        <span x-text="pickerUploading ? 'Uploading...' : 'Upload'"></span>
                    </button>
                    <button type="button" x-on:click="closePicker()" class="p-1.5 text-gray-400 hover:text-gray-600 rounded-lg">
                        <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"/></svg>
                    </button>
                </div>
            </div>

            {{-- Search --}}
            <div class="p-3 border-b border-gray-200 dark:border-gray-700">
                <input type="text" x-model="pickerSearch" x-on:input="onPickerSearch()"
                       placeholder="Search images..."
                       class="w-full border border-gray-300 dark:border-gray-600 rounded-lg px-3 py-2 text-sm bg-white dark:bg-gray-700 text-gray-900 dark:text-white">
            </div>

            {{-- Grid --}}
            <div class="flex-1 overflow-y-auto p-4">
                <div x-show="pickerLoading" class="flex justify-center py-12 text-gray-400">
                    <svg class="w-6 h-6 animate-spin" fill="none" viewBox="0 0 24 24"><circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"/><path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4z"/></svg>
                </div>
                <div x-show="!pickerLoading" class="grid grid-cols-4 sm:grid-cols-6 gap-2">
                    <template x-for="item in pickerItems" :key="item.id">
                        <div x-on:click="selectPickerItem(item)"
                             :class="pickerSelected?.id === item.id ? 'ring-2 ring-indigo-500' : 'hover:ring-2 hover:ring-indigo-300'"
                             class="relative aspect-square rounded-lg overflow-hidden bg-gray-100 dark:bg-gray-700 transition-all cursor-pointer group">
                            <img :src="item.url" :alt="item.name" class="w-full h-full object-cover group-hover:opacity-90 transition-opacity">
                            <div x-show="pickerSelected?.id === item.id"
                                 class="absolute top-1 right-1 w-5 h-5 bg-indigo-600 rounded-full flex items-center justify-center">
                                <svg class="w-3 h-3 text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="3" d="M5 13l4 4L19 7"/></svg>
                            </div>
                        </div>
                    </template>
                    <p x-show="!pickerLoading && pickerItems.length === 0" class="col-span-full text-center text-sm text-gray-400 py-8">
                        No images found.
                    </p>
                </div>

                {{-- Pagination --}}
                <div x-show="pickerLastPage > 1" class="flex justify-center gap-1 mt-4">
                    <template x-for="p in Array.from({length: pickerLastPage}, (_, i) => i + 1)" :key="p">
                        <button type="button" x-on:click="loadMedia(p)"
                                :class="p === pickerPage ? 'bg-indigo-600 text-white' : 'bg-gray-100 dark:bg-gray-700 text-gray-600 dark:text-gray-300 hover:bg-gray-200'"
                                class="w-8 h-8 rounded text-xs font-medium" x-text="p"></button>
                    </template>
                </div>
            </div>

            {{-- Select footer --}}
            <div x-show="pickerSelected" class="border-t border-gray-200 dark:border-gray-700 p-4 bg-gray-50 dark:bg-gray-700/30 flex items-center gap-3">
                <img :src="pickerSelected?.url" class="w-12 h-12 object-cover rounded-lg flex-shrink-0">
                <div class="flex-1 min-w-0">
                    <p class="text-xs font-medium text-gray-700 dark:text-gray-300 truncate mb-1" x-text="pickerSelected?.name"></p>
                    <p class="text-xs text-gray-400 italic truncate" x-show="pickerSelected?.caption" x-text="pickerSelected?.caption"></p>
                </div>
                <button type="button" x-on:click="pickMedia(pickerSelected)"
                        class="flex-shrink-0 px-4 py-2 bg-indigo-600 hover:bg-indigo-700 text-white text-sm font-medium rounded-lg">
                    Select
                </button>
            </div>
        </div>
    </div>
</div>
@endsection
