summaryrefslogtreecommitdiff
path: root/lib/vendor-document-list/ship/enhanced-documents-table.tsx
blob: 47bce2750bd640718a5e280afdcd6fac9f0d7f52 (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
// simplified-documents-table.tsx
"use client"

import React from "react"
import type {
  DataTableAdvancedFilterField,
  DataTableFilterField,
  DataTableRowAction,
} from "@/types/table"

import { useDataTable } from "@/hooks/use-data-table"
import { getEnhancedDocumentsShip } from "../enhanced-document-service"
import { DataTableAdvancedToolbar } from "@/components/data-table/data-table-advanced-toolbar"
import { toast } from "sonner"

import { Label } from "@/components/ui/label"
import { DataTable } from "@/components/data-table/data-table"
import { SimplifiedDocumentsView } from "@/db/schema"
import { getSimplifiedDocumentColumns } from "./enhanced-doc-table-columns"

interface SimplifiedDocumentsTableProps {
  promises: Promise<{ 
    data: SimplifiedDocumentsView[], 
    pageCount: number, 
    total: number 
  }>
}

export function SimplifiedDocumentsTable({
  promises,
}: SimplifiedDocumentsTableProps) {
  // React.use()로 Promise 결과를 받고, 그 다음에 destructuring
  const result = React.use(promises)
  const { data, pageCount, total } = result

  // 기존 상태들
  const [rowAction, setRowAction] = React.useState<DataTableRowAction<SimplifiedDocumentsView> | null>(null) // ✅ 타입 변경
  const [expandedRows,] = React.useState<Set<string>>(new Set())

  const columns = React.useMemo(
    () => getSimplifiedDocumentColumns({ setRowAction }),
    [setRowAction]
  )

  // ✅ SimplifiedDocumentsView에 맞게 필터 필드 업데이트
  const advancedFilterFields: DataTableAdvancedFilterField<SimplifiedDocumentsView>[] = [
    {
      id: "docNumber",
      label: "문서번호",
      type: "text",
    },
    {
      id: "vendorDocNumber",
      label: "벤더 문서번호", 
      type: "text",
    },
    {
      id: "title",
      label: "문서제목",
      type: "text",
    },
    {
      id: "drawingKind",
      label: "문서종류",
      type: "select",
      options: [
        { label: "B3", value: "B3" },
        { label: "B4", value: "B4" },
        { label: "B5", value: "B5" },
      ],
    },
    {
      id: "projectCode",
      label: "프로젝트 코드",
      type: "text",
    },
    {
      id: "vendorName",
      label: "벤더명",
      type: "text",
    },
    {
      id: "vendorCode",
      label: "벤더 코드",
      type: "text",
    },
    {
      id: "pic",
      label: "담당자",
      type: "text",
    },
    {
      id: "status",
      label: "문서 상태",
      type: "select",
      options: [
        { label: "활성", value: "ACTIVE" },
        { label: "비활성", value: "INACTIVE" },
        { label: "보류", value: "PENDING" },
        { label: "완료", value: "COMPLETED" },
      ],
    },
    {
      id: "firstStageName",
      label: "1차 스테이지",
      type: "text",
    },
    {
      id: "secondStageName",
      label: "2차 스테이지", 
      type: "text",
    },
    {
      id: "firstStagePlanDate",
      label: "1차 계획일",
      type: "date",
    },
    {
      id: "firstStageActualDate",
      label: "1차 실제일",
      type: "date",
    },
    {
      id: "secondStagePlanDate",
      label: "2차 계획일",
      type: "date",
    },
    {
      id: "secondStageActualDate",
      label: "2차 실제일",
      type: "date",
    },
    {
      id: "issuedDate",
      label: "발행일",
      type: "date",
    },
    {
      id: "createdAt",
      label: "생성일",
      type: "date",
    },
    {
      id: "updatedAt",
      label: "수정일",
      type: "date",
    },
  ]

  // ✅ B4 전용 필드들 (조건부로 추가)
  const b4FilterFields: DataTableAdvancedFilterField<SimplifiedDocumentsView>[] = [
    {
      id: "cGbn",
      label: "C 구분",
      type: "text",
    },
    {
      id: "dGbn", 
      label: "D 구분",
      type: "text",
    },
    {
      id: "degreeGbn",
      label: "Degree 구분",
      type: "text", 
    },
    {
      id: "deptGbn",
      label: "Dept 구분",
      type: "text",
    },
    {
      id: "jGbn",
      label: "J 구분", 
      type: "text",
    },
    {
      id: "sGbn",
      label: "S 구분",
      type: "text",
    },
  ]

  // B4 문서가 있는지 확인하여 B4 전용 필드 추가
  const hasB4Documents = data.some(doc => doc.drawingKind === 'B4')
  const finalFilterFields = hasB4Documents 
    ? [...advancedFilterFields, ...b4FilterFields]
    : advancedFilterFields

  const { table } = useDataTable({
    data: data,
    columns,
    pageCount,
    enablePinning: true,
    enableAdvancedFilter: true,
    initialState: {
      sorting: [{ id: "createdAt", desc: true }],
      columnPinning: { right: ["actions"] },
    },
    getRowId: (originalRow) => String(originalRow.documentId),
    shallow: false,
    clearOnDefault: true,
    columnResizeMode: "onEnd",
  })

  // ✅ 행 액션 처리 (필요에 따라 구현)
  React.useEffect(() => {
    if (rowAction?.type === "view") {
      toast.info(`문서 조회: ${rowAction.row.docNumber}`)
      setRowAction(null)
    } else if (rowAction?.type === "edit") {
      toast.info(`문서 편집: ${rowAction.row.docNumber}`)
      setRowAction(null)
    } else if (rowAction?.type === "delete") {
      toast.error(`문서 삭제: ${rowAction.row.docNumber}`)
      setRowAction(null)
    }
  }, [rowAction])

  return (
    <div className="w-full" style={{maxWidth:'100%'}}>
    <DataTable table={table}>
      <DataTableAdvancedToolbar
        table={table}
        filterFields={finalFilterFields}
        shallow={false}
      >
        {/* ✅ 추가 툴바 컨텐츠 (필요시) */}
        <div className="flex items-center gap-2">
          <Label className="text-sm font-medium">
            총 {total}개 문서
          </Label>
        </div>
      </DataTableAdvancedToolbar>
    </DataTable>
    </div>
  )
}