<x-admin-layout>
    <x-slot name="header">
        {{ __('Pengaturan Video Konten') }}
    </x-slot>

    <div class="space-y-8">

        {{-- ===== PANEL 1: Toggle Aktif/Non-Aktif ===== --}}
        <div class="bg-white dark:bg-slate-800 rounded-2xl shadow-sm border border-gray-100 dark:border-slate-700 overflow-hidden">
            <form action="{{ route('admin.settings.video.store') }}" method="POST">
                @csrf
                <div class="p-8">
                    <h3 class="text-lg font-bold text-gray-800 dark:text-gray-100 mb-4">Status Visibilitas Section</h3>
                    <label class="relative inline-flex items-center cursor-pointer">
                        <input type="checkbox" name="video_section_active" value="1" class="sr-only peer"
                            {{ ($settings['video_section_active'] ?? '0') == '1' ? 'checked' : '' }}>
                        <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 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 dark:bg-gray-700"></div>
                        <span class="ml-3 text-sm font-medium text-gray-700 dark:text-gray-300">Tampilkan Section Video di Homepage</span>
                    </label>
                </div>
                <div class="px-8 py-4 bg-gray-50 dark:bg-slate-700/50 border-t border-gray-100 dark:border-slate-700 flex justify-end">
                    <button type="submit" class="px-5 py-2.5 bg-indigo-600 hover:bg-indigo-700 rounded-lg shadow-sm font-medium text-white text-sm transition-colors focus:ring-2 focus:ring-indigo-500">
                        <x-heroicon-s-check class="w-4 h-4 mr-1 inline" /> Simpan Status
                    </button>
                </div>
            </form>
        </div>

        {{-- ===== PANEL 2: Tambah Video Baru ===== --}}
        <div class="bg-white dark:bg-slate-800 rounded-2xl shadow-sm border border-gray-100 dark:border-slate-700 overflow-hidden">
            <div class="px-8 py-6 border-b border-gray-100 dark:border-slate-700">
                <h3 class="text-lg font-bold text-gray-800 dark:text-gray-100">Tambah Video Baru</h3>
                <p class="text-sm text-gray-500 dark:text-gray-400 mt-1">Tambahkan beberapa video untuk ditampilkan di carousel homepage.</p>
            </div>
            <form action="{{ route('admin.videos.store') }}" method="POST" enctype="multipart/form-data"
                  x-data="{ sourceType: 'youtube' }">
                @csrf
                <div class="p-8 space-y-6">
                    <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                        <div>
                            <label for="title" class="block text-sm font-medium text-gray-700 dark:text-gray-200 mb-2">Judul Video <span class="text-red-500">*</span></label>
                            <input type="text" name="title" id="title" value="{{ old('title') }}" required
                                class="block w-full rounded-lg border-gray-300 dark:border-slate-600 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 py-2.5 px-3 border dark:bg-slate-700 dark:text-white"
                                placeholder="Contoh: Demo Fitur Admin Panel">
                            @error('title') <p class="mt-1 text-sm text-red-600">{{ $message }}</p> @enderror
                        </div>
                        <div>
                            <label for="description" class="block text-sm font-medium text-gray-700 dark:text-gray-200 mb-2">Deskripsi Singkat</label>
                            <input type="text" name="description" id="description" value="{{ old('description') }}"
                                class="block w-full rounded-lg border-gray-300 dark:border-slate-600 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 py-2.5 px-3 border dark:bg-slate-700 dark:text-white"
                                placeholder="Penjelasan singkat isi video">
                        </div>
                    </div>

                    <div>
                        <label class="block text-sm font-medium text-gray-700 dark:text-gray-200 mb-3">Sumber Video <span class="text-red-500">*</span></label>
                        <div class="flex gap-6">
                            <label class="flex items-center cursor-pointer">
                                <input type="radio" name="source_type" value="youtube" x-model="sourceType" class="w-4 h-4 text-indigo-600 border-gray-300" checked>
                                <span class="ml-2 text-sm text-gray-700 dark:text-gray-300">YouTube Embed</span>
                            </label>
                            <label class="flex items-center cursor-pointer">
                                <input type="radio" name="source_type" value="upload" x-model="sourceType" class="w-4 h-4 text-indigo-600 border-gray-300">
                                <span class="ml-2 text-sm text-gray-700 dark:text-gray-300">Upload Video Lokal</span>
                            </label>
                        </div>
                    </div>

                    <div x-show="sourceType === 'youtube'" class="bg-slate-50 dark:bg-slate-700/30 p-5 rounded-xl border border-gray-100 dark:border-slate-600">
                        <label for="youtube_id" class="block text-sm font-medium text-gray-700 dark:text-gray-200 mb-2">YouTube Video ID</label>
                        <input type="text" name="youtube_id" id="youtube_id" value="{{ old('youtube_id') }}"
                            class="block w-full rounded-lg border-gray-300 dark:border-slate-600 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 py-2.5 px-3 border dark:bg-slate-700 dark:text-white"
                            placeholder="Contoh: dQw4w9WgXcQ">
                        <p class="mt-2 text-xs text-gray-500">Cukup masukkan 11-karakter ID dari URL YouTube.</p>
                        @error('youtube_id') <p class="mt-1 text-sm text-red-600">{{ $message }}</p> @enderror
                    </div>

                    <div x-show="sourceType === 'upload'" style="display:none;" class="bg-slate-50 dark:bg-slate-700/30 p-5 rounded-xl border border-gray-100 dark:border-slate-600">
                        <label for="video_file" class="block text-sm font-medium text-gray-700 dark:text-gray-200 mb-2">Upload File Video</label>
                        <input type="file" name="video_file" id="video_file" accept="video/mp4,video/webm"
                            class="block w-full text-sm text-gray-500 file:mr-4 file:py-2.5 file:px-4 file:rounded-lg file:border-0 file:text-sm file:font-semibold file:bg-indigo-50 file:text-indigo-700 hover:file:bg-indigo-100 border border-gray-200 rounded-lg dark:bg-slate-800 dark:text-white dark:border-slate-700">
                        <p class="mt-2 text-xs text-gray-500">Maksimal 50MB. Format: MP4, WebM.</p>
                        @error('video_file') <p class="mt-1 text-sm text-red-600">{{ $message }}</p> @enderror
                    </div>
                </div>

                <div class="px-8 py-4 bg-gray-50 dark:bg-slate-700/50 border-t border-gray-100 dark:border-slate-700 flex justify-end">
                    <button type="submit" class="px-5 py-2.5 bg-emerald-600 hover:bg-emerald-700 rounded-lg shadow-sm font-medium text-white text-sm transition-colors focus:ring-2 focus:ring-emerald-500">
                        <x-heroicon-o-plus class="w-4 h-4 mr-1 inline" /> Tambah Video
                    </button>
                </div>
            </form>
        </div>

        {{-- ===== PANEL 3: Daftar Video ===== --}}
        <div class="bg-white dark:bg-slate-800 rounded-2xl shadow-sm border border-gray-100 dark:border-slate-700 overflow-hidden">
            <div class="px-8 py-6 border-b border-gray-100 dark:border-slate-700 flex items-center justify-between">
                <div>
                    <h3 class="text-lg font-bold text-gray-800 dark:text-gray-100">Daftar Video ({{ $videos->count() }})</h3>
                    <p class="text-sm text-gray-500 dark:text-gray-400 mt-0.5">Video aktif akan tampil di carousel homepage.</p>
                </div>
            </div>

            @if($videos->isEmpty())
                <div class="py-16 text-center text-gray-400">
                    <x-heroicon-o-video-camera class="w-12 h-12 mx-auto mb-3 opacity-30" />
                    <p class="text-sm">Belum ada video ditambahkan.</p>
                </div>
            @else
                <div class="divide-y divide-gray-100 dark:divide-slate-700">
                    @foreach($videos as $video)
                    <div class="flex items-center gap-4 px-8 py-5 hover:bg-gray-50 dark:hover:bg-slate-700/30 transition-colors"
                         x-data="{ editing: false }">

                        {{-- Thumbnail/Icon --}}
                        <div class="w-24 h-14 rounded-lg overflow-hidden bg-slate-900 flex-shrink-0 flex items-center justify-center relative">
                            @if($video->source_type === 'youtube' && $video->youtube_id)
                                <img src="https://img.youtube.com/vi/{{ $video->youtube_id }}/mqdefault.jpg"
                                     alt="{{ $video->title }}" class="w-full h-full object-cover">
                                <div class="absolute inset-0 flex items-center justify-center bg-black/30">
                                    <x-heroicon-s-play class="w-5 h-5 text-white" />
                                </div>
                            @elseif($video->source_type === 'upload' && $video->file_path)
                                <x-heroicon-o-film class="w-7 h-7 text-slate-400" />
                            @else
                                <x-heroicon-o-video-camera class="w-7 h-7 text-slate-500" />
                            @endif
                        </div>

                        {{-- Info --}}
                        <div class="flex-1 min-w-0">
                            <div x-show="!editing">
                                <p class="font-semibold text-gray-900 dark:text-white truncate">{{ $video->title }}</p>
                                <p class="text-xs text-gray-500 dark:text-gray-400 mt-0.5 truncate">
                                    {{ $video->source_type === 'youtube' ? '▶ YouTube: ' . $video->youtube_id : '📁 File Upload' }}
                                    @if($video->description) · {{ Str::limit($video->description, 50) }} @endif
                                </p>
                            </div>

                            {{-- Edit Form --}}
                            <form x-show="editing" style="display:none;" action="{{ route('admin.videos.update', $video) }}" method="POST" class="space-y-2">
                                @csrf
                                @method('PUT')
                                <input type="text" name="title" value="{{ $video->title }}"
                                    class="block w-full rounded-lg border-gray-300 dark:border-slate-600 py-1.5 px-3 border text-sm dark:bg-slate-700 dark:text-white focus:ring-indigo-500 focus:border-indigo-500">
                                <input type="text" name="description" value="{{ $video->description }}"
                                    class="block w-full rounded-lg border-gray-300 dark:border-slate-600 py-1.5 px-3 border text-sm dark:bg-slate-700 dark:text-white focus:ring-indigo-500 focus:border-indigo-500"
                                    placeholder="Deskripsi...">
                                <label class="flex items-center gap-2 text-sm text-gray-600 dark:text-gray-300">
                                    <input type="checkbox" name="is_active" value="1" {{ $video->is_active ? 'checked' : '' }}
                                        class="rounded border-gray-300 text-indigo-600 focus:ring-indigo-500">
                                    Tampilkan di carousel
                                </label>
                                <div class="flex gap-2 pt-1">
                                    <button type="submit" class="px-3 py-1.5 text-xs font-semibold bg-indigo-600 text-white rounded-lg hover:bg-indigo-700">Simpan</button>
                                    <button type="button" @click="editing = false" class="px-3 py-1.5 text-xs font-semibold bg-gray-100 text-gray-700 rounded-lg hover:bg-gray-200">Batal</button>
                                </div>
                            </form>
                        </div>

                        {{-- Status Badge --}}
                        <div x-show="!editing" class="flex-shrink-0">
                            @if($video->is_active)
                                <span class="inline-flex items-center gap-1 px-2.5 py-1 rounded-md text-xs font-semibold bg-emerald-50 text-emerald-700 border border-emerald-100">
                                    <span class="w-1.5 h-1.5 rounded-full bg-emerald-500"></span> Aktif
                                </span>
                            @else
                                <span class="inline-flex items-center gap-1 px-2.5 py-1 rounded-md text-xs font-semibold bg-gray-100 text-gray-500 border border-gray-200">
                                    <span class="w-1.5 h-1.5 rounded-full bg-gray-400"></span> Nonaktif
                                </span>
                            @endif
                        </div>

                        {{-- Actions --}}
                        <div x-show="!editing" class="flex items-center gap-2 flex-shrink-0">
                            <button @click="editing = true" class="p-2 text-gray-400 hover:text-indigo-600 rounded-lg hover:bg-indigo-50 transition-colors" title="Edit">
                                <x-heroicon-o-pencil-square class="w-4 h-4" />
                            </button>
                            <form action="{{ route('admin.videos.destroy', $video) }}" method="POST"
                                  data-confirm="Hapus video '{{ $video->title }}'?" class="inline">
                                @csrf @method('DELETE')
                                <button type="submit" class="p-2 text-gray-400 hover:text-red-600 rounded-lg hover:bg-red-50 transition-colors" title="Hapus">
                                    <x-heroicon-o-trash class="w-4 h-4" />
                                </button>
                            </form>
                        </div>
                    </div>
                    @endforeach
                </div>
            @endif
        </div>

    </div>
</x-admin-layout>
