@extends('layouts.app')

@section('content')

<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 2rem; margin-top: 15px;">
    <h2 style="color: #d4af37; font-size: 1.5rem; font-weight: bold;">
        {{ app()->getLocale() == 'ar' ? 'سجل الصيانة' : 'Maintenance Records' }}
    </h2>
    <div style="display: flex; gap: 1rem;">
        <a href="{{ route('admin.rentals.dashboard') }}" style="background: #666; color: white; padding: 0.5rem 1rem; border-radius: 0.5rem; text-decoration: none; font-size: 0.875rem;">
            {{ app()->getLocale() == 'ar' ? 'عودة' : 'Back' }}
        </a>
        <a href="{{ route('admin.maintenance.create') }}" style="background: #d4af37; color: black; padding: 0.5rem 1rem; border-radius: 0.5rem; text-decoration: none; font-size: 0.875rem; font-weight: bold;">
            {{ app()->getLocale() == 'ar' ? 'إضافة سجل صيانة' : 'Add Maintenance' }}
        </a>
    </div>
</div>

<!-- Advanced Search Form -->
<div class="bg-[#111] border border-gray-800 rounded-2xl p-6 mb-8">
    <form action="{{ route('admin.maintenance.index') }}" method="GET">
        <div class="grid grid-cols-1 md:grid-cols-4 gap-6">
            <!-- General Search -->
            <div class="md:col-span-2">
                <label class="block text-xs text-gray-500 mb-2 uppercase tracking-wider">{{ app()->getLocale() == 'ar' ? 'بحث عام (السيارة، الوصف)' : 'General Search (Car, Description)' }}</label>
                <div class="relative">
                    <span class="absolute inset-y-0 right-0 flex items-center pr-3 text-gray-500">🔍</span>
                    <input type="text" name="search" value="{{ request('search') }}" class="w-full bg-black border border-gray-800 rounded-lg pr-10 pl-4 py-2.5 text-white focus:border-[#d4af37] outline-none text-sm" placeholder="{{ app()->getLocale() == 'ar' ? 'ابحث...' : 'Search...' }}">
                </div>
            </div>

            <!-- Car Filter -->
            <div>
                <label class="block text-xs text-gray-500 mb-2 uppercase tracking-wider">{{ app()->getLocale() == 'ar' ? 'السيارة' : 'Car' }}</label>
                <select name="car_id" class="w-full bg-black border border-gray-800 rounded-lg px-4 py-2.5 text-white focus:border-[#d4af37] outline-none text-sm">
                    <option value="">{{ app()->getLocale() == 'ar' ? 'الكل' : 'All' }}</option>
                    @foreach($cars ?? [] as $car)
                        <option value="{{ $car->id }}" {{ request('car_id') == $car->id ? 'selected' : '' }}>{{ $car->title }}</option>
                    @endforeach
                </select>
            </div>

            <!-- Date Range -->
            <div>
                <label class="block text-xs text-gray-500 mb-2 uppercase tracking-wider">{{ app()->getLocale() == 'ar' ? 'من التاريخ' : 'From Date' }}</label>
                <input type="date" name="from_date" value="{{ request('from_date') }}" class="w-full bg-black border border-gray-800 rounded-lg px-4 py-2.5 text-white focus:border-[#d4af37] outline-none text-sm">
            </div>

            <!-- To Date -->
            <div>
                <label class="block text-xs text-gray-500 mb-2 uppercase tracking-wider">{{ app()->getLocale() == 'ar' ? 'إلى التاريخ' : 'To Date' }}</label>
                <input type="date" name="to_date" value="{{ request('to_date') }}" class="w-full bg-black border border-gray-800 rounded-lg px-4 py-2.5 text-white focus:border-[#d4af37] outline-none text-sm">
            </div>

            <!-- Min Cost -->
            <div>
                <label class="block text-xs text-gray-500 mb-2 uppercase tracking-wider">{{ app()->getLocale() == 'ar' ? 'الحد الأدنى للتكلفة' : 'Min Cost' }}</label>
                <input type="number" step="0.01" name="min_cost" value="{{ request('min_cost') }}" class="w-full bg-black border border-gray-800 rounded-lg px-4 py-2.5 text-white focus:border-[#d4af37] outline-none text-sm" placeholder="0">
            </div>

            <!-- Max Cost -->
            <div>
                <label class="block text-xs text-gray-500 mb-2 uppercase tracking-wider">{{ app()->getLocale() == 'ar' ? 'الحد الأقصى للتكلفة' : 'Max Cost' }}</label>
                <input type="number" step="0.01" name="max_cost" value="{{ request('max_cost') }}" class="w-full bg-black border border-gray-800 rounded-lg px-4 py-2.5 text-white focus:border-[#d4af37] outline-none text-sm" placeholder="999999">
            </div>

            <!-- Sort By -->
            <div>
                <label class="block text-xs text-gray-500 mb-2 uppercase tracking-wider">{{ app()->getLocale() == 'ar' ? 'ترتيب حسب' : 'Sort By' }}</label>
                <select name="sort" class="w-full bg-black border border-gray-800 rounded-lg px-4 py-2.5 text-white focus:border-[#d4af37] outline-none text-sm">
                    <option value="date_desc" {{ request('sort') == 'date_desc' ? 'selected' : '' }}>{{ app()->getLocale() == 'ar' ? 'الأحدث' : 'Newest' }}</option>
                    <option value="date_asc" {{ request('sort') == 'date_asc' ? 'selected' : '' }}>{{ app()->getLocale() == 'ar' ? 'الأقدم' : 'Oldest' }}</option>
                    <option value="cost_high" {{ request('sort') == 'cost_high' ? 'selected' : '' }}>{{ app()->getLocale() == 'ar' ? 'التكلفة الأعلى' : 'Highest Cost' }}</option>
                    <option value="cost_low" {{ request('sort') == 'cost_low' ? 'selected' : '' }}>{{ app()->getLocale() == 'ar' ? 'التكلفة الأقل' : 'Lowest Cost' }}</option>
                </select>
            </div>

            <!-- Buttons -->
            <div class="flex items-end gap-2">
                <button type="submit" class="flex-1 px-4 py-2.5 rounded-lg bg-[#d4af37] text-black text-sm font-bold hover:bg-[#b08d2a] transition-colors">
                    {{ app()->getLocale() == 'ar' ? 'بحث' : 'Search' }}
                </button>
                <a href="{{ route('admin.maintenance.index') }}" class="px-4 py-2.5 rounded-lg bg-gray-800 text-white text-sm hover:bg-gray-700 transition-colors">
                    {{ app()->getLocale() == 'ar' ? 'إعادة تعيين' : 'Reset' }}
                </a>
            </div>
        </div>
    </form>
