"use client"; import { useRouter } from "next/navigation"; import { useState } from "react"; import { InfoBubble, TAX_INFO } from "./InfoBubble"; type TransitionFormProps = { fromPhaseId: string; toPhaseId: string; planId: string; scenarioId: string; securities: Array<{ id: string; name: string }>; properties: Array<{ id: string; name: string }>; initial?: { securityActions: Array<{ securityId: string; action: "carry_over" | "payout"; payoutAmount?: number; }>; propertyActions: Array<{ propertyId: string; action: "carry_over" | "sell"; saleProceeds?: number; saleTaxRatePercent?: number; }>; }; }; export function TransitionForm({ fromPhaseId, toPhaseId, planId, scenarioId, securities, properties, initial, }: TransitionFormProps) { const router = useRouter(); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const [securityActions, setSecurityActions] = useState( securities.map((s) => { const existing = initial?.securityActions.find( (a) => a.securityId === s.id ); return { securityId: s.id, name: s.name, action: existing?.action ?? ("carry_over" as const), payoutAmount: existing?.payoutAmount ?? 0, }; }) ); const [propertyActions, setPropertyActions] = useState( properties.map((p) => { const existing = initial?.propertyActions.find( (a) => a.propertyId === p.id ); return { propertyId: p.id, name: p.name, action: existing?.action ?? ("carry_over" as const), saleProceeds: existing?.saleProceeds ?? 0, saleTaxRatePercent: existing?.saleTaxRatePercent ?? 15, }; }) ); async function handleSubmit(e: React.FormEvent) { e.preventDefault(); setLoading(true); setError(null); const res = await fetch("/api/transitions", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ fromPhaseId, toPhaseId, securityActions: securityActions.map( ({ securityId, action, payoutAmount }) => ({ securityId, action, payoutAmount: action === "payout" ? payoutAmount : undefined, }) ), propertyActions: propertyActions.map( ({ propertyId, action, saleProceeds, saleTaxRatePercent }) => ({ propertyId, action, saleProceeds: action === "sell" ? saleProceeds : undefined, saleTaxRatePercent: action === "sell" ? saleTaxRatePercent : undefined, }) ), }), }); setLoading(false); if (!res.ok) { const data = await res.json().catch(() => ({})); setError(data.error ?? "Speichern fehlgeschlagen"); return; } router.push(`/plans/${planId}/scenarios/${scenarioId}`); router.refresh(); } return (

Phasenübergang

Legen Sie fest, welche Wertschriften übernommen oder ausbezahlt werden und ob Immobilien verkauft werden.

{error && (

{error}

)} {securities.length > 0 && (

Wertschriften

{securityActions.map((item, idx) => (
{item.name} {item.action === "payout" && ( { const next = [...securityActions]; next[idx] = { ...next[idx], payoutAmount: Number(e.target.value), }; setSecurityActions(next); }} /> )}
))}
)} {properties.length > 0 && (

Immobilien {TAX_INFO.propertySale}

{propertyActions.map((item, idx) => (
{item.name} {item.action === "sell" && ( <> { const next = [...propertyActions]; next[idx] = { ...next[idx], saleProceeds: Number(e.target.value), }; setPropertyActions(next); }} /> { const next = [...propertyActions]; next[idx] = { ...next[idx], saleTaxRatePercent: Number(e.target.value), }; setPropertyActions(next); }} /> )}
))}
)}
); }