"use client" import * as React from "react" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" import { Textarea } from "@/components/ui/textarea" import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select" import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "@/components/ui/card" import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, AlertDialogTrigger, } from "@/components/ui/alert-dialog" import { Badge } from "@/components/ui/badge" import { Separator } from "@/components/ui/separator" import { Save, Trash2, AlertTriangle, Info, Calendar, User, Hash, Copy } from "lucide-react" import { toast } from "sonner" import { useRouter } from "next/navigation" import { type TemplateWithVariables } from "@/db/schema" import { deleteTemplate, duplicateTemplate, updateTemplateAction } from "../service" import { TEMPLATE_CATEGORY_OPTIONS, getCategoryDisplayName } from "../validations" interface TemplateSettingsProps { template: TemplateWithVariables onUpdate: (template: TemplateWithVariables) => void } export function TemplateSettings({ template, onUpdate }: TemplateSettingsProps) { const router = useRouter() const [isLoading, setIsLoading] = React.useState(false) const [formData, setFormData] = React.useState({ name: template.name, description: template.description || '', category: template.category || '', sampleData: JSON.stringify(template.sampleData || {}, null, 2) }) // 폼 데이터 업데이트 const updateFormData = (field: keyof typeof formData, value: string) => { setFormData(prev => ({ ...prev, [field]: value })) } // 기본 정보 저장 const handleSaveBasicInfo = async () => { setIsLoading(true) try { // 샘플 데이터 JSON 파싱 검증 let parsedSampleData = {} if (formData.sampleData.trim()) { try { parsedSampleData = JSON.parse(formData.sampleData) } catch (error) { toast.error('샘플 데이터 JSON 형식이 올바르지 않습니다.') setIsLoading(false) return } } const result = await updateTemplateAction(template.slug, { name: formData.name, description: formData.description || undefined, sampleData: parsedSampleData, updatedBy: 'current-user-id' // TODO: 실제 사용자 ID }) if (result.success) { toast.success('템플릿 설정이 저장되었습니다.') onUpdate({ ...template, name: formData.name, description: formData.description, sampleData: parsedSampleData, version: template.version + 1 }) } else { toast.error(result.error || '저장에 실패했습니다.') } } catch (error) { toast.error('저장 중 오류가 발생했습니다.') } finally { setIsLoading(false) } } // 템플릿 복제 const handleDuplicate = async () => { setIsLoading(true) try { const copyName = `${template.name} (복사본)` const copySlug = `${template.slug}-copy-${Date.now()}` const result = await duplicateTemplate( template.id, copyName, copySlug, 'current-user-id' // TODO: 실제 사용자 ID ) if (result.success && result.data) { toast.success('템플릿이 복제되었습니다.') router.push(`/evcp/templates/${result.data.slug}`) } else { toast.error(result.error || '복제에 실패했습니다.') } } catch (error) { toast.error('복제 중 오류가 발생했습니다.') } finally { setIsLoading(false) } } // 템플릿 삭제 const handleDelete = async () => { setIsLoading(true) try { const result = await deleteTemplate(template.id) if (result.success) { toast.success('템플릿이 삭제되었습니다.') router.push('/evcp/templates') } else { toast.error(result.error || '삭제에 실패했습니다.') } } catch (error) { toast.error('삭제 중 오류가 발생했습니다.') } finally { setIsLoading(false) } } // 샘플 데이터 포맷팅 const formatSampleData = () => { try { const parsed = JSON.parse(formData.sampleData) const formatted = JSON.stringify(parsed, null, 2) updateFormData('sampleData', formatted) toast.success('JSON이 포맷팅되었습니다.') } catch (error) { toast.error('유효한 JSON이 아닙니다.') } } // 기본 샘플 데이터 생성 const generateDefaultSampleData = () => { const defaultData: Record = {} template.variables.forEach(variable => { switch (variable.variableType) { case 'string': defaultData[variable.variableName] = variable.defaultValue || `샘플 ${variable.variableName}` break case 'number': defaultData[variable.variableName] = variable.defaultValue ? parseFloat(variable.defaultValue) : 123 break case 'boolean': defaultData[variable.variableName] = variable.defaultValue ? variable.defaultValue === 'true' : true break case 'date': defaultData[variable.variableName] = variable.defaultValue || new Date().toLocaleDateString('ko-KR') break } }) const formatted = JSON.stringify(defaultData, null, 2) updateFormData('sampleData', formatted) toast.success('기본 샘플 데이터가 생성되었습니다.') } return (
{/* 기본 정보 */} 기본 정보 템플릿의 기본 정보를 수정할 수 있습니다.
updateFormData('name', e.target.value)} placeholder="템플릿 이름을 입력하세요" />