'use client'; import { useState, useEffect } from 'react'; import TabLayout from '@/components/TabLayout'; import AppointmentForm from '@/components/AppointmentForm'; import AppointmentList from '@/components/AppointmentList'; import ConfirmationModal from '@/components/ConfirmationModal'; import { Appointment, AppointmentPayload } from '@/types/appointment'; export default function Home() { const [appointments, setAppointments] = useState([]); const [isLoading, setIsLoading] = useState(true); const [editingAppointment, setEditingAppointment] = useState(null); const [deleteId, setDeleteId] = useState(null); const fetchAppointments = async () => { try { const res = await fetch('/api/appointments', { cache: 'no-store' }); if (!res.ok) throw new Error(`HTTP ${res.status}`); const data = await res.json(); if (data.success) { setAppointments(data.data); } } catch (error) { console.error('Fehler beim Laden der Termine:', error); } finally { setIsLoading(false); } }; // Termine einmalig beim Mount laden. useEffect(() => { // eslint-disable-next-line react-hooks/set-state-in-effect fetchAppointments(); }, []); // CREATE const addAppointment = async (payload: AppointmentPayload) => { try { const res = await fetch('/api/appointments', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(payload), }); if (!res.ok) throw new Error('Fehler beim Hinzufügen.'); const data = await res.json(); if (data.success) { setAppointments((prev) => [...prev, data.data]); } } catch (error) { console.error('Fehler beim Speichern des Termins:', error); alert('Fehler beim Speichern!'); } }; // UPDATE (vollständig, vom Formular) const updateAppointment = async (id: string, payload: AppointmentPayload) => { try { const res = await fetch(`/api/appointments/${id}`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(payload), }); if (!res.ok) throw new Error('Fehler beim Aktualisieren.'); const data = await res.json(); if (data.success) { setAppointments((prev) => prev.map((a) => (a.id === id ? data.data : a)) ); setEditingAppointment(null); } } catch (error) { console.error('Fehler beim Aktualisieren des Termins:', error); alert('Fehler beim Aktualisieren!'); } }; // Statuswechsel (erledigt) const toggleDone = async (id: string) => { const current = appointments.find((a) => a.id === id); if (!current) return; const newStatus = !current.erledigt; // Optimistisches Update setAppointments((prev) => prev.map((a) => (a.id === id ? { ...a, erledigt: newStatus } : a)) ); try { const res = await fetch(`/api/appointments/${id}`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ erledigt: newStatus }), }); if (!res.ok) throw new Error('Fehler beim Statuswechsel.'); } catch (error) { console.error('Fehler beim Statuswechsel:', error); // Rückgängig machen bei Fehler setAppointments((prev) => prev.map((a) => (a.id === id ? { ...a, erledigt: current.erledigt } : a)) ); } }; // DELETE (Soft-Delete) mit Bestätigung const confirmDelete = async () => { const id = deleteId; setDeleteId(null); if (!id) return; try { const res = await fetch(`/api/appointments/${id}`, { method: 'DELETE' }); if (!res.ok) throw new Error('Fehler beim Löschen.'); setAppointments((prev) => prev.filter((a) => a.id !== id)); if (editingAppointment && editingAppointment.id === id) { setEditingAppointment(null); } } catch (error) { console.error('Fehler beim Löschen des Termins:', error); alert('Fehler beim Löschen!'); } }; const startEdit = (appointment: Appointment) => { setEditingAppointment(appointment); window.scrollTo({ top: 0, behavior: 'smooth' }); }; const handlePrint = () => window.print(); // Aktuelle (offene) Termine ab heute für die Druckansicht const startOfToday = new Date(); startOfToday.setHours(0, 0, 0, 0); const currentAppointments = appointments .filter((a) => !a.erledigt && new Date(a.termin) >= startOfToday) .sort( (a, b) => new Date(a.termin).getTime() - new Date(b.termin).getTime() ); return ( <> {/* Bildschirmansicht */}

{editingAppointment ? '✍️ Termin bearbeiten' : '🗓️ Neuen Termin hinzufügen'}

setEditingAppointment(null)} />

Alle Termine

{isLoading ? (
Lade Daten...
) : ( )}
{/* Druckansicht: nur aktuelle (offene) Termine ab heute */}

Aktuelle Arzt-Termine

{currentAppointments.length === 0 ? (

Keine aktuellen Termine vorhanden.

) : ( {currentAppointments.map((app) => ( ))}
Arzt Fachrichtung Datum Bemerkungen
{app.arztName} {app.arztArt || ''} {new Date(app.termin).toLocaleString('de-DE')} {app.bemerkungen || ''}
)}
setDeleteId(null)} /> ); }