@extends('layouts.app')
@section('title', 'Shop Parts - AutoPart')

@section('content')
<div class="bg-gray-50 min-h-screen">
    {{-- Page Header --}}
    <div class="bg-brand-dark text-white py-12 mb-8">
        <div class="container mx-auto px-4 text-center">
            <h1 class="text-4xl font-extrabold uppercase tracking-widest mb-4">Shop Parts</h1>
            <div class="flex items-center justify-center space-x-2 text-sm text-gray-400">
                <a href="{{ url('/') }}" class="hover:text-brand-red transition-colors">Home</a>
                <i data-lucide="chevron-right" class="w-3.5 h-3.5"></i>
                <span class="text-white">Shop</span>
            </div>
        </div>
    </div>

    <div class="container mx-auto px-4 pb-16">
        <div class="flex flex-col lg:flex-row gap-8">
            {{-- Sidebar Filters --}}
            <div class="w-full lg:w-1/4">
                <form method="GET" action="{{ url('/shop') }}" id="filter-form">
                    @if(request('search'))
                        <input type="hidden" name="search" value="{{ request('search') }}">
                    @endif

                    {{-- Categories --}}
                    <div class="bg-white p-6 rounded-lg shadow-sm border border-gray-100 mb-6">
                        <h3 class="text-lg font-bold text-brand-dark uppercase tracking-wide mb-4 border-b border-gray-100 pb-3">Categories</h3>
                        <ul class="space-y-3">
                            @foreach(['Engine Parts', 'Body Parts', 'Interior Accessories', 'Lighting', 'Wheels & Tires', 'Brakes & Rotors', 'Maintenance', 'Electrical'] as $cat)
                            <li>
                                <a href="{{ url('/shop?category=' . urlencode($cat) . (request('search') ? '&search=' . request('search') : '')) }}"
                                   class="flex justify-between items-center cursor-pointer transition-colors group {{ request('category') === $cat ? 'text-brand-red font-bold' : 'text-gray-600 hover:text-brand-red' }}">
                                    <span class="group-hover:translate-x-1 transition-transform">{{ $cat }}</span>
                                </a>
                            </li>
                            @endforeach
                        </ul>
                        @if(request('category'))
                            <a href="{{ url('/shop' . (request('search') ? '?search=' . request('search') : '')) }}" class="text-xs text-brand-red font-bold mt-4 block hover:underline">✕ Clear category filter</a>
                        @endif
                    </div>

                    {{-- Brands --}}
                    <div class="bg-white p-6 rounded-lg shadow-sm border border-gray-100 mb-6">
                        <h3 class="text-lg font-bold text-brand-dark uppercase tracking-wide mb-4 border-b border-gray-100 pb-3">Brands</h3>
                        <ul class="space-y-3">
                            @foreach($brands as $brand)
                            <li class="flex items-center space-x-3">
                                <span class="text-gray-600 text-sm">{{ $brand }}</span>
                            </li>
                            @endforeach
                        </ul>
                    </div>

                    {{-- Promo Widget --}}
                    <div class="bg-brand-dark rounded-lg p-6 text-center text-white relative overflow-hidden group">
                        <div class="absolute inset-0 bg-brand-red opacity-0 group-hover:opacity-90 transition-opacity duration-300 z-0"></div>
                        <div class="relative z-10">
                            <h4 class="text-brand-red font-bold text-sm tracking-widest uppercase mb-2 group-hover:text-white transition-colors">Special Offer</h4>
                            <p class="text-2xl font-extrabold mb-4">SAVE 20% ON BRAKES</p>
                            <a href="{{ url('/shop?category=Brakes+%26+Rotors') }}" class="bg-white text-brand-dark px-6 py-2 rounded-full font-bold text-sm uppercase hover:bg-black hover:text-white transition-colors inline-block">Shop Now</a>
                        </div>
                    </div>
                </form>
            </div>

            {{-- Main Content --}}
            <div class="flex-1">
                {{-- Top Bar --}}
                <div class="bg-white p-4 rounded-lg shadow-sm border border-gray-100 mb-6 flex flex-col sm:flex-row justify-between items-center">
                    <p class="text-sm text-gray-500 mb-4 sm:mb-0">
                        Showing {{ $products->firstItem() ?? 0 }}–{{ $products->lastItem() ?? 0 }} of {{ $products->total() }} results
                        @if(request('category'))
                            in <span class="font-bold text-brand-red">{{ request('category') }}</span>
                        @endif
                        @if(request('search'))
                            for "<span class="font-bold text-brand-red">{{ request('search') }}</span>"
                        @endif
                    </p>
                    <div class="flex items-center space-x-2 text-sm">
                        <span class="text-gray-500 font-medium">Sort By:</span>
                        <select onchange="window.location.href=this.value" class="border border-gray-200 rounded px-3 py-1.5 text-sm outline-none focus:border-brand-red cursor-pointer">
                            <option value="{{ request()->fullUrlWithQuery(['sort' => 'latest']) }}" {{ request('sort','latest') == 'latest' ? 'selected' : '' }}>Latest</option>
                            <option value="{{ request()->fullUrlWithQuery(['sort' => 'price_low']) }}" {{ request('sort') == 'price_low' ? 'selected' : '' }}>Price: Low to High</option>
                            <option value="{{ request()->fullUrlWithQuery(['sort' => 'price_high']) }}" {{ request('sort') == 'price_high' ? 'selected' : '' }}>Price: High to Low</option>
                            <option value="{{ request()->fullUrlWithQuery(['sort' => 'name']) }}" {{ request('sort') == 'name' ? 'selected' : '' }}>Name A-Z</option>
                        </select>
                    </div>
                </div>

                {{-- Product Grid --}}
                @if($products->count() > 0)
                <div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-6">
                    @foreach($products as $product)
                        @include('partials.product-card', ['product' => $product])
                    @endforeach
                </div>
                @else
                <div class="text-center py-12 text-gray-500 bg-white border border-gray-100 rounded-lg">
                    <p class="text-lg font-bold mb-2">No products found</p>
                    <p>Try adjusting your filters or search criteria.</p>
                </div>
                @endif

                {{-- Pagination --}}
                @if($products->hasPages())
                <div class="mt-12 flex justify-center">
                    {{ $products->withQueryString()->links() }}
                </div>
                @endif
            </div>
        </div>
    </div>
</div>
@endsection
