"use client"; import { Plus, Trash2 } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { cn } from "@/lib/utils"; import type { ConditionalConfig, ConditionalRule } from "@/lib/api/batch"; interface ConditionalEditorProps { // 평가 대상 필드명 (REST API 응답 필드명 또는 DB 컬럼명) evaluateField: string; // 선택 가능한 필드 후보 — REST API 응답 필드 목록 등 fieldOptions: string[]; // 현재 conditional 규칙 config: ConditionalConfig; onEvaluateFieldChange: (field: string) => void; onConfigChange: (cfg: ConditionalConfig) => void; } // vexplor_rps 의 ConditionalEditor 1:1 포팅. // 예: enrlFg 가 "J01" → "active", "J05" → "inactive", 나머지 → "" 식 lookup. export function ConditionalEditor({ evaluateField, fieldOptions, config, onEvaluateFieldChange, onConfigChange, }: ConditionalEditorProps) { const cfg: ConditionalConfig = config?.rules ? config : { rules: [{ when: "", then: "" }], default: "" }; const isEvaluateFieldMissing = !evaluateField; const updateRule = (idx: number, patch: Partial) => { const rules = cfg.rules.map((r, i) => (i === idx ? { ...r, ...patch } : r)); onConfigChange({ ...cfg, rules }); }; const addRule = () => onConfigChange({ ...cfg, rules: [...cfg.rules, { when: "", then: "" }] }); const removeRule = (idx: number) => onConfigChange({ ...cfg, rules: cfg.rules.filter((_, i) => i !== idx) }); return (
평가 필드 *

이 필드 값에 따라 아래 규칙으로 변환됩니다 (예: status 컬럼에{" "} enrlFg 의 값을 보고 J01→active 변환 시 평가 필드 ={" "} enrlFg)

{cfg.rules.map((rule, idx) => (
값이 updateRule(idx, { when: e.target.value })} placeholder="예: J01" className="h-7 flex-1 text-xs" /> updateRule(idx, { then: e.target.value })} placeholder="저장값" className="h-7 flex-1 text-xs" />
))}
매칭 없음(기본) onConfigChange({ ...cfg, default: e.target.value })} placeholder="예: 0 또는 빈값" className="h-7 flex-1 text-xs" />
); } // 빈 conditionalConfig 기본값 — page 측에서 모드 전환 시 사용 export const emptyConditionalConfig = (): ConditionalConfig => ({ rules: [{ when: "", then: "" }], default: "", }); // 저장된 mapping_config (string|object|null) 를 안전하게 ConditionalConfig 로 normalize export function normalizeConditionalConfig(raw: unknown): ConditionalConfig { if (!raw) return emptyConditionalConfig(); let parsed: any = raw; if (typeof raw === "string") { try { parsed = JSON.parse(raw); } catch { return emptyConditionalConfig(); } } return { rules: Array.isArray(parsed?.rules) ? parsed.rules : [], default: typeof parsed?.default === "string" ? parsed.default : "", }; }