"use client" import * as React from "react" import { zodResolver } from "@hookform/resolvers/zod" import { useForm } from "react-hook-form" import { CalendarIcon, Loader } from "lucide-react" import { format } from "date-fns" import { toast } from "sonner" import { Button } from "@/components/ui/button" import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form" import { Textarea } from "@/components/ui/textarea" import { Sheet, SheetClose, SheetContent, SheetDescription, SheetFooter, SheetHeader, SheetTitle, } from "@/components/ui/sheet" import { Select, SelectContent, SelectGroup, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select" import { Calendar } from "@/components/ui/calendar" import { Popover, PopoverContent, PopoverTrigger, } from "@/components/ui/popover" import { updateVendorInvestigationProgressSchema, type UpdateVendorInvestigationProgressSchema, } from "../validations" import getPQSubmissionTypeAction, { updateVendorInvestigationProgressAction } from "../service" import { VendorInvestigationsViewWithContacts } from "@/config/vendorInvestigationsColumnsConfig" interface InvestigationProgressSheetProps extends React.ComponentPropsWithoutRef { investigation: VendorInvestigationsViewWithContacts | null } /** * 실사 진행 관리 시트 */ export function InvestigationProgressSheet({ investigation, ...props }: InvestigationProgressSheetProps) { const [isPending, startTransition] = React.useTransition() const [isProjectPQ, setIsProjectPQ] = React.useState(false) // RHF + Zod const form = useForm({ resolver: zodResolver(updateVendorInvestigationProgressSchema), defaultValues: { investigationId: investigation?.investigationId ?? 0, investigationAddress: investigation?.investigationAddress ?? "", investigationMethod: investigation?.investigationMethod ?? undefined, forecastedAt: investigation?.forecastedAt ?? undefined, confirmedAt: investigation?.confirmedAt ?? undefined, }, }) // investigation이 변경될 때마다 폼 리셋 React.useEffect(() => { if (investigation) { form.reset({ investigationId: investigation.investigationId, investigationAddress: investigation.investigationAddress ?? "", investigationMethod: investigation.investigationMethod ?? undefined, forecastedAt: investigation.forecastedAt ?? undefined, confirmedAt: investigation.confirmedAt ?? undefined, }) // PQ 타입 조회 (PROJECT면 구매자체평가 비활성화) if (investigation.pqSubmissionId) { getPQSubmissionTypeAction(investigation.pqSubmissionId).then((res) => { if (res.success) setIsProjectPQ(res.type === "PROJECT") }) } else { setIsProjectPQ(false) } } }, [investigation, form]) // Submit handler async function onSubmit(values: UpdateVendorInvestigationProgressSchema) { console.log("실사 진행 관리 onSubmit 호출됨:", values) if (!values.investigationId) { console.log("investigationId가 없음:", values.investigationId) return } startTransition(async () => { try { console.log("실사 진행 관리 startTransition 시작") // FormData 생성 const formData = new FormData() // 필수 필드 formData.append("investigationId", String(values.investigationId)) // 선택적 필드들 if (values.investigationAddress) { formData.append("investigationAddress", values.investigationAddress) } if (values.investigationMethod) { formData.append("investigationMethod", values.investigationMethod) } if (values.forecastedAt) { formData.append("forecastedAt", values.forecastedAt.toISOString()) } if (values.confirmedAt) { formData.append("confirmedAt", values.confirmedAt.toISOString()) } // 실사 진행 관리 업데이트 (PLANNED -> IN_PROGRESS) const { error } = await updateVendorInvestigationProgressAction(formData) if (error) { toast.error(error) return } toast.success("실사 진행 정보가 업데이트되었습니다!") form.reset() props.onOpenChange?.(false) } catch (error) { console.error("실사 진행 관리 업데이트 오류:", error) toast.error("실사 진행 관리 업데이트 중 오류가 발생했습니다.") } }) } // 디버깅을 위한 버튼 클릭 핸들러 const handleSaveClick = async () => { console.log("실사 진행 관리 저장 버튼 클릭됨") console.log("현재 폼 값:", form.getValues()) console.log("폼 에러:", form.formState.errors) // 폼 검증 실행 const isValid = await form.trigger() console.log("폼 검증 결과:", isValid) if (isValid) { form.handleSubmit(onSubmit)() } else { console.log("폼 검증 실패, 에러:", form.formState.errors) } } return ( 실사 진행 관리 {investigation?.vendorName && ( {investigation.vendorName} )}의 실사 진행 정보를 관리합니다.
{/* 실사 주소 */} ( 실사 주소