@extends('layouts.app')

@section('title', $activeCategory ? $activeCategory->name : 'Shop All Products')

@section('content')

<div class="hhd-breadcrumb">
    <div class="container small">
        <a href="{{ route('home') }}" class="text-muted-soft">Home</a> /
        <span class="text-charcoal">{{ $activeCategory ? $activeCategory->name : 'Shop' }}</span>
    </div>
</div>

<div class="container py-5">
    <div class="row">
        <div class="col-lg-3 mb-4">
            <form action="{{ route('shop.index') }}" method="GET">
                @if(request('search'))
                    <input type="hidden" name="search" value="{{ request('search') }}">
                @endif

                <h5 class="font-serif mb-3">Categories</h5>
                <ul class="list-unstyled mb-4">
                    <li class="mb-2">
                        <a href="{{ route('shop.index') }}" class="{{ !$activeCategory ? 'text-terracotta fw-semibold' : 'text-charcoal' }}">All Products</a>
                    </li>
                    @foreach($categories as $cat)
                        <li class="mb-2">
                            <a href="{{ route('shop.index', ['category' => $cat->slug]) }}" class="{{ $activeCategory && $activeCategory->id === $cat->id ? 'text-terracotta fw-semibold' : 'text-charcoal' }}">{{ $cat->name }}</a>
                        </li>
                    @endforeach
                </ul>

                <h5 class="font-serif mb-3">Price Range</h5>
                <div class="row g-2 mb-3">
                    <div class="col-6">
                        <input type="number" name="min_price" value="{{ request('min_price') }}" class="form-control form-control-sm" placeholder="Min">
                    </div>
                    <div class="col-6">
                        <input type="number" name="max_price" value="{{ request('max_price') }}" class="form-control form-control-sm" placeholder="Max">
                    </div>
                </div>
                @if($activeCategory)
                    <input type="hidden" name="category" value="{{ $activeCategory->slug }}">
                @endif
                <button type="submit" class="btn btn-outline-terracotta btn-sm w-100">Apply Filters</button>
            </form>
        </div>

        <div class="col-lg-9">
            <div class="d-flex flex-wrap justify-content-between align-items-center mb-4 gap-2">
                <p class="mb-0 text-muted-soft small">{{ $products->total() }} products found</p>
                <form action="{{ route('shop.index') }}" method="GET" class="d-flex align-items-center gap-2">
                    @if($activeCategory)<input type="hidden" name="category" value="{{ $activeCategory->slug }}">@endif
                    @if(request('search'))<input type="hidden" name="search" value="{{ request('search') }}">@endif
                    <label class="small text-muted-soft mb-0">Sort:</label>
                    <select name="sort" class="form-select form-select-sm" style="width:auto;" onchange="this.form.submit()">
                        <option value="newest" {{ request('sort', 'newest') === 'newest' ? 'selected' : '' }}>Newest</option>
                        <option value="price_asc" {{ request('sort') === 'price_asc' ? 'selected' : '' }}>Price: Low to High</option>
                        <option value="price_desc" {{ request('sort') === 'price_desc' ? 'selected' : '' }}>Price: High to Low</option>
                    </select>
                </form>
            </div>

            @if($products->isEmpty())
                <div class="text-center py-5">
                    <i class="bi bi-box-seam fs-1 text-muted-soft"></i>
                    <p class="mt-3 text-muted-soft">No products found. Try adjusting your filters.</p>
                </div>
            @else
                <div class="row g-4">
                    @foreach($products as $product)
                        <div class="col-6 col-md-4">
                            @include('partials.product-card', ['product' => $product])
                        </div>
                    @endforeach
                </div>

                <div class="mt-5">
                    {{ $products->links() }}
                </div>
            @endif
        </div>
    </div>
</div>

@endsection
