"use client"; import React, { useState, useEffect } from "react"; import { Search } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, DialogFooter, } from "@/components/ui/dialog"; import { WorkItemDetail, DetailTypeDefinition, InspectionStandard } from "../types"; import { InspectionStandardLookup } from "./InspectionStandardLookup"; interface DetailFormModalProps { open: boolean; onClose: () => void; onSubmit: (data: Partial) => void; detailTypes: DetailTypeDefinition[]; editData?: WorkItemDetail | null; mode: "add" | "edit"; } const LOOKUP_TARGETS = [ { value: "equipment", label: "설비정보" }, { value: "material", label: "자재정보" }, { value: "worker", label: "작업자정보" }, { value: "tool", label: "공구정보" }, { value: "document", label: "문서정보" }, ]; const INPUT_TYPES = [ { value: "text", label: "텍스트" }, { value: "number", label: "숫자" }, { value: "date", label: "날짜" }, { value: "textarea", label: "장문텍스트" }, { value: "select", label: "선택형" }, ]; export function DetailFormModal({ open, onClose, onSubmit, detailTypes, editData, mode, }: DetailFormModalProps) { const [formData, setFormData] = useState>({}); const [inspectionLookupOpen, setInspectionLookupOpen] = useState(false); const [selectedInspection, setSelectedInspection] = useState(null); useEffect(() => { if (open) { if (mode === "edit" && editData) { setFormData({ ...editData }); if (editData.inspection_code) { setSelectedInspection({ id: "", inspection_code: editData.inspection_code, inspection_item: editData.content || "", inspection_method: editData.inspection_method || "", unit: editData.unit || "", lower_limit: editData.lower_limit || "", upper_limit: editData.upper_limit || "", }); } } else { setFormData({ detail_type: detailTypes[0]?.value || "", content: "", is_required: "Y", }); setSelectedInspection(null); } } }, [open, mode, editData, detailTypes]); const updateField = (field: string, value: any) => { setFormData((prev) => ({ ...prev, [field]: value })); }; const handleInspectionSelect = (item: InspectionStandard) => { setSelectedInspection(item); setFormData((prev) => ({ ...prev, inspection_code: item.inspection_code, content: item.inspection_item, inspection_method: item.inspection_method, unit: item.unit, lower_limit: item.lower_limit || "", upper_limit: item.upper_limit || "", })); }; const handleSubmit = () => { if (!formData.detail_type) return; const type = formData.detail_type; if (type === "check" && !formData.content?.trim()) return; if (type === "inspect" && !formData.content?.trim()) return; if (type === "procedure" && !formData.content?.trim()) return; if (type === "input" && !formData.content?.trim()) return; if (type === "info" && !formData.lookup_target) return; const submitData = { ...formData }; if (type === "info" && !submitData.content?.trim()) { const targetLabel = LOOKUP_TARGETS.find(t => t.value === submitData.lookup_target)?.label || submitData.lookup_target; submitData.content = `${targetLabel} 조회`; } onSubmit(submitData); onClose(); }; const currentType = formData.detail_type || ""; return ( <> !v && onClose()}> 상세 항목 {mode === "add" ? "추가" : "수정"} 상세 항목의 유형을 선택하고 내용을 입력하세요
{/* 유형 선택 */}
{/* 체크리스트 */} {currentType === "check" && ( <>
updateField("content", e.target.value)} placeholder="예: 전원 상태 확인" className="mt-1 h-8 text-xs sm:h-10 sm:text-sm" />
)} {/* 검사항목 */} {currentType === "inspect" && ( <>
{selectedInspection && (

선택된 검사기준 정보

검사코드: {selectedInspection.inspection_code}

검사항목: {selectedInspection.inspection_item}

검사방법: {selectedInspection.inspection_method || "-"}

단위: {selectedInspection.unit || "-"}

하한값: {selectedInspection.lower_limit || "-"}

상한값: {selectedInspection.upper_limit || "-"}

)}
updateField("content", e.target.value)} placeholder="예: 외경 치수" className="mt-1 h-8 text-xs sm:h-10 sm:text-sm" />
updateField("inspection_method", e.target.value)} placeholder="예: 마이크로미터" className="mt-1 h-8 text-xs sm:h-10 sm:text-sm" />
updateField("unit", e.target.value)} placeholder="예: mm" className="mt-1 h-8 text-xs sm:h-10 sm:text-sm" />
updateField("lower_limit", e.target.value)} placeholder="예: 7.95" className="mt-1 h-8 text-xs sm:h-10 sm:text-sm" />
updateField("upper_limit", e.target.value)} placeholder="예: 8.05" className="mt-1 h-8 text-xs sm:h-10 sm:text-sm" />
)} {/* 작업절차 */} {currentType === "procedure" && ( <>
updateField("content", e.target.value)} placeholder="예: 자재 투입" className="mt-1 h-8 text-xs sm:h-10 sm:text-sm" />
updateField( "duration_minutes", e.target.value ? Number(e.target.value) : undefined ) } placeholder="예: 5" className="mt-1 h-8 text-xs sm:h-10 sm:text-sm" />
)} {/* 직접입력 */} {currentType === "input" && ( <>
updateField("content", e.target.value)} placeholder="예: 작업자 의견" className="mt-1 h-8 text-xs sm:h-10 sm:text-sm" />
)} {/* 정보조회 */} {currentType === "info" && ( <>
updateField("display_fields", e.target.value)} placeholder="예: 설비명, 설비코드" className="mt-1 h-8 text-xs sm:h-10 sm:text-sm" />
)} {/* 필수 여부 (모든 유형 공통) */} {currentType && (
)}
setInspectionLookupOpen(false)} onSelect={handleInspectionSelect} /> ); }