summaryrefslogtreecommitdiff
path: root/lib/general-contracts_old/detail/general-contract-communication-channel.tsx
blob: f5cd79b21557ebbb995b079b550d80aa9ba324f2 (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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
'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, MessageSquare } from 'lucide-react'
import { updateCommunicationChannel, getCommunicationChannel } from '../service'
import { toast } from 'sonner'

interface CommunicationChannelProps {
  contractType?: string
  contractId: number
}

interface Representative {
  id: string
  position: string
  name: string
  telNo: string
  email: string
  isActive: boolean
}

export function CommunicationChannel({ contractId }: CommunicationChannelProps) {
  const session = useSession()
  const [isLoading, setIsLoading] = useState(false)
  const [isEnabled, setIsEnabled] = useState(true)
  
  // 일단 모든 계약종류에서 활성화
  const isDisabled = false
  
  const [contractorReps, setContractorReps] = useState<Representative[]>([])
  const [supplierReps, setSupplierReps] = useState<Representative[]>([])

  // 초기 데이터 로드
  useEffect(() => {
    const loadCommunicationChannel = async () => {
      try {
        const data = await getCommunicationChannel(contractId)
        if (data && data.enabled !== undefined) {
          setIsEnabled(data.enabled)
          setContractorReps(data.contractorRepresentatives || [])
          setSupplierReps(data.supplierRepresentatives || [])
        }
      } catch (error) {
        console.error('Error loading communication channel:', error)

      }
    }

    loadCommunicationChannel()
  }, [contractId])

  const addContractorRow = () => {
    const newId = (contractorReps.length + 1).toString()
    setContractorReps([...contractorReps, {
      id: newId,
      position: '',
      name: '',
      telNo: '',
      email: '',
      isActive: false
    }])
  }

  const removeContractorRow = () => {
    const selectedRows = contractorReps.filter(rep => rep.isActive)
    if (selectedRows.length > 0) {
      setContractorReps(contractorReps.filter(rep => !rep.isActive))
    }
  }

  const addSupplierRow = () => {
    const newId = (supplierReps.length + 1).toString()
    setSupplierReps([...supplierReps, {
      id: newId,
      position: '',
      name: '',
      telNo: '',
      email: '',
      isActive: false
    }])
  }

  const removeSupplierRow = () => {
    const selectedRows = supplierReps.filter(rep => rep.isActive)
    if (selectedRows.length > 0) {
      setSupplierReps(supplierReps.filter(rep => !rep.isActive))
    }
  }

  const updateContractorRep = (id: string, field: keyof Representative, value: string | boolean) => {
    setContractorReps(contractorReps.map(rep => 
      rep.id === id ? { ...rep, [field]: value } : rep
    ))
  }

  const updateSupplierRep = (id: string, field: keyof Representative, value: string | boolean) => {
    setSupplierReps(supplierReps.map(rep => 
      rep.id === id ? { ...rep, [field]: value } : rep
    ))
  }

  const handleSaveCommunicationChannel = async () => {
    const userId = session.data?.user?.id ? Number(session.data.user.id) : null
    
    if (!userId) {
      toast.error('사용자 정보를 찾을 수 없습니다.')
      return
    }

    try {
      setIsLoading(true)
      
      const communicationData = {
        enabled: isEnabled,
        contractorRepresentatives: contractorReps,
        supplierRepresentatives: supplierReps
      }

      await updateCommunicationChannel(contractId, communicationData, userId)
      toast.success('커뮤니케이션 채널이 저장되었습니다.')
    } catch (error) {
      console.error('Error saving communication channel:', error)
      toast.error('커뮤니케이션 채널 저장에 실패했습니다.')
    } finally {
      setIsLoading(false)
    }
  }

  return (
    <div className="w-full">
      <Accordion type="single" collapsible className="w-full">
          {/* Communication Channel 활성화 */}
          <AccordionItem value="communication-channel">
            <AccordionTrigger className="hover:no-underline">
              <div className="flex items-center gap-3 w-full">
                <MessageSquare className="w-5 h-5" />
                <span className="font-medium">Communication Channel</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">Communication Channel 활성화</span>
                </div>

                {/* Table 1: The Contractor's Representatives */}
                <div className="space-y-4">
                  <div className="flex items-center justify-between">
                    <h3 className="text-lg font-medium">Table 1: The Contractor &apos;s Representatives</h3>
                    <div className="flex gap-2">
                      <Button
                        type="button"
                        variant="outline"
                        size="sm"
                        onClick={addContractorRow}
                        disabled={isDisabled || !isEnabled}
                      >
                        <Plus className="w-4 h-4 mr-1" />
                        행 추가
                      </Button>
                      <Button
                        type="button"
                        variant="outline"
                        size="sm"
                        onClick={removeContractorRow}
                        disabled={isDisabled || !isEnabled}
                      >
                        <Trash2 className="w-4 h-4 mr-1" />
                        행 삭제
                      </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-12"></th>
                          <th className="border border-gray-300 p-2 w-16">No.</th>
                          <th className="border border-gray-300 p-2">Position</th>
                          <th className="border border-gray-300 p-2">Name</th>
                          <th className="border border-gray-300 p-2">Tel. No.</th>
                          <th className="border border-gray-300 p-2">Email</th>
                        </tr>
                      </thead>
                      <tbody>
                        {contractorReps.map((rep) => (
                          <tr key={rep.id} className="bg-yellow-50">
                            <td className="border border-gray-300 p-2 text-center">
                              <Checkbox
                                checked={rep.isActive}
                                onCheckedChange={(checked) => updateContractorRep(rep.id, 'isActive', checked as boolean)}
                                disabled={isDisabled || !isEnabled}
                              />
                            </td>
                            <td className="border border-gray-300 p-2 text-center">{rep.id}</td>
                            <td className="border border-gray-300 p-2">
                              <Input
                                value={rep.position}
                                onChange={(e) => updateContractorRep(rep.id, 'position', 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={rep.name}
                                onChange={(e) => updateContractorRep(rep.id, 'name', 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={rep.telNo}
                                onChange={(e) => updateContractorRep(rep.id, 'telNo', 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={rep.email}
                                onChange={(e) => updateContractorRep(rep.id, 'email', e.target.value)}
                                disabled={isDisabled || !isEnabled}
                                className="border-0 bg-transparent p-0 h-auto"
                              />
                            </td>
                          </tr>
                        ))}
                      </tbody>
                    </table>
                  </div>
                </div>

                {/* Table 2: The Supplier's Representatives */}
                <div className="space-y-4">
                  <div className="flex items-center justify-between">
                    <h3 className="text-lg font-medium">Table 2: The Supplier &apos;s Representatives</h3>
                    <div className="flex gap-2">
                      <Button
                        type="button"
                        variant="outline"
                        size="sm"
                        onClick={addSupplierRow}
                        disabled={isDisabled || !isEnabled}
                      >
                        <Plus className="w-4 h-4 mr-1" />
                        행 추가
                      </Button>
                      <Button
                        type="button"
                        variant="outline"
                        size="sm"
                        onClick={removeSupplierRow}
                        disabled={isDisabled || !isEnabled}
                      >
                        <Trash2 className="w-4 h-4 mr-1" />
                        행 삭제
                      </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-12"></th>
                          <th className="border border-gray-300 p-2 w-16">No.</th>
                          <th className="border border-gray-300 p-2">Position</th>
                          <th className="border border-gray-300 p-2">Name</th>
                          <th className="border border-gray-300 p-2">Tel. No.</th>
                          <th className="border border-gray-300 p-2">Email</th>
                        </tr>
                      </thead>
                      <tbody>
                        {supplierReps.map((rep) => (
                          <tr key={rep.id} className="bg-yellow-50">
                            <td className="border border-gray-300 p-2 text-center">
                              <Checkbox
                                checked={rep.isActive}
                                onCheckedChange={(checked) => updateSupplierRep(rep.id, 'isActive', checked as boolean)}
                                disabled={isDisabled || !isEnabled}
                              />
                            </td>
                            <td className="border border-gray-300 p-2 text-center">{rep.id}</td>
                            <td className="border border-gray-300 p-2">
                              <Input
                                value={rep.position}
                                onChange={(e) => updateSupplierRep(rep.id, 'position', 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={rep.name}
                                onChange={(e) => updateSupplierRep(rep.id, 'name', 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={rep.telNo}
                                onChange={(e) => updateSupplierRep(rep.id, 'telNo', 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={rep.email}
                                onChange={(e) => updateSupplierRep(rep.id, 'email', e.target.value)}
                                disabled={isDisabled || !isEnabled}
                                className="border-0 bg-transparent p-0 h-auto"
                              />
                            </td>
                          </tr>
                        ))}
                      </tbody>
                    </table>
                  </div>
                </div>

                {/* 저장 버튼 */}
                <div className="flex justify-end pt-4 border-t">
                  <Button 
                    onClick={handleSaveCommunicationChannel}
                    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" />
                    )}
                    커뮤니케이션 채널 저장
                  </Button>
                </div>
              </div>
            </AccordionContent>
          </AccordionItem>
        </Accordion>
    </div>
  )
}