summaryrefslogtreecommitdiff
path: root/lib/bidding/detail/table/bidding-detail-header.tsx
blob: fcbbeb9ae1a0b8287f120a9c9e993221eac6dcbe (plain)
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
'use client'

import * as React from 'react'
import { useRouter } from 'next/navigation'
import { Bidding, biddingStatusLabels, contractTypeLabels, biddingTypeLabels } from '@/db/schema'
import { Badge } from '@/components/ui/badge'
import { Button } from '@/components/ui/button'
import {
  ArrowLeft,
  Send,
  RotateCcw,
  XCircle,
  Calendar,
  Building2,
  User,
  Package,
  DollarSign,
  Hash
} from 'lucide-react'

import { formatDate } from '@/lib/utils'
import {
  registerBidding,
  markAsDisposal,
  createRebidding
} from '@/lib/bidding/detail/service'
import { useToast } from '@/hooks/use-toast'
import { useTransition } from 'react'

interface BiddingDetailHeaderProps {
  bidding: Bidding
}

export function BiddingDetailHeader({ bidding }: BiddingDetailHeaderProps) {
  const router = useRouter()
  const { toast } = useToast()
  const [isPending, startTransition] = useTransition()

  const handleGoBack = () => {
    router.push('/evcp/bid')
  }

  const handleRegister = () => {
    // 상태 검증
    if (bidding.status !== 'bidding_generated') {
      toast({
        title: '실행 불가',
        description: '입찰 등록은 입찰 생성 상태에서만 가능합니다.',
        variant: 'destructive',
      })
      return
    }

    if (!confirm('입찰을 등록하시겠습니까?')) return

    startTransition(async () => {
      const result = await registerBidding(bidding.id, 'current-user') // TODO: 실제 사용자 ID

      if (result.success) {
        toast({
          title: '성공',
          description: result.message,
        })
        router.refresh()
      } else {
        toast({
          title: '오류',
          description: result.error,
          variant: 'destructive',
        })
      }
    })
  }

  const handleMarkAsDisposal = () => {
    // 상태 검증
    if (bidding.status !== 'bidding_closed') {
      toast({
        title: '실행 불가',
        description: '유찰 처리는 입찰 마감 상태에서만 가능합니다.',
        variant: 'destructive',
      })
      return
    }

    if (!confirm('입찰을 유찰 처리하시겠습니까?')) return

    startTransition(async () => {
      const result = await markAsDisposal(bidding.id, 'current-user') // TODO: 실제 사용자 ID

      if (result.success) {
        toast({
          title: '성공',
          description: result.message,
        })
        router.refresh()
      } else {
        toast({
          title: '오류',
          description: result.error,
          variant: 'destructive',
        })
      }
    })
  }

  const handleCreateRebidding = () => {
    // 상태 검증
    if (bidding.status !== 'bidding_disposal') {
      toast({
        title: '실행 불가',
        description: '재입찰은 유찰 상태에서만 가능합니다.',
        variant: 'destructive',
      })
      return
    }

    if (!confirm('재입찰을 생성하시겠습니까?')) return

    startTransition(async () => {
      const result = await createRebidding(bidding.id, 'current-user') // TODO: 실제 사용자 ID

      if (result.success) {
        toast({
          title: '성공',
          description: result.message,
        })
        // 새로 생성된 입찰로 이동
        if (result.data) {
          router.push(`/evcp/bid/${result.data.id}`)
        } else {
          router.refresh()
        }
      } else {
        toast({
          title: '오류',
          description: result.error,
          variant: 'destructive',
        })
      }
    })
  }

  const getActionButtons = () => {
    const buttons = []

    // 기본 액션 버튼들 (항상 표시)


    // 모든 액션 버튼을 항상 표시 (상태 검증은 각 핸들러에서)
    buttons.push(
      <Button
        key="register"
        onClick={handleRegister}
        disabled={isPending}
      >
        <Send className="w-4 h-4 mr-2" />
        입찰등록
      </Button>
    )

    buttons.push(
      <Button
        key="disposal"
        variant="destructive"
        onClick={handleMarkAsDisposal}
        disabled={isPending}
      >
        <XCircle className="w-4 h-4 mr-2" />
        유찰
      </Button>
    )

    buttons.push(
      <Button
        key="rebidding"
        onClick={handleCreateRebidding}
        disabled={isPending}
      >
        <RotateCcw className="w-4 h-4 mr-2" />
        재입찰
      </Button>
    )

    return buttons
  }

  return (
    <div className="border-b bg-background/95 backdrop-blur supports-[backdrop-filter]:bg-background/60">
      <div className="px-6 py-4">
        {/* 헤더 메인 영역 */}
        <div className="flex items-center justify-between mb-4">
          <div className="flex items-center gap-4 flex-1 min-w-0">
            {/* 제목과 배지 */}
            <div className="flex items-center gap-3 flex-1 min-w-0">
              <h1 className="text-xl font-semibold truncate">{bidding.title}</h1>
              <div className="flex items-center gap-2 flex-shrink-0">
                <Badge variant="outline" className="font-mono text-xs">
                  <Hash className="w-3 h-3 mr-1" />
                  {bidding.biddingNumber}
                  {bidding.revision && bidding.revision > 0 && ` Rev.${bidding.revision}`}
                </Badge>
                <Badge variant={
                  bidding.status === 'bidding_disposal' ? 'destructive' :
                  bidding.status === 'vendor_selected' ? 'default' :
                  'secondary'
                } className="text-xs">
                  {biddingStatusLabels[bidding.status]}
                </Badge>
              </div>
            </div>

            {/* 액션 버튼들 */}
            <div className="flex items-center gap-2 flex-shrink-0">
              {getActionButtons()}
            </div>
          </div>
        </div>

        {/* 세부 정보 영역 */}

        {/* 일정 정보 */}
        {/* {(bidding.submissionStartDate || bidding.evaluationDate || bidding.preQuoteDate || bidding.biddingRegistrationDate) && (
          <div className="flex flex-wrap items-center gap-4 mt-3 pt-3 border-t border-border/50">
            <Calendar className="w-4 h-4 text-muted-foreground flex-shrink-0" />
            <div className="flex flex-wrap items-center gap-4 text-sm text-muted-foreground">
              {bidding.submissionStartDate && bidding.submissionEndDate && (
                <div>
                  <span className="font-medium">제출기간:</span> {formatDate(bidding.submissionStartDate, 'KR')} ~ {formatDate(bidding.submissionEndDate, 'KR')}
                </div>
              )}
              {bidding.evaluationDate && (
                <div>
                  <span className="font-medium">평가일:</span> {formatDate(bidding.evaluationDate, 'KR')}
                </div>
              )}
              {bidding.preQuoteDate && (
                <div>
                  <span className="font-medium">사전견적일:</span> {formatDate(bidding.preQuoteDate, 'KR')}
                </div>
              )}
              {bidding.biddingRegistrationDate && (
                <div>
                  <span className="font-medium">입찰등록일:</span> {formatDate(bidding.biddingRegistrationDate, 'KR')}
                </div>
              )}
            </div>
          </div>
        )} */}
      </div>
    </div>
  )
}