summaryrefslogtreecommitdiff
path: root/lib/b-rfq/initial/initial-rfq-detail-table.tsx
blob: 5ea6b0bf2edbe8679a0699297a599d44375f0415 (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
"use client"

import * as React from "react"
import { type DataTableAdvancedFilterField, type DataTableFilterField } from "@/types/table"
import { useDataTable } from "@/hooks/use-data-table"
import { DataTable } from "@/components/data-table/data-table"
import { DataTableAdvancedToolbar } from "@/components/data-table/data-table-advanced-toolbar"
import { getInitialRfqDetail } from "../service" // 앞서 만든 서버 액션
import { 
  getInitialRfqDetailColumns, 
  type DataTableRowAction 
} from "./initial-rfq-detail-columns"
import { InitialRfqDetailTableToolbarActions } from "./initial-rfq-detail-toolbar-actions"
import { DeleteInitialRfqDialog } from "./delete-initial-rfq-dialog"
import { UpdateInitialRfqSheet } from "./update-initial-rfq-sheet"
import { InitialRfqDetailView } from "@/db/schema"

interface InitialRfqDetailTableProps {
  promises: Promise<Awaited<ReturnType<typeof getInitialRfqDetail>>>
  rfqId?: number
}

export function InitialRfqDetailTable({ promises, rfqId }: InitialRfqDetailTableProps) {
  const { data, pageCount } = React.use(promises)

  // 선택된 상세 정보
  const [selectedDetail, setSelectedDetail] = React.useState<any>(null)
  
  // Row action 상태 (update/delete)
  const [rowAction, setRowAction] = React.useState<DataTableRowAction<InitialRfqDetailView> | null>(null)

  const columns = React.useMemo(
    () => getInitialRfqDetailColumns({
      onSelectDetail: setSelectedDetail,
      setRowAction: setRowAction
    }),
    []
  )

  /**
   * 필터 필드 정의
   */
  const filterFields: DataTableFilterField<any>[] = [
    {
      id: "rfqCode",
      label: "RFQ 코드",
      placeholder: "RFQ 코드로 검색...",
    },
    {
      id: "vendorName",
      label: "벤더명",
      placeholder: "벤더명으로 검색...",
    },
    {
      id: "rfqStatus",
      label: "RFQ 상태",
      options: [
        { label: "Draft", value: "DRAFT", count: 0 },
        { label: "문서 접수", value: "Doc. Received", count: 0 },
        { label: "담당자 배정", value: "PIC Assigned", count: 0 },
        { label: "문서 확정", value: "Doc. Confirmed", count: 0 },
        { label: "초기 RFQ 발송", value: "Init. RFQ Sent", count: 0 },
        { label: "초기 RFQ 응답", value: "Init. RFQ Answered", count: 0 },
        { label: "TBE 시작", value: "TBE started", count: 0 },
        { label: "TBE 완료", value: "TBE finished", count: 0 },
        { label: "최종 RFQ 발송", value: "Final RFQ Sent", count: 0 },
        { label: "견적 접수", value: "Quotation Received", count: 0 },
        { label: "벤더 선정", value: "Vendor Selected", count: 0 },
      ],
    },
    {
      id: "initialRfqStatus",
      label: "초기 RFQ 상태",
      options: [
        { label: "초안", value: "DRAFT", count: 0 },
        { label: "발송", value: "Init. RFQ Sent", count: 0 },
        { label: "응답", value: "Init. RFQ Answered", count: 0 },
        { label: "거절", value: "S/L Decline", count: 0 },
      ],
    },
    {
      id: "vendorCountry",
      label: "벤더 국가",
      options: [
        { label: "한국", value: "KR", count: 0 },
        { label: "중국", value: "CN", count: 0 },
        { label: "일본", value: "JP", count: 0 },
        { label: "미국", value: "US", count: 0 },
        { label: "독일", value: "DE", count: 0 },
      ],
    },
  ]

  /**
   * 고급 필터 필드
   */
  const advancedFilterFields: DataTableAdvancedFilterField<any>[] = [
    {
      id: "rfqCode",
      label: "RFQ 코드",
      type: "text",
    },
    {
      id: "vendorName",
      label: "벤더명",
      type: "text",
    },
    {
      id: "vendorCode",
      label: "벤더 코드",
      type: "text",
    },
    {
      id: "vendorCountry",
      label: "벤더 국가",
      type: "multi-select",
      options: [
        { label: "한국", value: "KR" },
        { label: "중국", value: "CN" },
        { label: "일본", value: "JP" },
        { label: "미국", value: "US" },
        { label: "독일", value: "DE" },
      ],
    },
    {
      id: "rfqStatus",
      label: "RFQ 상태",
      type: "multi-select",
      options: [
        { label: "Draft", value: "DRAFT" },
        { label: "문서 접수", value: "Doc. Received" },
        { label: "담당자 배정", value: "PIC Assigned" },
        { label: "문서 확정", value: "Doc. Confirmed" },
        { label: "초기 RFQ 발송", value: "Init. RFQ Sent" },
        { label: "초기 RFQ 응답", value: "Init. RFQ Answered" },
        { label: "TBE 시작", value: "TBE started" },
        { label: "TBE 완료", value: "TBE finished" },
        { label: "최종 RFQ 발송", value: "Final RFQ Sent" },
        { label: "견적 접수", value: "Quotation Received" },
        { label: "벤더 선정", value: "Vendor Selected" },
      ],
    },
    {
      id: "initialRfqStatus",
      label: "초기 RFQ 상태",
      type: "multi-select",
      options: [
        { label: "초안", value: "DRAFT" },
        { label: "발송", value: "Init. RFQ Sent" },
        { label: "응답", value: "Init. RFQ Answered" },
        { label: "거절", value: "S/L Decline" },
      ],
    },
    {
      id: "vendorBusinessSize",
      label: "벤더 규모",
      type: "multi-select",
      options: [
        { label: "대기업", value: "LARGE" },
        { label: "중기업", value: "MEDIUM" },
        { label: "소기업", value: "SMALL" },
        { label: "스타트업", value: "STARTUP" },
      ],
    },
    {
      id: "incotermsCode",
      label: "Incoterms",
      type: "text",
    },
    {
      id: "dueDate",
      label: "마감일",
      type: "date",
    },
    {
      id: "validDate",
      label: "유효일",
      type: "date",
    },
    {
      id: "shortList",
      label: "Short List",
      type: "boolean",
    },
    {
      id: "returnYn",
      label: "Return 여부",
      type: "boolean",
    },
    {
      id: "cpRequestYn",
      label: "CP Request 여부",
      type: "boolean",
    },
    {
      id: "prjectGtcYn",
      label: "Project GTC 여부",
      type: "boolean",
    },
    {
      id: "classification",
      label: "분류",
      type: "text",
    },
    {
      id: "sparepart",
      label: "예비부품",
      type: "text",
    },
    {
      id: "createdAt",
      label: "등록일",
      type: "date",
    },
  ]

  const { table } = useDataTable({
    data,
    columns,
    pageCount,
    filterFields,
    enableAdvancedFilter: true,
    initialState: {
      sorting: [{ id: "createdAt", desc: true }],
      columnPinning: { right: ["actions"] },
    },
    getRowId: (originalRow) => originalRow.initialRfqId ? originalRow.initialRfqId.toString():"1",
    shallow: false,
    clearOnDefault: true,
  })

  return (
    <div className="space-y-6">
      {/* 메인 테이블 */}
      <div className="h-full w-full">
        <DataTable table={table} className="h-full">
          <DataTableAdvancedToolbar
            table={table}
            filterFields={advancedFilterFields}
            shallow={false}
          >
            <InitialRfqDetailTableToolbarActions table={table} rfqId={rfqId} />
          </DataTableAdvancedToolbar>
        </DataTable>
      </div>

      {/* Update Sheet */}
      <UpdateInitialRfqSheet
        open={rowAction?.type === "update"}
        onOpenChange={() => setRowAction(null)}
        initialRfq={rowAction?.type === "update" ? rowAction.row.original : null}
      />

      {/* Delete Dialog */}
      <DeleteInitialRfqDialog
        open={rowAction?.type === "delete"}
        onOpenChange={() => setRowAction(null)}
        initialRfqs={rowAction?.type === "delete" ? [rowAction.row.original] : []}
        showTrigger={false}
        onSuccess={() => {
          setRowAction(null)
          // 테이블 리프레시는 revalidatePath로 자동 처리됨
        }}
      />
    </div>
  )
}