summaryrefslogtreecommitdiff
path: root/lib/esg-check-list/table/esg-evaluations-table-columns.tsx
blob: af0b87af27fc6dec95282bcba662823ddae4ca18 (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
"use client"

import * as React from "react"
import { type DataTableRowAction } from "@/types/table"
import { type ColumnDef } from "@tanstack/react-table"
import { Ellipsis, InfoIcon, PenToolIcon, TrashIcon } from "lucide-react"

import { formatDate } from "@/lib/utils"
import { Button } from "@/components/ui/button"
import { Checkbox } from "@/components/ui/checkbox"
import {
  Tooltip,
  TooltipContent,
  TooltipProvider,
  TooltipTrigger,
} from "@/components/ui/tooltip"
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuSeparator,
  DropdownMenuShortcut,
  DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu"
import { Badge } from "@/components/ui/badge"

import { DataTableColumnHeaderSimple } from "@/components/data-table/data-table-column-simple-header"
import { EsgEvaluationsView } from "@/db/schema"

interface GetColumnsProps {
  setRowAction: React.Dispatch<React.SetStateAction<DataTableRowAction<EsgEvaluationsView> | null>>
}

/**
 * ESG 평가표 테이블 컬럼 정의
 */
export function getColumns({ setRowAction }: GetColumnsProps): ColumnDef<EsgEvaluationsView>[] {
  
  // ----------------------------------------------------------------
  // 1) select 컬럼 (체크박스)
  // ----------------------------------------------------------------
  const selectColumn: ColumnDef<EsgEvaluationsView> = {
    id: "select",
    header: ({ table }) => (
      <Checkbox
        checked={
          table.getIsAllPageRowsSelected() ||
          (table.getIsSomePageRowsSelected() && "indeterminate")
        }
        onCheckedChange={(value) => table.toggleAllPageRowsSelected(!!value)}
        aria-label="Select all"
        className="translate-y-0.5"
      />
    ),
    cell: ({ row }) => (
      <Checkbox
        checked={row.getIsSelected()}
        onCheckedChange={(value) => row.toggleSelected(!!value)}
        aria-label="Select row"
        className="translate-y-0.5"
      />
    ),
    enableSorting: false,
    enableHiding: false,
    size: 40,
  }

  // ----------------------------------------------------------------
  // 2) 기본 정보 컬럼들
  // ----------------------------------------------------------------
  const basicColumns: ColumnDef<EsgEvaluationsView>[] = [
    {
      accessorKey: "serialNumber",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="시리얼번호" />
      ),
      cell: ({ row }) => (
        <div className="font-medium">
          {row.getValue("serialNumber")}
        </div>
      ),
      enableSorting: true,
      enableHiding: true,
    },
    
    {
      accessorKey: "category",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="분류" />
      ),
      cell: ({ row }) => (
        <Badge variant="secondary">
          {row.getValue("category")}
        </Badge>
      ),
    },
    {
      accessorKey: "inspectionItem",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="점검항목" />
      ),
      cell: ({ row }) => (
        <div className="max-w-[300px] truncate" title={row.getValue("inspectionItem")}>
          {row.getValue("inspectionItem")}
        </div>
      ),
    },
  ]

  // ----------------------------------------------------------------
  // 3) 통계 정보 컬럼들
  // ----------------------------------------------------------------
  const statsColumns: ColumnDef<EsgEvaluationsView>[] = [
    {
      id: "evaluationItems",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="평가항목" />
      ),
      cell: ({ row }) => {
        const evaluation = row.original;
        const count = evaluation.totalEvaluationItems || 0;
        
        // evaluationItemsList가 있다면 사용, 없다면 개수만 표시
        const items = (evaluation as any).evaluationItemsList || [];
        
        if (items.length > 0) {
          return (
            <div className="max-w-[250px]">
              <TooltipProvider>
                <Tooltip>
                  <TooltipTrigger asChild>
                    <div className="cursor-help">
                      <div className="flex flex-wrap gap-1">
                        {items.slice(0, 3).map((item: string, index: number) => (
                          <Badge key={index} variant="outline" className="text-xs">
                            {item.length > 15 ? `${item.substring(0, 15)}...` : item}
                          </Badge>
                        ))}
                        {items.length > 3 && (
                          <Badge variant="secondary" className="text-xs">
                            +{items.length - 3}개 더
                          </Badge>
                        )}
                      </div>
                    </div>
                  </TooltipTrigger>
                  <TooltipContent className="max-w-[300px]">
                    <div className="space-y-1">
                      <p className="font-medium">평가항목 목록:</p>
                      {items.map((item: string, index: number) => (
                        <p key={index} className="text-sm">
                          {index + 1}. {item}
                        </p>
                      ))}
                    </div>
                  </TooltipContent>
                </Tooltip>
              </TooltipProvider>
            </div>
          );
        }
        
        // 평가항목이 없는 경우
        return (
          <div className="text-center text-muted-foreground">
            <Badge variant="outline">
              {count > 0 ? `${count}개 항목` : "항목 없음"}
            </Badge>
          </div>
        );
      },
      enableSorting: false,
    },
    {
      accessorKey: "totalAnswerOptions",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="답변옵션" />
      ),
      cell: ({ row }) => (
        <div className="text-center">
          <Badge variant="outline">
            {row.getValue("totalAnswerOptions") || 0}개
          </Badge>
        </div>
      ),
    },
  ]

  // ----------------------------------------------------------------
  // 4) 메타데이터 컬럼들
  // ----------------------------------------------------------------
  const metaColumns: ColumnDef<EsgEvaluationsView>[] = [
    {
      accessorKey: "isActive",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="상태" />
      ),
      cell: ({ row }) => (
        <Badge variant={row.getValue("isActive") ? "default" : "secondary"}>
          {row.getValue("isActive") ? "활성" : "비활성"}
        </Badge>
      ),
    },
    {
      accessorKey: "createdAt",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="생성일" />
      ),
      cell: ({ row }) => {
        const date = row.getValue("createdAt") as Date
        return formatDate(date, "KR")
      },
    },
    {
      accessorKey: "updatedAt",
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="수정일" />
      ),
      cell: ({ row }) => {
        const date = row.getValue("updatedAt") as Date
        return formatDate(date, "KR")
      },
    },
  ]

  // ----------------------------------------------------------------
  // 5) actions 컬럼 (드롭다운 메뉴)
  // ----------------------------------------------------------------
  const actionsColumn: ColumnDef<EsgEvaluationsView> = {
    id: "actions",
    header: "작업",
    enableHiding: false,
    cell: function Cell({ row }) {
      return (
        <DropdownMenu>
          <DropdownMenuTrigger asChild>
            <Button
              aria-label="Open menu"
              variant="ghost"
              className="flex size-8 p-0 data-[state=open]:bg-muted"
            >
              <Ellipsis className="size-4" aria-hidden="true" />
            </Button>
          </DropdownMenuTrigger>
          <DropdownMenuContent align="end" className="w-40">
            <DropdownMenuItem
              onSelect={() => setRowAction({ row, type: "view" })}
            >
              {/* <Eye className="mr-2 size-4" /> */}
              상세보기
            </DropdownMenuItem>
            <DropdownMenuItem
              onSelect={() => setRowAction({ row, type: "update" })}
            >
              {/* <Edit className="mr-2 size-4" /> */}
              수정하기
            </DropdownMenuItem>
            <DropdownMenuSeparator />
            <DropdownMenuItem
              onSelect={() => setRowAction({ row, type: "delete" })}
            >
              {/* <Trash2 className="mr-2 size-4" /> */}
              삭제하기
              <DropdownMenuShortcut>⌘⌫</DropdownMenuShortcut>
            </DropdownMenuItem>
          </DropdownMenuContent>
        </DropdownMenu>
      )
    },
    size: 80,
  }

  // ----------------------------------------------------------------
  // 6) 최종 컬럼 배열 (그룹화 버전)
  // ----------------------------------------------------------------
  return [
    selectColumn,
    {
      id: "basicInfo",
      header: "기본 정보",
      columns: basicColumns,
    },
    {
      id: "statistics",
      header: "통계",
      columns: statsColumns,
    },
    {
      id: "metadata",
      header: "메타데이터",
      columns: metaColumns,
    },
    actionsColumn,
  ]
}

