<x-app-layout>

    <x-module-hero
        title="What We Do Images"
        description="Upload and manage images for each of the nine What We Do activity cards on the homepage"
        icon="fa-image"
    >
        <x-slot name="tabs">
            @role('Manager|Super Manager')
            <a href="{{ route('admin.sliders.index') }}"
               class="module-hero-tab {{ request()->routeIs('admin.sliders.*') ? 'border-b-2 border-[#1e293b]' : 'opacity-70 hover:opacity-100 transition-opacity' }}
                      inline-flex items-center gap-2 px-4 py-3 text-[#1e293b] text-sm font-medium
                      md:w-auto w-full border-b border-[#cbd5e1] md:border-b-0">
                <i class="fas fa-images text-sm"></i> Hero Slider
            </a>
            <a href="{{ route('admin.hero-cards.index') }}"
               class="module-hero-tab {{ request()->routeIs('admin.hero-cards.*') ? 'border-b-2 border-[#1e293b]' : 'opacity-70 hover:opacity-100 transition-opacity' }}
                      inline-flex items-center gap-2 px-4 py-3 text-[#1e293b] text-sm font-medium
                      md:w-auto w-full border-b border-[#cbd5e1] md:border-b-0">
                <i class="fas fa-th-large text-sm"></i> Hero Cards
            </a>
            @endrole
            @can('manage what we do images')
            <a href="{{ route('admin.what-we-do-images.index') }}"
               class="module-hero-tab {{ request()->routeIs('admin.what-we-do-images.*') ? 'border-b-2 border-[#1e293b]' : 'opacity-70 hover:opacity-100 transition-opacity' }}
                      inline-flex items-center gap-2 px-4 py-3 text-[#1e293b] text-sm font-medium
                      md:w-auto w-full border-b border-[#cbd5e1] md:border-b-0">
                <i class="fas fa-image text-sm"></i> What We Do Images
            </a>
            @endcan
            @can('view pages')
            <a href="{{ route('admin.pages.index') }}"
               class="module-hero-tab {{ request()->routeIs('admin.pages.*') ? 'border-b-2 border-[#1e293b]' : 'opacity-70 hover:opacity-100 transition-opacity' }}
                      inline-flex items-center gap-2 px-4 py-3 text-[#1e293b] text-sm font-medium
                      md:w-auto w-full border-b border-[#cbd5e1] md:border-b-0">
                <i class="fas fa-file-alt text-sm"></i> Pages
            </a>
            @endcan
        </x-slot>
    </x-module-hero>

    <div class="p-6">

        {{-- Flash Messages --}}
        @if(session('success'))
            <div class="mb-4 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> {{ session('success') }}
            </div>
        @endif
        @if(session('error'))
            <div class="mb-4 p-4 bg-red-100 border border-red-400 text-red-700 rounded-lg flex items-center gap-2">
                <i class="fas fa-exclamation-circle"></i> {{ session('error') }}
            </div>
        @endif

        {{-- Validation Errors --}}
        @if($errors->any())
            <div class="mb-6 p-4 bg-red-100 border border-red-400 text-red-700 rounded-lg">
                <div class="flex items-center gap-2 mb-2">
                    <i class="fas fa-exclamation-circle"></i>
                    <strong>Please fix the following errors:</strong>
                </div>
                <ul class="list-disc list-inside space-y-1 text-sm">
                    @foreach($errors->all() as $error)
                        <li>{{ $error }}</li>
                    @endforeach
                </ul>
            </div>
        @endif

        {{-- Stats row --}}
        <div class="grid grid-cols-1 md:grid-cols-3 gap-4 mb-6">
            <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-4 flex items-center gap-4">
                <div class="w-12 h-12 rounded-full bg-green-100 flex items-center justify-center">
                    <i class="fas fa-image text-green-600 text-xl"></i>
                </div>
                <div>
                    <p class="text-xs text-gray-500 font-medium uppercase tracking-wide">Total Slots</p>
                    <p class="text-2xl font-bold text-gray-800">{{ $slots->count() }}</p>
                </div>
            </div>
            <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-4 flex items-center gap-4">
                <div class="w-12 h-12 rounded-full bg-blue-100 flex items-center justify-center">
                    <i class="fas fa-check-circle text-blue-600 text-xl"></i>
                </div>
                <div>
                    <p class="text-xs text-gray-500 font-medium uppercase tracking-wide">With Custom Image</p>
                    <p class="text-2xl font-bold text-gray-800">{{ $slots->whereNotNull('image_path')->count() }}</p>
                </div>
            </div>
            <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-4 flex items-center gap-4">
                <div class="w-12 h-12 rounded-full bg-yellow-100 flex items-center justify-center">
                    <i class="fas fa-clock text-yellow-600 text-xl"></i>
                </div>
                <div>
                    <p class="text-xs text-gray-500 font-medium uppercase tracking-wide">Using Default</p>
                    <p class="text-2xl font-bold text-gray-800">{{ $slots->whereNull('image_path')->count() }}</p>
                </div>
            </div>
        </div>

        {{-- Card Grid --}}
        <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
            @foreach($slots as $slot)
                <div class="bg-white rounded-lg shadow-sm border border-gray-200 overflow-hidden flex flex-col">

                    {{-- Image Preview --}}
                    <div class="relative w-full h-48 bg-gray-100 overflow-hidden">
                        @if($slot->image_path)
                            <img src="{{ asset('storage/' . $slot->image_path) }}"
                                 alt="{{ $slot->card_title }}"
                                 class="w-full h-full object-cover"
                                 onerror="this.style.display='none'; this.nextElementSibling.style.display='flex'">
                            <div class="hidden w-full h-full items-center justify-center flex-col text-gray-400">
                                <i class="fas fa-image text-4xl mb-2"></i>
                                <span class="text-sm">Image not found</span>
                            </div>
                            <span class="absolute top-2 right-2 inline-flex items-center gap-1 px-2 py-0.5 rounded-full text-xs font-semibold bg-green-500 text-white">
                                <i class="fas fa-check" style="font-size:9px"></i> Custom
                            </span>
                        @else
                            <div class="w-full h-full flex items-center justify-center flex-col text-gray-400">
                                <i class="fas fa-image text-4xl mb-2"></i>
                                <span class="text-sm">Using default image</span>
                            </div>
                            <span class="absolute top-2 right-2 inline-flex items-center px-2 py-0.5 rounded-full text-xs font-semibold bg-gray-400 text-white">
                                Default
                            </span>
                        @endif
                    </div>

                    {{-- Card Body --}}
                    <div class="p-5 flex flex-col flex-1">
                        {{-- Slot number + title --}}
                        <div class="flex items-center gap-2 mb-4">
                            <span class="inline-flex items-center justify-center w-7 h-7 rounded-full bg-green-100 text-green-700 text-xs font-bold flex-shrink-0">
                                {{ $slot->slot }}
                            </span>
                            <h3 class="text-sm font-semibold text-gray-800 leading-snug">{{ $slot->card_title }}</h3>
                        </div>

                        {{-- Upload Form --}}
                        <form action="{{ route('admin.what-we-do-images.update', $slot->slot) }}"
                              method="POST"
                              enctype="multipart/form-data"
                              class="mt-auto">
                            @csrf
                            <input type="hidden" name="slot" value="{{ $slot->slot }}">

                            <div class="mb-3">
                                <label for="image_{{ $slot->slot }}"
                                       class="block text-xs font-semibold text-gray-700 mb-1">
                                    Upload New Image
                                    <span class="text-gray-400 font-normal ml-1">(JPEG, PNG, WebP · max 2MB)</span>
                                </label>
                                <input type="file"
                                       id="image_{{ $slot->slot }}"
                                       name="image"
                                       accept="image/jpeg,image/png,image/webp"
                                       class="block w-full text-sm text-gray-500
                                              file:mr-3 file:py-1.5 file:px-3
                                              file:rounded-lg file:border-0
                                              file:text-xs file:font-semibold
                                              file:bg-green-50 file:text-green-700
                                              hover:file:bg-green-100 transition">
                                @if($errors->has('image') && old('slot') == $slot->slot)
                                    <p class="mt-1 text-xs text-red-600">{{ $errors->first('image') }}</p>
                                @endif
                            </div>

                            <button type="submit"
                                    class="w-full inline-flex items-center justify-center gap-2 px-4 py-2 bg-[#1a4731] text-white text-sm font-semibold rounded-lg hover:bg-[#276749] transition">
                                <i class="fas fa-upload"></i> Upload Image
                            </button>
                        </form>
                    </div>

                </div>
            @endforeach
        </div>

    </div>
</x-app-layout>
