import { createFileRoute, Link } from '@tanstack/react-router' import { RequireAuth } from '../../../../auth' import { ActionIcon, Alert, Anchor, Badge, Box, Breadcrumbs, Button, Card, Container, Divider, Group, Loader, Paper, SimpleGrid, Stack, Text, Title, Tooltip, } from '@pikku/mantine/core' import { m, mKey } from '@/i18n/messages' import { useLocale, getLocale } from '@/i18n/config' import { asI18n } from '@pikku/react' import { useMemo, useState } from 'react' import { usePikkuQuery, usePikkuMutation } from '@germantax/functions-sdk/pikku/api.gen' type FieldType = 'text' | 'textarea' | 'number' | 'date' | 'currency' const formatValue = ( value: unknown, type: FieldType, locale: string, ): string => { if (value === undefined || value === null || value === '') return '—' if (type === 'currency') { const n = typeof value === 'number' ? value : Number(value) if (Number.isFinite(n)) { return new Intl.NumberFormat(locale, { style: 'currency', currency: 'EUR', maximumFractionDigits: 2, }).format(n) } } if (type === 'date') { const d = new Date(String(value)) if (!Number.isNaN(d.getTime())) { return new Intl.DateTimeFormat(locale, { dateStyle: 'medium' }).format(d) } } if (type === 'number') { const n = typeof value === 'number' ? value : Number(value) if (Number.isFinite(n)) return new Intl.NumberFormat(locale).format(n) } return String(value) } const formatTimestamp = (iso: string | null | undefined, locale: string) => { if (!iso) return null const d = new Date(iso) if (Number.isNaN(d.getTime())) return null return new Intl.DateTimeFormat(locale, { dateStyle: 'medium', timeStyle: 'short', }).format(d) } const statusColor = (status: string) => status === 'published' ? 'green' : status === 'archived' ? 'gray' : 'blue' function ResolutionPage() { const { resolutionId, companyId } = Route.useParams() useLocale() const locale = getLocale() || 'en' const r = usePikkuQuery('getResolution', { resolutionId }) const company = usePikkuQuery('getCompany', { companyId }) const templates = usePikkuQuery( 'listResolutionTemplates', undefined as never, ) const pdfUrl = usePikkuMutation('getResolutionPdfUrl') const publish = usePikkuMutation('publishResolution', { onSuccess: () => r.refetch(), }) const archive = usePikkuMutation('archiveResolution', { onSuccess: () => r.refetch(), }) const [embeddedPdf, setEmbeddedPdf] = useState(null) const isMd = company.data?.myRole === 'managing_director' const template = useMemo( () => templates.data?.find((tpl) => tpl.key === r.data?.templateKey), [templates.data, r.data?.templateKey], ) const labelLang = locale.startsWith('de') ? 'labelDe' : 'labelEn' const titleLang = locale.startsWith('de') ? 'titleDe' : 'titleEn' const fieldRows = useMemo(() => { if (!r.data) return [] const values = r.data.fieldValues as Record if (template?.fields?.length) { return template.fields.map((f) => ({ key: f.key, label: (f as any)[labelLang] ?? f.key, value: formatValue(values?.[f.key], f.type as FieldType, locale), })) } return Object.entries(values ?? {}).map(([k, v]) => ({ key: k, label: k, value: formatValue(v, 'text', locale), })) }, [r.data, template, labelLang, locale]) const openPdf = async (embed: boolean) => { const res = await pdfUrl.mutateAsync({ resolutionId }) if (embed) { setEmbeddedPdf(res.url) } else { window.open(res.url, '_blank', 'noopener,noreferrer') } } const createdAt = formatTimestamp(r.data?.createdAt, locale) const publishedAt = formatTimestamp(r.data?.publishedAt, locale) return ( {m.app_nav_home({ defaultValue: 'Home' })} {asI18n(company.data?.name ?? '…')} {m.app_resolutions_resolution({ defaultValue: 'Resolution' })} {r.error && {asI18n(String(r.error.message))}} {!r.data && !r.error && ( )} {r.data && ( <> {template ? (template as any)[titleLang] : r.data.templateKey} {asI18n(r.data.title)} {mKey(`app.resolutions.${r.data.status}`)} {m.app_resolutions_created({ defaultValue: 'Created' })} {asI18n(createdAt ?? '—')} {m.app_resolutions_published({ defaultValue: 'Published' })} {asI18n(publishedAt ?? '—')} {m.app_resolutions_id({ defaultValue: 'ID' })} {asI18n(`${resolutionId.slice(0, 8)}…`)} {isMd && r.data.status === 'draft' && ( )} {r.data.status === 'published' && ( <> )} {isMd && r.data.status !== 'archived' && ( )} {m.app_resolutions_fields({ defaultValue: 'Details' })} {template && ( {(template as any).descriptionDe} )} {fieldRows.length === 0 ? ( {m.app_resolutions_no_fields({ defaultValue: 'No fields recorded.', })} ) : ( {fieldRows.map((row, i) => ( {i > 0 && } {row.label} {asI18n(row.value)} ))} )} {embeddedPdf && ( {m.app_resolutions_pdf_preview({ defaultValue: 'PDF preview', })} setEmbeddedPdf(null)} aria-label={m.app_common_close({ defaultValue: 'Close' })} > ×