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

import React, { useState, useEffect } from 'react'
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'
import { Input } from '@/components/ui/input'
import { Button } from '@/components/ui/button'
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '@/components/ui/table'
import { Save, Plus, Trash2, LoaderIcon } from 'lucide-react'
import { toast } from 'sonner'
import { useSession } from 'next-auth/react'
import { getStorageInfo, saveStorageInfo } from '../service'

interface StorageInfoItem {
  id?: number
  poNumber: string
  hullNumber: string
  remainingAmount: number
}

interface ContractStorageInfoProps {
  contractId: number
  readOnly?: boolean
}

export function ContractStorageInfo({ contractId, readOnly = false }: ContractStorageInfoProps) {
  const session = useSession()
  const userId = session.data?.user?.id ? Number(session.data.user.id) : null
  const [isLoading, setIsLoading] = useState(false)
  const [isSaving, setIsSaving] = useState(false)
  const [items, setItems] = useState<StorageInfoItem[]>([])

  // 데이터 로드
  useEffect(() => {
    const loadStorageInfo = async () => {
      setIsLoading(true)
      try {
        const data = await getStorageInfo(contractId)
        setItems(data || [])
      } catch (error) {
        console.error('Error loading storage info:', error)
        toast.error('임치계약 정보를 불러오는 중 오류가 발생했습니다.')
      } finally {
        setIsLoading(false)
      }
    }

    if (contractId) {
      loadStorageInfo()
    }
  }, [contractId])

  // 행 추가
  const addRow = () => {
    setItems(prev => [...prev, {
      poNumber: '',
      hullNumber: '',
      remainingAmount: 0
    }])
  }

  // 행 삭제
  const deleteRow = (index: number) => {
    setItems(prev => prev.filter((_, i) => i !== index))
  }

  // 항목 업데이트
  const updateItem = (index: number, field: keyof StorageInfoItem, value: string | number) => {
    setItems(prev => prev.map((item, i) => 
      i === index ? { ...item, [field]: value } : item
    ))
  }

  // 저장
  const handleSave = async () => {
    if (!userId) {
      toast.error('사용자 정보를 찾을 수 없습니다.')
      return
    }

    // 유효성 검사
    const errors: string[] = []
    items.forEach((item, index) => {
      if (!item.poNumber.trim()) errors.push(`${index + 1}번째 행의 PO No.`)
      if (!item.hullNumber.trim()) errors.push(`${index + 1}번째 행의 호선`)
      if (item.remainingAmount < 0) errors.push(`${index + 1}번째 행의 미입고 잔여금액`)
    })

    if (errors.length > 0) {
      toast.error(`다음 항목을 확인해주세요: ${errors.join(', ')}`)
      return
    }

    setIsSaving(true)
    try {
      await saveStorageInfo(contractId, items, userId)
      toast.success('임치계약 정보가 저장되었습니다.')
    } catch (error) {
      console.error('Error saving storage info:', error)
      toast.error('임치계약 정보 저장 중 오류가 발생했습니다.')
    } finally {
      setIsSaving(false)
    }
  }

  if (isLoading) {
    return (
      <Card>
        <CardHeader>
          <CardTitle>임치(물품보관)계약 상세 정보</CardTitle>
        </CardHeader>
        <CardContent>
          <div className="flex items-center justify-center py-8">
            <LoaderIcon className="w-6 h-6 animate-spin mr-2" />
            <span>로딩 중...</span>
          </div>
        </CardContent>
      </Card>
    )
  }

  return (
    <Card>
      <CardHeader>
        <div className="flex items-center justify-between">
          <CardTitle>임치(물품보관)계약 상세 정보</CardTitle>
          {!readOnly && (
            <div className="flex items-center gap-2">
              <Button
                variant="outline"
                size="sm"
                onClick={addRow}
              >
                <Plus className="w-4 h-4 mr-2" />
                행 추가
              </Button>
              <Button
                onClick={handleSave}
                disabled={isSaving}
              >
                {isSaving ? (
                  <>
                    <LoaderIcon className="w-4 h-4 mr-2 animate-spin" />
                    저장 중...
                  </>
                ) : (
                  <>
                    <Save className="w-4 h-4 mr-2" />
                    저장
                  </>
                )}
              </Button>
            </div>
          )}
        </div>
      </CardHeader>
      <CardContent>
        {items.length === 0 ? (
          <div className="text-center py-8 text-muted-foreground">
            <p>등록된 정보가 없습니다.</p>
            {!readOnly && (
              <Button
                variant="outline"
                className="mt-4"
                onClick={addRow}
              >
                <Plus className="w-4 h-4 mr-2" />
                정보 추가
              </Button>
            )}
          </div>
        ) : (
          <div className="space-y-4">
            <Table>
              <TableHeader>
                <TableRow>
                  <TableHead className="w-12">번호</TableHead>
                  <TableHead>PO No.</TableHead>
                  <TableHead>호선</TableHead>
                  <TableHead className="text-right">미입고 잔여금액</TableHead>
                  {!readOnly && <TableHead className="w-20">삭제</TableHead>}
                </TableRow>
              </TableHeader>
              <TableBody>
                {items.map((item, index) => (
                  <TableRow key={index}>
                    <TableCell>{index + 1}</TableCell>
                    <TableCell>
                      {readOnly ? (
                        <span className="text-sm">{item.poNumber || '-'}</span>
                      ) : (
                        <Input
                          value={item.poNumber}
                          onChange={(e) => updateItem(index, 'poNumber', e.target.value)}
                          placeholder="PO No. 입력"
                          className="h-8 text-sm"
                        />
                      )}
                    </TableCell>
                    <TableCell>
                      {readOnly ? (
                        <span className="text-sm">{item.hullNumber || '-'}</span>
                      ) : (
                        <Input
                          value={item.hullNumber}
                          onChange={(e) => updateItem(index, 'hullNumber', e.target.value)}
                          placeholder="호선 입력"
                          className="h-8 text-sm"
                        />
                      )}
                    </TableCell>
                    <TableCell>
                      {readOnly ? (
                        <span className="text-sm text-right block">
                          {item.remainingAmount.toLocaleString()}
                        </span>
                      ) : (
                        <Input
                          type="number"
                          value={item.remainingAmount}
                          onChange={(e) => updateItem(index, 'remainingAmount', parseFloat(e.target.value) || 0)}
                          placeholder="0"
                          className="h-8 text-sm text-right"
                          min="0"
                        />
                      )}
                    </TableCell>
                    {!readOnly && (
                      <TableCell>
                        <Button
                          variant="ghost"
                          size="sm"
                          onClick={() => deleteRow(index)}
                          className="text-red-600 hover:text-red-700"
                        >
                          <Trash2 className="w-4 h-4" />
                        </Button>
                      </TableCell>
                    )}
                  </TableRow>
                ))}
              </TableBody>
            </Table>
          </div>
        )}
      </CardContent>
    </Card>
  )
}