'use client' import * as React from 'react' import { useTransition } from 'react' import { useSession } from 'next-auth/react' import { Button } from '@/components/ui/button' import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from '@/components/ui/dialog' import { Label } from '@/components/ui/label' import { Textarea } from '@/components/ui/textarea' import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card' import { Badge } from '@/components/ui/badge' import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '@/components/ui/table' import { Trophy, Building2, Calculator } from 'lucide-react' import { useToast } from '@/hooks/use-toast' import { getAwardedCompanies, awardBidding } from '@/lib/bidding/detail/service' import { AwardSimpleFileUpload } from './components/award-simple-file-upload' interface BiddingAwardDialogProps { biddingId: number open: boolean onOpenChange: (open: boolean) => void onSuccess: () => void } interface AwardedCompany { companyId: number companyName: string | null finalQuoteAmount: number awardRatio: number } export function BiddingAwardDialog({ biddingId, open, onOpenChange, onSuccess }: BiddingAwardDialogProps) { const { toast } = useToast() const { data: session } = useSession() const [isPending, startTransition] = useTransition() const [selectionReason, setSelectionReason] = React.useState('') const [awardedCompanies, setAwardedCompanies] = React.useState([]) const [isLoading, setIsLoading] = React.useState(false) const userId = session?.user?.id || '2'; // 낙찰된 업체 정보 로드 React.useEffect(() => { if (open) { setIsLoading(true) getAwardedCompanies(biddingId) .then(companies => { setAwardedCompanies(companies) }) .catch(error => { console.error('Failed to load awarded companies:', error) toast({ title: '오류', description: '낙찰 업체 정보를 불러오는데 실패했습니다.', variant: 'destructive', }) }) .finally(() => { setIsLoading(false) }) } }, [open, biddingId, toast]) // 최종입찰가 계산 const finalBidPrice = React.useMemo(() => { return awardedCompanies.reduce((sum, company) => { return sum + (company.finalQuoteAmount * company.awardRatio / 100) }, 0) }, [awardedCompanies]) const handleSubmit = (e: React.FormEvent) => { e.preventDefault() if (!selectionReason.trim()) { toast({ title: '유효성 오류', description: '낙찰 사유를 입력해주세요.', variant: 'destructive', }) return } if (awardedCompanies.length === 0) { toast({ title: '유효성 오류', description: '낙찰된 업체가 없습니다. 먼저 발주비율을 산정해주세요.', variant: 'destructive', }) return } startTransition(async () => { const result = await awardBidding(biddingId, selectionReason, userId) if (result.success) { toast({ title: '성공', description: result.message, }) onSuccess() onOpenChange(false) // 폼 초기화 setSelectionReason('') } else { toast({ title: '오류', description: result.error, variant: 'destructive', }) } }) } return ( 낙찰 처리 낙찰된 업체의 발주비율과 선정 사유를 확인하고 낙찰을 완료하세요.
{/* 낙찰 업체 정보 */} 낙찰 업체 정보 {isLoading ? (

낙찰 업체 정보를 불러오는 중...

) : awardedCompanies.length > 0 ? (
업체명 견적금액 발주비율 발주금액 {awardedCompanies.map((company) => (
낙찰 {company.companyName}
{company.finalQuoteAmount.toLocaleString()}원 {company.awardRatio}% {(company.finalQuoteAmount * company.awardRatio / 100).toLocaleString()}원
))}
{/* 최종입찰가 요약 */}
최종입찰가
{finalBidPrice.toLocaleString()}원
) : (

낙찰된 업체가 없습니다

먼저 업체 수정 다이얼로그에서 발주비율을 산정해주세요.

)}
{/* 낙찰 사유 */}