summaryrefslogtreecommitdiff
path: root/lib/bidding/selection/biddings-selection-table.tsx
blob: 912a7154b0b80d542af82ae9f1e9fa080483a2ea (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
"use client"

import * as React from "react"
import { useRouter } from "next/navigation"
import { useSession } from "next-auth/react"
import type {
    DataTableAdvancedFilterField,
    DataTableFilterField,
    DataTableRowAction,
} 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 { getBiddingsSelectionColumns } from "./biddings-selection-columns"
import { getBiddingsForSelection } from "@/lib/bidding/service"
import {
    biddingStatusLabels,
    contractTypeLabels,
} from "@/db/schema"
import { SpecificationMeetingDialog, PrDocumentsDialog } from "../list/bidding-detail-dialogs"

type BiddingSelectionItem = {
  id: number
  biddingNumber: string
  originalBiddingNumber: string | null
  title: string
  status: string
  contractType: string
  prNumber: string | null
  submissionStartDate: Date | null
  submissionEndDate: Date | null
  bidPicName: string | null
  supplyPicName: string | null
  createdBy: string | null
  createdAt: Date | null
  updatedAt: Date | null

  // 입찰 결과 정보 (개찰 이후에만 의미 있음)
  participantCount?: number
  submittedCount?: number
  avgBidPrice?: number | null
  minBidPrice?: number | null
  maxBidPrice?: number | null
  targetPrice?: number | null
  currency?: string | null
}

interface BiddingsSelectionTableProps {
    promises: Promise<
        [
            Awaited<ReturnType<typeof getBiddingsForSelection>>
        ]
    >
}

export function BiddingsSelectionTable({ promises }: BiddingsSelectionTableProps) {
    const [biddingsResult] = React.use(promises)

    // biddingsResult에서 data와 pageCount 추출
    const { data, pageCount } = biddingsResult

    const [isCompact, setIsCompact] = React.useState<boolean>(false)
    const [specMeetingDialogOpen, setSpecMeetingDialogOpen] = React.useState(false)
    const [prDocumentsDialogOpen, setPrDocumentsDialogOpen] = React.useState(false)
    const [selectedBidding, setSelectedBidding] = React.useState<BiddingSelectionItem | null>(null)

    const [rowAction, setRowAction] = React.useState<DataTableRowAction<BiddingSelectionItem> | null>(null)

    const router = useRouter()
    const { data: session } = useSession()

    const columns = React.useMemo(
        () => getBiddingsSelectionColumns({ setRowAction }),
        [setRowAction]
    )

    // rowAction 변경 감지하여 해당 다이얼로그 열기
    React.useEffect(() => {
        if (rowAction) {
            setSelectedBidding(rowAction.row.original)

            switch (rowAction.type) {
                case "view":
                    // 상세 페이지로 이동
                    router.push(`/evcp/bid/${rowAction.row.original.id}`)
                    break
                case "detail":
                    // 상세분석 페이지로 이동 (추후 구현)
                    router.push(`/evcp/bid-selection/${rowAction.row.original.id}/detail`)
                    break
                case "close_bidding":
                    // 입찰마감 (추후 구현)
                    console.log('입찰마감:', rowAction.row.original)
                    break
                case "evaluate_bidding":
                    // 평가하기 (추후 구현)
                    console.log('평가하기:', rowAction.row.original)
                    break
                default:
                    break
            }
        }
    }, [rowAction])

    const filterFields: DataTableFilterField<BiddingSelectionItem>[] = [
        {
            id: "biddingNumber",
            label: "입찰번호",
            type: "text",
            placeholder: "입찰번호를 입력하세요",
        },
        {
            id: "prNumber",
            label: "P/R번호",
            type: "text",
            placeholder: "P/R번호를 입력하세요",
        },
        {
            id: "title",
            label: "입찰명",
            type: "text",
            placeholder: "입찰명을 입력하세요",
        },
    ]

    const advancedFilterFields: DataTableAdvancedFilterField<BiddingSelectionItem>[] = [
        { id: "title", label: "입찰명", type: "text" },
        { id: "biddingNumber", label: "입찰번호", type: "text" },
        { id: "bidPicName", label: "입찰담당자", type: "text" },
        {
            id: "status",
            label: "진행상태",
            type: "multi-select",
            options: Object.entries(biddingStatusLabels).map(([value, label]) => ({
                label,
                value,
            })),
        },
        {
            id: "contractType",
            label: "계약구분",
            type: "select",
            options: Object.entries(contractTypeLabels).map(([value, label]) => ({
                label,
                value,
            })),
        },
        { id: "createdAt", label: "등록일", type: "date" },
        { id: "submissionStartDate", label: "제출시작일", type: "date" },
        { id: "submissionEndDate", label: "제출마감일", type: "date" },
    ]

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

    const handleCompactChange = React.useCallback((compact: boolean) => {
        setIsCompact(compact)
    }, [])

    const handleSpecMeetingDialogClose = React.useCallback(() => {
        setSpecMeetingDialogOpen(false)
        setRowAction(null)
        setSelectedBidding(null)
    }, [])

    const handlePrDocumentsDialogClose = React.useCallback(() => {
        setPrDocumentsDialogOpen(false)
        setRowAction(null)
        setSelectedBidding(null)
    }, [])

    return (
        <>
            <DataTable
                table={table}
                compact={isCompact}
            >
                <DataTableAdvancedToolbar
                    table={table}
                    filterFields={advancedFilterFields}
                    shallow={false}
                    enableCompactToggle={true}
                    compactStorageKey="biddingsSelectionTableCompact"
                    onCompactChange={handleCompactChange}
                >
                </DataTableAdvancedToolbar>
            </DataTable>

            {/* 사양설명회 다이얼로그 */}
            <SpecificationMeetingDialog
                open={specMeetingDialogOpen}
                onOpenChange={handleSpecMeetingDialogClose}
                bidding={selectedBidding}
            />

            {/* PR 문서 다이얼로그 */}
            <PrDocumentsDialog
                open={prDocumentsDialogOpen}
                onOpenChange={handlePrDocumentsDialogClose}
                bidding={selectedBidding}
            />
        </>
    )
}