'use client' import * as React from 'react' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' import { Label } from '@/components/ui/label' import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from '@/components/ui/dialog' import { updateBiddingDetailVendor } from '@/lib/bidding/detail/service' import { QuotationVendor } from '@/lib/bidding/detail/service' import { useToast } from '@/hooks/use-toast' import { useTransition } from 'react' interface BiddingDetailVendorEditDialogProps { vendor: QuotationVendor | null open: boolean onOpenChange: (open: boolean) => void onSuccess: () => void biddingAwardCount?: string // 낙찰업체 수 정보 추가 biddingStatus?: string // 입찰 상태 정보 추가 allVendors?: QuotationVendor[] // 전체 벤더 목록 추가 } export function BiddingDetailVendorEditDialog({ vendor, open, onOpenChange, onSuccess, biddingAwardCount, biddingStatus, allVendors = [] }: BiddingDetailVendorEditDialogProps) { const { toast } = useToast() const [isPending, startTransition] = useTransition() // 폼 상태 (간소화 - 수정 가능한 필드만) const [formData, setFormData] = React.useState({ awardRatio: 0, }) // 단수낙찰의 경우 이미 100%인 벤더가 있는지 확인 const hasWinnerWith100Percent = React.useMemo(() => { if (biddingAwardCount === 'single') { return allVendors.some(v => v.awardRatio === 100 && v.id !== vendor?.id) } return false }, [allVendors, biddingAwardCount, vendor?.id]) // vendor가 변경되면 폼 데이터 업데이트 React.useEffect(() => { if (vendor) { // 낙찰업체 수가 단수인 경우 발주비율을 100%로 자동 설정 const defaultAwardRatio = biddingAwardCount === 'single' ? 100 : (vendor.awardRatio || 0) setFormData({ awardRatio: defaultAwardRatio, }) } }, [vendor, biddingAwardCount]) const handleEdit = () => { if (!vendor) return startTransition(async () => { const response = await updateBiddingDetailVendor( vendor.id, vendor.quotationAmount, // 기존 견적금액 유지 vendor.currency, // 기존 통화 유지 formData.awardRatio ) if (response.success) { toast({ title: '성공', description: response.message, }) onOpenChange(false) onSuccess() } else { toast({ title: '오류', description: response.error, variant: 'destructive', }) } }) } return ( 협력업체 발주비율 산정 협력업체 발주비율을 산정해주세요.
{/* 읽기 전용 업체 정보 */} {vendor && (

협력업체 정보

협력업체명: {vendor.vendorName}
협력업체코드: {vendor.vendorCode}
)} {/* 수정 가능한 필드들 */} {vendor && vendor.isBiddingParticipated !== true && (
입찰 참여 안내

{vendor.isBiddingParticipated === null ? '이 업체는 아직 입찰참여 여부가 결정되지 않았습니다. 입찰에 참여한 업체만 발주비율을 설정할 수 있습니다.' : '이 업체는 입찰에 참여하지 않습니다. 발주비율을 설정할 수 없습니다.' }

)}
setFormData({ ...formData, awardRatio: Number(e.target.value) })} placeholder="발주비율을 입력하세요" disabled={vendor?.isBiddingParticipated !== true || biddingAwardCount === 'single' || biddingStatus === 'vendor_selected' || hasWinnerWith100Percent} /> {vendor?.isBiddingParticipated !== true && (

입찰에 참여한 업체만 발주비율을 설정할 수 있습니다.

)} {biddingAwardCount === 'single' && vendor?.isBiddingParticipated === true && (

단수 낙찰의 경우 발주비율은 자동으로 100%로 설정됩니다.

)} {biddingStatus === 'vendor_selected' && (

낙찰이 완료되어 발주비율을 수정할 수 없습니다.

)} {hasWinnerWith100Percent && (

단수 낙찰의 경우 이미 100% 발주비율이 설정된 업체가 있어 다른 업체의 발주비율을 수정할 수 없습니다.

)}
) }