<x-app-layout>
    <x-slot name="header">
        <div class="flex justify-between items-center">
            <h2 class="font-semibold text-xl text-gray-800 leading-tight">
                {{ __('Maintenance') }}
            </h2>
            <a href="{{ route('maintenances.create') }}" class="bg-blue-600 text-white px-4 py-2 rounded shadow hover:bg-blue-700">Add Record</a>
        </div>
    </x-slot>

    <div class="py-6">
        <div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
            
            @if(session('success'))
                <div class="bg-green-100 border-l-4 border-green-500 text-green-700 p-4 mb-4 rounded shadow-sm" role="alert">
                    <p>{{ session('success') }}</p>
                </div>
            @endif

            <!-- Mobile First Card Layout -->
            <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4 px-4 sm:px-0">
                @foreach($maintenances as $m)
                    <div class="bg-white rounded-lg shadow-md p-4 border-t-4 border-yellow-500">
                        <div class="flex justify-between items-start mb-2">
                            <div>
                                <span class="text-xs font-bold text-gray-400 uppercase">{{ $m->date->format('d M Y') }}</span>
                                <h3 class="font-black text-xl text-gray-800">{{ $m->vehicle->vehicle_number ?? 'Unknown' }}</h3>
                            </div>
                            <span class="px-2 py-1 bg-yellow-100 text-yellow-800 font-black rounded">₹{{ number_format($m->cost, 2) }}</span>
                        </div>
                        
                        <div class="text-sm text-gray-600 mb-2 border-b pb-2">
                            <p class="mb-1 text-gray-800">{{ $m->description }}</p>
                            <p><strong>Service KM:</strong> {{ $m->km }}</p>
                        </div>
                        
                        <div class="grid grid-cols-2 gap-2 text-xs">
                            <div>
                                <p class="text-gray-500">Next Service KM</p>
                                <p class="font-bold">{{ $m->next_service_km ?? 'N/A' }}</p>
                            </div>
                            <div>
                                <p class="text-gray-500">Next Service Date</p>
                                <p class="font-bold">{{ $m->next_service_date ? $m->next_service_date->format('d/m/y') : 'N/A' }}</p>
                            </div>
                        </div>
                        
                        <!-- Actions -->
                        <div class="flex gap-2 mt-3 pt-3 border-t">
                            <a href="{{ route('maintenances.edit', $m) }}" class="flex-1 text-center text-sm font-medium px-3 py-1.5 rounded bg-blue-50 text-blue-700 hover:bg-blue-100 transition">✏️ Edit</a>
                            <form action="{{ route('maintenances.destroy', $m) }}" method="POST" class="flex-1" onsubmit="return confirm('Delete this record?')">
                                @csrf
                                @method('DELETE')
                                <button type="submit" class="w-full text-sm font-medium px-3 py-1.5 rounded bg-red-50 text-red-700 hover:bg-red-100 transition">🗑️ Delete</button>
                            </form>
                        </div>
                    </div>
                @endforeach
            </div>
            
            <div class="mt-4 px-4 sm:px-0">
                {{ $maintenances->links() }}
            </div>

        </div>
    </div>
</x-app-layout>
