@extends('admin.layouts.app')
@section('title', 'Media')
@section('page-title', 'Media')

@section('content')
<div x-data="mediaManager()" x-init="init()" class="space-y-6">
    {{-- Upload Zone --}}
    <div class="bg-white dark:bg-gray-800 rounded-xl shadow-sm p-6">
        <div class="border-2 border-dashed border-gray-300 dark:border-gray-600 rounded-xl p-8 text-center"
             @dragover.prevent="dragging = true"
             @dragleave.prevent="dragging = false"
             @drop.prevent="handleDrop($event)"
             :class="{ 'border-indigo-400 bg-indigo-50 dark:bg-indigo-900/20': dragging }">
            <svg class="w-10 h-10 text-gray-400 mx-auto mb-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" 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>
            <p class="text-sm text-gray-600 dark:text-gray-400 mb-2">Drag & drop files here or</p>
            <label class="cursor-pointer inline-flex items-center gap-2 px-4 py-2 bg-indigo-600 hover:bg-indigo-700 text-white rounded-lg text-sm font-medium">
                <input type="file" class="hidden" multiple @change="uploadFiles($event.target.files)" accept="image/*,video/*,application/pdf">
                Browse files
            </label>
            <div x-show="uploading" class="mt-4">
                <div class="w-full bg-gray-200 dark:bg-gray-700 rounded-full h-2">
                    <div class="bg-indigo-600 h-2 rounded-full transition-all" :style="`width: ${uploadProgress}%`"></div>
                </div>
                <p class="text-xs text-gray-500 mt-1" x-text="`Uploading... ${uploadProgress}%`"></p>
            </div>
        </div>
    </div>

    {{-- Search / Filter --}}
    <div class="flex flex-col sm:flex-row sm:items-center gap-3">
        <div class="relative flex-1 sm:max-w-sm">
            <svg class="absolute left-3 top-2.5 w-4 h-4 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"/>
            </svg>
            <input type="text" x-model="search" @input="onSearch()" placeholder="Search media..."
                   class="pl-9 w-full text-sm border border-gray-300 dark:border-gray-600 rounded-lg px-3 py-2 bg-white dark:bg-gray-700 text-gray-900 dark:text-white">
        </div>
        <div class="flex items-center justify-between sm:flex-1 gap-3">
            <div class="flex gap-1 p-1 bg-gray-100 dark:bg-gray-700 rounded-lg">
                @foreach(['' => 'All', 'image' => 'Images', 'video' => 'Videos'] as $val => $label)
                <button type="button" @click="type = '{{ $val }}'; fetch(1)"
                        :class="type === '{{ $val }}' ? 'bg-white dark:bg-gray-600 text-gray-900 dark:text-white shadow-sm' : 'text-gray-500 dark:text-gray-400 hover:text-gray-700'"
                        class="px-3 py-1.5 rounded-md text-xs font-medium transition-all">{{ $label }}</button>
                @endforeach
            </div>
            <p class="text-sm text-gray-500 dark:text-gray-400 flex-shrink-0">Total: <span x-text="meta.total"></span></p>
        </div>
    </div>

    {{-- Grid --}}
    <template x-if="loading">
        <div class="flex items-center justify-center py-16 gap-2 text-gray-400">
            <svg class="w-5 h-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>
            Loading...
        </div>
    </template>

    <template x-if="!loading">
        <div>
            <div class="grid grid-cols-2 sm:grid-cols-3 md:grid-cols-4 lg:grid-cols-6 gap-4">
                <template x-if="items.length === 0">
                    <div class="col-span-full py-12 text-center text-gray-400">No media yet.</div>
                </template>
                <template x-for="item in items" :key="item.id">
                    <div class="group relative bg-white dark:bg-gray-800 rounded-xl overflow-hidden shadow-sm border border-gray-200 dark:border-gray-700 hover:border-indigo-300 transition-colors cursor-pointer"
                         @click="selectMedia(item.id, item.url, item.name)">
                        <img x-show="isImage(item.mime_type)" :src="item.thumb" :alt="item.name" class="w-full h-24 object-cover">
                        <div x-show="!isImage(item.mime_type)" class="w-full h-24 flex items-center justify-center bg-gray-50 dark:bg-gray-700">
                            <svg class="w-8 h-8 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M7 21h10a2 2 0 002-2V9.414a1 1 0 00-.293-.707l-5.414-5.414A1 1 0 0012.586 3H7a2 2 0 00-2 2v14a2 2 0 002 2z"/>
                            </svg>
                        </div>
                        <div class="p-2">
                            <p class="text-xs text-gray-600 dark:text-gray-400 truncate" x-text="item.name"></p>
                            <p x-show="item.caption" class="text-xs text-gray-400 dark:text-gray-500 truncate italic" x-text="item.caption"></p>
                        </div>
                        <div class="absolute inset-0 bg-black/50 opacity-0 group-hover:opacity-100 transition-opacity flex items-center justify-center gap-2">
                            <button type="button" @click.stop="openPreview(item)" class="p-1.5 bg-white/20 hover:bg-white/30 rounded-lg">
                                <svg class="w-4 h-4 text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"/><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z"/></svg>
                            </button>
                            <button type="button" @click.stop="deleteMedia(item.id)" :disabled="deletingId === item.id" class="p-1.5 bg-white/20 hover:bg-red-500/70 rounded-lg disabled:opacity-50">
                                <svg x-show="deletingId === item.id" class="w-4 h-4 text-white 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>
                                <svg x-show="deletingId !== item.id" class="w-4 h-4 text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"/></svg>
                            </button>
                        </div>
                    </div>
                </template>
            </div>

            <div x-show="meta.last_page > 1" class="mt-6 flex items-center justify-center gap-1">
                <button @click="fetch(meta.current_page - 1)" :disabled="meta.current_page === 1"
                        class="px-3 py-1.5 text-sm rounded border border-gray-300 dark:border-gray-600 disabled:opacity-40 hover:bg-gray-50 dark:hover:bg-gray-700">←</button>
                <template x-for="p in pages()" :key="p">
                    <button @click="fetch(p)"
                            :class="p === meta.current_page ? 'bg-indigo-600 text-white border-indigo-600' : 'border-gray-300 dark:border-gray-600 hover:bg-gray-50 dark:hover:bg-gray-700 text-gray-700 dark:text-gray-300'"
                            class="px-3 py-1.5 text-sm rounded border" x-text="p"></button>
                </template>
                <button @click="fetch(meta.current_page + 1)" :disabled="meta.current_page === meta.last_page"
                        class="px-3 py-1.5 text-sm rounded border border-gray-300 dark:border-gray-600 disabled:opacity-40 hover:bg-gray-50 dark:hover:bg-gray-700">→</button>
            </div>
        </div>
    </template>
    {{-- Preview Modal --}}
    <div x-show="previewItem" x-cloak
         style="position:fixed; inset:0; z-index:9999;"
         x-on:keydown.escape.window="closePreview()">
        <div style="position:absolute; inset:0; background:rgba(0,0,0,0.75);" @click="closePreview()"></div>
        <div style="position:absolute; inset:0; display:flex; align-items:center; justify-content:center; padding:24px; pointer-events:none;">
            <div style="position:relative; background:#fff; border-radius:12px; max-width:800px; width:100%; overflow:hidden; box-shadow:0 20px 60px rgba(0,0,0,0.4); pointer-events:auto;"
                 class="dark:bg-gray-800">
                {{-- Header --}}
                <div class="flex items-center justify-between px-5 py-3 border-b border-gray-200 dark:border-gray-700">
                    <p class="text-sm font-medium text-gray-900 dark:text-white truncate" x-text="previewItem?.name"></p>
                    <div class="flex items-center gap-2 flex-shrink-0 ml-4">
                        <a :href="previewItem?.url" target="_blank" @click.stop
                           class="text-xs text-indigo-600 dark:text-indigo-400 hover:underline font-medium">
                            Open original
                        </a>
                        <button type="button" @click="closePreview()" class="text-gray-400 hover:text-gray-600 dark:hover:text-gray-200 ml-2">
                            <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>
                {{-- Image --}}
                <div class="p-4 flex items-center justify-center bg-gray-50 dark:bg-gray-900" style="max-height:70vh; overflow:auto;">
                    <template x-if="previewItem && isImage(previewItem.mime_type)">
                        <img :src="previewItem.url" :alt="previewItem.name" style="max-width:100%; max-height:65vh; object-fit:contain; border-radius:6px;">
                    </template>
                    <template x-if="previewItem && !isImage(previewItem.mime_type)">
                        <div class="py-12 text-center text-gray-400">
                            <svg class="w-16 h-16 mx-auto mb-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M7 21h10a2 2 0 002-2V9.414a1 1 0 00-.293-.707l-5.414-5.414A1 1 0 0012.586 3H7a2 2 0 00-2 2v14a2 2 0 002 2z"/>
                            </svg>
                            <p class="text-sm" x-text="previewItem.name"></p>
                        </div>
                    </template>
                </div>
                {{-- Caption --}}
                <div class="px-5 py-3 border-t border-gray-200 dark:border-gray-700">
                    <label class="block text-xs font-medium text-gray-500 dark:text-gray-400 mb-1">Caption</label>
                    <div class="flex gap-2">
                        <input type="text" x-model="previewCaption" placeholder="Add a caption..."
                               class="flex-1 text-sm border border-gray-300 dark:border-gray-600 rounded-lg px-3 py-1.5 bg-white dark:bg-gray-700 text-gray-900 dark:text-white outline-none focus:ring-2 focus:ring-indigo-500">
                        <button type="button" @click="saveCaption()" :disabled="savingCaption"
                                class="px-3 py-1.5 bg-indigo-600 hover:bg-indigo-700 disabled:opacity-60 text-white rounded-lg text-xs font-medium flex items-center gap-1">
                            <svg x-show="savingCaption" class="w-3 h-3 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>
                            Save
                        </button>
                    </div>
                </div>
                {{-- Meta --}}
                <div class="px-5 py-3 border-t border-gray-200 dark:border-gray-700 flex items-center gap-6 text-xs text-gray-500 dark:text-gray-400">
                    <span x-text="previewItem?.mime_type"></span>
                    <span x-text="previewItem?.size"></span>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
