<?php
use Livewire\Component;
use App\Models\Gallery;
?>

<div class="max-content">
    <header class="flex justify-between items-center mb-10">
        <div>
            <h1 class="text-2xl font-semibold tracking-tight">Gallery Archive</h1>
            <p class="text-sm text-gray-500 mt-1">Manage visual moments and community highlights.</p>
        </div>
        <a href="{{ route('admin.gallery.create') }}" wire:navigate class="btn-primary" style="background-color: #99BC85; color: white; padding: 8px 16px; border-radius: 6px; font-size: 13px; font-weight: 500; display: inline-flex; align-items: center; gap: 8px;">
            <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="M12 4v16m8-8H4"></path></svg>
            Add to Gallery
        </a>
    </header>

    @if (session()->has('message'))
        <div class="bg-green-100 border border-green-400 text-green-700 px-4 py-3 rounded relative mb-4" role="alert">
            <span class="block sm:inline">{{ session('message') }}</span>
        </div>
    @endif

    <div class="card">
        <div class="overflow-x-auto">
            <table>
                <thead>
                    <tr>
                        <th width="80">Preview</th>
                        <th>Title</th>
                        <th>Category</th>
                        <th class="text-right">Actions</th>
                    </tr>
                </thead>
                <tbody class="bg-white divide-y divide-gray-200">
                    @forelse($items as $item)
                        <tr>
                            <td class="px-6 py-4">
                                <div class="w-12 h-12 rounded-lg overflow-hidden border border-gray-100 bg-gray-50">
                                    <img src="{{ Str::startsWith($item->image_path, 'http') ? $item->image_path : asset('storage/' . $item->image_path) }}" class="w-full h-full object-cover">
                                </div>
                            </td>
                            <td class="px-6 py-4 font-medium text-sm text-gray-900">{{ $item?->title }}</td>
                            <td class="px-6 py-4 text-sm text-gray-500">{{ $item?->category?->name }}</td>
                            <td class="px-6 py-4 text-right">
                                <button wire:click="delete({{ $item->id }})" wire:confirm="Are you sure?" wire:loading.attr="disabled" class="text-red-500 font-semibold text-xs px-2">Remove</button>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="4" class="px-6 py-4 text-center text-gray-500 text-sm">No gallery items found.</td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>
</div>