@extends('layouts.admin')

@section('title', 'Ready Stock Inventory – Admin CRM')
@section('page_title', 'Showroom Ready Stock Management')

@section('content')
<div class="space-y-6">
    
    <!-- Top Action & Filter Header -->
    <div class="bg-slate-900 border border-white/10 rounded-2xl p-4 flex flex-col sm:flex-row items-center justify-between gap-4 shadow-xl">
        <div class="flex items-center gap-2 w-full sm:w-auto">
            <form method="GET" action="{{ route('admin.inventory.index') }}" class="flex items-center gap-2 flex-1 sm:flex-initial">
                <div class="relative">
                    <i class="fa-solid fa-magnifying-glass absolute left-3 top-2.5 text-slate-400 text-xs"></i>
                    <input type="text" name="search" value="{{ request('search') }}" placeholder="Search stock #, model, chassis..." class="bg-slate-950 border border-white/10 rounded-xl pl-8 pr-3 py-1.5 text-white text-xs focus:border-red-500 focus:outline-none">
                </div>
                <select name="status" class="bg-slate-950 border border-white/10 rounded-xl px-3 py-1.5 text-white text-xs">
                    <option value="">All Statuses</option>
                    <option value="available" {{ request('status') === 'available' ? 'selected' : '' }}>Available</option>
                    <option value="reserved" {{ request('status') === 'reserved' ? 'selected' : '' }}>Reserved</option>
                    <option value="sold" {{ request('status') === 'sold' ? 'selected' : '' }}>Sold</option>
                </select>
                <button type="submit" class="px-3 py-1.5 rounded-xl bg-white/10 hover:bg-white/20 text-white font-bold text-xs transition">
                    Filter
                </button>
            </form>
        </div>

        <a href="{{ route('admin.inventory.create') }}" class="w-full sm:w-auto px-4 py-2 rounded-xl bg-red-600 hover:bg-red-700 text-white font-bold text-xs shadow-lg transition flex items-center justify-center gap-2">
            <i class="fa-solid fa-plus"></i>
            <span>Add Ready Stock Vehicle</span>
        </a>
    </div>

    <!-- Vehicles Table -->
    <div class="bg-slate-900 border border-white/10 rounded-2xl overflow-hidden shadow-xl">
        <div class="overflow-x-auto">
            <table class="w-full text-left text-xs">
                <thead class="bg-slate-950 text-slate-400 uppercase tracking-wider text-[10px] border-b border-white/10">
                    <tr>
                        <th class="p-4">Vehicle Details</th>
                        <th class="p-4">Stock & Chassis #</th>
                        <th class="p-4">Specs</th>
                        <th class="p-4">Price (BDT)</th>
                        <th class="p-4">Grade</th>
                        <th class="p-4">Status</th>
                        <th class="p-4 text-right">Actions</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-white/5 text-slate-300">
                    @forelse($vehicles as $vehicle)
                        <tr class="hover:bg-white/5 transition">
                            <td class="p-4">
                                <div class="flex items-center gap-3">
                                    <div class="w-14 h-11 rounded-lg overflow-hidden bg-slate-800 shrink-0">
                                        <img src="{{ $vehicle->main_image }}" alt="Car" class="w-full h-full object-cover">
                                    </div>
                                    <div>
                                        <a href="{{ route('inventory.show', $vehicle->slug ?? $vehicle->id) }}" target="_blank" class="font-bold text-white hover:text-red-400">
                                            {{ $vehicle->full_name }}
                                        </a>
                                        <span class="text-[10px] text-slate-400 block">{{ $vehicle->variant ?? $vehicle->body_type }} · {{ $vehicle->color }}</span>
                                    </div>
                                </div>
                            </td>
                            <td class="p-4 font-mono text-[11px]">
                                <span class="text-red-400 font-bold block">{{ $vehicle->stock_number }}</span>
                                <span class="text-slate-400">{{ $vehicle->chassis_number }}</span>
                            </td>
                            <td class="p-4 text-slate-300">
                                {{ number_format($vehicle->mileage) }} km<br>
                                <span class="text-[10px] text-slate-500">{{ $vehicle->engine_cc }}cc · {{ $vehicle->fuel_type }}</span>
                            </td>
                            <td class="p-4">
                                <span class="font-extrabold text-white text-sm">৳{{ number_format($vehicle->final_price_bdt) }}</span>
                                @if($vehicle->discount_bdt > 0)
                                    <span class="text-[10px] text-rose-400 block line-through">৳{{ number_format($vehicle->selling_price_bdt) }}</span>
                                @endif
                            </td>
                            <td class="p-4 font-bold text-amber-400">
                                {{ $vehicle->auction_grade }}
                            </td>
                            <td class="p-4">
                                @php
                                    $badge = match($vehicle->status) {
                                        'available' => 'bg-emerald-500/20 text-emerald-400',
                                        'reserved' => 'bg-amber-500/20 text-amber-400',
                                        'sold' => 'bg-slate-700 text-slate-400',
                                        default => 'bg-blue-500/20 text-blue-400'
                                    };
                                @endphp
                                <span class="px-2.5 py-0.5 rounded-full text-[10px] font-bold uppercase {{ $badge }}">
                                    {{ $vehicle->status }}
                                </span>
                            </td>
                            <td class="p-4 text-right">
                                <div class="flex items-center justify-end gap-2">
                                    <a href="{{ route('admin.inventory.edit', $vehicle->id) }}" class="px-2.5 py-1.5 rounded-lg bg-white/10 hover:bg-white/20 text-white font-bold transition">
                                        <i class="fa-solid fa-pen-to-square"></i>
                                    </a>
                                    <form method="POST" action="{{ route('admin.inventory.destroy', $vehicle->id) }}" onsubmit="return confirm('Delete this vehicle from showroom inventory?')">
                                        @csrf
                                        @method('DELETE')
                                        <button type="submit" class="px-2.5 py-1.5 rounded-lg bg-rose-500/20 hover:bg-rose-500/40 text-rose-300 transition">
                                            <i class="fa-solid fa-trash"></i>
                                        </button>
                                    </form>
                                </div>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="7" class="p-8 text-center text-slate-400">
                                No ready stock vehicles found. Click "Add Ready Stock Vehicle" to create one.
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        <div class="p-4 border-t border-white/10">
            {{ $vehicles->links() }}
        </div>
    </div>

</div>
@endsection
