"use client" import { useForm } from "react-hook-form" import { zodResolver } from "@hookform/resolvers/zod" import { z } from "zod" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select" import { DatePicker } from "@/components/ui/date-picker" import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription } from "@/components/ui/dialog" import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from "@/components/ui/form" import { Loader2, Plus } from "lucide-react" import { ProjectSelector } from "@/components/ProjectSelector" const pqListFormSchema = z.object({ name: z.string().min(1, "PQ 목록 명을 입력해주세요"), type: z.enum(["GENERAL", "PROJECT", "NON_INSPECTION"], { required_error: "PQ 유형을 선택해주세요" }), projectId: z.number().optional().nullable(), validTo: z.date().optional().nullable(), }).refine((data) => { // 프로젝트 PQ인 경우 프로젝트 선택 필수 if (data.type === "PROJECT" && !data.projectId) { return false } return true }, { message: "프로젝트 PQ인 경우 프로젝트를 선택해야 합니다", path: ["projectId"] }) type PqListFormData = z.infer interface PqListFormProps { open: boolean onOpenChange: (open: boolean) => void initialData?: Partial & { id?: number } onSubmit: (data: PqListFormData & { id?: number }) => Promise isLoading?: boolean } const typeLabels = { GENERAL: "일반 PQ", PROJECT: "프로젝트 PQ", NON_INSPECTION: "미실사 PQ" } export function AddPqDialog({ open, onOpenChange, initialData, onSubmit, isLoading = false }: PqListFormProps) { const isEditing = !!initialData?.id const form = useForm({ resolver: zodResolver(pqListFormSchema), defaultValues: { name: initialData?.name || "", type: initialData?.type || "GENERAL", projectId: initialData?.projectId || null, validTo: initialData?.validTo || undefined, } }) const selectedType = form.watch("type") const formState = form.formState const handleSubmit = async (data: PqListFormData) => { try { await onSubmit({ ...data, id: initialData?.id }) form.reset() onOpenChange(false) } catch (error) { // 에러는 상위 컴포넌트에서 처리 console.error("Failed to submit PQ list:", error) } } return ( {isEditing ? "PQ 목록 수정" : "신규 PQ 목록 생성"} {isEditing ? "PQ 목록 정보를 수정합니다." : "새로운 PQ 목록을 생성합니다."}
{/* PQ 유형 */} ( PQ 유형 * )} /> {/* PQ 목록 명 */} ( PQ 리스트명 * )} /> {/* 프로젝트 선택 (프로젝트 PQ인 경우만) */} {selectedType === "PROJECT" && ( ( 프로젝트 * field.onChange(project.id)} placeholder="프로젝트를 선택하세요" /> )} /> )} {/* 유효기간 (프로젝트 PQ인 경우) */} {selectedType === "PROJECT" && ( ( 유효일 * field.onChange(date ?? null)} placeholder="유효일 선택" minDate={new Date()} /> )} /> )} {/* 버튼들 */}
) }