summaryrefslogtreecommitdiff
path: root/lib/pcr/table/pcr-table-column.tsx
blob: 98cf9502f557af090b717e05698836064a415628 (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
410
411
412
413
414
415
416
417
418
419
420
import { ColumnDef, Column, Row } from "@tanstack/react-table"
import { Badge } from "@/components/ui/badge"
import { Button } from "@/components/ui/button"
import { Checkbox } from "@/components/ui/checkbox"
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuLabel,
  DropdownMenuSeparator,
  DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu"
import { DataTableColumnHeaderSimple } from "@/components/data-table/data-table-column-simple-header"
import { PcrPoData, PCR_APPROVAL_STATUS_CONFIG, PCR_CHANGE_TYPE_CONFIG } from "@/lib/pcr/types"
import { MoreHorizontal, Edit } from "lucide-react"
import type { DataTableRowAction } from "@/types/table"
import { formatNumber } from "@/lib/utils"

interface GetColumnsProps {
  setRowAction: React.Dispatch<React.SetStateAction<DataTableRowAction<PcrPoData> | null>>
  isEvcpPage?: boolean; // EvcP 페이지인지 여부
}

export function getColumns({ setRowAction, isEvcpPage = false }: GetColumnsProps): ColumnDef<PcrPoData>[] {
  const columns: ColumnDef<PcrPoData>[] = [
    {
      id: "select",
      header: ({ table }) => (
        <Checkbox
          checked={table.getSelectedRowModel().rows.length > 0}
          onCheckedChange={(value) => {
            if (value) {
              // 하나만 선택 가능하도록 현재 행만 선택
              table.getRowModel().rows.forEach((row) => {
                if (row.getIsSelected()) {
                  row.toggleSelected(false);
                }
              });
            } else {
              // 모든 선택 해제
              table.toggleAllPageRowsSelected(false);
            }
          }}
          aria-label="행 선택"
          className="translate-y-[2px]"
        />
      ),
      cell: ({ row, table }) => (
        <Checkbox
          checked={row.getIsSelected()}
          onCheckedChange={(value) => {
            if (value) {
              // 다른 모든 행 선택 해제 후 현재 행만 선택
              table.getRowModel().rows.forEach((r) => {
                if (r.id !== row.id) {
                  r.toggleSelected(false);
                }
              });
            }
            row.toggleSelected(!!value);
          }}
          aria-label="행 선택"
          className="translate-y-[2px]"
        />
      ),
      enableSorting: false,
      enableHiding: false,
    },
    {
      accessorKey: "pcrApprovalStatus",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="PCR 승인상태" />
      ),
      cell: ({ row }) => {
        const status = row.getValue("pcrApprovalStatus") as string
        const config = PCR_APPROVAL_STATUS_CONFIG[status as keyof typeof PCR_APPROVAL_STATUS_CONFIG]

        if (!config) {
          return <Badge variant="outline">{status}</Badge>
        }

        return (
          <Badge
            variant={config.variant as "default" | "secondary" | "destructive" | "outline"}
            className={config.color}
          >
            {config.label}
          </Badge>
        )
      },
      filterFn: (row, id, value) => {
        return value.includes(row.getValue(id))
      },
    },
    {
      accessorKey: "changeType",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="변경구분" />
      ),
      cell: ({ row }) => {
        const changeType = row.getValue("changeType") as string
        const config = PCR_CHANGE_TYPE_CONFIG[changeType as keyof typeof PCR_CHANGE_TYPE_CONFIG]

        if (!config) {
          return <Badge variant="outline">{changeType}</Badge>
        }

        return (
          <Badge variant="outline">
            {config.label}
          </Badge>
        )
      },
      filterFn: (row, id, value) => {
        return value.includes(row.getValue(id))
      },
    },
    {
      accessorKey: "poContractNumber",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="PO/계약번호" />
      ),
      cell: ({ row }) => {
        const value = row.getValue("poContractNumber") as string
        return (
          <div className="font-medium">
            {value || "-"}
          </div>
        )
      },
    },
    {
      accessorKey: "project",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="프로젝트" />
      ),
      cell: ({ row }) => {
        const value = row.getValue("project") as string
        return (
          <div className="max-w-[200px]" title={value}>
            {value || "-"}
          </div>
        )
      },
    },
    {
      accessorKey: "pcrRequestDate",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="PCR 요청일자" />
      ),
      cell: ({ row }) => {
        const value = row.getValue("pcrRequestDate") as Date
        if (!value) return "-"

        return (
          <div className="text-sm">
            {value.toLocaleDateString('ko-KR', {
              year: 'numeric',
              month: '2-digit',
              day: '2-digit'
            })}
          </div>
        )
      },
    },
    {
      accessorKey: "revItemNumber",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="Rev./품번" />
      ),
      cell: ({ row }) => {
        const value = row.getValue("revItemNumber") as string
        return (
          <div className="max-w-[150px]" title={value}>
            {value || "-"}
          </div>
        )
      },
    },
    {
      accessorKey: "purchaseContractManager",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="구매/계약담당자" />
      ),
      cell: ({ row }) => {
        const value = row.getValue("purchaseContractManager") as string
        return (
          <div className="max-w-[120px]" title={value}>
            {value || "-"}
          </div>
        )
      },
    },
    {
      accessorKey: "pcrCreator",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="PCR 생성자" />
      ),
      cell: ({ row }) => {
        const value = row.getValue("pcrCreator") as string
        return (
          <div className="max-w-[120px]" title={value}>
            {value || "-"}
          </div>
        )
      },
    },
    {
      accessorKey: "poContractAmountBefore",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="PO/계약금액(전)" />
      ),
      cell: ({ row }) => {
        const value = row.getValue("poContractAmountBefore") as number

        return (
          <div className="text-right font-mono text-sm">
            {formatNumber(value)}
          </div>
        )
      },
    },
    {
      accessorKey: "poContractAmountAfter",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="PO/계약금액(후)" />
      ),
      cell: ({ row }) => {
        const value = row.getValue("poContractAmountAfter") as number

        return (
          <div className="text-right font-mono text-sm">
            {formatNumber(value)}
          </div>
        )
      },
    },
    {
      accessorKey: "contractCurrency",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="계약통화" />
      ),
      cell: ({ row }) => {
        const value = row.getValue("contractCurrency") as string
        return (
          <div className="text-center">
            {value || "-"}
          </div>
        )
      },
    },
    // EvcP 페이지에서만 협력업체 정보 표시
    ...(isEvcpPage ? [{
      accessorKey: "vendorName" as const,
      header: ({ column }: { column: Column<PcrPoData, unknown> }) => (
        <DataTableColumnHeaderSimple column={column} title="협력업체" />
      ),
      cell: ({ row }: { row: Row<PcrPoData> }) => {
        const vendorName = row.getValue("vendorName") as string
        const vendorCode = row.original.vendorCode as string
        const displayText = vendorName
          ? vendorCode
            ? `${vendorName} (${vendorCode})`
            : vendorName
          : "-"

        return (
          <div className="max-w-[150px]" title={displayText}>
            {displayText}
          </div>
        )
      },
    }] : []),
    {
      accessorKey: "details",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="상세" />
      ),
      cell: ({ row }) => {
        const value = row.getValue("details") as string
        
        if (!value) return <div>-</div>
        
        // 세미콜론으로 구분하고 빈 문자열 제거
        const items = value.split(";").filter(item => item.trim() !== "")
        
        if (items.length === 0) return <div>-</div>
        
        return (
          <div className="flex flex-wrap gap-1">
            {items.map((item, index) => (
              <Badge key={index} variant="secondary" className="text-xs">
                {item.trim()}
              </Badge>
            ))}
          </div>
        )
      },
    },
    {
      accessorKey: "pcrReason",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="PCR 사유" />
      ),
      cell: ({ row }) => {
        const value = row.getValue("pcrReason") as string
        return (
          <div className="max-w-[150px]" title={value}>
            {value || "-"}
          </div>
        )
      },
    },
    {
      accessorKey: "detailsReason",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="상세 사유" />
      ),
      cell: ({ row }) => {
        const value = row.getValue("detailsReason") as string
        return (
          <div className="max-w-[150px]" title={value}>
            {value || "-"}
          </div>
        )
      },
    },
    {
      accessorKey: "rejectionReason",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="거절 사유" />
      ),
      cell: ({ row }) => {
        const value = row.getValue("rejectionReason") as string
        return (
          <div className="max-w-[150px]" title={value}>
            {value || "-"}
          </div>
        )
      },
    },
    {
      accessorKey: "pcrResponseDate",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="PCR 회신일" />
      ),
      cell: ({ row }) => {
        const value = row.getValue("pcrResponseDate") as Date
        if (!value) return "-"

        return (
          <div className="text-sm">
            {value.toLocaleDateString('ko-KR', {
              year: 'numeric',
              month: '2-digit',
              day: '2-digit'
            })}
          </div>
        )
      },
    },
    {
      accessorKey: "createdAt",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="생성일" />
      ),
      cell: ({ row }) => {
        const value = row.getValue("createdAt") as Date
        if (!value) return "-"

        return (
          <div className="text-sm">
            {value.toLocaleDateString('ko-KR', {
              year: 'numeric',
              month: '2-digit',
              day: '2-digit'
            })}
          </div>
        )
      },
    },
    {
      id: "actions",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="작업" />
      ),
      cell: ({ row }) => {
        return (
          <DropdownMenu>
            <DropdownMenuTrigger asChild>
              <Button
                variant="ghost"
                className="flex size-8 p-0 data-[state=open]:bg-muted"
              >
                <MoreHorizontal className="size-4" />
                <span className="sr-only">메뉴 열기</span>
              </Button>
            </DropdownMenuTrigger>
            <DropdownMenuContent align="end" className="w-[160px]">
              <DropdownMenuLabel>작업</DropdownMenuLabel>
              <DropdownMenuSeparator />

              <DropdownMenuItem
                onClick={() => setRowAction({ row, type: "update" })}
              >
                <Edit className="mr-2 size-4" />
                수정
              </DropdownMenuItem>

            </DropdownMenuContent>
          </DropdownMenu>
        )
      },
      enableSorting: false,
      enableHiding: false,
    },
  ]

  return columns
}