<div>
    <div class="mb-8">
        <h1 class="text-2xl font-bold text-gray-900">Work Orders</h1>
        <p class="text-gray-600">Track the progress of your vehicle services.</p>
    </div>

    <!-- Filters -->
    <div class="bg-white rounded-lg shadow p-6 mb-6">
        <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
            <div>
                <label class="block text-sm font-medium text-gray-700 mb-2">Status</label>
                <select wire:model.live="filterStatus" class="w-full border-gray-300 rounded-md shadow-sm focus:border-blue-500 focus:ring-blue-500">
                    <option value="">All Statuses</option>
                    <option value="pending">Pending</option>
                    <option value="inspection">Inspection</option>
                    <option value="diagnosis">Diagnosis</option>
                    <option value="estimate_sent">Estimate Sent</option>
                    <option value="estimate_approved">Approved</option>
                    <option value="in_progress">In Progress</option>
                    <option value="completed">Completed</option>
                </select>
            </div>
            <div class="flex items-end">
                <button wire:click="$set('filterStatus', '')" 
                        class="px-4 py-2 border border-gray-300 rounded-md text-gray-700 hover:bg-gray-50">
                    Clear Filters
                </button>
            </div>
        </div>
    </div>

    <!-- Work Orders List -->
    <div class="space-y-6">
        @if($workOrders->count() > 0)
            @foreach($workOrders as $workOrder)
                <div class="bg-white rounded-lg shadow">
                    <div class="p-6">
                        <div class="flex justify-between items-start mb-4">
                            <div>
                                <h3 class="text-lg font-semibold text-gray-900">Work Order #{{ $workOrder->id }}</h3>
                                <p class="text-sm text-gray-600">
                                    {{ $workOrder->vehicle->year ?? '' }} 
                                    {{ $workOrder->vehicle->make ?? '' }} 
                                    {{ $workOrder->vehicle->model ?? '' }}
                                    @if($workOrder->vehicle->license_plate)
                                        • {{ $workOrder->vehicle->license_plate }}
                                    @endif
                                </p>
                                <p class="text-sm text-gray-500">Created: {{ $workOrder->created_at->format('M j, Y g:i A') }}</p>
                            </div>
                            <div class="text-right">
                                <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium
                                    @switch($workOrder->status)
                                        @case('pending')
                                            bg-yellow-100 text-yellow-800
                                            @break
                                        @case('inspection')
                                            bg-blue-100 text-blue-800
                                            @break
                                        @case('diagnosis')
                                            bg-purple-100 text-purple-800
                                            @break
                                        @case('estimate_sent')
                                            bg-orange-100 text-orange-800
                                            @break
                                        @case('estimate_approved')
                                            bg-green-100 text-green-800
                                            @break
                                        @case('in_progress')
                                            bg-blue-100 text-blue-800
                                            @break
                                        @case('completed')
                                            bg-green-100 text-green-800
                                            @break
                                        @default
                                            bg-gray-100 text-gray-800
                                    @endswitch
                                ">
                                    {{ ucfirst(str_replace('_', ' ', $workOrder->status)) }}
                                </span>
                            </div>
                        </div>

                        @if($workOrder->customer_reported_issues)
                            <div class="mb-4">
                                <h4 class="text-sm font-medium text-gray-700 mb-1">Reported Issues</h4>
                                <p class="text-sm text-gray-600">{{ $workOrder->customer_reported_issues }}</p>
                            </div>
                        @endif

                        @if($workOrder->serviceAdvisor)
                            <div class="mb-4">
                                <h4 class="text-sm font-medium text-gray-700 mb-1">Service Advisor</h4>
                                <p class="text-sm text-gray-600">{{ $workOrder->serviceAdvisor->name }}</p>
                            </div>
                        @endif

                        @if($workOrder->estimates->count() > 0)
                            <div class="mb-4">
                                <h4 class="text-sm font-medium text-gray-700 mb-2">Estimates</h4>
                                <div class="space-y-2">
                                    @foreach($workOrder->estimates as $estimate)
                                        <div class="flex justify-between items-center p-3 bg-gray-50 rounded-md">
                                            <div>
                                                <span class="text-sm font-medium">Estimate #{{ $estimate->id }}</span>
                                                <span class="ml-2 text-sm text-gray-600">${{ number_format($estimate->total_amount, 2) }}</span>
                                            </div>
                                            <div class="flex items-center space-x-2">
                                                <span class="inline-flex items-center px-2 py-1 rounded-full text-xs font-medium
                                                    @switch($estimate->status)
                                                        @case('approved')
                                                            bg-green-100 text-green-800
                                                            @break
                                                        @case('rejected')
                                                            bg-red-100 text-red-800
                                                            @break
                                                        @case('sent')
                                                        @case('viewed')
                                                            bg-yellow-100 text-yellow-800
                                                            @break
                                                        @default
                                                            bg-gray-100 text-gray-800
                                                    @endswitch
                                                ">
                                                    {{ ucfirst($estimate->status) }}
                                                </span>
                                                @if(in_array($estimate->status, ['sent', 'viewed']))
                                                    <a href="{{ route('customer-portal.estimate', [$workOrder, $estimate]) }}" 
                                                       class="text-blue-600 hover:text-blue-800 text-xs">
                                                        Review
                                                    </a>
                                                @endif
                                            </div>
                                        </div>
                                    @endforeach
                                </div>
                            </div>
                        @endif

                        <div class="flex justify-between items-center pt-4 border-t border-gray-200">
                            <div class="text-sm text-gray-500">
                                Last updated: {{ $workOrder->updated_at->diffForHumans() }}
                            </div>
                            <a href="{{ route('customer-portal.status', $workOrder) }}" 
                               class="inline-flex items-center px-3 py-2 border border-transparent text-sm leading-4 font-medium rounded-md text-blue-700 bg-blue-100 hover:bg-blue-200 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500">
                                View Details
                            </a>
                        </div>
                    </div>
                </div>
            @endforeach

            @if($workOrders->hasPages())
                <div class="mt-6">
                    {{ $workOrders->links() }}
                </div>
            @endif
        @else
            <div class="bg-white rounded-lg shadow">
                <div class="text-center py-12">
                    <svg class="mx-auto h-12 w-12 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"></path>
                    </svg>
                    <h3 class="mt-2 text-sm font-medium text-gray-900">No work orders found</h3>
                    <p class="mt-1 text-sm text-gray-500">
                        @if($filterStatus)
                            Try adjusting your filters or contact us about your vehicle service.
                        @else
                            You don't have any work orders yet. Contact us to schedule a service.
                        @endif
                    </p>
                </div>
            </div>
        @endif
    </div>
</div>
