"use client" import * as React from "react" import { useRouter } from "next/navigation" import { useTransition } from "react" import { Button } from "@/components/ui/button" import { Plus, Send, RotateCcw, XCircle, Trophy, FileText, DollarSign } from "lucide-react" import { registerBidding, markAsDisposal, createRebidding } from "@/lib/bidding/detail/service" import { sendBiddingBasicContracts, getSelectedVendorsForBidding } from "@/lib/bidding/pre-quote/service" import { BiddingDetailVendorCreateDialog } from "../../../../components/bidding/manage/bidding-detail-vendor-create-dialog" import { BiddingDocumentUploadDialog } from "./bidding-document-upload-dialog" import { BiddingVendorPricesDialog } from "./bidding-vendor-prices-dialog" import { Bidding } from "@/db/schema" import { useToast } from "@/hooks/use-toast" import { BiddingInvitationDialog } from "./bidding-invitation-dialog" interface BiddingDetailVendorToolbarActionsProps { biddingId: number bidding: Bidding userId: string onOpenTargetPriceDialog: () => void onOpenAwardDialog: () => void onSuccess: () => void } export function BiddingDetailVendorToolbarActions({ biddingId, bidding, userId, onOpenTargetPriceDialog, onOpenAwardDialog, onSuccess }: BiddingDetailVendorToolbarActionsProps) { const router = useRouter() const { toast } = useToast() const [isPending, startTransition] = useTransition() const [isCreateDialogOpen, setIsCreateDialogOpen] = React.useState(false) const [isDocumentDialogOpen, setIsDocumentDialogOpen] = React.useState(false) const [isPricesDialogOpen, setIsPricesDialogOpen] = React.useState(false) const [isBiddingInvitationDialogOpen, setIsBiddingInvitationDialogOpen] = React.useState(false) const [selectedVendors, setSelectedVendors] = React.useState([]) // 본입찰 초대 다이얼로그가 열릴 때 선정된 업체들 조회 React.useEffect(() => { if (isBiddingInvitationDialogOpen) { getSelectedVendors().then(vendors => { setSelectedVendors(vendors) }) } }, [isBiddingInvitationDialogOpen, biddingId]) const handleCreateVendor = () => { setIsCreateDialogOpen(true) } const handleDocumentUpload = () => { setIsDocumentDialogOpen(true) } const handleViewVendorPrices = () => { setIsPricesDialogOpen(true) } const handleRegister = () => { if (!bidding.targetPrice) { toast({ title: '오류', description: '내정가가 산정되어야 입찰 초대를 할 수 있습니다.', variant: 'destructive', }) return } // 본입찰 초대 다이얼로그 열기 setIsBiddingInvitationDialogOpen(true) } const handleBiddingInvitationSend = async (data: any) => { try { // 1. 기본계약 발송 const contractResult = await sendBiddingBasicContracts( biddingId, data.vendors, data.generatedPdfs, data.message ) if (!contractResult.success) { toast({ title: '기본계약 발송 실패', description: contractResult.error, variant: 'destructive', }) return } // 2. 입찰 등록 진행 const registerResult = await registerBidding(bidding.id, userId) if (registerResult.success) { toast({ title: '본입찰 초대 완료', description: '기본계약 발송 및 본입찰 초대가 완료되었습니다.', }) setIsBiddingInvitationDialogOpen(false) router.refresh() onSuccess() } else { toast({ title: '오류', description: registerResult.error, variant: 'destructive', }) } } catch (error) { console.error('본입찰 초대 실패:', error) toast({ title: '오류', description: '본입찰 초대에 실패했습니다.', variant: 'destructive', }) } } // 선정된 업체들 조회 (서버 액션 함수 사용) const getSelectedVendors = async () => { try { const result = await getSelectedVendorsForBidding(biddingId) if (result.success) { return result.vendors } else { console.error('선정된 업체 조회 실패:', result.error) return [] } } catch (error) { console.error('선정된 업체 조회 실패:', error) return [] } } const handleMarkAsDisposal = () => { startTransition(async () => { const result = await markAsDisposal(bidding.id, userId) if (result.success) { toast({ title: result.message, description: result.message, }) router.refresh() } else { toast({ title: result.error, description: result.error, variant: 'destructive', }) } }) } const handleCreateRebidding = () => { startTransition(async () => { const result = await createRebidding(bidding.id, userId) if (result.success) { toast({ title: result.message, description: result.message, }) router.refresh() onSuccess() } else { toast({ title: result.error, description: result.error, variant: 'destructive', }) } }) } return ( <>
{/* 상태별 액션 버튼 */} {bidding.status !== 'bidding_closed' && bidding.status !== 'vendor_selected' && ( <> {bidding.status === 'bidding_disposal' && ( )} {/* 구분선 */} {(bidding.status === 'bidding_generated' || bidding.status === 'bidding_disposal') && (
)} {/* 공통 관리 버튼들 */} {/* */} )}
{ onSuccess() setIsCreateDialogOpen(false) }} /> ) }