"use client"; import { useState, useEffect } from "react"; import { cn } from "@/lib/utils"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Button } from "@/components/ui/button"; import { Checkbox } from "@/components/ui/checkbox"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { ChevronLeft, ChevronRight, Plus, Trash2, Loader2 } from "lucide-react"; import type { PopSearchConfig, SearchInputType, DatePresetOption, ModalSelectConfig, ModalDisplayStyle, ModalSearchMode, ModalFilterTab, } from "./types"; import { SEARCH_INPUT_TYPE_LABELS, DATE_PRESET_LABELS, MODAL_DISPLAY_STYLE_LABELS, MODAL_SEARCH_MODE_LABELS, MODAL_FILTER_TAB_LABELS, normalizeInputType, } from "./types"; import { tableManagementApi, getTableColumns } from "@/lib/api/tableManagement"; import type { TableInfo, ColumnTypeInfo } from "@/lib/api/tableManagement"; // ======================================== // 기본값 // ======================================== const DEFAULT_CONFIG: PopSearchConfig = { inputType: "text", fieldName: "", placeholder: "검색어 입력", debounceMs: 500, triggerOnEnter: true, labelPosition: "top", labelText: "", labelVisible: true, }; // ======================================== // 설정 패널 메인 // ======================================== interface ConfigPanelProps { config: PopSearchConfig | undefined; onUpdate: (config: PopSearchConfig) => void; } export function PopSearchConfigPanel({ config, onUpdate }: ConfigPanelProps) { const [step, setStep] = useState(0); const rawCfg = { ...DEFAULT_CONFIG, ...(config || {}) }; const cfg: PopSearchConfig = { ...rawCfg, inputType: normalizeInputType(rawCfg.inputType as string), }; const update = (partial: Partial) => { onUpdate({ ...cfg, ...partial }); }; const STEPS = ["기본 설정", "상세 설정"]; return (
{/* Stepper 헤더 */}
{STEPS.map((s, i) => ( ))}
{step === 0 && } {step === 1 && }
); } // ======================================== // STEP 1: 기본 설정 // ======================================== interface StepProps { cfg: PopSearchConfig; update: (partial: Partial) => void; } function StepBasicSettings({ cfg, update }: StepProps) { return (
update({ placeholder: e.target.value })} placeholder="입력 힌트 텍스트" className="h-8 text-xs" />
update({ labelVisible: Boolean(checked) })} />
{cfg.labelVisible !== false && ( <>
update({ labelText: e.target.value })} placeholder="예: 거래처명" className="h-8 text-xs" />
)}
); } // ======================================== // STEP 2: 타입별 상세 설정 // ======================================== function StepDetailSettings({ cfg, update }: StepProps) { const normalized = normalizeInputType(cfg.inputType as string); switch (normalized) { case "text": case "number": return ; case "select": return ; case "date-preset": return ; case "modal": return ; case "toggle": return (

토글은 추가 설정이 없습니다. ON/OFF 값이 바로 전달됩니다.

); default: return (

{cfg.inputType} 타입의 상세 설정은 후속 구현 예정입니다.

); } } // ======================================== // text/number 상세 설정 // ======================================== function TextDetailSettings({ cfg, update }: StepProps) { return (
update({ debounceMs: Math.max(0, Number(e.target.value)) })} min={0} max={5000} step={100} className="h-8 text-xs" />

입력 후 대기 시간. 0이면 즉시 발행 (권장: 300~500)

