1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
|
'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<AwardedCompany[]>([])
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 (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent className="max-w-4xl max-h-[90vh] overflow-y-auto">
<DialogHeader>
<DialogTitle className="flex items-center gap-2">
<Trophy className="w-5 h-5 text-yellow-600" />
낙찰 처리
</DialogTitle>
<DialogDescription>
낙찰된 업체의 발주비율과 선정 사유를 확인하고 낙찰을 완료하세요.
</DialogDescription>
</DialogHeader>
<form onSubmit={handleSubmit}>
<div className="space-y-6">
{/* 낙찰 업체 정보 */}
<Card>
<CardHeader>
<CardTitle className="flex items-center gap-2">
<Building2 className="w-4 h-4" />
낙찰 업체 정보
</CardTitle>
</CardHeader>
<CardContent>
{isLoading ? (
<div className="text-center py-4">
<div className="animate-spin rounded-full h-8 w-8 border-b-2 border-primary mx-auto"></div>
<p className="mt-2 text-sm text-muted-foreground">낙찰 업체 정보를 불러오는 중...</p>
</div>
) : awardedCompanies.length > 0 ? (
<div className="space-y-4">
<Table>
<TableHeader>
<TableRow>
<TableHead>업체명</TableHead>
<TableHead className="text-right">견적금액</TableHead>
<TableHead className="text-right">발주비율</TableHead>
<TableHead className="text-right">발주금액</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{awardedCompanies.map((company) => (
<TableRow key={company.companyId}>
<TableCell className="font-medium">
<div className="flex items-center gap-2">
<Badge variant="default" className="bg-green-600">낙찰</Badge>
{company.companyName}
</div>
</TableCell>
<TableCell className="text-right">
{company.finalQuoteAmount.toLocaleString()}원
</TableCell>
<TableCell className="text-right">
{company.awardRatio}%
</TableCell>
<TableCell className="text-right font-semibold">
{(company.finalQuoteAmount * company.awardRatio / 100).toLocaleString()}원
</TableCell>
</TableRow>
))}
</TableBody>
</Table>
{/* 최종입찰가 요약 */}
<div className="flex items-center justify-between p-4 bg-blue-50 border border-blue-200 rounded-lg">
<div className="flex items-center gap-2">
<Calculator className="w-5 h-5 text-blue-600" />
<span className="font-semibold text-blue-800">최종입찰가</span>
</div>
<span className="text-xl font-bold text-blue-800">
{finalBidPrice.toLocaleString()}원
</span>
</div>
</div>
) : (
<div className="text-center py-8">
<Trophy className="w-12 h-12 text-gray-400 mx-auto mb-4" />
<p className="text-gray-500 mb-2">낙찰된 업체가 없습니다</p>
<p className="text-sm text-gray-400">
먼저 업체 수정 다이얼로그에서 발주비율을 산정해주세요.
</p>
</div>
)}
</CardContent>
</Card>
{/* 낙찰 사유 */}
<div className="space-y-2">
<Label htmlFor="selectionReason">
낙찰 사유 <span className="text-red-500">*</span>
</Label>
<Textarea
id="selectionReason"
placeholder="낙찰 사유를 상세히 입력해주세요..."
value={selectionReason}
onChange={(e) => setSelectionReason(e.target.value)}
rows={4}
className="resize-none"
/>
</div>
{/* 첨부파일 */}
<AwardSimpleFileUpload
biddingId={biddingId}
userId={userId}
readOnly={false}
/>
</div>
<DialogFooter className="mt-6">
<Button
type="button"
variant="outline"
onClick={() => onOpenChange(false)}
disabled={isPending}
>
취소
</Button>
<Button
type="submit"
disabled={isPending || awardedCompanies.length === 0}
>
{isPending ? '처리 중...' : '낙찰 완료'}
</Button>
</DialogFooter>
</form>
</DialogContent>
</Dialog>
)
}
|