summaryrefslogtreecommitdiff
path: root/lib/avl/table/avl-table-columns.tsx
blob: d95a29b07b4e69343e54b5bc7c18dfbb7361f45b (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
import { Checkbox } from "@/components/ui/checkbox"
import { Button } from "@/components/ui/button"
import { Badge } from "@/components/ui/badge"
import { Eye, Edit, Trash2, History } from "lucide-react"
import { type ColumnDef } from "@tanstack/react-table"
import { DataTableColumnHeaderSimple } from "@/components/data-table/data-table-column-simple-header"
import { AvlListItem } from "../types"

interface GetColumnsProps {
  selectedRows?: number[]
  onRowSelect?: (id: number, selected: boolean) => void
}

// 테이블 메타 타입 확장
declare module "@tanstack/react-table" {
  interface TableMeta<TData> {
    onCellUpdate?: (id: string, field: keyof TData, newValue: string | boolean) => Promise<void>
    onCellCancel?: (id: string, field: keyof TData) => void
    onAction?: (action: string, data?: Partial<AvlListItem>) => void
    onSaveEmptyRow?: (tempId: string) => Promise<void>
    onCancelEmptyRow?: (tempId: string) => void
    isEmptyRow?: (id: string) => boolean
  }
}

// 테이블 컬럼 정의 함수
export function getColumns({ selectedRows = [], onRowSelect }: GetColumnsProps): ColumnDef<AvlListItem>[] {
  const columns: ColumnDef<AvlListItem>[] = [
    // 기본 정보 그룹
    {
      header: "AVL 정보",
      columns: [
      {
        id: "select",
        header: () => <div className="text-center">선택</div>,
        cell: ({ row }) => (
          <div className="flex justify-center">
            <Checkbox
              checked={selectedRows.includes(row.original.id)}
              onCheckedChange={(checked) => {
                onRowSelect?.(row.original.id, !!checked)
              }}
              aria-label="행 선택"
              className="translate-y-[2px]"
            />
          </div>
        ),
        enableSorting: false,
        enableHiding: false,
        size: 40,
      },
      {
        accessorKey: "no",
        header: ({ column }) => (
          <DataTableColumnHeaderSimple column={column} title="No" />
        ),
        cell: ({ getValue }) => <div className="text-center">{getValue() as number}</div>,
        size: 60,
      },
      {
        accessorKey: "isTemplate",
        header: ({ column }) => (
          <DataTableColumnHeaderSimple column={column} title="AVL 분류" />
        ),
        cell: ({ getValue }) => {
          const value = getValue() as boolean
          return (
            <div className="text-center">
              {value ? "표준 AVL" : "프로젝트 AVL"}
            </div>
          )
        },
        size: 120,
      },
      {
        accessorKey: "constructionSector",
        header: ({ column }) => (
          <DataTableColumnHeaderSimple column={column} title="공사부문" />
        ),
        cell: ({ getValue }) => {
          const value = getValue() as string
          return (
            <div className="text-center">
              {value}
            </div>
          )
        },
        size: 100,
      },
      {
        accessorKey: "projectCode",
        header: ({ column }) => (
          <DataTableColumnHeaderSimple column={column} title="프로젝트 코드" />
        ),
        cell: ({ getValue }) => {
          const value = getValue() as string
          return (
            <div className="text-center">
              {value}
            </div>
          )
        },
        size: 140,
      },
      {
        accessorKey: "shipType",
        header: ({ column }) => (
          <DataTableColumnHeaderSimple column={column} title="선종" />
        ),
        cell: ({ getValue }) => {
          const value = getValue() as string
          return (
            <div className="text-center">
              {value}
            </div>
          )
        },
        size: 100,
      },
      {
        accessorKey: "avlKind",
        header: ({ column }) => (
          <DataTableColumnHeaderSimple column={column} title="AVL 종류" />
        ),
        cell: ({ getValue }) => {
          const value = getValue() as string
          return (
            <div className="text-center">
              {value}
            </div>
          )
        },
        size: 120,
      },
      {
        accessorKey: "htDivision",
        header: ({ column }) => (
          <DataTableColumnHeaderSimple column={column} title="H/T 구분" />
        ),
        cell: ({ getValue }) => {
          const value = getValue() as string
          return (
            <div className="text-center">
              {value}
            </div>
          )
        },
        size: 80,
      },
      {
        accessorKey: "rev",
        header: ({ column }) => (
          <DataTableColumnHeaderSimple column={column} title="Rev" />
        ),
        cell: ({ getValue, row, table }) => {
          const value = getValue() as number
          return (
            <div className="flex items-center gap-1">
              <Badge variant="outline" className="font-mono">
                {value || 1}
              </Badge>
              <Button
                variant="ghost"
                size="sm"
                className="h-6 w-6 p-0"
                onClick={() => table.options.meta?.onAction?.('view-history', row.original)}
                title="리비전 히스토리 보기"
              >
                <History className="h-3 w-3" />
              </Button>
            </div>
          )
        },
        size: 100,
      },
    ],
  },

  // 집계 그룹
  {
    header: "등록정보",
    columns: [
      {
        accessorKey: "PKG",
        header: ({ column }) => (
          <DataTableColumnHeaderSimple column={column} title="PKG" />
        ),
      },
      {
        accessorKey: "materialGroup",
        header: ({ column }) => (
          <DataTableColumnHeaderSimple column={column} title="자재그룹" />
        ),
      },
      {
        accessorKey: "vendor",
        header: ({ column }) => (
          <DataTableColumnHeaderSimple column={column} title="협력업체" />
        ),
      },
      {
        accessorKey: "Tier",
        header: ({ column }) => (
          <DataTableColumnHeaderSimple column={column} title="Tier" />
        ),
      },
      {
        accessorKey: "ownerSuggestion",
        header: ({ column }) => (
          <DataTableColumnHeaderSimple column={column} title="선주 제안" />
        ),
      },
      {
        accessorKey: "shiSuggestion",
        header: ({ column }) => (
          <DataTableColumnHeaderSimple column={column} title="SHI 제안" />
        ),
      },
    ],
  },

  // 등록 정보 그룹
  {
    header: "작성정보",
    columns: [
      {
        accessorKey: "createdAt",
        header: ({ column }) => (
          <DataTableColumnHeaderSimple column={column} title="등록일" />
        ),
        cell: ({ getValue }) => {
          const date = getValue() as string
          return <div className="text-center text-sm">{date}</div>
        },
        size: 100,
      },
      {
        accessorKey: "createdBy",
        header: ({ column }) => (
          <DataTableColumnHeaderSimple column={column} title="등록자" />
        ),
        cell: ({ getValue }) => {
          const date = getValue() as string
          return <div className="text-center text-sm">{date}</div>
        },
        size: 100,
      },
      {
        accessorKey: "updatedAt",
        header: ({ column }) => (
          <DataTableColumnHeaderSimple column={column} title="최종변경일" />
        ),
        cell: ({ getValue }) => {
          const date = getValue() as string
          return <div className="text-center text-sm">{date}</div>
        },
        size: 100,
      },
      {
        accessorKey: "updatedBy",
        header: ({ column }) => (
          <DataTableColumnHeaderSimple column={column} title="최종변경자" />
        ),
        cell: ({ getValue }) => {
          const date = getValue() as string
          return <div className="text-center text-sm">{date}</div>
        },
        size: 100,
      },
    ],
  },

  // 액션 그룹
  {
    id: "actions",
    header: "액션",
    columns: [
      {
        id: "actions",
        header: () => <div className="text-center">액션</div>,
        cell: ({ row, table }) => {
          const isEmptyRow = table.options.meta?.isEmptyRow?.(row.id) || false

          if (isEmptyRow) {
            return (
              <div className="flex items-center justify-center gap-1">
                <Button
                  variant="ghost"
                  size="sm"
                  onClick={() => table.options.meta?.onSaveEmptyRow?.(row.id)}
                  className="h-8 w-8 p-0"
                >
                  저장
                </Button>
                <Button
                  variant="ghost"
                  size="sm"
                  onClick={() => table.options.meta?.onCancelEmptyRow?.(row.id)}
                  className="h-8 w-8 p-0 text-destructive hover:text-destructive"
                >
                  취소
                </Button>
              </div>
            )
          }

          return (
            <div className="flex items-center justify-center gap-1">
              <Button
                variant="ghost"
                size="sm"
                onClick={() => table.options.meta?.onAction?.("view-detail", { id: row.original.id })}
                className="h-8 w-8 p-0"
                title="상세보기"
              >
                <Eye className="h-4 w-4" />
              </Button>
            </div>
          )
        },
        enableSorting: false,
        enableHiding: false,
        size: 120,
      },
      ],
    },
  ]

  return columns
}