"use client"; import { Plus, Trash2 } from "lucide-react"; import { useRouter } from "next/navigation"; import { useState } from "react"; import { deleteEmployeeDependent } from "@/actions/employees"; import { Button } from "@/components/ui/Button"; import { useToast } from "@/components/ui/Toast"; import { fmtDate, todayIso } from "@/lib/format"; import type { Database } from "@/lib/supabase/types"; import { AddDependentModal } from "./AddDependentModal"; type Dependent = Database["public"]["Tables"]["employee_dependents"]["Row"]; // `effectiveDate` lets DatenAendernPanel embed this section and drive // add/remove off its own "Wirksam ab" field; standalone usage (Stammdaten // tab) omits it and defaults to today, i.e. immediate. export function AngehoerigeSection({ employeeId, dependents, effectiveDate }: { employeeId: string; dependents: Dependent[]; effectiveDate?: string }) { const { showToast } = useToast(); const router = useRouter(); const [modalOpen, setModalOpen] = useState(false); const [deletingId, setDeletingId] = useState(null); const resolvedEffectiveDate = effectiveDate || todayIso(); async function handleDelete(dependentId: string) { setDeletingId(dependentId); const result = await deleteEmployeeDependent({ dependent_id: dependentId, employee_id: employeeId, effective_date: resolvedEffectiveDate }); setDeletingId(null); if (result.success) { showToast("Angehörige:r entfernt."); router.refresh(); } else { showToast(result.error ?? "Fehler beim Entfernen.", "error"); } } return (

Angehörige

{dependents.length} Personen
{dependents.length === 0 ? (

Keine Angehörigen hinterlegt.

) : (
{dependents.map((d) => ( ))}
Name Verhältnis SVNR Geburtsdatum
{d.first_name} {d.last_name} {d.relationship} {d.sv_nummer ?? "–"} {fmtDate(d.birth_date)}
)} setModalOpen(false)} employeeId={employeeId} defaultEffectiveDate={resolvedEffectiveDate} />
); }