@extends('layouts.app')

@section('title', 'Galleries')

@section('content')
<x-app-layout>
    <x-module-hero
        title="Pictorial Galleries"
        description="Manage and organize image galleries by type"
        icon="fa-image">

        <x-slot name="backButton">
            <a href="{{ route('dashboard') }}"
               class="inline-flex items-center gap-2 text-[#475569] hover:text-[#1e293b] text-sm transition">
                <i class="fas fa-arrow-left text-xs"></i> Back to Dashboard
            </a>
        </x-slot>

        <x-slot name="actions">
            @can('create galleries')
            <a href="{{ route('media.galleries.create') }}"
               class="inline-flex items-center gap-2 px-4 py-2 bg-[#1e293b] text-white text-sm font-semibold rounded-lg hover:bg-[#334155] transition">
                <i class="fas fa-plus"></i> Create Gallery
            </a>
            @endcan
        </x-slot>
    </x-module-hero>

    <div class="p-6">
        {{-- Flash Messages --}}
        @if(session('success'))
        <div class="mb-6 p-4 bg-green-100 border border-green-400 text-green-700 rounded-lg flex items-center gap-2">
            <i class="fas fa-check-circle"></i>
            <span>{{ session('success') }}</span>
        </div>
        @endif

        @if(session('error'))
        <div class="mb-6 p-4 bg-red-100 border border-red-400 text-red-700 rounded-lg flex items-center gap-2">
            <i class="fas fa-times-circle"></i>
            <span>{{ session('error') }}</span>
        </div>
        @endif

        {{-- Filters & Search --}}
        <div class="mb-6 bg-white rounded-lg shadow-sm border border-gray-200 p-4">
            <form method="GET" action="{{ route('media.galleries.index') }}" class="grid grid-cols-1 md:grid-cols-4 gap-4">
                {{-- Search --}}
                <div>
                    <input type="text" name="search" placeholder="Search galleries..."
                           value="{{ request('search') }}"
                           class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent">
                </div>

                {{-- Organization Filter --}}
                <div>
                    <select name="organization_id" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent">
                        <option value="">All Organizations</option>
                        @foreach($organizations as $org)
                            <option value="{{ $org->id }}" {{ request('organization_id') == $org->id ? 'selected' : '' }}>
                                {{ $org->name }}
                            </option>
                        @endforeach
                    </select>
                </div>

                {{-- Gallery Type Filter --}}
                <div>
                    <select name="gallery_type_id" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent">
                        <option value="">All Types</option>
                        @foreach($galleryTypes as $type)
                            <option value="{{ $type->id }}" {{ request('gallery_type_id') == $type->id ? 'selected' : '' }}>
                                {{ $type->name }}
                            </option>
                        @endforeach
                    </select>
                </div>

                {{-- Status Filter --}}
                <div class="flex gap-2">
                    <select name="is_published" class="flex-1 px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent">
                        <option value="">All Status</option>
                        <option value="1" {{ request('is_published') === '1' ? 'selected' : '' }}>Published</option>
                        <option value="0" {{ request('is_published') === '0' ? 'selected' : '' }}>Unpublished</option>
                    </select>
                    <button type="submit" class="px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 transition font-medium">
                        <i class="fas fa-search"></i>
                    </button>
                    @if(request('search') || request('organization_id') || request('gallery_type_id') || request('is_published'))
                    <a href="{{ route('media.galleries.index') }}" class="px-4 py-2 bg-gray-300 text-gray-700 rounded-lg hover:bg-gray-400 transition">
                        Clear
                    </a>
                    @endif
                </div>
            </form>
        </div>

        {{-- Galleries Grid --}}
        <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
            @forelse ($galleries as $gallery)
                <div class="bg-white rounded-lg shadow-sm border border-gray-200 overflow-hidden hover:shadow-md transition-shadow">
                    {{-- Gallery Thumbnail --}}
                    <div class="relative h-48 bg-gray-100 overflow-hidden">
                        @if($gallery->images_count > 0)
                            <img src="{{ asset('storage/' . $gallery->images->first()->image_path) }}"
                                 alt="{{ $gallery->title }}"
                                 class="w-full h-full object-cover">
                        @else
                            <div class="w-full h-full flex flex-col items-center justify-center">
                                <i class="fas fa-image text-5xl text-gray-300 mb-2"></i>
                                <p class="text-sm text-gray-400">No images</p>
                            </div>
                        @endif

                        {{-- Gallery Type Badge --}}
                        <div class="absolute top-3 right-3">
                            <span class="inline-block px-3 py-1 text-xs font-semibold rounded-full text-white" 
                                  style="background-color: {{ $gallery->type->color }}">
                                {{ $gallery->type->name }}
                            </span>
                        </div>

                        {{-- Status Badge --}}
                        <div class="absolute top-3 left-3">
                            <span class="inline-block px-3 py-1 text-xs font-semibold rounded-full
                                {{ $gallery->is_published ? 'bg-green-500 text-white' : 'bg-yellow-500 text-white' }}">
                                {{ $gallery->is_published ? 'Published' : 'Draft' }}
                            </span>
                        </div>
                    </div>

                    {{-- Gallery Content --}}
                    <div class="p-4">
                        {{-- Title & Organization --}}
                        <h3 class="font-semibold text-gray-900 mb-1 truncate">{{ $gallery->title }}</h3>
                        <p class="text-xs text-gray-500 mb-3">
                            <i class="fas fa-building"></i> {{ $gallery->organization->name }}
                        </p>

                        {{-- Description --}}
                        @if($gallery->description)
                        <p class="text-sm text-gray-600 mb-3 line-clamp-2">{{ $gallery->description }}</p>
                        @endif

                        {{-- Image Count --}}
                        <div class="mb-4 pb-4 border-b border-gray-200">
                            <p class="text-sm text-gray-600">
                                <i class="fas fa-images text-blue-600"></i>
                                <strong>{{ $gallery->images_count }}</strong> {{ Str::plural('image', $gallery->images_count) }}
                            </p>
                        </div>

                        {{-- Actions --}}
                        <div class="flex gap-2">
                            <a href="{{ route('media.galleries.show', $gallery) }}"
                               class="flex-1 text-center px-3 py-2 text-sm bg-blue-50 text-blue-600 rounded hover:bg-blue-100 transition font-medium">
                                <i class="fas fa-eye"></i> View
                            </a>
                            @can('edit galleries')
                            <a href="{{ route('media.galleries.edit', $gallery) }}"
                               class="flex-1 text-center px-3 py-2 text-sm bg-amber-50 text-amber-600 rounded hover:bg-amber-100 transition font-medium">
                                <i class="fas fa-edit"></i> Edit
                            </a>
                            @endcan
                            @can('delete galleries')
                            <form action="{{ route('media.galleries.destroy', $gallery) }}" method="POST" class="flex-1"
                                  onsubmit="return confirm('Delete this gallery and all its images?');">
                                @csrf @method('DELETE')
                                <button type="submit"
                                    class="w-full px-3 py-2 text-sm bg-red-50 text-red-600 rounded hover:bg-red-100 transition font-medium">
                                    <i class="fas fa-trash"></i>
                                </button>
                            </form>
                            @endcan
                        </div>
                    </div>
                </div>
            @empty
                <div class="col-span-3">
                    <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-12 text-center">
                        <i class="fas fa-inbox text-5xl text-gray-300 mb-4"></i>
                        <h3 class="text-xl font-semibold text-gray-700 mb-2">No Galleries Found</h3>
                        <p class="text-gray-500 mb-6">Create your first gallery to start uploading images.</p>
                        @can('create galleries')
                        <a href="{{ route('media.galleries.create') }}" class="inline-flex items-center gap-2 px-6 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 transition">
                            <i class="fas fa-plus"></i> Create Gallery
                        </a>
                        @endcan
                    </div>
                </div>
            @endforelse
        </div>

        {{-- Pagination --}}
        @if($galleries->hasPages())
        <div class="mt-8">
            {{ $galleries->links() }}
        </div>
        @endif
    </div>
</x-app-layout>
@endsection
