"use client"; import { Suspense, useEffect, useState } from "react"; import { useRouter, useSearchParams } from "next/navigation"; import Link from "next/link"; import Card from "@/components/ui/Card"; import Button from "@/components/ui/Button"; import Badge from "@/components/ui/Badge"; interface TipoServicio { id: string; nombre: string; duracionHoras: number; } interface Slot { fechaInicio: string; fechaFin: string; } interface Turno { id: string; fechaInicio: string; fechaFin: string; estado: "PENDIENTE" | "CONFIRMADO" | "CANCELADO" | "COMPLETADO"; tipoServicio: { nombre: string }; auto: { marca: string; modelo: string; patente: string }; } type Step = "servicio" | "slot" | "confirmado"; function TurnosContent() { const router = useRouter(); const params = useSearchParams(); const autoId = params.get("autoId"); const [step, setStep] = useState("servicio"); const [servicios, setServicios] = useState([]); const [servicioSel, setServicioSel] = useState(null); const [slots, setSlots] = useState([]); const [slotSel, setSlotSel] = useState(null); const [loadingSlots, setLoadingSlots] = useState(false); const [reservando, setReservando] = useState(false); const [turnoCreado, setTurnoCreado] = useState(null); const [misTurnos, setMisTurnos] = useState([]); const [loadingTurnos, setLoadingTurnos] = useState(true); useEffect(() => { if (!autoId) { router.push("/cliente/dashboard"); return; } fetch("/api/cliente/servicios") .then((r) => r.json()) .then((data) => setServicios(data)); fetch("/api/cliente/turnos") .then((r) => r.json()) .then((data) => { setMisTurnos(data.filter((t: Turno & { autoId: string }) => t.autoId === autoId)); setLoadingTurnos(false); }); }, [autoId, router]); async function seleccionarServicio(s: TipoServicio) { setServicioSel(s); setLoadingSlots(true); setStep("slot"); const res = await fetch(`/api/cliente/slots?tipoServicioId=${s.id}`); const data = await res.json(); setSlots(data); setLoadingSlots(false); } async function reservar() { if (!slotSel || !servicioSel || !autoId) return; setReservando(true); const res = await fetch("/api/cliente/turnos", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ autoId, tipoServicioId: servicioSel.id, fechaInicio: slotSel.fechaInicio }), }); const data = await res.json(); setReservando(false); if (res.ok) { setTurnoCreado(data); setStep("confirmado"); } } function formatSlot(slot: Slot) { const inicio = new Date(slot.fechaInicio); const fin = new Date(slot.fechaFin); const dia = inicio.toLocaleDateString("es-AR", { weekday: "long", day: "numeric", month: "long" }); const h1 = inicio.toLocaleTimeString("es-AR", { hour: "2-digit", minute: "2-digit" }); const h2 = fin.toLocaleTimeString("es-AR", { hour: "2-digit", minute: "2-digit" }); return { dia: dia.charAt(0).toUpperCase() + dia.slice(1), horas: `${h1} - ${h2}` }; } if (step === "confirmado" && turnoCreado) { const { dia, horas } = formatSlot({ fechaInicio: turnoCreado.fechaInicio, fechaFin: turnoCreado.fechaFin }); return (
✅

¡Turno reservado!

Servicio

{turnoCreado.tipoServicio.nombre}

Fecha

{dia}

{horas}

); } return (
{step === "slot" ? ( ) : ( )}

{step === "servicio" ? "Elegí el servicio" : "Elegí el horario"}

{step === "servicio" && ( <>
{servicios.map((s) => ( ))}
{!loadingTurnos && misTurnos.length > 0 && (
{misTurnos.map((t) => { const { dia, horas } = formatSlot({ fechaInicio: t.fechaInicio, fechaFin: t.fechaFin }); return (

{t.tipoServicio.nombre}

{dia} · {horas}

); })}
)} )} {step === "slot" && (
{loadingSlots ? (
Buscando horarios disponibles...
) : slots.length === 0 ? (

No hay horarios disponibles en los próximos 14 días. Consultá al taller.

) : ( <>

Servicio: {servicioSel?.nombre}

{slots.map((slot, i) => { const { dia, horas } = formatSlot(slot); const sel = slotSel?.fechaInicio === slot.fechaInicio; return ( ); })}
)}
)}
); } export default function TurnosPage() { return ( Cargando...}> ); }