summaryrefslogtreecommitdiff
path: root/lib/vendor-document-list/ship/enhanced-doc-table-toolbar-actions.tsx
blob: 3960bbce35aeb08ecebe97c1042c6a82d0e23221 (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
"use client"
import * as React from "react"
import { type Table } from "@tanstack/react-table"
import { Download, Upload, Plus, Files, RefreshCw } from "lucide-react"
import { toast } from "sonner"

import { exportTableToExcel } from "@/lib/export"
import { Button } from "@/components/ui/button"
import { EnhancedDocumentsView } from "@/db/schema/vendorDocu"
import { AddDocumentListDialog } from "./add-doc-dialog"
import { DeleteDocumentsDialog } from "./delete-docs-dialog"
import { BulkUploadDialog } from "./bulk-upload-dialog"
import type { EnhancedDocument } from "@/types/enhanced-documents"
import { SendToSHIButton } from "./send-to-shi-button"
import { ImportFromDOLCEButton } from "./import-from-dolce-button"
import { SWPWorkflowPanel } from "./swp-workflow-panel"

interface EnhancedDocTableToolbarActionsProps {
  table: Table<EnhancedDocument>
  projectType: "ship" | "plant"
  selectedPackageId: number
  onNewDocument: () => void
  onBulkAction: (action: string, selectedRows: any[]) => Promise<void>
}

export function EnhancedDocTableToolbarActions({
  table,
  projectType,
  selectedPackageId,
  onNewDocument,
  onBulkAction
}: EnhancedDocTableToolbarActionsProps) {
  const [bulkUploadDialogOpen, setBulkUploadDialogOpen] = React.useState(false)
  
  // 현재 테이블의 모든 데이터 (필터링된 상태)
  const allDocuments = table.getFilteredRowModel().rows.map(row => row.original)

  const handleSyncComplete = () => {
    // 동기화 완료 후 테이블 새로고침
    table.resetRowSelection()
    // 필요시 추가 액션 수행
  }

  const handleDocumentAdded = () => {
    // 테이블 새로고침
    table.resetRowSelection()
    
    // 추가적인 새로고침 시도
    setTimeout(() => {
      window.location.reload() // 강제 새로고침
    }, 500)
  }

  const handleImportComplete = () => {
    // 가져오기 완료 후 테이블 새로고침
    table.resetRowSelection()
    setTimeout(() => {
      window.location.reload()
    }, 500)
  }

  return (
    <div className="flex items-center gap-2">
      {/* 삭제 버튼 */}
      {table.getFilteredSelectedRowModel().rows.length > 0 ? (
        <DeleteDocumentsDialog
          documents={table
            .getFilteredSelectedRowModel()
            .rows.map((row) => row.original)}
          onSuccess={() => table.toggleAllRowsSelected(false)}
        />
      ) : null}

      {/* projectType에 따른 조건부 렌더링 */}
      {projectType === "ship" ? (
        <>
          {/* SHIP: DOLCE에서 목록 가져오기 */}
          <ImportFromDOLCEButton
            contractId={selectedPackageId}
            onImportComplete={handleImportComplete}
          />
        </>
      ) : (
        <>
          {/* PLANT: 수동 문서 추가 */}
          <AddDocumentListDialog 
            projectType={projectType}
            contractId={selectedPackageId}
            onSuccess={handleDocumentAdded}
          />
        </>
      )}

      {/* 일괄 업로드 버튼 (공통) */}
      <Button
        variant="outline"
        onClick={() => setBulkUploadDialogOpen(true)}
        className="flex items-center gap-2"
      >
        <Files className="w-4 h-4" />
        일괄 업로드
      </Button>

      {/* Export 버튼 (공통) */}
      <Button
        variant="outline"
        size="sm"
        onClick={() =>
          exportTableToExcel(table, {
            filename: "Document-list",
            excludeColumns: ["select", "actions"],
          })
        }
        className="gap-2"
      >
        <Download className="size-4" aria-hidden="true" />
        <span className="hidden sm:inline">Export</span>
      </Button>

      {/* Send to SHI 버튼 (공통) - 내부 → 외부로 보내기 */}
      <SendToSHIButton
        contractId={selectedPackageId}
        documents={allDocuments}
        onSyncComplete={handleSyncComplete}
        projectType={projectType}
      />

      {/* SWP 전용 워크플로우 패널 */}
      {projectType === "plant" && (
        <SWPWorkflowPanel
          contractId={selectedPackageId}
          documents={allDocuments}
          onWorkflowUpdate={handleSyncComplete}
        />
      )}

      {/* 일괄 업로드 다이얼로그 */}
      <BulkUploadDialog
        open={bulkUploadDialogOpen}
        onOpenChange={setBulkUploadDialogOpen}
        documents={allDocuments}
        projectType={projectType}
        contractId={selectedPackageId}
      />
    </div>
  )
}