summaryrefslogtreecommitdiff
path: root/lib/techsales-rfq/table/rfq-table-column.tsx
blob: caaa1c9704211165aca01a9ba97e87e646e9d609 (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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
"use client"

import * as React from "react"
import { ColumnDef } from "@tanstack/react-table"
import { formatDate, formatDateTime } from "@/lib/utils"
import { Checkbox } from "@/components/ui/checkbox"
import { DataTableColumnHeaderSimple } from "@/components/data-table/data-table-column-simple-header"
import { DataTableRowAction } from "@/types/table"
import { Check, Pencil, X, Info } from "lucide-react"
import { Button } from "@/components/ui/button"
import { toast } from "sonner"
import { Input } from "@/components/ui/input"

// 기본적인 RFQ 타입 정의 (rfq-table.tsx 파일과 일치해야 함)
type TechSalesRfq = {
  id: number
  rfqCode: string | null
  itemId: number
  itemName: string | null
  materialCode: string | null
  dueDate: Date
  rfqSendDate: Date | null
  status: "RFQ Created" | "RFQ Vendor Assignned" | "RFQ Sent" | "Quotation Analysis" | "Closed"
  picCode: string | null
  remark: string | null
  cancelReason: string | null
  createdAt: Date
  updatedAt: Date
  createdBy: number | null
  createdByName: string
  updatedBy: number | null
  updatedByName: string
  sentBy: number | null
  sentByName: string | null
  projectSnapshot: any
  seriesSnapshot: any
  pspid: string
  projNm: string
  sector: string
  projMsrm: number
  ptypeNm: string
  attachmentCount: number
  quotationCount: number
  // 나머지 필드는 사용할 때마다 추가
  [key: string]: any
}

interface GetColumnsProps {
  setRowAction: React.Dispatch<React.SetStateAction<DataTableRowAction<TechSalesRfq> | null>>;
  // 상태와 상태 설정 함수를 props로 받음
  editingCell: EditingCellState | null;
  setEditingCell: (state: EditingCellState | null) => void;
  updateRemark: (rfqId: number, remark: string) => Promise<void>;
}

export interface EditingCellState {
  rowId: string | number;
  value: string;
}


export function getColumns({
  setRowAction,
  editingCell,
  setEditingCell,
  updateRemark,
}: GetColumnsProps): ColumnDef<TechSalesRfq>[] {
  return [
    {
      id: "select",
      // Remove the "Select all" checkbox in header since we're doing single-select
      header: () => <span className="sr-only">Select</span>,
      cell: ({ row, table }) => (
        <Checkbox
          checked={row.getIsSelected()}
          onCheckedChange={(value) => {
            // If selecting this row
            if (value) {
              // First deselect all rows (to ensure single selection)
              table.toggleAllRowsSelected(false)
              // Then select just this row
              row.toggleSelected(true)
              // Trigger the same action that was in the "Select" button
              setRowAction({ row, type: "select" })
            } else {
              // Just deselect this row
              row.toggleSelected(false)
            }
          }}
          aria-label="Select row"
          className="translate-y-0.5"
        />
      ),
      enableSorting: false,
      enableHiding: false,
      enableResizing: false,
      size: 40,
      minSize: 40,
      maxSize: 40,
    },
    
    {
      accessorKey: "status",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="진행상태" />
      ),
      cell: ({ row }) => <div>{row.getValue("status")}</div>,
      meta: {
        excelHeader: "진행상태"
      },
      enableResizing: true,
      minSize: 80,
      size: 100,
    },
    {
      accessorKey: "rfqCode",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="RFQ No." />
      ),
      cell: ({ row }) => <div>{row.getValue("rfqCode")}</div>,
      meta: {
        excelHeader: "RFQ No."
      },
      enableResizing: true,
      size: 120,
    },
    {
      accessorKey: "materialCode",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="자재코드" />
      ),
      cell: ({ row }) => <div>{row.getValue("materialCode")}</div>,
      meta: {
        excelHeader: "자재코드"
      },
      enableResizing: true,
      minSize: 80,
      size: 120,
    },
    {
      accessorKey: "itemName",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="자재명" />
      ),
      cell: ({ row }) => <div>{row.getValue("itemName")}</div>,
      meta: {
        excelHeader: "자재명"
      },
      enableResizing: true,
      size: 180,
    },
    {
      accessorKey: "pspid",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="프로젝트 번호" />
      ),
      cell: ({ row }) => <div>{row.getValue("pspid")}</div>,
      meta: {
        excelHeader: "프로젝트 번호"
      },
      enableResizing: true,
      size: 120,
    },
    {
      accessorKey: "projNm",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="프로젝트명" />
      ),
      cell: ({ row }) => <div>{row.getValue("projNm")}</div>,
      meta: {
        excelHeader: "프로젝트명"
      },
      enableResizing: true,
      size: 160,
    },
    {
      accessorKey: "projMsrm",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="척수" />
      ),
      cell: ({ row }) => <div>{row.getValue("projMsrm")}</div>,
      meta: {
        excelHeader: "척수"
      },
      enableResizing: true,
      minSize: 60,
      size: 80,
    },
    {
      accessorKey: "ptypeNm",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="선종" />
      ),
      cell: ({ row }) => <div>{row.getValue("ptypeNm")}</div>,
      meta: {
        excelHeader: "선종"
      },
      enableResizing: true,
      size: 120,
    },
    {
      accessorKey: "quotationCount",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="견적수" />
      ),
      cell: ({ row }) => <div>{row.getValue("quotationCount")}</div>,
      meta: {
        excelHeader: "견적수"
      },
      enableResizing: true,
      size: 80,
    },
    {
      accessorKey: "rfqSendDate",
      header: ({ column }) => (
      <DataTableColumnHeaderSimple column={column} title="RFQ 전송일" />
      ),
      cell: ({ cell }) => {
        const value = cell.getValue();
        return value ? formatDate(value as Date, "KR") : "";
      },
      meta: {
        excelHeader: "RFQ 전송일"
      },
      enableResizing: true,
      size: 120,
    },
    {
      accessorKey: "dueDate",
      header: ({ column }) => (
      <DataTableColumnHeaderSimple column={column} title="RFQ 마감일" />
      ),
      cell: ({ cell }) => {
        const value = cell.getValue();
        return value ? formatDate(value as Date, "KR") : "";
      },
      meta: {
        excelHeader: "RFQ 마감일"
      },
      enableResizing: true,
      minSize: 80,
      size: 120,
    },
    {
      accessorKey: "createdByName",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="요청자" />
      ),
      cell: ({ row }) => <div>{row.getValue("createdByName")}</div>,
      meta: {
        excelHeader: "요청자"
      },
      enableResizing: true,
      size: 120,
    },
    {
      accessorKey: "createdAt",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="등록일" />
      ),
      cell: ({ cell }) => {
        const value = cell.getValue();
        return value ? formatDateTime(value as Date) : "";
      },
      meta: {
        excelHeader: "등록일"
      },
      enableResizing: true,
      size: 160,
    },
    {
      accessorKey: "updatedAt",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="수정일" />
      ),
      cell: ({ cell }) => {
        const value = cell.getValue();
        return value ? formatDateTime(value as Date) : "";
      },
      meta: {
        excelHeader: "수정일"
      },
      enableResizing: true,
      size: 160,
    },
    // {
    //   accessorKey: "remark",
    //   header: ({ column }) => (
    //     <DataTableColumnHeaderSimple column={column} title="비고" />
    //   ),
    //   cell: ({ row }) => {
    //     const id = row.original.id;
    //     const value = row.getValue("remark") as string;
        
    //     const isEditing = 
    //       editingCell?.rowId === row.id &&
    //       editingCell.value !== undefined;
        
    //     const startEditing = () => {
    //       setEditingCell({
    //         rowId: row.id,
    //         value: value || ""
    //       });
    //     };
        
    //     const cancelEditing = () => {
    //       setEditingCell(null);
    //     };
        
    //     const saveChanges = async () => {
    //       if (!editingCell) return;
          
    //       try {
    //         await updateRemark(id, editingCell.value);
    //         setEditingCell(null);
    //       } catch (error) {
    //         toast.error("비고 업데이트 중 오류가 발생했습니다.");
    //         console.error("Error updating remark:", error);
    //       }
    //     };
        
    //     const handleKeyDown = (e: React.KeyboardEvent) => {
    //       if (e.key === "Enter") {
    //         saveChanges();
    //       } else if (e.key === "Escape") {
    //         cancelEditing();
    //       }
    //     };
        
    //     if (isEditing) {
    //       return (
    //         <div className="flex items-center gap-1">
    //           <Input
    //             value={editingCell?.value || ""}
    //             onChange={(e) => setEditingCell({
    //               rowId: row.id, 
    //               value: e.target.value
    //             })}
    //             onKeyDown={handleKeyDown}
    //             autoFocus
    //             className="h-8 w-full"
    //           />
    //             <Button
    //               variant="ghost"
    //               size="icon"
    //               onClick={saveChanges}
    //             className="h-8 w-8"
    //             >
    //             <Check className="h-4 w-4" />
    //             </Button>
    //             <Button
    //               variant="ghost"
    //               size="icon"
    //               onClick={cancelEditing}
    //             className="h-8 w-8"
    //             >
    //             <X className="h-4 w-4" />
    //             </Button>
    //         </div>
    //       );
    //     }
        
    //     return (
    //       <div className="flex items-center gap-1 group">
    //         <span className="truncate">{value || ""}</span>
    //         <Button
    //           variant="ghost"
    //           size="icon"
    //           onClick={startEditing}
    //           className="h-6 w-6 opacity-0 group-hover:opacity-100 transition-opacity"
    //         >
    //           <Pencil className="h-3 w-3" />
    //         </Button>
    //       </div>
    //     );
    //   },
    //   meta: {
    //     excelHeader: "비고"
    //   },
    //   enableResizing: true,
    //   size: 200,
    // },
    {
      id: "actions",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="액션" />
      ),
      cell: ({ row }) => {
        return (
          <Button
            variant="ghost"
            size="sm"
            onClick={() => setRowAction({ row, type: "project-detail" })}
            className="h-8 px-2 gap-1"
          >
            <Info className="h-4 w-4" />
            <span className="hidden sm:inline">프로젝트 상세</span>
          </Button>
        );
      },
      enableSorting: false,
      enableHiding: false,
      enableResizing: false,
      size: 120,
      minSize: 120,
      maxSize: 120,
    },
  ]
}