import { Fragment, useState } from 'react' import { Button, EmptyState } from '../ui' import { deadlineTone, formatDate, nextCancellationDate } from '../../lib/legal' import ContractForm from './ContractForm' import { FileLinks, todayIso } from './legalUi' const TONE_ORDER = { red: 0, orange: 1, normal: 2, missing: 3, grey: 4 } const euro = new Intl.NumberFormat('de-DE', { style: 'currency', currency: 'EUR' }) function costLabel(c) { if (c.cost === null || c.cost === undefined) return '–' return `${euro.format(c.cost)}/${c.costInterval === 'year' ? 'Jahr' : 'Monat'}` } function termLabel(c) { if (c.termMonths === null || c.termMonths === undefined) return '–' if (c.termMonths === 0) return 'jederzeit kündbar' const renew = c.renewalMonths ? `, dann je ${c.renewalMonths} Monate` : ', endet danach' return `${c.termMonths} Monate${renew}` } function noticeLabel(c) { if (c.noticeValue === null || c.noticeValue === undefined) return '–' return `${c.noticeValue} ${c.noticeUnit === 'days' ? 'Tage' : 'Monate'}` } function deadlineLabel(r) { switch (r.state) { case 'active': return `kündbar bis ${formatDate(r.noticeBy)} (Periode endet ${formatDate(r.periodEnd)})` case 'anytime': return 'jederzeit kündbar' case 'ends': return `endet am ${formatDate(r.periodEnd)}` case 'ended': return `beendet seit ${formatDate(r.periodEnd)}` case 'cancelled': return r.periodEnd ? `gekündigt zum ${formatDate(r.periodEnd)}` : 'gekündigt' default: return 'Fristen fehlen' } } export default function ProviderContractsTab({ contracts, fileNames, saveContract, deleteContract }) { const [open, setOpen] = useState(null) const [editing, setEditing] = useState(null) const [cancelling, setCancelling] = useState(null) const [endsAt, setEndsAt] = useState('') const [actionError, setActionError] = useState(null) const today = todayIso() const rows = contracts .map((c) => { const result = nextCancellationDate(c, today) return { c, result, tone: deadlineTone(result, today) } }) .sort((a, b) => TONE_ORDER[a.tone] - TONE_ORDER[b.tone] || (a.result.noticeBy || a.result.periodEnd || '9999').localeCompare(b.result.noticeBy || b.result.periodEnd || '9999') ) const run = async (fn) => { setActionError(null) try { await fn() } catch (err) { setActionError(err.message) } } const confirmCancel = (c) => run(async () => { await saveContract({ cancelledAt: today, endsAt: endsAt || null }, [], c.$id) setCancelling(null) }) const remove = (c) => { if (window.confirm(`Vertrag „${c.provider}“ wirklich löschen?`)) run(() => deleteContract(c.$id)) } return (
{actionError &&
{actionError}
} {rows.length === 0 ? ( ) : ( {rows.map(({ c, result, tone }) => ( setOpen(open === c.$id ? null : c.$id)}> {open === c.$id && ( )} ))}
AnbieterWofürKostenLaufzeitKündigungsfristNächster Termin
{c.provider} {c.purpose || '–'} {costLabel(c)} {termLabel(c)} {noticeLabel(c)} {deadlineLabel(result)}
{c.startDate &&
Beginn: {formatDate(c.startDate)}
} {c.note &&
{c.note}
}
{!c.cancelledAt && cancelling !== c.$id && ( )} {cancelling === c.$id && ( setEndsAt(e.target.value)} /> )}
)} {editing && ( saveContract(doc, files, editing === 'new' ? undefined : editing.$id)} onClose={() => setEditing(null)} /> )}
) }