import { useState } from 'react' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' import { Textarea } from '@/components/ui/textarea' import { Label } from '@/components/ui/label' import { Trash2, Plus, ChevronUp, ChevronDown } from 'lucide-react' interface TemplateItemRow { id: string title: string description: string } interface TemplateSectionRow { id: string title: string description: string items: TemplateItemRow[] } interface Props { sections: TemplateSectionRow[] onChange: (sections: TemplateSectionRow[]) => void } function uid() { return Math.random().toString(36).slice(2) } export function TemplateSectionBuilder({ sections, onChange }: Props) { function addSection() { onChange([...sections, { id: uid(), title: '', description: '', items: [] }]) } function removeSection(idx: number) { onChange(sections.filter((_, i) => i !== idx)) } function moveSection(idx: number, dir: -1 | 1) { const next = [...sections] const [removed] = next.splice(idx, 1) next.splice(idx + dir, 0, removed) onChange(next) } function updateSection(idx: number, field: 'title' | 'description', value: string) { onChange(sections.map((s, i) => (i === idx ? { ...s, [field]: value } : s))) } function addItem(sIdx: number) { onChange(sections.map((s, i) => i === sIdx ? { ...s, items: [...s.items, { id: uid(), title: '', description: '' }] } : s, )) } function removeItem(sIdx: number, iIdx: number) { onChange(sections.map((s, i) => i === sIdx ? { ...s, items: s.items.filter((_, j) => j !== iIdx) } : s, )) } function moveItem(sIdx: number, iIdx: number, dir: -1 | 1) { onChange(sections.map((s, i) => { if (i !== sIdx) return s const next = [...s.items] const [removed] = next.splice(iIdx, 1) next.splice(iIdx + dir, 0, removed) return { ...s, items: next } })) } function updateItem(sIdx: number, iIdx: number, field: 'title' | 'description', value: string) { onChange(sections.map((s, i) => i === sIdx ? { ...s, items: s.items.map((item, j) => (j === iIdx ? { ...item, [field]: value } : item)) } : s, )) } return (
{sections.map((section, sIdx) => (
updateSection(sIdx, 'title', e.target.value)} required />