<x-app-layout
    :title="$category->name . ' for Sale'"
    :description="$category->description
        ? \Illuminate\Support\Str::limit($category->description, 155)
        : 'Browse ' . $category->name . ' listings. ' . $items->total() . ' items available.'"
>
    <x-slot name="header">
        <div class="flex justify-between items-center">
            <div class="flex items-center space-x-4">
                @if($category->icon)
                    <span class="text-3xl">{{ $category->icon }}</span>
                @endif
                <div>
                    <h2 class="font-semibold text-xl text-gray-800 leading-tight">
                        {{ $category->name }}
                    </h2>
                    @if($category->description)
                        <p class="text-gray-600 text-sm">{{ $category->description }}</p>
                    @endif
                </div>
            </div>
            <a href="{{ route('categories.index') }}" class="inline-flex items-center px-3 py-2 bg-gray-300 border border-transparent rounded-md font-semibold text-xs text-gray-700 uppercase tracking-widest hover:bg-gray-400 transition ease-in-out duration-150">
                All Categories
            </a>
        </div>
    </x-slot>

    <div class="py-12">
        <div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
            <div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
                <div class="p-6">
                    @if($items->count() > 0)
                        <div class="mb-6 flex justify-between items-center">
                            <p class="text-gray-600">{{ $items->total() }} items in {{ $category->name }}</p>
                            <a href="{{ route('items.index', ['category' => $category->id]) }}" class="text-blue-600 hover:text-blue-700 text-sm">
                                View with filters →
                            </a>
                        </div>

                        <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-6">
                            @foreach($items as $item)
                                <div class="group">
                                    <a href="{{ route('items.show', $item) }}" class="block">
                                        <div class="aspect-square bg-gray-200 rounded-lg overflow-hidden mb-3 relative">
                                            @if($item->primaryPhoto)
                                                <img src="{{ asset($item->primaryPhoto->url) }}" alt="{{ $item->title }}" class="w-full h-full object-cover group-hover:scale-105 transition-transform duration-200">
                                            @else
                                                <div class="w-full h-full flex items-center justify-center">
                                                    <span class="text-gray-400">No Image</span>
                                                </div>
                                            @endif
                                            
                                            <!-- Status Badge -->
                                            @if($item->status === 'sale_pending')
                                                <div class="absolute top-2 left-2 bg-orange-500 text-white text-xs px-2 py-1 rounded">
                                                    Sale Pending
                                                </div>
                                            @endif
                                            
                                            <!-- Listing Type Badge -->
                                            @if($item->listing_type === 'trade')
                                                <div class="absolute top-2 right-2 bg-green-500 text-white text-xs px-2 py-1 rounded">
                                                    Trade
                                                </div>
                                            @endif
                                        </div>
                                        
                                        <h3 class="font-semibold text-gray-900 group-hover:text-blue-600 line-clamp-2 mb-1">{{ $item->title }}</h3>
                                        
                                        @if($item->price && in_array($item->listing_type, ['sale', 'inventory_open']))
                                            <p class="font-bold text-lg text-gray-900">${{ number_format($item->price, 2) }}</p>
                                        @else
                                            <p class="font-bold text-lg text-green-600">Trade Only</p>
                                        @endif
                                        
                                        <div class="flex items-center justify-between text-xs text-gray-500 mt-2">
                                            <span>{{ $item->user->name }}</span>
                                            <span>{{ $item->created_at->diffForHumans() }}</span>
                                        </div>
                                        
                                        @if($item->location_city || $item->location_state)
                                            <p class="text-xs text-gray-500 mt-1">
                                                {{ collect([$item->location_city, $item->location_state])->filter()->join(', ') }}
                                            </p>
                                        @endif
                                    </a>
                                </div>
                            @endforeach
                        </div>

                        <!-- Pagination -->
                        <div class="mt-8">
                            {{ $items->links() }}
                        </div>
                    @else
                        <div class="text-center py-12">
                            @if($category->icon)
                                <div class="text-6xl mb-4">{{ $category->icon }}</div>
                            @else
                                <svg class="mx-auto h-12 w-12 text-gray-400" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 11H5m14 0a2 2 0 012 2v6a2 2 0 01-2 2H5a2 2 0 01-2-2v-6a2 2 0 012-2m14 0V9a2 2 0 00-2-2M5 11V9a2 2 0 012-2m0 0V5a2 2 0 012-2h6a2 2 0 012 2v2M7 7h10"></path>
                                </svg>
                            @endif
                            <h3 class="text-lg font-semibold text-gray-900 mb-2 mt-4">No items in {{ $category->name }}</h3>
                            <p class="text-gray-600 mb-6">Be the first to list an item in this category!</p>
                            
                            @auth
                                <div class="space-x-4">
                                    <a href="{{ route('my-items.create') }}" class="inline-flex items-center px-4 py-2 bg-blue-600 border border-transparent rounded-md font-semibold text-xs text-white uppercase tracking-widest hover:bg-blue-700 transition ease-in-out duration-150">
                                        List an Item
                                    </a>
                                    <a href="{{ route('items.index') }}" class="inline-flex items-center px-4 py-2 bg-gray-300 border border-transparent rounded-md font-semibold text-xs text-gray-700 uppercase tracking-widest hover:bg-gray-400 transition ease-in-out duration-150">
                                        Browse All Items
                                    </a>
                                </div>
                            @else
                                <div class="space-x-4">
                                    <a href="{{ route('register') }}" class="inline-flex items-center px-4 py-2 bg-blue-600 border border-transparent rounded-md font-semibold text-xs text-white uppercase tracking-widest hover:bg-blue-700 transition ease-in-out duration-150">
                                        Join to List Items
                                    </a>
                                    <a href="{{ route('items.index') }}" class="inline-flex items-center px-4 py-2 bg-gray-300 border border-transparent rounded-md font-semibold text-xs text-gray-700 uppercase tracking-widest hover:bg-gray-400 transition ease-in-out duration-150">
                                        Browse All Items
                                    </a>
                                </div>
                            @endauth
                        </div>
                    @endif
                </div>
            </div>
        </div>
    </div>
</x-app-layout>