"use client"; import React, { useState, useEffect, useCallback } from "react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Checkbox } from "@/components/ui/checkbox"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import { Accordion, AccordionContent, AccordionItem, AccordionTrigger } from "@/components/ui/accordion"; import { Badge } from "@/components/ui/badge"; import { Separator } from "@/components/ui/separator"; import { Plus, Trash2, GripVertical, ChevronUp, ChevronDown, Settings, Database, Layout, Table, } from "lucide-react"; import { cn } from "@/lib/utils"; import { apiClient } from "@/lib/api/client"; import { getNumberingRules } from "@/lib/api/numberingRule"; import { UniversalFormModalConfig, UniversalFormModalConfigPanelProps, FormSectionConfig, FormFieldConfig, MODAL_SIZE_OPTIONS, SECTION_TYPE_OPTIONS, } from "./types"; import { defaultSectionConfig, defaultTableSectionConfig, generateSectionId, } from "./config"; // 모달 import import { FieldDetailSettingsModal } from "./modals/FieldDetailSettingsModal"; import { SaveSettingsModal } from "./modals/SaveSettingsModal"; import { SectionLayoutModal } from "./modals/SectionLayoutModal"; import { TableSectionSettingsModal } from "./modals/TableSectionSettingsModal"; // 도움말 텍스트 컴포넌트 const HelpText = ({ children }: { children: React.ReactNode }) => (

