'use client' import { useState, useEffect } from 'react' import { Button } from '@/components/ui/button' import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card' import { Label } from '@/components/ui/label' import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select' import { Calendar, Clock, User } from 'lucide-react' import { format } from 'date-fns' import { es } from 'date-fns/locale' interface Service { id: string name: string category: string duration_minutes: number base_price: number } interface Location { id: string name: string timezone: string } export default function ServiciosPage() { const [services, setServices] = useState([]) const [locations, setLocations] = useState([]) const [selectedService, setSelectedService] = useState('') const [selectedLocation, setSelectedLocation] = useState('') const [selectedDate, setSelectedDate] = useState(format(new Date(), 'yyyy-MM-dd')) const [timeSlots, setTimeSlots] = useState([]) const [selectedTime, setSelectedTime] = useState('') const [loading, setLoading] = useState(false) useEffect(() => { fetchServices() fetchLocations() }, []) useEffect(() => { if (selectedService && selectedLocation && selectedDate) { fetchTimeSlots() } }, [selectedService, selectedLocation, selectedDate]) const fetchServices = async () => { try { const response = await fetch('/api/services') const data = await response.json() if (data.services) { setServices(data.services) } } catch (error) { console.error('Error fetching services:', error) } } const fetchLocations = async () => { try { const response = await fetch('/api/locations') const data = await response.json() if (data.locations) { setLocations(data.locations) if (data.locations.length > 0) { setSelectedLocation(data.locations[0].id) } } } catch (error) { console.error('Error fetching locations:', error) } } const fetchTimeSlots = async () => { if (!selectedService || !selectedLocation || !selectedDate) return setLoading(true) try { const response = await fetch( `/api/availability/time-slots?location_id=${selectedLocation}&service_id=${selectedService}&date=${selectedDate}` ) const data = await response.json() if (data.availability) { setTimeSlots(data.availability) } } catch (error) { console.error('Error fetching time slots:', error) } finally { setLoading(false) } } const handleContinue = () => { if (selectedService && selectedLocation && selectedDate && selectedTime) { const params = new URLSearchParams({ service_id: selectedService, location_id: selectedLocation, date: selectedDate, time: selectedTime }) window.location.href = `/cita?${params.toString()}` } } const selectedServiceData = services.find(s => s.id === selectedService) return (

Reservar Cita

Selecciona el servicio y horario de tu preferencia

Servicios Selecciona el servicio que deseas reservar
Fecha y Hora Selecciona la fecha y hora disponible
setSelectedDate(e.target.value)} min={format(new Date(), 'yyyy-MM-dd')} className="w-full px-4 py-3 border rounded-lg" style={{ borderColor: 'var(--mocha-taupe)' }} />
{selectedServiceData && (
)} {loading ? (
Cargando horarios...
) : (
{timeSlots.length === 0 ? (

No hay horarios disponibles para esta fecha. Selecciona otra fecha.

) : (
{timeSlots.map((slot, index) => ( ))}
)}
)}
{selectedServiceData && selectedTime && (

Resumen de la reserva

Servicio: {selectedServiceData.name}

Fecha: {format(new Date(selectedDate), 'PPP', { locale: es })}

Hora: {format(new Date(selectedTime), 'HH:mm', { locale: es })}

Precio: ${selectedServiceData.base_price.toFixed(2)}

)}
) }