"use client"; import { useState, useEffect } from "react"; import { Button, Input } from "@/components/ui"; import { api } from "@/lib/api"; import type { Visit } from "@/types"; interface Props { childId: string } export function VisitTab({ childId }: Props) { const [visits, setVisits] = useState([]); const [editingVisit, setEditingVisit] = useState(null); const [showAdd, setShowAdd] = useState(false); const [doctor, setDoctor] = useState(""); const [reason, setReason] = useState(""); const [date, setDate] = useState(""); const [notes, setNotes] = useState(""); useEffect(() => { fetchVisits(); }, [childId]); const fetchVisits = () => api.get<{ visits: Visit[] }>(`/api/visits?childId=${childId}`) .then(d => setVisits(d.visits || [])) .catch(console.error); const save = async () => { if (!doctor) return; try { if (editingVisit) { await api.patch("/api/visits", { id: editingVisit.id, doctorName: doctor, reason, date, notes }); } else { await api.post("/api/visits", { childId, doctorName: doctor, reason, date, notes }); } fetchVisits(); } catch (err) { console.error("Failed to save visit:", err); } reset(); }; const remove = async (id: string) => { try { await api.delete(`/api/visits?id=${id}`); fetchVisits(); } catch (err) { console.error("Failed to delete visit:", err); } }; const edit = (v: Visit) => { setEditingVisit(v); setDoctor(v.doctorName); setReason(v.reason); setDate(v.date); setNotes(v.notes); setShowAdd(true); }; const reset = () => { setEditingVisit(null); setDoctor(""); setReason(""); setDate(""); setNotes(""); setShowAdd(false); }; return (

Doctor Visits

{showAdd && (
setDoctor(e.target.value)} /> setReason(e.target.value)} /> setDate(e.target.value)} /> setNotes(e.target.value)} />
)} {visits.length === 0 && !showAdd ? (

No visits recorded

) : ( visits.map(v => (
{v.doctorName}
{new Date(v.date).toLocaleDateString()} {v.reason && ` · ${v.reason}`} {v.notes && ` · ${v.notes}`}
)) )} {!showAdd && ( )}
); }