{children}

); export function UniversalFormModalConfigPanel({ config, onChange }: UniversalFormModalConfigPanelProps) { // 테이블 목록 const [tables, setTables] = useState<{ name: string; label: string }[]>([]); const [tableColumns, setTableColumns] = useState<{ [tableName: string]: { name: string; type: string; label: string }[]; }>({}); // 채번규칙 목록 const [numberingRules, setNumberingRules] = useState<{ id: string; name: string }[]>([]); // 모달 상태 const [saveSettingsModalOpen, setSaveSettingsModalOpen] = useState(false); const [sectionLayoutModalOpen, setSectionLayoutModalOpen] = useState(false); const [fieldDetailModalOpen, setFieldDetailModalOpen] = useState(false); const [tableSectionSettingsModalOpen, setTableSectionSettingsModalOpen] = useState(false); const [selectedSection, setSelectedSection] = useState(null); const [selectedField, setSelectedField] = useState(null); // 테이블 목록 로드 useEffect(() => { loadTables(); loadNumberingRules(); }, []); // 저장 테이블 변경 시 컬럼 로드 useEffect(() => { if (config.saveConfig.tableName) { loadTableColumns(config.saveConfig.tableName); } }, [config.saveConfig.tableName]); const loadTables = async () => { try { const response = await apiClient.get("/table-management/tables"); const data = response.data?.data; if (response.data?.success && Array.isArray(data)) { setTables( data.map((t: { tableName?: string; table_name?: string; tableLabel?: string; table_label?: string }) => ({ name: t.tableName || t.table_name || "", label: t.tableLabel || t.table_label || t.tableName || t.table_name || "", })), ); } } catch (error) { console.error("테이블 목록 로드 실패:", error); } }; const loadTableColumns = async (tableName: string) => { if (!tableName || (tableColumns[tableName] && tableColumns[tableName].length > 0)) return; try { const response = await apiClient.get(`/table-management/tables/${tableName}/columns`); // API 응답 구조: { success, data: { columns: [...], total, page, ... } } const columns = response.data?.data?.columns; if (response.data?.success && Array.isArray(columns)) { setTableColumns((prev) => ({ ...prev, [tableName]: columns.map( (c: { columnName?: string; column_name?: string; dataType?: string; data_type?: string; displayName?: string; columnComment?: string; column_comment?: string; }) => ({ name: c.columnName || c.column_name || "", type: c.dataType || c.data_type || "text", label: c.displayName || c.columnComment || c.column_comment || c.columnName || c.column_name || "", }), ), })); } } catch (error) { console.error(`테이블 컬럼 로드 실패 (${tableName}):`, error); } }; const loadNumberingRules = async () => { try { const response = await getNumberingRules(); const data = response?.data; if (response?.success && Array.isArray(data)) { const rules = data.map( (r: { id?: string | number; ruleId?: string; rule_id?: string; name?: string; ruleName?: string; rule_name?: string; }) => ({ id: String(r.id || r.ruleId || r.rule_id || ""), name: r.name || r.ruleName || r.rule_name || "", }), ); setNumberingRules(rules); } } catch (error) { console.error("채번규칙 목록 로드 실패:", error); } }; // 설정 업데이트 헬퍼 const updateModalConfig = useCallback( (updates: Partial) => { onChange({ ...config, modal: { ...config.modal, ...updates }, }); }, [config, onChange], ); // 섹션 관리 const addSection = useCallback((type: "fields" | "table" = "fields") => { const newSection: FormSectionConfig = { ...defaultSectionConfig, id: generateSectionId(), title: type === "table" ? `테이블 섹션 ${config.sections.length + 1}` : `섹션 ${config.sections.length + 1}`, type, fields: type === "fields" ? [] : undefined, tableConfig: type === "table" ? { ...defaultTableSectionConfig } : undefined, }; onChange({ ...config, sections: [...config.sections, newSection], }); }, [config, onChange]); // 섹션 타입 변경 const changeSectionType = useCallback( (sectionId: string, newType: "fields" | "table") => { onChange({ ...config, sections: config.sections.map((s) => { if (s.id !== sectionId) return s; if (newType === "table") { return { ...s, type: "table", fields: undefined, tableConfig: { ...defaultTableSectionConfig }, }; } else { return { ...s, type: "fields", fields: [], tableConfig: undefined, }; } }), }); }, [config, onChange] ); // 테이블 섹션 설정 모달 열기 const handleOpenTableSectionSettings = (section: FormSectionConfig) => { setSelectedSection(section); setTableSectionSettingsModalOpen(true); }; const updateSection = useCallback( (sectionId: string, updates: Partial) => { onChange({ ...config, sections: config.sections.map((s) => (s.id === sectionId ? { ...s, ...updates } : s)), }); }, [config, onChange], ); const removeSection = useCallback( (sectionId: string) => { onChange({ ...config, sections: config.sections.filter((s) => s.id !== sectionId), }); }, [config, onChange], ); const moveSectionUp = useCallback( (index: number) => { if (index <= 0) return; const newSections = [...config.sections]; [newSections[index - 1], newSections[index]] = [newSections[index], newSections[index - 1]]; onChange({ ...config, sections: newSections }); }, [config, onChange], ); const moveSectionDown = useCallback( (index: number) => { if (index >= config.sections.length - 1) return; const newSections = [...config.sections]; [newSections[index], newSections[index + 1]] = [newSections[index + 1], newSections[index]]; onChange({ ...config, sections: newSections }); }, [config, onChange], ); // 필드 타입별 색상 const getFieldTypeColor = (fieldType: FormFieldConfig["fieldType"]): string => { switch (fieldType) { case "text": case "email": case "password": case "tel": return "text-blue-600 bg-blue-50 border-blue-200"; case "number": return "text-cyan-600 bg-cyan-50 border-cyan-200"; case "date": case "datetime": return "text-purple-600 bg-purple-50 border-purple-200"; case "select": return "text-green-600 bg-green-50 border-green-200"; case "checkbox": return "text-pink-600 bg-pink-50 border-pink-200"; case "textarea": return "text-orange-600 bg-orange-50 border-orange-200"; default: return "text-gray-600 bg-gray-50 border-gray-200"; } }; // 섹션 레이아웃 모달 열기 const handleOpenSectionLayout = (section: FormSectionConfig) => { setSelectedSection(section); setSectionLayoutModalOpen(true); }; // 필드 상세 설정 모달 열기 const handleOpenFieldDetail = (section: FormSectionConfig, field: FormFieldConfig) => { setSelectedSection(section); setSelectedField(field); setFieldDetailModalOpen(true); }; return (
{/* 모달 기본 설정 */}
모달 기본 설정
updateModalConfig({ title: e.target.value })} className="h-9 text-sm w-full max-w-full" /> 모달 상단에 표시될 제목입니다
모달 창의 크기를 선택하세요
{/* 저장 버튼 표시 설정 */}
updateModalConfig({ showSaveButton: checked === true })} />
체크 해제 시 모달 하단의 저장 버튼이 숨겨집니다
updateModalConfig({ saveButtonText: e.target.value })} className="h-9 text-sm w-full max-w-full" />
updateModalConfig({ cancelButtonText: e.target.value })} className="h-9 text-sm w-full max-w-full" />
{/* 저장 설정 */}
저장 설정

{config.saveConfig.tableName || "(미설정)"}

{config.saveConfig.customApiSave?.enabled && config.saveConfig.customApiSave?.multiTable?.enabled && ( 다중 테이블 모드 )}
데이터를 저장할 테이블과 방식을 설정합니다.
"저장 설정 열기"를 클릭하여 상세 설정을 변경하세요.
{/* 섹션 구성 */}
섹션 구성 {config.sections.length}개
{/* 섹션 추가 버튼들 */}