import { useState, useEffect, useCallback } from 'react'; import { type OilSpillReportData, type ReportType } from './OilSpillReportTemplate'; import { templateTypes, type TemplateType, ANALYSIS_SEP, ANALYSIS_FIELD_ORDER } from './reportTypes'; import { saveReport } from '../services/reportsApi'; interface TemplateEditPageProps { reportType: ReportType; initialData: OilSpillReportData; onSave: () => void; onBack: () => void; } // ── Textarea-type keys that map to analysis field ────────── const ANALYSIS_KEYS = new Set([ 'initialResponse', 'futurePlan', 'responseStatus', 'suggestions', 'spreadSummary', 'responseDetail', 'damageReport', 'futurePlanDetail', 'spreadAnalysis', 'analysis', ]) function getInitialValue(key: string, data: OilSpillReportData, reportType: ReportType): string { if (key.startsWith('incident.')) { const field = key.slice('incident.'.length) as keyof OilSpillReportData['incident']; return (data.incident as Record)[field] ?? ''; } if (key === 'author') return data.author; if (ANALYSIS_KEYS.has(key)) { const fields = ANALYSIS_FIELD_ORDER[reportType]; if (fields) { const idx = fields.indexOf(key); if (idx < 0) return ''; const analysis = data.analysis || ''; if (!analysis.includes(ANALYSIS_SEP)) { // 레거시 데이터: 첫 번째 필드에만 배분 return idx === 0 ? analysis : ''; } const parts = analysis.split(ANALYSIS_SEP); return parts[idx] || ''; } return data.analysis || ''; } if (key.startsWith('__')) return ''; return ''; } function buildAnalysis(reportType: ReportType, formData: Record): string { const fields = ANALYSIS_FIELD_ORDER[reportType]; if (fields) { return fields.map(k => formData[k] || '').join(ANALYSIS_SEP); } switch (reportType) { case '예측보고서': return formData['analysis'] || ''; default: return ''; } } // ── Section content renderer ──────────────────────────────── interface SectionBlockProps { section: TemplateType['sections'][number]; getVal: (key: string) => string; setVal: (key: string, val: string) => void; } function SectionBlock({ section, getVal, setVal }: SectionBlockProps) { return (
해양오염방제지원시스템
{/* Section header */}
{section.title}
{/* Fields table */} {section.fields.map((field, fIdx) => { // ── Read-only auto-calculated keys ── if (field.key.startsWith('__')) { return ( ); } // ── Textarea (no label = full-width, or labeled textarea) ── if (field.type === 'textarea' || !field.label) { return ( {field.label && ( )}
{section.title} — 자동 계산 데이터 (확인 전용)
{field.label}