summaryrefslogtreecommitdiff
path: root/lib/docu-list-rule/combo-box-settings/table/combo-box-options-expandable-row.tsx
blob: 07b63de5d491912bd492a069eaf0ec7db224177e (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
"use client"

import * as React from "react"
import { useState, useEffect } from "react"
import { MoreHorizontal, Settings } from "lucide-react"
import { Button } from "@/components/ui/button"
import { Input } from "@/components/ui/input"
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuSeparator,
  DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu"
import {
  Table,
  TableBody,
  TableCell,
  TableHead,
  TableHeader,
  TableRow,
} from "@/components/ui/table"
import { toast } from "sonner"
import { codeGroups } from "@/db/schema/codeGroups"
import { getComboBoxOptions, updateComboBoxOption, deleteComboBoxOption } from "../service"
import { DocumentClassOptionsSheet } from "./document-class-options-sheet"  


interface ComboBoxOptionsExpandableRowProps {
  codeGroup: typeof codeGroups.$inferSelect
}

interface ComboBoxOption {
  id: number
  codeGroupId: number
  code: string
  description: string
  remark: string | null
  createdAt: Date
  updatedAt: Date
}

export function ComboBoxOptionsExpandableRow({ codeGroup }: ComboBoxOptionsExpandableRowProps) {
  const [options, setOptions] = useState<ComboBoxOption[]>([])
  const [loading, setLoading] = useState(true)
  const [editingOption, setEditingOption] = useState<ComboBoxOption | null>(null)
  const [selectedOptionForSubOptions, setSelectedOptionForSubOptions] = useState<ComboBoxOption | null>(null)

  // 옵션 목록 로드
  const loadOptions = async () => {
    try {
      setLoading(true)
      const result = await getComboBoxOptions(codeGroup.id)
      if (result.success && result.data) {
        setOptions(result.data as ComboBoxOption[])
      } else {
        toast.error("옵션 목록을 불러오는데 실패했습니다.")
      }
    } catch (error) {
      console.error("옵션 로드 실패:", error)
      toast.error("옵션 목록을 불러오는데 실패했습니다.")
    } finally {
      setLoading(false)
    }
  }

  useEffect(() => {
    loadOptions()
  }, [codeGroup.id])

  // 기존 옵션 수정
  const handleUpdateOption = async (option: ComboBoxOption) => {
    if (!option.code.trim() || !option.description.trim()) {
      toast.error("Code와 Description은 필수 입력 항목입니다.")
      return
    }

    try {
      const result = await updateComboBoxOption({
        id: option.id,
        code: option.code.trim(),
        description: option.description.trim(),
        remark: option.remark || undefined,
      })
      
      if (result.success) {
        await loadOptions() // 목록 새로고침
        setEditingOption(null) // 편집 모드 종료
        toast.success("옵션이 수정되었습니다.")
      } else {
        toast.error("옵션 수정에 실패했습니다.")
      }
    } catch (error) {
      console.error("옵션 수정 실패:", error)
      toast.error("옵션 수정에 실패했습니다.")
    }
  }

  // 기존 옵션 삭제
  const handleDeleteOption = async (optionId: number) => {
    if (!confirm("정말로 이 옵션을 삭제하시겠습니까?")) {
      return
    }

    try {
      const result = await deleteComboBoxOption(optionId)
      if (result.success) {
        await loadOptions() // 목록 새로고침
        toast.success("옵션이 삭제되었습니다.")
      } else {
        toast.error("옵션 삭제에 실패했습니다.")
      }
    } catch (error) {
      console.error("옵션 삭제 실패:", error)
      toast.error("옵션 삭제에 실패했습니다.")
    }
  }

  // Document Class인지 확인 (Description이 "Document Class"인 경우)
  const isDocumentClass = codeGroup.description === "Document Class"

  return (
    <div className="bg-muted/20 border-t">
      <div className="space-y-0 ml-[60px]">
        {/* 커스텀 테이블 */}
        <div className="border overflow-hidden bg-white">
          <Table className="w-full table-fixed">
            <TableHeader>
              <TableRow className="bg-muted/30">
                <TableHead className="w-[20%] font-medium text-muted-foreground">Code</TableHead>
                <TableHead className="w-[30%] font-medium text-muted-foreground">Description</TableHead>
                <TableHead className="w-[25%] font-medium text-muted-foreground">Remark</TableHead>
                {isDocumentClass && (
                  <TableHead className="w-[15%] font-medium text-muted-foreground">하위 옵션</TableHead>
                )}
                <TableHead className="w-[10%] font-medium text-muted-foreground">작업</TableHead>
              </TableRow>
            </TableHeader>
            <TableBody>
              {/* 기존 옵션들 */}
              {options.map((option) => (
                <TableRow key={option.id} className="hover:bg-muted/30 transition-colors">
                  <TableCell className="font-medium text-sm">
                    {editingOption?.id === option.id ? (
                      <Input
                        value={editingOption.code}
                        onChange={(e) => setEditingOption(prev => prev ? { ...prev, code: e.target.value } : null)}
                        placeholder="Code (*)"
                        className="border-0 focus-visible:ring-1 bg-transparent h-8"
                      />
                    ) : (
                      option.code
                    )}
                  </TableCell>
                  <TableCell className="text-sm">
                    {editingOption?.id === option.id ? (
                      <Input
                        value={editingOption.description}
                        onChange={(e) => setEditingOption(prev => prev ? { ...prev, description: e.target.value } : null)}
                        placeholder="Description (*)"
                        className="border-0 focus-visible:ring-1 bg-transparent h-8"
                      />
                    ) : (
                      option.description
                    )}
                  </TableCell>
                  <TableCell className="text-sm text-muted-foreground">
                    {editingOption?.id === option.id ? (
                      <Input
                        value={editingOption.remark || ""}
                        onChange={(e) => setEditingOption(prev => prev ? { ...prev, remark: e.target.value } : null)}
                        placeholder="Remark"
                        className="border-0 focus-visible:ring-1 bg-transparent h-8"
                      />
                    ) : (
                      option.remark || "-"
                    )}
                  </TableCell>
                  {isDocumentClass && (
                    <TableCell className="text-sm">
                      <Button
                        variant="outline"
                        size="sm"
                        onClick={() => setSelectedOptionForSubOptions(option)}
                        className="h-6 px-2 text-xs"
                      >
                        <Settings className="h-3 w-3 mr-1" />
                        관리
                      </Button>
                    </TableCell>
                  )}
                  <TableCell className="text-sm">
                    {editingOption?.id === option.id ? (
                      <div className="flex gap-1">
                        <Button
                          onClick={() => handleUpdateOption(editingOption)}
                          size="sm"
                          variant="outline"
                          className="h-6 px-2 text-xs"
                        >
                          저장
                        </Button>
                        <Button
                          onClick={() => setEditingOption(null)}
                          size="sm"
                          variant="ghost"
                          className="h-6 px-2 text-xs"
                        >
                          취소
                        </Button>
                      </div>
                    ) : (
                      <DropdownMenu>
                        <DropdownMenuTrigger asChild>
                          <Button
                            variant="ghost"
                            className="h-6 w-6 p-0"
                          >
                            <MoreHorizontal className="h-3 w-3" />
                          </Button>
                        </DropdownMenuTrigger>
                        <DropdownMenuContent align="end">
                          <DropdownMenuItem onClick={() => setEditingOption(option)}>
                            수정
                          </DropdownMenuItem>
                          <DropdownMenuSeparator />
                          <DropdownMenuItem onClick={() => handleDeleteOption(option.id)}>
                            삭제
                          </DropdownMenuItem>
                        </DropdownMenuContent>
                      </DropdownMenu>
                    )}
                  </TableCell>
                </TableRow>
              ))}

              {options.length === 0 && (
                <TableRow>
                  <TableCell colSpan={isDocumentClass ? 5 : 4} className="text-center text-muted-foreground py-8">
                    등록된 옵션이 없습니다.
                  </TableCell>
                </TableRow>
              )}
            </TableBody>
          </Table>
        </div>
      </div>

      {/* Document Class 하위 옵션 관리 시트 */}
      {selectedOptionForSubOptions && (
        <DocumentClassOptionsSheet
          open={!!selectedOptionForSubOptions}
          onOpenChange={(open) => !open && setSelectedOptionForSubOptions(null)}
          comboBoxOption={selectedOptionForSubOptions}
          onSuccess={() => {
            setSelectedOptionForSubOptions(null)
            // 필요시 하위 옵션 목록 새로고침
          }}
        />
      )}
    </div>
  )
}