@extends('layouts.app')

@section('title', 'Ready Stock Japanese Reconditioned Cars in Dhaka Showroom | MotoSolution Bangladesh')
@section('meta_description', 'Explore our verified ready stock Japanese inventory available for immediate delivery in Niketan, Gulshan, Dhaka. Verified auction grade 4.5+ Toyotas, Hondas, and Nissans.')

@section('content')
<!-- Header Banner -->
<div class="bg-[#0B0F19] text-white py-12 border-b border-white/10">
    <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 flex flex-col md:flex-row md:items-center justify-between gap-4">
        <div>
            <div class="inline-flex items-center gap-2 px-3 py-1 rounded-full bg-emerald-500/20 text-emerald-400 text-xs font-bold mb-2 border border-emerald-500/30">
                <i class="fa-solid fa-circle-check"></i> Immediate Handover in Dhaka
            </div>
            <h1 class="text-3xl sm:text-4xl font-extrabold font-heading">Ready Stock Showroom Inventory</h1>
            <p class="text-slate-400 text-sm mt-1">Inspected, reconditioned, and ready for immediate BRTA registration and delivery.</p>
        </div>
        <div class="text-xs text-slate-400">
            Showing <span class="font-bold text-white">{{ $vehicles->total() }}</span> Vehicles in Stock
        </div>
    </div>
</div>

