'use client' import React, { useState, useMemo } from 'react' interface Question { id: string mc_id: string mc_name: string question: string question_type: string evidence_required: boolean pass_criteria: string[] fail_criteria: string[] severity: string regulation: string depends_on?: string } interface Answer { question_id: string value: unknown comment: string status: string } interface Props { questions: Question[] answers: Answer[] onAnswer: (questionId: string, value: unknown, comment: string) => void onSkip: (questionId: string) => void } const SEVERITY_COLORS: Record = { HIGH: 'bg-red-100 text-red-700 border-red-200', MEDIUM: 'bg-yellow-100 text-yellow-700 border-yellow-200', LOW: 'bg-green-100 text-green-700 border-green-200', } export function QuestionnaireView({ questions, answers, onAnswer, onSkip }: Props) { const [comments, setComments] = useState>({}) const [showCriteria, setShowCriteria] = useState(null) const answerMap = useMemo(() => { const m: Record = {} for (const a of answers) { m[a.question_id] = a } return m }, [answers]) // Filter visible questions (dependency check) const visibleQuestions = useMemo(() => { return questions.filter(q => { if (!q.depends_on) return true const depAnswer = answerMap[q.depends_on] if (!depAnswer) return false return depAnswer.value === true || depAnswer.value === 'yes' || depAnswer.value === 'ja' }) }, [questions, answerMap]) // Group by regulation const grouped = useMemo(() => { const groups: Record = {} for (const q of visibleQuestions) { const key = q.regulation || 'Allgemein' if (!groups[key]) groups[key] = [] groups[key].push(q) } return groups }, [visibleQuestions]) return (
{Object.entries(grouped).map(([reg, qs]) => (

{reg} {qs.length} Fragen

{qs.map((q, idx) => { const existing = answerMap[q.id] const isAnswered = !!existing && existing.status !== 'skipped' const isSkipped = existing?.status === 'skipped' const isPassed = existing?.value === true || existing?.value === 'yes' return (
{q.id}

{q.question}

{q.severity} {q.evidence_required && ( Nachweis )}
{q.mc_name && (

MC: {q.mc_name} {q.mc_id && `(${q.mc_id})`}

)} {/* Criteria toggle */} {showCriteria === q.id && (
{q.pass_criteria?.length > 0 && (
Pass
{q.pass_criteria.map((c, i) => (
{c}
))}
)} {q.fail_criteria?.length > 0 && (
Fail
{q.fail_criteria.map((c, i) => (
{c}
))}
)}
)} {/* Answer controls */} {!isAnswered && !isSkipped && (
setComments(prev => ({ ...prev, [q.id]: e.target.value }))} className="flex-1 px-3 py-1.5 border border-gray-200 rounded-lg text-sm focus:ring-1 focus:ring-blue-500" />
)} {/* Already answered */} {isAnswered && (
{isPassed ? 'Ja' : 'Nein'} {existing.comment && ( — {existing.comment} )}
)} {isSkipped && (
Uebersprungen
)}
) })}
))}
) }