import React from 'react'; import { motion } from 'framer-motion'; import { AlertCircle } from 'lucide-react'; /** * TemplateForm — Dynamically renders form fields based on a template's `fields` array. * * ARCHITECTURE: * This component is the core of the structured template system. Instead of dumping * a markdown blob into a textarea, it renders proper form inputs (text, textarea, * select, date, checkbox) that guide the user through structured data entry. * * At submit time, the parent component calls `serializeFieldsToText()` from * ticketTemplates.js to convert the structured data back into formatted text * for backend API compatibility. * * Props: * fields - Array of field definitions from the template * values - Object of current field values keyed by field.key * onChange - Callback(key, value) when any field value changes * disabled - Whether to disable all fields (during submission) * accentColor - CSS color class prefix for styling (e.g., 'emerald') */ const TemplateForm = ({ fields, values, onChange, disabled = false, accentColor = 'emerald' }) => { if (!fields || fields.length === 0) return null; // Staggered animation for field entries const containerVariants = { hidden: { opacity: 0 }, visible: { opacity: 1, transition: { staggerChildren: 0.06 }, }, }; const itemVariants = { hidden: { opacity: 0, y: 12 }, visible: { opacity: 1, y: 0, transition: { duration: 0.3, ease: 'easeOut' } }, }; // Shared input style classes const inputBase = `w-full rounded-xl border bg-gray-50/50 focus:bg-white transition-all text-sm px-4 py-3 outline-none border-gray-100 focus:border-${accentColor}-300 focus:ring-2 focus:ring-${accentColor}-100 disabled:opacity-50 disabled:cursor-not-allowed`; return ( {fields.map((field) => { const value = values[field.key] ?? (field.type === 'checkbox' ? false : ''); return ( {/* Field label */} {field.type !== 'checkbox' && ( )} {/* ── Text input ── */} {field.type === 'text' && ( onChange(field.key, e.target.value)} placeholder={field.placeholder || ''} disabled={disabled} className={inputBase} /> )} {/* ── Textarea ── */} {field.type === 'textarea' && (