summaryrefslogtreecommitdiff
path: root/lib/general-contracts_old/detail/general-contract-field-service-rate.tsx
blob: a815830739b0a6b7c7b8a6cd8a3b54c3e3804e80 (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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
'use client'

import React, { useState, useEffect } from 'react'
import { useSession } from 'next-auth/react'
import { Input } from '@/components/ui/input'
import { Button } from '@/components/ui/button'
import { Accordion, AccordionContent, AccordionItem, AccordionTrigger } from '@/components/ui/accordion'
import { Checkbox } from '@/components/ui/checkbox'
import { Plus, Trash2, Save, LoaderIcon, DollarSign } from 'lucide-react'
import { getFieldServiceRate, updateFieldServiceRate } from '../service'
import { toast } from 'sonner'

interface FieldServiceRateProps {
  contractId: number
  contractType?: string
}

interface FieldServiceRateItem {
  id: string
  levelOfSpecialist: string
  description: string
  rateCurrency: string
  onshoreRate: string
  offshoreRate: string
  rateUnit: string
  remark: string
}

export function FieldServiceRate({ contractId }: FieldServiceRateProps) {
  const session = useSession()
  const [isLoading, setIsLoading] = useState(false)
  const [isEnabled, setIsEnabled] = useState(true)
  
  // 특정 계약종류를 제외한 일반계약은 Default로 표시
  const isDisabled = false
  
  const [fieldServiceRates, setFieldServiceRates] = useState<FieldServiceRateItem[]>([])

  // 초기 데이터 로드
  useEffect(() => {
    const loadFieldServiceRate = async () => {
      try {
        const data = await getFieldServiceRate(contractId)
        if (data && data.enabled !== undefined) {
          setIsEnabled(data.enabled)
          setFieldServiceRates(data.fieldServiceRates || [])
        } else {
        }
      } catch (error) {
        console.error('Field Service Rate 데이터 로드 실패:', error)
        toast.error('Field Service Rate 데이터를 불러오는데 실패했습니다.')
      }
    }

    loadFieldServiceRate()
  }, [contractId])

  const addFieldServiceRateRow = () => {
    const newRow: FieldServiceRateItem = {
      id: Date.now().toString(),
      levelOfSpecialist: '',
      description: '',
      rateCurrency: 'USD',
      onshoreRate: '',
      offshoreRate: '',
      rateUnit: 'day',
      remark: ''
    }
    setFieldServiceRates([...fieldServiceRates, newRow])
  }

  const removeFieldServiceRateRow = (id: string) => {
    setFieldServiceRates(fieldServiceRates.filter(item => item.id !== id))
  }

  const updateFieldServiceRateData = (id: string, field: keyof FieldServiceRateItem, value: string) => {
    setFieldServiceRates(prev => 
      prev.map(item => 
        item.id === id ? { ...item, [field]: value } : item
      )
    )
  }

  const handleSaveFieldServiceRate = async () => {
    if (!session.data?.user?.id) {
      toast.error('로그인이 필요합니다.')
      return
    }

    setIsLoading(true)
    try {
      const fieldServiceRateData = {
        enabled: isEnabled,
        fieldServiceRates: fieldServiceRates
      }

      await updateFieldServiceRate(contractId, fieldServiceRateData, Number(session.data.user.id))
      toast.success('Field Service Rate가 성공적으로 저장되었습니다.')
    } catch (error) {
      console.error('Field Service Rate 저장 실패:', error)
      toast.error('Field Service Rate 저장에 실패했습니다.')
    } finally {
      setIsLoading(false)
    }
  }

  return (
    <div className="w-full">
      <Accordion type="single" collapsible className="w-full">
        <AccordionItem value="field-service-rate">
          <AccordionTrigger className="hover:no-underline">
            <div className="flex items-center gap-3 w-full">
              <DollarSign className="w-5 h-5" />
              <span className="font-medium">Field Service Rate</span>
            </div>
          </AccordionTrigger>
          <AccordionContent>
            <div className="space-y-6">
              {/* 체크박스 */}
              <div className="flex items-center gap-2">
                <Checkbox 
                  checked={isEnabled}
                  disabled={isDisabled}
                  onCheckedChange={(checked) => {
                    if (!isDisabled) {
                      setIsEnabled(checked as boolean)
                    }
                  }}
                />
                <span className="text-sm font-medium">Field Service Rate 활성화</span>
              </div>

              {/* Field Service Rate 테이블 */}
              <div className="space-y-4">
                <div className="flex items-center justify-between">
                  <h3 className="text-lg font-medium">Field Service Rate</h3>
                  <div className="flex gap-2">
                    <Button
                      type="button"
                      variant="outline"
                      size="sm"
                      onClick={addFieldServiceRateRow}
                      disabled={isDisabled || !isEnabled}
                      className="flex items-center gap-2"
                    >
                      <Plus className="w-4 h-4" />
                      행 추가
                    </Button>
                    <Button
                      type="button"
                      variant="outline"
                      size="sm"
                      onClick={() => {
                        if (confirm('선택된 행들을 삭제하시겠습니까?')) {
                          // 선택된 행들 삭제 로직 (필요시 구현)
                        }
                      }}
                      disabled={isDisabled || !isEnabled}
                      className="flex items-center gap-2"
                    >
                      <Trash2 className="w-4 h-4" />
                      행 삭제
                    </Button>
                  </div>
                </div>
                
                <div className="overflow-x-auto">
                  <table className={`w-full border-collapse border border-gray-300 ${!isEnabled ? 'opacity-50' : ''}`}>
                    <thead>
                      <tr className="bg-yellow-100">
                        <th className="border border-gray-300 p-2 w-16">No.</th>
                        <th className="border border-gray-300 p-2 w-40">Level of Specialist</th>
                        <th className="border border-gray-300 p-2">Description</th>
                        <th className="border border-gray-300 p-2 w-32">Rate Currency</th>
                        <th className="border border-gray-300 p-2 w-32">Onshore Rate</th>
                        <th className="border border-gray-300 p-2 w-32">Offshore Rate</th>
                        <th className="border border-gray-300 p-2 w-24">Rate Unit</th>
                        <th className="border border-gray-300 p-2 w-32">Remark</th>
                        <th className="border border-gray-300 p-2 w-20">Action</th>
                      </tr>
                    </thead>
                    <tbody>
                      {fieldServiceRates.map((item, index) => (
                        <tr key={item.id} className="bg-yellow-50">
                          <td className="border border-gray-300 p-2 text-center">{index + 1}</td>
                          <td className="border border-gray-300 p-2">
                            <Input
                              value={item.levelOfSpecialist}
                              onChange={(e) => updateFieldServiceRateData(item.id, 'levelOfSpecialist', e.target.value)}
                              disabled={isDisabled || !isEnabled}
                              className="border-0 bg-transparent p-0 h-auto"
                            />
                          </td>
                          <td className="border border-gray-300 p-2">
                            <Input
                              value={item.description}
                              onChange={(e) => updateFieldServiceRateData(item.id, 'description', e.target.value)}
                              disabled={isDisabled || !isEnabled}
                              className="border-0 bg-transparent p-0 h-auto"
                            />
                          </td>
                          <td className="border border-gray-300 p-2">
                            <Input
                              value={item.rateCurrency}
                              onChange={(e) => updateFieldServiceRateData(item.id, 'rateCurrency', e.target.value)}
                              disabled={isDisabled || !isEnabled}
                              className="border-0 bg-transparent p-0 h-auto"
                            />
                          </td>
                          <td className="border border-gray-300 p-2">
                            <Input
                              value={item.onshoreRate}
                              onChange={(e) => updateFieldServiceRateData(item.id, 'onshoreRate', e.target.value)}
                              disabled={isDisabled || !isEnabled}
                              className="border-0 bg-transparent p-0 h-auto"
                            />
                          </td>
                          <td className="border border-gray-300 p-2">
                            <Input
                              value={item.offshoreRate}
                              onChange={(e) => updateFieldServiceRateData(item.id, 'offshoreRate', e.target.value)}
                              disabled={isDisabled || !isEnabled}
                              className="border-0 bg-transparent p-0 h-auto"
                            />
                          </td>
                          <td className="border border-gray-300 p-2">
                            <Input
                              value={item.rateUnit}
                              onChange={(e) => updateFieldServiceRateData(item.id, 'rateUnit', e.target.value)}
                              disabled={isDisabled || !isEnabled}
                              className="border-0 bg-transparent p-0 h-auto"
                            />
                          </td>
                          <td className="border border-gray-300 p-2">
                            <Input
                              value={item.remark}
                              onChange={(e) => updateFieldServiceRateData(item.id, 'remark', e.target.value)}
                              disabled={isDisabled || !isEnabled}
                              className="border-0 bg-transparent p-0 h-auto"
                            />
                          </td>
                          <td className="border border-gray-300 p-2 text-center">
                            <Button
                              type="button"
                              variant="ghost"
                              size="sm"
                              onClick={() => removeFieldServiceRateRow(item.id)}
                              disabled={isDisabled || !isEnabled}
                              className="h-8 w-8 p-0 text-red-600 hover:text-red-700"
                            >
                              <Trash2 className="w-4 h-4" />
                            </Button>
                          </td>
                        </tr>
                      ))}
                    </tbody>
                  </table>
                </div>

                {/* Note 정보 */}
                <div className="space-y-2 text-sm text-gray-600">
                  <p><strong>Note #1:</strong> Air fare, travelling costs and hours, Visa, training, medical test and any additional applications are included.</p>
                  <p><strong>Note #2:</strong> Accommodation, meal and local transportation are included.</p>
                </div>
              </div>

              {/* 저장 버튼 */}
              <div className="flex justify-end pt-4 border-t">
                <Button 
                  onClick={handleSaveFieldServiceRate} 
                  disabled={isLoading || isDisabled || !isEnabled}
                  className="flex items-center gap-2"
                >
                  {isLoading ? (
                    <LoaderIcon className="w-4 h-4 animate-spin" />
                  ) : (
                    <Save className="w-4 h-4" />
                  )}
                  Field Service Rate 저장
                </Button>
              </div>
            </div>
          </AccordionContent>
        </AccordionItem>
      </Accordion>
    </div>
  )
}