"use client" import * as React from "react" import { useFormContext } from "react-hook-form" import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card" import { Label } from "@/components/ui/label" import { Input } from "@/components/ui/input" import { Textarea } from "@/components/ui/textarea" import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select" import { Checkbox } from "@/components/ui/checkbox" import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group" import { Alert, AlertDescription } from "@/components/ui/alert" import { InfoIcon, ChevronsUpDown, Check, CalendarIcon, Loader2 } from "lucide-react" import { format } from "date-fns" import { Calendar } from "@/components/ui/calendar" import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover" import { Button } from "@/components/ui/button" import { cn } from "@/lib/utils" import { Badge } from "@/components/ui/badge" import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList, } from "@/components/ui/command" import { getIncotermsForSelection, getPaymentTermsForSelection, getPlaceOfShippingForSelection, getPlaceOfDestinationForSelection } from "@/lib/procurement-select/service" import { TAX_CONDITIONS } from "@/lib/tax-conditions/types" import { toast } from "sonner" interface CommercialTermsFormProps { rfqDetail: any rfq: any } interface SelectOption { id: number code: string description: string } export default function CommercialTermsForm({ rfqDetail, rfq }: CommercialTermsFormProps) { const { register, setValue, watch, formState: { errors } } = useFormContext() console.log(rfqDetail,"rfqDetail") // RFQ 코드가 F로 시작하는지 확인 const isFrameContract = rfq?.rfqCode?.startsWith("F") // Select 옵션들 상태 const [incoterms, setIncoterms] = React.useState([]) const [paymentTerms, setPaymentTerms] = React.useState([]) const [shippingPlaces, setShippingPlaces] = React.useState([]) const [destinationPlaces, setDestinationPlaces] = React.useState([]) // 로딩 상태 const [incotermsLoading, setIncotermsLoading] = React.useState(false) const [paymentTermsLoading, setPaymentTermsLoading] = React.useState(false) const [shippingLoading, setShippingLoading] = React.useState(false) const [destinationLoading, setDestinationLoading] = React.useState(false) // Popover 열림 상태 const [incotermsOpen, setIncotermsOpen] = React.useState(false) const [paymentTermsOpen, setPaymentTermsOpen] = React.useState(false) const [shippingOpen, setShippingOpen] = React.useState(false) const [destinationOpen, setDestinationOpen] = React.useState(false) const vendorCurrency = watch("vendorCurrency") const vendorPaymentTermsCode = watch("vendorPaymentTermsCode") const vendorIncotermsCode = watch("vendorIncotermsCode") const vendorDeliveryDate = watch("vendorDeliveryDate") const vendorContractDuration = watch("vendorContractDuration") const vendorFirstYn = watch("vendorFirstYn") const vendorSparepartYn = watch("vendorSparepartYn") const vendorMaterialPriceRelatedYn = watch("vendorMaterialPriceRelatedYn") // 구매자 제시 조건과 다른지 확인 const isDifferentCurrency = vendorCurrency !== rfqDetail.currency const isDifferentPaymentTerms = vendorPaymentTermsCode !== rfqDetail.paymentTermsCode const isDifferentIncoterms = vendorIncotermsCode !== rfqDetail.incotermsCode // 날짜만 비교 (년월일만 체크) - 로컬 시간대 기준 const formatDateOnly = (date: Date | string | null) => { if (!date) return null const d = new Date(date) const year = d.getFullYear() const month = String(d.getMonth() + 1).padStart(2, '0') const day = String(d.getDate()).padStart(2, '0') return `${year}-${month}-${day}` } const isDifferentDeliveryDate = !isFrameContract && formatDateOnly(vendorDeliveryDate) !== formatDateOnly(rfqDetail.deliveryDate) const isDifferentContractDuration = isFrameContract && vendorContractDuration !== rfqDetail.contractDuration // 데이터 로드 함수들 const loadIncoterms = React.useCallback(async () => { setIncotermsLoading(true) try { const data = await getIncotermsForSelection() setIncoterms(data) } catch (error) { console.error("Failed to load incoterms:", error) toast.error("Incoterms 목록을 불러오는데 실패했습니다.") } finally { setIncotermsLoading(false) } }, []) const loadPaymentTerms = React.useCallback(async () => { setPaymentTermsLoading(true) try { const data = await getPaymentTermsForSelection() setPaymentTerms(data) } catch (error) { console.error("Failed to load payment terms:", error) toast.error("결제조건 목록을 불러오는데 실패했습니다.") } finally { setPaymentTermsLoading(false) } }, []) const loadShippingPlaces = React.useCallback(async () => { setShippingLoading(true) try { const data = await getPlaceOfShippingForSelection() setShippingPlaces(data) } catch (error) { console.error("Failed to load shipping places:", error) toast.error("선적지 목록을 불러오는데 실패했습니다.") } finally { setShippingLoading(false) } }, []) const loadDestinationPlaces = React.useCallback(async () => { setDestinationLoading(true) try { const data = await getPlaceOfDestinationForSelection() setDestinationPlaces(data) } catch (error) { console.error("Failed to load destination places:", error) toast.error("도착지 목록을 불러오는데 실패했습니다.") } finally { setDestinationLoading(false) } }, []) // 컴포넌트 마운트 시 데이터 로드 React.useEffect(() => { loadIncoterms() loadPaymentTerms() loadShippingPlaces() loadDestinationPlaces() }, [loadIncoterms, loadPaymentTerms, loadShippingPlaces, loadDestinationPlaces]) // 선택된 옵션 찾기 const selectedIncoterm = incoterms.find(i => i.code === vendorIncotermsCode) const selectedPaymentTerm = paymentTerms.find(p => p.code === vendorPaymentTermsCode) const selectedShipping = shippingPlaces.find(s => s.code === watch("vendorPlaceOfShipping")) const selectedDestination = destinationPlaces.find(d => d.code === watch("vendorPlaceOfDestination")) return (
{/* 기본 상업 조건 */} 기본 상업 조건 구매자가 제시한 조건과 다른 경우, 변경 사유를 반드시 입력해주세요. {/* 통화 */}
{isDifferentCurrency && (