<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-10">
    <div class="grid grid-cols-1 lg:grid-cols-4 gap-8">
        
        <!-- Left Filter Sidebar -->
        <div class="lg:col-span-1">
            <div class="bg-white p-6 rounded-2xl border border-slate-200 shadow-sm sticky top-24 space-y-6">
                <div class="flex items-center justify-between pb-3 border-b border-slate-100">
                    <h3 class="font-bold text-slate-900 text-sm uppercase tracking-wider flex items-center gap-2">
                        <i class="fa-solid fa-filter text-red-600"></i> Filters
                    </h3>
                    <a href="{{ route('inventory.index') }}" class="text-xs text-red-600 font-bold hover:underline">Reset</a>
                </div>

                <form method="GET" action="{{ route('inventory.index') }}" class="space-y-4 text-xs">
                    <!-- Brand -->
                    <div>
                        <label class="block font-bold text-slate-600 uppercase mb-1">Make</label>
                        <select name="make" onchange="this.form.submit()" class="w-full bg-slate-50 border border-slate-200 rounded-lg p-2 font-medium focus:border-red-600 focus:outline-none">
                            <option value="">All Makes</option>
                            @foreach($makes as $m)
                                <option value="{{ $m }}" {{ request('make') === $m ? 'selected' : '' }}>{{ $m }}</option>
                            @endforeach
                        </select>
                    </div>

                    <!-- Body Type -->
                    <div>
                        <label class="block font-bold text-slate-600 uppercase mb-1">Body Type</label>
                        <select name="body_type" onchange="this.form.submit()" class="w-full bg-slate-50 border border-slate-200 rounded-lg p-2 font-medium focus:border-red-600 focus:outline-none">
                            <option value="">All Types</option>
                            @foreach($bodyTypes as $bt)
                                <option value="{{ $bt }}" {{ request('body_type') === $bt ? 'selected' : '' }}>{{ $bt }}</option>
                            @endforeach
                        </select>
                    </div>

                    <!-- Year -->
                    <div>
                        <label class="block font-bold text-slate-600 uppercase mb-1">Year</label>
                        <select name="year" onchange="this.form.submit()" class="w-full bg-slate-50 border border-slate-200 rounded-lg p-2 font-medium focus:border-red-600 focus:outline-none">
                            <option value="">Any Year</option>
                            @foreach($years as $yr)
                                <option value="{{ $yr }}" {{ request('year') == $yr ? 'selected' : '' }}>{{ $yr }}</option>
                            @endforeach
                        </select>
                    </div>

                    <!-- Fuel Type -->
                    <div>
                        <label class="block font-bold text-slate-600 uppercase mb-1">Fuel Type</label>
                        <select name="fuel_type" onchange="this.form.submit()" class="w-full bg-slate-50 border border-slate-200 rounded-lg p-2 font-medium focus:border-red-600 focus:outline-none">
                            <option value="">All Fuels</option>
                            @foreach($fuelTypes as $ft)
                                <option value="{{ $ft }}" {{ request('fuel_type') === $ft ? 'selected' : '' }}>{{ $ft }}</option>
                            @endforeach
                        </select>
                    </div>

                    <!-- Auction Grade -->
                    <div>
                        <label class="block font-bold text-slate-600 uppercase mb-1">Auction Grade</label>
                        <select name="grade" onchange="this.form.submit()" class="w-full bg-slate-50 border border-slate-200 rounded-lg p-2 font-medium focus:border-red-600 focus:outline-none">
                            <option value="">Any Grade</option>
                            @foreach($grades as $g)
                                <option value="{{ $g }}" {{ request('grade') === $g ? 'selected' : '' }}>Grade {{ $g }}</option>
                            @endforeach
                        </select>
                    </div>

                    <!-- Max Price -->
                    <div>
                        <label class="block font-bold text-slate-600 uppercase mb-1">Max Price (BDT)</label>
                        <select name="price_max" onchange="this.form.submit()" class="w-full bg-slate-50 border border-slate-200 rounded-lg p-2 font-medium focus:border-red-600 focus:outline-none">
                            <option value="">No Maximum</option>
                            <option value="3000000" {{ request('price_max') == '3000000' ? 'selected' : '' }}>Under ৳30 Lac</option>
                            <option value="5000000" {{ request('price_max') == '5000000' ? 'selected' : '' }}>Under ৳50 Lac</option>
                            <option value="7000000" {{ request('price_max') == '7000000' ? 'selected' : '' }}>Under ৳70 Lac</option>
                            <option value="10000000" {{ request('price_max') == '10000000' ? 'selected' : '' }}>Under ৳1.00 Crore</option>
                        </select>
                    </div>

                    <button type="submit" class="w-full py-2.5 bg-slate-900 text-white font-bold rounded-lg hover:bg-slate-800 transition">
                        Apply Filters
                    </button>
                </form>
            </div>
        </div>

        <!-- Right Vehicle Grid -->
        <div class="lg:col-span-3 space-y-6">
            
            <!-- Sorting Header Bar -->
            <div class="bg-white p-4 rounded-xl border border-slate-200 flex items-center justify-between text-xs">
                <span class="text-slate-500 font-medium">Found <strong class="text-slate-900">{{ $vehicles->total() }}</strong> verified vehicles</span>
                
                <div class="flex items-center gap-2">
                    <span class="text-slate-500">Sort By:</span>
                    <form method="GET" action="{{ route('inventory.index') }}">
                        @foreach(request()->except('sort') as $key => $val)
                            <input type="hidden" name="{{ $key }}" value="{{ $val }}">
                        @endforeach
                        <select name="sort" onchange="this.form.submit()" class="bg-slate-50 border border-slate-200 rounded-lg p-1.5 font-bold text-slate-800 focus:outline-none">
                            <option value="newest" {{ request('sort') === 'newest' ? 'selected' : '' }}>Newest Arrivals</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>
                            <option value="mileage_asc" {{ request('sort') === 'mileage_asc' ? 'selected' : '' }}>Lowest Mileage</option>
                            <option value="year_desc" {{ request('sort') === 'year_desc' ? 'selected' : '' }}>Latest Model Year</option>
                        </select>
                    </form>
                </div>
            </div>

            <!-- Vehicles Grid -->
            @if($vehicles->isEmpty())
                <div class="p-12 text-center bg-white rounded-2xl border border-slate-200 space-y-3">
                    <i class="fa-solid fa-car-side text-4xl text-slate-300"></i>
                    <h3 class="text-base font-bold text-slate-700">No Ready Stock vehicles match your filters</h3>
                    <p class="text-xs text-slate-500">Try adjusting your filters or browse upcoming Japan Live Auctions.</p>
                    <a href="{{ route('inventory.index') }}" class="inline-block py-2 px-4 bg-red-600 text-white rounded-lg text-xs font-bold">Clear Filters</a>
                </div>
            @else
                <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
                    @foreach($vehicles as $car)
                        <div class="bg-white rounded-2xl border border-slate-200 shadow-sm hover:shadow-xl transition-all overflow-hidden flex flex-col justify-between">
                            <div>
                                <div class="relative aspect-[16/10] overflow-hidden group">
                                    <img src="{{ $car->main_image }}" alt="{{ $car->full_name }}" class="w-full h-full object-cover group-hover:scale-105 transition duration-500">
                                    
                                    <div class="absolute top-3 left-3">
                                        <span class="bg-emerald-600 text-white text-[10px] font-black px-2 py-0.5 rounded shadow">
                                            Ready Stock
                                        </span>
                                    </div>
                                    <div class="absolute top-3 right-3">
                                        <span class="bg-slate-900/80 backdrop-blur-md text-amber-300 text-[11px] font-black px-2 py-0.5 rounded">
                                            Grade {{ $car->auction_grade }}
                                        </span>
                                    </div>
                                    <div class="absolute bottom-2.5 left-3">
                                        <span class="bg-black/60 text-white text-[10px] px-2 py-0.5 rounded font-mono">
                                            {{ $car->stock_number }}
                                        </span>
                                    </div>
                                </div>

                                <div class="p-4">
                                    <h3 class="font-bold text-slate-900 text-base leading-snug">
                                        <a href="{{ route('inventory.show', $car->slug) }}" class="hover:text-red-600 transition">
                                            {{ $car->full_name }}
                                        </a>
                                    </h3>

                                    <div class="flex flex-wrap gap-1.5 mt-2.5 text-[11px] text-slate-500">
                                        <span class="bg-slate-100 px-2 py-0.5 rounded"><i class="fa-solid fa-gauge mr-1"></i>{{ $car->formatted_mileage }}</span>
                                        <span class="bg-slate-100 px-2 py-0.5 rounded"><i class="fa-solid fa-gas-pump mr-1"></i>{{ $car->fuel_type }}</span>
                                        <span class="bg-slate-100 px-2 py-0.5 rounded"><i class="fa-solid fa-gears mr-1"></i>{{ $car->engine_cc }}cc</span>
                                    </div>

                                    <div class="mt-4 pt-3 border-t border-slate-100 flex items-baseline justify-between">
                                        <div>
                                            <span class="text-[9px] uppercase font-bold text-slate-400 block">Showroom Price</span>
                                            <span class="text-lg font-extrabold text-slate-900">{{ $car->formatted_price }}</span>
                                        </div>
                                        @if($car->discount_bdt > 0)
                                            <span class="text-[10px] font-bold text-emerald-600 bg-emerald-50 px-1.5 py-0.5 rounded">Discounted</span>
                                        @endif
                                    </div>
                                </div>
                            </div>

                            <div class="p-4 pt-0 grid grid-cols-2 gap-2">
                                <a href="{{ route('inventory.show', $car->slug) }}" class="py-2 px-3 bg-slate-100 hover:bg-slate-200 text-slate-800 text-xs font-bold rounded-lg text-center transition">
                                    Details
                                </a>
                                <a href="https://wa.me/{{ config('motosolution.contact.whatsapp') }}?text=Interested%20in%20Ready%20Stock%20{{ urlencode($car->full_name) }}%20({{ $car->stock_number }})." target="_blank" class="py-2 px-3 bg-emerald-600 hover:bg-emerald-500 text-white text-xs font-bold rounded-lg text-center shadow transition flex items-center justify-center gap-1">
                                    <i class="fa-brands fa-whatsapp"></i> Chat
                                </a>
                            </div>
                        </div>
                    @endforeach
                </div>

                <!-- Pagination -->
                <div class="pt-6">
                    {{ $vehicles->links() }}
                </div>
            @endif
        </div>
    </div>
</div>
@endsection
