import { useState } from "react"; import { useMutation, useQuery } from "@tanstack/react-query"; import { queryClient } from "@/client/rpc-client"; export function BookingForm() { const [selectedTreatment, setSelectedTreatment] = useState(""); const [customerName, setCustomerName] = useState(""); const [customerEmail, setCustomerEmail] = useState(""); const [customerPhone, setCustomerPhone] = useState(""); const [appointmentDate, setAppointmentDate] = useState(""); const [selectedSlotId, setSelectedSlotId] = useState(""); const [notes, setNotes] = useState(""); const { data: treatments } = useQuery( queryClient.treatments.live.list.experimental_liveOptions() ); const { data: slotsByDate } = useQuery( appointmentDate ? queryClient.availability.live.byDate.experimental_liveOptions(appointmentDate) : queryClient.availability.live.byDate.experimental_liveOptions("") ); const { mutate: createBooking, isPending } = useMutation( queryClient.bookings.create.mutationOptions() ); const selectedTreatmentData = treatments?.find(t => t.id === selectedTreatment); const availableSlots = (slotsByDate || []).filter(s => s.status === "free"); const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); if (!selectedTreatment || !customerName || !customerEmail || !customerPhone || !appointmentDate || !selectedSlotId) { alert("Bitte fülle alle erforderlichen Felder aus"); return; } const slot = availableSlots.find(s => s.id === selectedSlotId); const appointmentTime = slot?.time || ""; createBooking({ treatmentId: selectedTreatment, customerName, customerEmail, customerPhone, appointmentDate, appointmentTime, notes, slotId: selectedSlotId, }, { onSuccess: () => { setSelectedTreatment(""); setCustomerName(""); setCustomerEmail(""); setCustomerPhone(""); setAppointmentDate(""); setSelectedSlotId(""); setNotes(""); alert("Buchung erfolgreich erstellt! Wir werden dich kontaktieren, um deinen Termin zu bestätigen."); } }); }; // Get minimum date (today) const today = new Date().toISOString().split('T')[0]; return (

Buche deine Nagelbehandlung

{/* Treatment Selection */}
{selectedTreatmentData && (

{selectedTreatmentData.description}

)}
{/* Customer Information */}
setCustomerName(e.target.value)} className="w-full p-3 border border-gray-300 rounded-md focus:ring-2 focus:ring-pink-500 focus:border-pink-500" required />
setCustomerEmail(e.target.value)} className="w-full p-3 border border-gray-300 rounded-md focus:ring-2 focus:ring-pink-500 focus:border-pink-500" required />
setCustomerPhone(e.target.value)} className="w-full p-3 border border-gray-300 rounded-md focus:ring-2 focus:ring-pink-500 focus:border-pink-500" required />
{/* Date and Time Selection */}
setAppointmentDate(e.target.value)} min={today} className="w-full p-3 border border-gray-300 rounded-md focus:ring-2 focus:ring-pink-500 focus:border-pink-500" required />
{/* Notes */}