// ----------------------------------------------------------------
// 7) 컬럼 설정 (필터링용)
// ----------------------------------------------------------------
export const esgEvaluationsColumnsConfig = [
  {
    id: "serialNumber",
    label: "시리얼번호",
    group: "기본 정보",
    type: "text",
    excelHeader: "Serial Number",
  },
  {
    id: "category",
    label: "분류",
    group: "기본 정보",
    type: "text",
    excelHeader: "Category",
  },
  {
    id: "inspectionItem",
    label: "점검항목",
    group: "기본 정보",
    type: "text",
    excelHeader: "Inspection Item",
  },
  {
    id: "totalEvaluationItems",
    label: "평가항목 수",
    group: "통계",
    type: "number",
    excelHeader: "Total Evaluation Items",
  },
  {
    id: "totalAnswerOptions",
    label: "답변옵션 수",
    group: "통계",
    type: "number",
    excelHeader: "Total Answer Options",
  },
  // {
  //   id: "maxPossibleScore",
  //   label: "최대점수",
  //   group: "통계",
  //   type: "number",
  //   excelHeader: "Max Possible Score",
  // },
  {
    id: "isActive",
    label: "상태",
    group: "메타데이터",
    type: "boolean",
    excelHeader: "Is Active",
  },
  {
    id: "createdAt",
    label: "생성일",
    group: "메타데이터",
    type: "date",
    excelHeader: "Created At",
  },
  {
    id: "updatedAt",
    label: "수정일",
    group: "메타데이터",
    type: "date",
    excelHeader: "Updated At",
  },
] as const;