summaryrefslogtreecommitdiff
path: root/lib/basic-contract/gtc-vendor/gtc-clauses-table-toolbar-actions.tsx
blob: 3a0fbdb6b5f531023fab28d0b28419fc94caea45 (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
"use client"

import * as React from "react"
import { type Table } from "@tanstack/react-table"
import {
  Download,
  Upload,
  Settings2,
  ArrowUpDown,
  Edit,
  Eye,
  FileText,
  Wand2
} from "lucide-react"

import { Button } from "@/components/ui/button"
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuSeparator,
  DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu"
import {
  Tooltip,
  TooltipContent,
  TooltipProvider,
  TooltipTrigger,
} from "@/components/ui/tooltip"

import { type GtcClauseTreeView } from "@/db/schema/gtc"
import { CreateVendorGtcClauseDialog } from "./create-gtc-clause-dialog"
import { PreviewDocumentDialog } from "./preview-document-dialog"
import { DeleteGtcClausesDialog } from "./delete-gtc-clauses-dialog"
import { exportTableToExcel } from "@/lib/export"
import { exportFullDataToExcel, type ExcelColumnDef } from "@/lib/export"
import { getAllGtcClausesForExport, importGtcClausesFromExcel } from "@/lib/gtc-contract/service"
import { ImportExcelDialog } from "./import-excel-dialog"
import { toast } from "@/hooks/use-toast"

interface GtcClausesTableToolbarActionsProps {
  table: Table<GtcClauseTreeView>
  documentId: number
  document: any
  currentUserId?: number // 현재 사용자 ID 추가
}

// GTC 조항을 위한 Excel 컬럼 정의 (실용적으로 간소화)
const gtcClauseExcelColumns: ExcelColumnDef[] = [
  {
    id: "itemNumber",
    header: "채번",
    accessor: "itemNumber",
    group: "필수 정보"
  },
  {
    id: "subtitle",
    header: "소제목",
    accessor: "subtitle",
    group: "필수 정보"
  },
  {
    id: "content",
    header: "상세항목",
    accessor: "content",
    group: "기본 정보"
  },
  {
    id: "category",
    header: "분류",
    accessor: "category",
    group: "기본 정보"
  },
  {
    id: "sortOrder",
    header: "순서",
    accessor: "sortOrder",
    group: "순서"
  },
  {
    id: "parentId",
    header: "상위 조항 ID",
    accessor: "parentId",
    group: "계층 구조"
  },
  {
    id: "isActive",
    header: "활성 상태",
    accessor: (row) => row.isActive ? "활성" : "비활성",
    group: "상태"
  },
  {
    id: "editReason",
    header: "편집 사유",
    accessor: "editReason",
    group: "추가 정보"
  }
]

export function GtcClausesTableToolbarActions({
  table,
  documentId,
  document,
  currentUserId = 1, // 기본값 설정 (실제로는 auth에서 가져와야 함)
}: GtcClausesTableToolbarActionsProps) {
  const [showCreateDialog, setShowCreateDialog] = React.useState(false)
  const [showReorderDialog, setShowReorderDialog] = React.useState(false)
  const [showBulkUpdateDialog, setShowBulkUpdateDialog] = React.useState(false)
  const [showGenerateVariablesDialog, setShowGenerateVariablesDialog] = React.useState(false)
  const [showPreviewDialog, setShowPreviewDialog] = React.useState(false)
  const [isExporting, setIsExporting] = React.useState(false)

  const selectedRows = table.getSelectedRowModel().rows
  const selectedCount = selectedRows.length

  // 테이블의 모든 데이터 가져오기 (현재 페이지만)
  const allClauses = table.getRowModel().rows.map(row => row.original)

  const transformClausesForPreview = (clauses: GtcClauseTreeView[]) => {
    return clauses.map(clause => {
      // vendorInfo가 있고 수정된 값이 있는 경우 적용
      if (clause.vendorInfo) {
        const { vendorInfo } = clause;
        return {
          ...clause,
          // null이 아닌 수정값이 있으면 해당 값으로 대체
          category: vendorInfo.modifiedCategory ?? clause.category,
          content: vendorInfo.modifiedContent ?? clause.content,
          itemNumber: vendorInfo.modifiedItemNumber ?? clause.itemNumber,
          subtitle: vendorInfo.modifiedSubtitle ?? clause.subtitle,
        };
      }
      // vendorInfo가 없으면 원본 그대로 반환
      return clause;
    });
  };

  // 컴포넌트 내에서 변환된 데이터 생성
  const previewClauses = React.useMemo(
    () => transformClausesForPreview(allClauses),
    [allClauses]
  );

  console.log(allClauses, "allClauses")

  // 현재 페이지 데이터만 Excel로 내보내기
  const handleExportCurrentPageToExcel = () => {
    exportTableToExcel(table, {
      filename: `gtc-clauses-page-${new Date().toISOString().split('T')[0]}`,
      excludeColumns: ["select", "actions"],
    })
  }

  // 전체 데이터를 Excel로 내보내기
  const handleExportAllToExcel = async () => {
    try {
      setIsExporting(true)

      // 서버에서 전체 데이터 가져오기
      const allData = await getAllGtcClausesForExport(documentId)

      // 전체 데이터를 Excel로 내보내기
      await exportFullDataToExcel(
        allData,
        gtcClauseExcelColumns,
        {
          filename: `gtc-clauses-all-${new Date().toISOString().split('T')[0]}`,
          useGroupHeader: true
        }
      )

      toast({
        title: "내보내기 완료",
        description: `총 ${allData.length}개의 조항이 Excel 파일로 내보내졌습니다.`,
      })
    } catch (error) {
      console.error("Excel export failed:", error)
      toast({
        title: "내보내기 실패",
        description: "Excel 파일 내보내기 중 오류가 발생했습니다.",
        variant: "destructive"
      })
    } finally {
      setIsExporting(false)
    }
  }

  // Excel 데이터 가져오기 처리
  const handleImportExcelData = async (data: Partial<GtcClauseTreeView>[]) => {
    try {
      const result = await importGtcClausesFromExcel(documentId, data, currentUserId)

      if (result.success) {
        toast({
          title: "가져오기 성공",
          description: `${result.importedCount}개의 조항이 성공적으로 가져와졌습니다.`,
        })

        // 테이블 새로고침
        handleRefreshTable()
      } else {
        const errorMessage = result.errors.length > 0
          ? `오류: ${result.errors.slice(0, 3).join(', ')}${result.errors.length > 3 ? '...' : ''}`
          : "알 수 없는 오류가 발생했습니다."

        toast({
          title: "가져오기 실패",
          description: errorMessage,
          variant: "destructive"
        })

        // 오류가 있어도 일부는 성공했을 수 있음
        if (result.importedCount > 0) {
          handleRefreshTable()
        }

        throw new Error("Import failed with errors")
      }
    } catch (error) {
      console.error("Excel import failed:", error)
      throw error // ImportExcelDialog에서 처리하도록 다시 throw
    }
  }

  const handlePreviewDocument = () => {
    setShowPreviewDialog(true)
  }

  const handleGenerateDocument = () => {
    // 최종 문서 생성
    console.log("Generate final document")
  }

  const handleReorderClauses = () => {
    setShowReorderDialog(true)
  }

  const handleBulkUpdate = () => {
    setShowBulkUpdateDialog(true)
  }

  const handleGenerateVariables = () => {
    setShowGenerateVariablesDialog(true)
  }

  const handleRefreshTable = () => {
    // 테이블 새로고침 로직
    console.log("Refresh table after creation")
    // table.reset() 또는 상위 컴포넌트의 refetch 함수 호출
  }

  return (
    <>
      <div className="flex items-center gap-2">
        {/* 조항 추가 버튼 */}
        <CreateVendorGtcClauseDialog
          documentId={documentId}
          document={document}
          onSuccess={handleRefreshTable}
        />

        {/* 선택된 항목이 있을 때 표시되는 액션들 */}
        {selectedCount > 0 && (
          <>
            <DeleteGtcClausesDialog
              gtcClauses={allClauses}
              onSuccess={() => table.toggleAllRowsSelected(false)}
            />
          </>
        )}

        {/* 관리 도구 드롭다운 */}
        <DropdownMenu>
          <DropdownMenuTrigger asChild>
            <Button variant="outline" size="sm" disabled={isExporting}>
              <Settings2 className="mr-2 h-4 w-4" />
              관리 도구
            </Button>
          </DropdownMenuTrigger>
          <DropdownMenuContent align="end" className="w-64">
            <DropdownMenuItem onClick={handleExportCurrentPageToExcel}>
              <Download className="mr-2 h-4 w-4" />
              현재 페이지 Excel로 내보내기
            </DropdownMenuItem>

            <DropdownMenuItem
              onClick={handleExportAllToExcel}
              disabled={isExporting}
            >
              <Download className="mr-2 h-4 w-4" />
              {isExporting ? "내보내는 중..." : "전체 데이터 Excel로 내보내기"}
            </DropdownMenuItem>

            <DropdownMenuSeparator />

            {/* <ImportExcelDialog
              documentId={documentId}
              columns={gtcClauseExcelColumns}
              onSuccess={handleRefreshTable}
              onImport={handleImportExcelData}
              trigger={
                <DropdownMenuItem onSelect={(e) => e.preventDefault()}>
                  <Upload className="mr-2 h-4 w-4" />
                  Excel에서 가져오기
                </DropdownMenuItem>
              }
            /> */}

            <DropdownMenuSeparator />

            <DropdownMenuItem onClick={handlePreviewDocument}>
              <Eye className="mr-2 h-4 w-4" />
              문서 미리보기
            </DropdownMenuItem>
          </DropdownMenuContent>
        </DropdownMenu>

        {/* 조건부로 표시되는 다이얼로그들 */}
        {showReorderDialog && (
          <div>
            {/* ReorderGtcClausesDialog 컴포넌트가 여기에 올 예정 */}
          </div>
        )}

        {showBulkUpdateDialog && (
          <div>
            {/* BulkUpdateGtcClausesDialog 컴포넌트가 여기에 올 예정 */}
          </div>
        )}

        {showGenerateVariablesDialog && (
          <div>
            {/* GenerateVariableNamesDialog 컴포넌트가 여기에 올 예정 */}
          </div>
        )}
      </div>

      {/* 미리보기 다이얼로그 */}
      <PreviewDocumentDialog
        open={showPreviewDialog}
        onOpenChange={setShowPreviewDialog}
        clauses={previewClauses}
        document={document}
        onExport={() => {
          console.log("Export from preview dialog")
        }}
      />
    </>
  )
}