</div>

<div class="bg-[#111] border border-gray-800 rounded-2xl overflow-hidden">
    <table class="w-full {{ app()->getLocale() == 'ar' ? 'text-right' : 'text-left' }}">
        <thead class="bg-black/50 text-xs text-gray-500 uppercase">
            <tr>
                <th class="px-6 py-4">{{ app()->getLocale() == 'ar' ? 'السيارة' : 'Car' }}</th>
                <th class="px-6 py-4">{{ app()->getLocale() == 'ar' ? 'الوصف' : 'Description' }}</th>
                <th class="px-6 py-4">{{ app()->getLocale() == 'ar' ? 'التكلفة' : 'Cost' }}</th>
                <th class="px-6 py-4">{{ app()->getLocale() == 'ar' ? 'التاريخ' : 'Date' }}</th>
                <th class="px-6 py-4">{{ app()->getLocale() == 'ar' ? 'موعد الصيانة القادم' : 'Next Due' }}</th>
            </tr>
        </thead>
        <tbody class="divide-y divide-gray-800">
            @foreach($records as $record)
            <tr class="hover:bg-white/5">
                <td class="px-6 py-4">
                    <div class="text-sm font-bold text-white">{{ $record->car->title }}</div>
                    <div class="text-xs text-gray-500">{{ $record->car->plate_number }}</div>
                </td>
                <td class="px-6 py-4 text-sm text-gray-400">{{ $record->description }}</td>
                <td class="px-6 py-4 text-sm text-[#d4af37] font-bold">{{ number_format($record->cost) }} {{ app()->getLocale() == 'ar' ? 'ر.س' : 'SAR' }}</td>
                <td class="px-6 py-4 text-sm text-gray-400">{{ $record->maintenance_date->format('Y-m-d') }}</td>
                <td class="px-6 py-4 text-sm text-gray-400">{{ $record->next_due_km ? number_format($record->next_due_km) . ' ' . (app()->getLocale() == 'ar' ? 'كم' : 'KM') : '-' }}</td>
            </tr>
            @endforeach
        </tbody>
    </table>
    <div class="p-4 border-t border-gray-800">
        {{ $records->links() }}
    </div>
</div>

@endsection
