summaryrefslogtreecommitdiff
path: root/lib/general-contracts/detail/general-contract-detail.tsx
blob: f2a916f848181e0cd578c41e16398c9e01f8b4c0 (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
'use client'

import { useState, useEffect } from 'react'
import { useParams } from 'next/navigation'
import Link from 'next/link'
import { getContractById, getSubcontractChecklist } from '../service'
import { GeneralContractInfoHeader } from './general-contract-info-header'
import { Alert, AlertDescription } from '@/components/ui/alert'
import { Button } from '@/components/ui/button'
import { AlertCircle, ArrowLeft } from 'lucide-react'
import { Skeleton } from '@/components/ui/skeleton'
import { ContractItemsTable } from './general-contract-items-table'
import { SubcontractChecklist } from './general-contract-subcontract-checklist'
import { ContractBasicInfo } from './general-contract-basic-info'
import { ContractApprovalRequestDialog } from './general-contract-approval-request-dialog'
import { ContractStorageInfo } from './general-contract-storage-info'
import { ContractYardEntryInfo } from './general-contract-yard-entry-info'
import { ContractReviewComments } from './general-contract-review-comments'
import { ContractReviewRequestDialog } from './general-contract-review-request-dialog'

export default function ContractDetailPage() {
  const params = useParams()
  const contractId = params?.id ? parseInt(params.id as string) : null
  
  const [contract, setContract] = useState<Record<string, unknown> | null>(null)
  const [loading, setLoading] = useState(true)
  const [error, setError] = useState<string | null>(null)
  const [showApprovalDialog, setShowApprovalDialog] = useState(false)
  const [subcontractChecklistData, setSubcontractChecklistData] = useState<Record<string, unknown> | null>(null)
  const [showReviewDialog, setShowReviewDialog] = useState(false)

  useEffect(() => {
    const fetchContract = async () => {
      try {
        setLoading(true)
        setError(null)

        // 계약 기본 정보 로드
        const contractData = await getContractById(contractId!)
        setContract(contractData)

        // 하도급법 체크리스트 데이터 로드
        try {
          const checklistData = await getSubcontractChecklist(contractId!)
          if (checklistData.success && checklistData.data) {
            setSubcontractChecklistData(checklistData.data)
          }
        } catch (checklistError) {
          console.log('하도급법 체크리스트 데이터 로드 실패:', checklistError)
          // 체크리스트 로드 실패는 전체 로드를 실패시키지 않음
        }

      } catch (err) {
        console.error('Error fetching contract:', err)
        setError('계약 정보를 불러오는 중 오류가 발생했습니다.')
      } finally {
        setLoading(false)
      }
    }

    if (contractId && !isNaN(contractId)) {
      fetchContract()
    } else {
      setError('유효하지 않은 계약 ID입니다.')
      setLoading(false)
    }
  }, [contractId])

  if (loading) {
    return (
      <div className="container mx-auto py-6 space-y-6">
        <Skeleton className="h-8 w-64" />
        <div className="grid gap-6">
          <div className="grid grid-cols-2 gap-4">
            <Skeleton className="h-10 w-full" />
            <Skeleton className="h-10 w-full" />
          </div>
          <div className="grid grid-cols-3 gap-4">
            <Skeleton className="h-10 w-full" />
            <Skeleton className="h-10 w-full" />
            <Skeleton className="h-10 w-full" />
          </div>
          <Skeleton className="h-32 w-full" />
        </div>
      </div>
    )
  }

  if (error) {
    return (
      <div className="container mx-auto py-6">
        <Alert variant="destructive">
          <AlertCircle className="h-4 w-4" />
          <AlertDescription>
            {error}
          </AlertDescription>
        </Alert>
      </div>
    )
  }

  return (
    <div className="container mx-auto py-6 space-y-6">

      
      <div className="flex items-center justify-between">
        <div>
          <h1 className="text-3xl font-bold tracking-tight">계약 상세</h1>
          <p className="text-muted-foreground">
            계약번호: {contract?.contractNumber as string} (Rev.{contract?.revision as number})
          </p>
        </div>
        <div className="flex gap-2">
          {/* 조건검토요청 버튼 - Draft 상태일 때만 표시 */}
          {contract?.status === 'Draft' && (
            <Button 
              onClick={() => setShowReviewDialog(true)}
              className="bg-green-600 hover:bg-green-700"
            >
              조건검토요청
            </Button>
          )}
          {/* 계약승인요청 버튼 */}
          <>
            <Button 
              onClick={() => setShowApprovalDialog(true)}
              className="bg-blue-600 hover:bg-blue-700"
            >
              계약승인요청
            </Button>
            </>
          {/* 계약목록으로 돌아가기 버튼 */}
          <Button asChild variant="outline" size="sm">
            <Link href="/evcp/general-contracts">
              <ArrowLeft className="h-4 w-4 mr-2" />
              계약목록으로 돌아가기
            </Link>
          </Button>
        </div>
      </div>
      {/* 계약 정보 헤더 */}
      {contract && <GeneralContractInfoHeader contract={contract} />}
      
      {/* 계약 상세 폼 */}
      {contract && (
        <div className="space-y-6">
          {/* ContractBasicInfo */}
          <ContractBasicInfo contractId={contract.id as number} />
          {/* 품목정보 */}
          {/* {!(contract?.contractScope === '단가' || contract?.contractScope === '물량(실적)') && (
            <div className="mb-4">
              <p className="text-sm text-gray-600 mb-2">
                <strong>품목정보 입력 안내:</strong>
                <br />
                단가/물량 확정 계약의 경우 수량 및 총 계약금액은 별도로 관리됩니다.
              </p>
            </div>
          )} */}
          <ContractItemsTable
            contractId={contract.id as number}
            items={[]}
            onItemsChange={() => {}}
            onTotalAmountChange={() => {}}
            availableBudget={0}
            readOnly={false}
            contractScope={contract?.contractScope as string || ''}
          />
          {/* 하도급법 자율점검 체크리스트 */}
          <SubcontractChecklist
            contractId={contract.id as number}
            onDataChange={(data) => setSubcontractChecklistData(data)}
            readOnly={false}
            initialData={subcontractChecklistData}
            contractType={contract?.type as string || ''}
            vendorCountry={(contract as any)?.vendorCountry || 'KR'}
          />
          
          {/* 임치(물품보관)계약 상세 정보 - SG 계약종류일 때만 표시 */}
          {contract?.type === 'SG' && (
            <ContractStorageInfo
              contractId={contract.id as number}
              readOnly={false}
            />
          )}
          
          {/* 사외업체 야드투입 정보 - externalYardEntry가 'Y'일 때만 표시 */}
          {contract?.externalYardEntry === 'Y' && (
            <ContractYardEntryInfo
              contractId={contract.id as number}
              readOnly={false}
            />
          )}
          
          {/* 계약 조건 검토 의견 섹션 */}
          <ContractReviewComments
            contractId={contract.id as number}
            contractStatus={contract.status as string}
          />
        </div>
      )}

      {/* 계약승인요청 다이얼로그 */}
      {contract && (
        <ContractApprovalRequestDialog
          contract={contract}
          open={showApprovalDialog}
          onOpenChange={setShowApprovalDialog}
        />
      )}

      {/* 조건검토요청 다이얼로그 */}
      {contract && (
        <ContractReviewRequestDialog
          contract={contract}
          open={showReviewDialog}
          onOpenChange={setShowReviewDialog}
        />
      )}
    </div>
  )
}