<div class="space-y-6">
    {{-- Date and Technician Selection --}}
    <div class="bg-white dark:bg-zinc-800 rounded-lg shadow p-6">
        <h3 class="text-lg font-medium text-zinc-900 dark:text-white dark:text-white mb-4">
            Select Date & Technician
        </h3>
        
        <div class="grid grid-cols-1 md:grid-cols-3 gap-4">
            {{-- Date Selection --}}
            <div>
                <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">Date</label>
                <input 
                    type="date" 
                    wire:model.live="selectedDate"
                    min="{{ now()->format('Y-m-d') }}"
                    class="block w-full rounded-md border-zinc-300 dark:border-zinc-600 border border-zinc-200 dark:border-zinc-700 focus:border-indigo-500 focus:ring-indigo-500 dark:border-zinc-700 dark:bg-gray-900 dark:text-gray-300"
                />
            </div>
            
            {{-- Technician Selection --}}
            <div>
                <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">Technician</label>
                <select wire:model.live="selectedTechnician" class="block w-full rounded-md border-zinc-300 dark:border-zinc-600 border border-zinc-200 dark:border-zinc-700 focus:border-indigo-500 focus:ring-indigo-500 dark:border-zinc-700 dark:bg-gray-900 dark:text-gray-300">
                    <option value="">Any Available</option>
                    @foreach($technicians as $technician)
                        <option value="{{ $technician->id }}">
                            {{ $technician->first_name }} {{ $technician->last_name }}
                        </option>
                    @endforeach
                </select>
            </div>
            
            {{-- Service Duration --}}
            <div>
                <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">Estimated Duration</label>
                <select wire:model.live="serviceDuration" class="block w-full rounded-md border-zinc-300 dark:border-zinc-600 border border-zinc-200 dark:border-zinc-700 focus:border-indigo-500 focus:ring-indigo-500 dark:border-zinc-700 dark:bg-gray-900 dark:text-gray-300">
                    <option value="30">30 minutes</option>
                    <option value="60">1 hour</option>
                    <option value="90">1.5 hours</option>
                    <option value="120">2 hours</option>
                    <option value="180">3 hours</option>
                    <option value="240">4 hours</option>
                </select>
            </div>
        </div>
        
        @if($selectedDate && \Carbon\Carbon::parse($selectedDate)->isPast() && !\Carbon\Carbon::parse($selectedDate)->isToday())
            <div class="mt-4 p-4 bg-red-50 dark:bg-red-900 border border-red-200 dark:border-red-700 rounded-md">
                <div class="flex">
                    <svg class="w-5 h-5 text-red-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-2.5L13.732 4c-.77-.833-1.964-.833-2.732 0L3.732 16.5c-.77.833.192 2.5 1.732 2.5z"></path>
                    </svg>
                    <div class="ml-3">
                        <p class="text-sm text-red-800 dark:text-red-200">
                            Cannot schedule appointments for past dates. Please select a future date.
                        </p>
                    </div>
                </div>
            </div>
        @endif
    </div>

    {{-- Time Slots Grid --}}
    @if(!empty($timeSlots))
        <div class="bg-white dark:bg-zinc-800 rounded-lg shadow">
            <div class="px-6 py-4 border-b border-zinc-200 dark:border-zinc-700">
                <div class="flex items-center justify-between">
                    <h3 class="text-lg font-medium text-zinc-900 dark:text-white dark:text-white">
                        Available Time Slots
                    </h3>
                    <div class="text-sm text-zinc-500 dark:text-zinc-400 dark:text-gray-400">
                        {{ \Carbon\Carbon::parse($selectedDate)->format('l, F j, Y') }}
                    </div>
                </div>
                
                @if($selectedSlot)
                    <div class="mt-2 flex items-center space-x-2">
                        <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-green-100 text-green-800">
                            Selected: {{ \Carbon\Carbon::parse($selectedDate . ' ' . $selectedSlot)->format('g:i A') }}
                        </span>
                        <button wire:click="clearSelection" class="text-sm text-zinc-500 dark:text-zinc-400 hover:text-gray-700">
                            Clear
                        </button>
                    </div>
                @endif
            </div>
            
            <div class="p-6">
                {{-- Legend --}}
                <div class="flex items-center space-x-6 mb-6 text-sm">
                    <div class="flex items-center space-x-2">
                        <div class="w-4 h-4 bg-green-100 border border-green-300 rounded"></div>
                        <span class="text-zinc-600 dark:text-zinc-400 dark:text-gray-400">Available</span>
                    </div>
                    <div class="flex items-center space-x-2">
                        <div class="w-4 h-4 bg-red-100 border border-red-300 rounded"></div>
                        <span class="text-zinc-600 dark:text-zinc-400 dark:text-gray-400">Booked</span>
                    </div>
                    <div class="flex items-center space-x-2">
                        <div class="w-4 h-4 bg-gray-100 border border-zinc-300 dark:border-zinc-600 rounded"></div>
                        <span class="text-zinc-600 dark:text-zinc-400 dark:text-gray-400">Unavailable</span>
                    </div>
                    <div class="flex items-center space-x-2">
                        <div class="w-4 h-4 bg-blue-100 border border-blue-300 rounded"></div>
                        <span class="text-zinc-600 dark:text-zinc-400 dark:text-gray-400">Selected</span>
                    </div>
                </div>
                
                {{-- Time Slots Grid --}}
                <div class="grid grid-cols-2 sm:grid-cols-3 md:grid-cols-4 lg:grid-cols-6 gap-3">
                    @foreach($timeSlots as $slot)
                        @php
                            $slotStatus = $this->getSlotStatus($slot['time']);
                            $isSelected = $this->isSlotSelected($slot['time']);
                        @endphp
                        
                        <div class="relative">
                            @if($slotStatus['status'] === 'available')
                                <button
                                    wire:click="selectSlot('{{ $slot['time'] }}')"
                                    class="w-full p-3 text-sm border rounded-lg transition-all duration-200 hover:shadow-md
                                        {{ $isSelected 
                                            ? 'bg-blue-100 border-blue-300 text-blue-800 ring-2 ring-blue-500' 
                                            : 'bg-green-50 border-green-200 text-green-800 hover:bg-green-100' }}"
                                >
                                    <div class="font-medium">{{ $slot['label'] }}</div>
                                    <div class="text-xs opacity-75">Available</div>
                                </button>
                                
                            @elseif($slotStatus['status'] === 'booked')
                                <div class="w-full p-3 text-sm bg-red-50 border border-red-200 text-red-800 rounded-lg cursor-not-allowed">
                                    <div class="font-medium">{{ $slot['label'] }}</div>
                                    <div class="text-xs opacity-75">
                                        {{ $slotStatus['data']['customer_name'] ?? 'Booked' }}
                                    </div>
                                </div>
                                
                                {{-- Tooltip for booked slot --}}
                                @if($slotStatus['data'])
                                    <div class="absolute z-10 invisible group-hover:visible bg-gray-900 text-white text-xs rounded py-1 px-2 -top-8 left-1/2 transform -translate-x-1/2">
                                        {{ $slotStatus['data']['service'] ?? 'Service appointment' }}
                                    </div>
                                @endif
                                
                            @else
                                <div class="w-full p-3 text-sm bg-zinc-50 border border-zinc-200 text-zinc-500 dark:text-zinc-400 rounded-lg cursor-not-allowed">
                                    <div class="font-medium">{{ $slot['label'] }}</div>
                                    <div class="text-xs opacity-75">Unavailable</div>
                                </div>
                            @endif
                        </div>
                    @endforeach
                </div>
                
                {{-- No available slots message --}}
                @if(empty($availableSlots))
                    <div class="text-center py-8">
                        <svg class="w-12 h-12 text-gray-400 mx-auto mb-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"></path>
                        </svg>
                        <h4 class="text-lg font-medium text-zinc-900 dark:text-white dark:text-white mb-2">
                            No Available Slots
                        </h4>
                        <p class="text-zinc-500 dark:text-zinc-400 dark:text-gray-400 mb-4">
                            All time slots are booked for this date
                            @if($selectedTechnician)
                                and technician
                            @endif
                            .
                        </p>
                        
                        @if($nextAvailableDate)
                            <button 
                                wire:click="$set('selectedDate', '{{ $nextAvailableDate }}')"
                                class="px-4 py-2 border border-zinc-300 dark:border-zinc-600 rounded-md text-sm font-medium text-gray-700 bg-white hover:bg-zinc-50"
                            >
                                Next Available: {{ \Carbon\Carbon::parse($nextAvailableDate)->format('M j, Y') }}
                            </button>
                        @else
                            <p class="text-sm text-gray-400">
                                No availability found in the next 30 days.
                            </p>
                        @endif
                    </div>
                @endif
            </div>
        </div>
        
        {{-- Business Hours Info --}}
        <div class="bg-blue-50 dark:bg-blue-900 border border-blue-200 dark:border-blue-700 rounded-lg p-4">
            <div class="flex">
                <svg class="w-5 h-5 text-blue-400 mt-0.5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path>
                </svg>
                <div class="ml-3">
                    <h4 class="text-sm font-medium text-blue-800 dark:text-blue-200">Business Hours</h4>
                    <div class="mt-1 text-sm text-blue-700 dark:text-blue-300">
                        <p>Monday - Friday: {{ \Carbon\Carbon::parse($businessStart)->format('g:i A') }} - {{ \Carbon\Carbon::parse($businessEnd)->format('g:i A') }}</p>
                        <p>Lunch Break: {{ \Carbon\Carbon::parse($lunchStart)->format('g:i A') }} - {{ \Carbon\Carbon::parse($lunchEnd)->format('g:i A') }}</p>
                        <p class="mt-1">Time slots are available in {{ $slotInterval }}-minute intervals.</p>
                    </div>
                </div>
            </div>
        </div>
        
    @else
        {{-- No slots for selected date --}}
        <div class="bg-white dark:bg-zinc-800 rounded-lg shadow p-8 text-center">
            <svg class="w-12 h-12 text-gray-400 mx-auto mb-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v12a2 2 0 002 2z"></path>
            </svg>
            <h3 class="text-lg font-medium text-zinc-900 dark:text-white dark:text-white mb-2">
                Select a Date
            </h3>
            <p class="text-zinc-500 dark:text-zinc-400 dark:text-gray-400">
                Choose a date to view available time slots.
            </p>
        </div>
    @endif
</div>

@script
<script>
    // Listen for slot selection
    $wire.on('slot-selected', (data) => {
        console.log('Slot selected:', data);
        // You can dispatch this to parent components or handle it as needed
    });
    
    $wire.on('slot-cleared', () => {
        console.log('Slot selection cleared');
    });
    
    $wire.on('date-changed', (data) => {
        console.log('Date changed:', data);
    });
    
    $wire.on('technician-changed', (data) => {
        console.log('Technician changed:', data);
    });
</script>
@endscript
