<x-admin-layout>
    <div class="mb-6 flex flex-col md:flex-row justify-between items-center gap-4">
        <div>
            <h2 class="text-2xl font-bold text-slate-800">مخزون المركبات</h2>
            <p class="text-slate-500 text-sm">إدارة وعرض جميع المركبات في النظام</p>
        </div>
        <a href="{{ route('admin.vehicles.create') }}" class="inline-flex items-center justify-center rounded-xl bg-blue-600 px-4 py-2 text-sm font-semibold text-white shadow-sm hover:bg-blue-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-blue-600 transition-all">
            <x-icon name="Plus" class="ml-2 -mr-0.5 h-5 w-5" />
            إضافة مركبة جديدة
        </a>
    </div>

    <div class="mb-6 rounded-2xl bg-white p-4 shadow-sm border border-slate-100">
        <form method="GET" action="{{ route('admin.vehicles.index') }}" class="flex flex-col md:flex-row items-center gap-4">
            <div class="relative flex-1 w-full">
                <x-icon name="Search" class="absolute right-3 top-1/2 -translate-y-1/2 h-5 w-5 text-gray-400" />
                <input type="text" name="search" placeholder="ابحث عن الماركة، الموديل، رقم المخزون..." value="{{ request('search') }}" class="w-full rounded-xl border-gray-200 pr-10 focus:border-blue-500 focus:ring-blue-500">
            </div>
            <div class="w-full md:w-48">
                <select name="status" class="w-full rounded-xl border-gray-200 focus:border-blue-500 focus:ring-blue-500">
                    <option value="all">جميع الحالات</option>
                    <option value="AVAILABLE" {{ request('status') == 'AVAILABLE' ? 'selected' : '' }}>متاح</option>
                    <option value="SOLD" {{ request('status') == 'SOLD' ? 'selected' : '' }}>مباع</option>
                    <option value="RESERVED" {{ request('status') == 'RESERVED' ? 'selected' : '' }}>محجوز</option>
                    <option value="MAINTENANCE" {{ request('status') == 'MAINTENANCE' ? 'selected' : '' }}>صيانة</option>
                </select>
            </div>
            <button type="submit" class="w-full md:w-auto inline-flex items-center justify-center rounded-xl bg-slate-800 px-6 py-2.5 text-sm font-semibold text-white shadow-sm hover:bg-slate-700 transition-all">
                <x-icon name="Filter" class="ml-2 h-4 w-4" />
                تصفية
            </button>
        </form>
    </div>

    <div class="overflow-hidden rounded-2xl bg-white shadow-sm border border-slate-100">
        <div class="overflow-x-auto">
            <table class="min-w-full divide-y divide-gray-200">
                <thead class="bg-gray-50/50">
                    <tr>
                        <th class="px-6 py-4 text-right text-xs font-bold text-gray-500 uppercase tracking-wider">المركبة</th>
                        <th class="px-6 py-4 text-right text-xs font-bold text-gray-500 uppercase tracking-wider">رقم المخزون</th>
                        <th class="px-6 py-4 text-right text-xs font-bold text-gray-500 uppercase tracking-wider">السعر</th>
                        <th class="px-6 py-4 text-right text-xs font-bold text-gray-500 uppercase tracking-wider">الحالة</th>
                        <th class="px-6 py-4 text-left text-xs font-bold text-gray-500 uppercase tracking-wider">إجراءات</th>
                    </tr>
                </thead>
                <tbody class="bg-white divide-y divide-gray-200">
                    @forelse($vehicles as $vehicle)
                        <tr class="hover:bg-slate-50/50 transition-colors">
                            <td class="px-6 py-4 whitespace-nowrap">
                                <div class="flex items-center">
                                    <div class="flex-shrink-0 h-12 w-12 relative group cursor-pointer">
                                        @if($vehicle->images->isNotEmpty())
                                            <img class="h-12 w-12 rounded-xl object-cover border border-gray-100 shadow-sm transition-transform group-hover:scale-110" src="{{ $vehicle->images->first()->image_url }}" alt="">
                                        @else
                                            <div class="h-12 w-12 rounded-xl bg-slate-100 flex items-center justify-center text-slate-400 border border-slate-200">
                                                <x-icon name="Car" class="h-6 w-6" />
                                            </div>
                                        @endif
                                    </div>
                                    <div class="mr-4">
                                        <div class="text-sm font-bold text-slate-900">{{ $vehicle->year }} {{ $vehicle->make }} {{ $vehicle->model }}</div>
                                        <div class="text-sm text-slate-500">{{ $vehicle->trim }}</div>
                                    </div>
                                </div>
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-slate-600 font-medium">
                                {{ $vehicle->stock_number }}
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm font-bold text-slate-900">
                                {{ number_format($vehicle->pricing->price ?? 0) }} ج.م
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap">
                                <span class="px-3 py-1 inline-flex text-xs leading-5 font-bold rounded-full 
                                    {{ $vehicle->status === 'AVAILABLE' ? 'bg-emerald-100 text-emerald-700' : 
                                       ($vehicle->status === 'SOLD' ? 'bg-slate-100 text-slate-700' : 
                                       ($vehicle->status === 'RESERVED' ? 'bg-amber-100 text-amber-700' : 'bg-red-100 text-red-700')) }}">
                                    {{ $vehicle->status === 'AVAILABLE' ? 'متاح' : 
                                       ($vehicle->status === 'SOLD' ? 'مباع' : 
                                       ($vehicle->status === 'RESERVED' ? 'محجوز' : 'صيانة')) }}
                                </span>
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-left text-sm font-medium">
                                <div class="flex items-center gap-2 justify-end">
                                    <a href="{{ route('admin.vehicles.show', $vehicle->id) }}" class="p-2 bg-blue-50 text-blue-600 rounded-lg hover:bg-blue-100 transition-colors" title="عرض">
                                        <x-icon name="Eye" class="h-4 w-4" />
                                    </a>
                                    <a href="{{ route('admin.vehicles.edit', $vehicle->id) }}" class="p-2 bg-amber-50 text-amber-600 rounded-lg hover:bg-amber-100 transition-colors" title="تعديل">
                                        <x-icon name="Settings" class="h-4 w-4" />
                                    </a>
                                    <form action="{{ route('admin.vehicles.destroy', $vehicle->id) }}" method="POST" class="inline-block" onsubmit="return confirm('هل أنت متأكد من حذف هذه المركبة؟');">
                                        @csrf
                                        @method('DELETE')
                                        <button type="submit" class="p-2 bg-red-50 text-red-600 rounded-lg hover:bg-red-100 transition-colors" title="حذف">
                                            <x-icon name="Trash2" class="h-4 w-4" />
                                        </button>
                                    </form>
                                </div>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="5" class="px-6 py-12 text-center text-slate-500">
                                <div class="flex flex-col items-center justify-center">
                                    <div class="h-16 w-16 bg-slate-50 rounded-full flex items-center justify-center mb-4">
                                        <x-icon name="Car" class="h-8 w-8 text-slate-300" />
                                    </div>
                                    <p class="text-lg font-medium">لا توجد مركبات</p>
                                    <p class="text-sm">لم يتم العثور على أي مركبات تطابق بحثك.</p>
                                </div>
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        <div class="border-t border-gray-100 bg-gray-50/50 p-4">
            {{ $vehicles->withQueryString()->links() }}
        </div>
    </div>
</x-admin-layout>