update({ triggerOnEnter: Boolean(checked) })} />
); } // ======================================== // select 상세 설정 // ======================================== function SelectDetailSettings({ cfg, update }: StepProps) { const options = cfg.options || []; const addOption = () => { update({ options: [...options, { value: `opt_${options.length + 1}`, label: `옵션 ${options.length + 1}` }], }); }; const removeOption = (index: number) => { update({ options: options.filter((_, i) => i !== index) }); }; const updateOption = (index: number, field: "value" | "label", val: string) => { update({ options: options.map((opt, i) => (i === index ? { ...opt, [field]: val } : opt)) }); }; return (
{options.length === 0 && (

옵션이 없습니다. 아래 버튼으로 추가하세요.

)} {options.map((opt, i) => (
updateOption(i, "value", e.target.value)} placeholder="값" className="h-7 flex-1 text-[10px]" /> updateOption(i, "label", e.target.value)} placeholder="라벨" className="h-7 flex-1 text-[10px]" />
))}
); } // ======================================== // date-preset 상세 설정 // ======================================== function DatePresetDetailSettings({ cfg, update }: StepProps) { const ALL_PRESETS: DatePresetOption[] = ["today", "this-week", "this-month", "custom"]; const activePresets = cfg.datePresets || ["today", "this-week", "this-month"]; const togglePreset = (preset: DatePresetOption) => { const next = activePresets.includes(preset) ? activePresets.filter((p) => p !== preset) : [...activePresets, preset]; update({ datePresets: next.length > 0 ? next : ["today"] }); }; return (
{ALL_PRESETS.map((preset) => (
togglePreset(preset)} />
))} {activePresets.includes("custom") && (

"직접" 선택 시 날짜 입력 UI가 표시됩니다 (후속 구현)

)}
); } // ======================================== // modal 상세 설정 // ======================================== const DEFAULT_MODAL_CONFIG: ModalSelectConfig = { displayStyle: "table", displayField: "", valueField: "", searchMode: "contains", }; function ModalDetailSettings({ cfg, update }: StepProps) { const mc: ModalSelectConfig = { ...DEFAULT_MODAL_CONFIG, ...(cfg.modalConfig || {}) }; const updateModal = (partial: Partial) => { update({ modalConfig: { ...mc, ...partial } }); }; const [tables, setTables] = useState([]); const [columns, setColumns] = useState([]); const [tablesLoading, setTablesLoading] = useState(false); const [columnsLoading, setColumnsLoading] = useState(false); useEffect(() => { let cancelled = false; setTablesLoading(true); tableManagementApi.getTableList().then((res) => { if (!cancelled && res.success && res.data) setTables(res.data); }).finally(() => !cancelled && setTablesLoading(false)); return () => { cancelled = true; }; }, []); useEffect(() => { if (!mc.tableName) { setColumns([]); return; } let cancelled = false; setColumnsLoading(true); getTableColumns(mc.tableName).then((res) => { if (!cancelled && res.success && res.data?.columns) setColumns(res.data.columns); }).finally(() => !cancelled && setColumnsLoading(false)); return () => { cancelled = true; }; }, [mc.tableName]); const toggleArrayItem = (field: "displayColumns" | "searchColumns", col: string) => { const current = mc[field] || []; const next = current.includes(col) ? current.filter((c) => c !== col) : [...current, col]; updateModal({ [field]: next }); }; const toggleFilterTab = (tab: ModalFilterTab) => { const current = mc.filterTabs || []; const next = current.includes(tab) ? current.filter((t) => t !== tab) : [...current, tab]; updateModal({ filterTabs: next }); }; const updateColumnLabel = (colName: string, label: string) => { const current = mc.columnLabels || {}; if (!label.trim()) { const { [colName]: _, ...rest } = current; updateModal({ columnLabels: Object.keys(rest).length > 0 ? rest : undefined }); } else { updateModal({ columnLabels: { ...current, [colName]: label } }); } }; const selectedDisplayCols = mc.displayColumns || []; return (
{/* 보여주기 방식 */}

테이블: 표 형태 / 아이콘: 아이콘 카드 형태

{/* 데이터 테이블 */}
{tablesLoading ? (
테이블 목록 로딩...
) : ( )}
{mc.tableName && ( <> {/* 표시할 컬럼 */}
{columnsLoading ? (
컬럼 로딩...
) : (
{columns.map((col) => (
toggleArrayItem("displayColumns", col.columnName)} />
))}
)}
{/* 컬럼 헤더 라벨 편집 (표시할 컬럼이 선택된 경우만) */} {selectedDisplayCols.length > 0 && (
{selectedDisplayCols.map((colName) => { const colInfo = columns.find((c) => c.columnName === colName); const defaultLabel = colInfo?.displayName || colName; return (
{colName} updateColumnLabel(colName, e.target.value)} placeholder={defaultLabel} className="h-6 flex-1 text-[10px]" />
); })}

비워두면 기본 컬럼명이 사용됩니다

)} {/* 검색 대상 컬럼 */}
{columns.map((col) => (
toggleArrayItem("searchColumns", col.columnName)} />
))}
{/* 검색 방식 */}

포함: 어디든 일치 / 시작: 앞에서 일치 / 같음: 정확히 일치

{/* 필터 탭 (가나다/ABC) */}
{(Object.entries(MODAL_FILTER_TAB_LABELS) as [ModalFilterTab, string][]).map(([key, label]) => (
toggleFilterTab(key)} />
))}

모달 상단에 초성(가나다) / 알파벳(ABC) 필터 탭 표시

{/* 검색창에 보일 값 */}

선택 후 검색 입력란에 표시될 값 (예: 회사명)

{/* 필터에 쓸 값 */}

연결된 리스트를 필터할 때 사용할 값 (예: 회사코드)

)}
); }