"use client" import * as React from "react" import { useForm } from "react-hook-form" import { zodResolver } from "@hookform/resolvers/zod" import * as z from "zod" import { Button } from "@/components/ui/button" import { Sheet, SheetContent, SheetDescription, SheetFooter, SheetHeader, SheetTitle, } from "@/components/ui/sheet" import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form" import { Textarea } from "@/components/ui/textarea" import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select" import { toast } from "sonner" import { PcrPoData } from "@/lib/pcr/types" import { updatePcrRejectionReasonAction } from "@/lib/pcr/actions" const pcrEditSchema = z.object({ rejectionReasonType: z.string().optional(), customRejectionReason: z.string().optional(), }) const REJECTION_REASON_OPTIONS = [ { value: "제작완료", label: "제작완료" }, { value: "납품 준비 중", label: "납품 준비 중" }, { value: "제작을 위한 원소재 준비 완료", label: "제작을 위한 원소재 준비 완료" }, { value: "협력사 거래중지로 인한 승인 불가", label: "협력사 거래중지로 인한 승인 불가" }, { value: "기타", label: "기타" }, ] type PcrEditFormData = z.infer interface EditPcrSheetProps { open: boolean onOpenChange: (open: boolean) => void pcrData: PcrPoData | null onSuccess: () => void } export function EditPcrSheet({ open, onOpenChange, pcrData, onSuccess, }: EditPcrSheetProps) { const [isSubmitting, setIsSubmitting] = React.useState(false) const form = useForm({ resolver: zodResolver(pcrEditSchema), defaultValues: { rejectionReason: "", }, }) // PCR 데이터가 변경될 때 폼 초기화 React.useEffect(() => { if (pcrData) { const rejectionReason = pcrData.rejectionReason || "" // 기존 rejectionReason을 파싱해서 미리 정의된 옵션인지 확인 const predefinedOption = REJECTION_REASON_OPTIONS.find(option => option.value === rejectionReason ) if (predefinedOption) { form.reset({ rejectionReasonType: rejectionReason, customRejectionReason: "", }) } else { form.reset({ rejectionReasonType: "기타", customRejectionReason: rejectionReason, }) } } }, [pcrData, form]) const onSubmit = async (data: PcrEditFormData) => { if (!pcrData) { toast.error("PCR 데이터가 없습니다") return } setIsSubmitting(true) try { // rejectionReason 조합 let rejectionReason = "" if (data.rejectionReasonType === "기타") { rejectionReason = data.customRejectionReason || "" } else { rejectionReason = data.rejectionReasonType || "" } const result = await updatePcrRejectionReasonAction({ id: pcrData.id, rejectionReason: rejectionReason, }) if (result.success) { toast.success("PCR이 성공적으로 수정되었습니다") onOpenChange(false) onSuccess() } else { toast.error(result.error || "PCR 수정에 실패했습니다") } } catch (error) { console.error("PCR 수정 오류:", error) toast.error("PCR 수정 중 오류가 발생했습니다") } finally { setIsSubmitting(false) } } if (!pcrData) return null return ( PCR 수정 PO/계약번호: {pcrData.poContractNumber}
{/* 읽기 전용 정보 표시 */}

{pcrData.project || "-"}

{pcrData.changeType}

{pcrData.pcrReason || "-"}

{pcrData.detailsReason || "-"}

{/* 편집 가능한 필드 */} ( 거절 사유 * )} /> {/* 기타 선택 시 텍스트 입력 필드 */} {form.watch("rejectionReasonType") === "기타" && ( ( 기타 거절 사유