summaryrefslogtreecommitdiff
path: root/lib/pcr/table/pcr-table-toolbar-actions.tsx
blob: 2102d1d3334a19db020932cf6247db83a406280e (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
"use client"

import * as React from "react"
import { Button } from "@/components/ui/button"
import { Loader2, RefreshCw, Download, CheckCircle, XCircle } from "lucide-react"
import type { Table } from "@tanstack/react-table"
import { CreatePcrDialog } from "./create-pcr-dialog"
import { ApproveRejectPcrDialog } from "./approve-reject-pcr-dialog"
import { PcrPoData } from "@/lib/pcr/types"

interface PcrTableToolbarActionsProps<TData> {
  selection: Table<TData>
  onRefresh: () => void
  isEvcpPage?: boolean
  isPartnersPage?: boolean
  currentVendorId?: number
}

export function PcrTableToolbarActions<TData>({
  selection,
  onRefresh,
  isEvcpPage = false,
  isPartnersPage = false,
  currentVendorId,
}: PcrTableToolbarActionsProps<TData>) {
  const [approveDialogOpen, setApproveDialogOpen] = React.useState(false)
  const [rejectDialogOpen, setRejectDialogOpen] = React.useState(false)
  const [selectedPcr, setSelectedPcr] = React.useState<PcrPoData | null>(null)

  // 선택된 행들 가져오기
  const selectedRows = selection.getSelectedRowModel().rows
  const selectedPcrData = selectedRows.length === 1 ? (selectedRows[0].original as PcrPoData) : null

  // 승인/거절 가능 여부 확인
  // const canApproveOrReject = selectedPcrData && selectedPcrData.pcrApprovalStatus === '승인대기'
  const canApproveOrReject = selectedPcrData // 임시로 주석 처리하여 테스트



  const handleExport = () => {
    // 추후 구현
    console.log("Export functionality to be implemented")
  }

  const handleApprove = () => {
    if (selectedPcrData) {
      setSelectedPcr(selectedPcrData)
      setApproveDialogOpen(true)
    }
  }

  const handleReject = () => {
    if (selectedPcrData) {
      setSelectedPcr(selectedPcrData)
      setRejectDialogOpen(true)
    }
  }

  const handleActionSuccess = () => {
    // 액션 성공 시 선택 해제
    selection.resetRowSelection()
    // revalidatePath로 인해 자동으로 페이지 새로고침됨
  }

  return (
    <div className="flex items-center gap-2">
      {isPartnersPage && (
      <>
      {/* 승인 버튼 */}
      <Button
        variant="default"
        size="sm"
        onClick={handleApprove}
        disabled={!canApproveOrReject}
        className="gap-2 bg-green-600 hover:bg-green-700"
      >
        <CheckCircle className="size-4" />
        승인
      </Button>

      {/* 거절 버튼 */}
      <Button
        variant="destructive"
        size="sm"
        onClick={handleReject}
        disabled={!canApproveOrReject}
        className="gap-2"
      >
        <XCircle className="size-4" />
        거절
      </Button>
      </>
      )}

      {/* PCR 생성 다이얼로그 - Partners 페이지에서는 표시하지 않음 */}
      {/* {!isPartnersPage && (
        <CreatePcrDialog
          isEvcpPage={isEvcpPage}
          currentVendorId={currentVendorId}
          onSuccess={onRefresh}
        />
      )} */}

      {/* 승인 다이얼로그 */}
      <ApproveRejectPcrDialog
        open={approveDialogOpen}
        onOpenChange={setApproveDialogOpen}
        pcrData={selectedPcr}
        actionType="approve"
        onSuccess={handleActionSuccess}
      />

      {/* 거절 다이얼로그 */}
      <ApproveRejectPcrDialog
        open={rejectDialogOpen}
        onOpenChange={setRejectDialogOpen}
        pcrData={selectedPcr}
        actionType="reject"
        onSuccess={handleActionSuccess}
      />
    </div>
  )
}