summaryrefslogtreecommitdiff
path: root/lib/vendor-investigation/table/investigation-table-toolbar-actions.tsx
blob: 371873e4b4b4d50a101ac1531b1b9da6379a34c1 (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
"use client"

import * as React from "react"
import { type Table } from "@tanstack/react-table"
import { Download, RotateCcw, UserCog } from "lucide-react"
import { toast } from "sonner"

import { exportTableToExcel } from "@/lib/export"
import { Button } from "@/components/ui/button"
import { VendorInvestigationsViewWithContacts } from "@/config/vendorInvestigationsColumnsConfig"
import { InvestigationCancelPlanButton } from "./investigation-cancel-plan-button"
import { ChangeQMManagerDialog } from "./change-qm-manager-dialog"


interface VendorsTableToolbarActionsProps {
  table: Table<VendorInvestigationsViewWithContacts>
}

export function VendorsTableToolbarActions({ table }: VendorsTableToolbarActionsProps) {
  // 선택된 행 분석
  const selectedRows = table.getFilteredSelectedRowModel().rows
  const selectedData = selectedRows.map(row => row.original)

  // QM 담당자 변경 가능 여부 체크 (선택된 항목이 1개이고 상태가 PLANNED)
  const canChangeQM = selectedData.length === 1 && selectedData[0].investigationStatus === "PLANNED"
  const selectedInvestigation = canChangeQM ? selectedData[0] : null

  // QM 담당자 변경 다이얼로그 상태
  const [showChangeQMDialog, setShowChangeQMDialog] = React.useState(false)

  const handleChangeQMClick = () => {
    if (canChangeQM) {
      setShowChangeQMDialog(true)
    }
  }

  const handleChangeQMSuccess = () => {
    // 테이블 선택 초기화
    table.toggleAllPageRowsSelected(false)
    setShowChangeQMDialog(false)
  }

  return (
    <div className="flex items-center gap-2">
      <InvestigationCancelPlanButton table={table} />

      {/* QM 담당자 변경 버튼 - 계획 상태이고 단일 선택일 때만 활성화 */}
      {canChangeQM && (
        <Button
          variant="outline"
          size="sm"
          onClick={handleChangeQMClick}
          className="gap-2"
        >
          <UserCog className="size-4" aria-hidden="true" />
          <span className="hidden sm:inline">QM 담당자 변경</span>
        </Button>
      )}

      {/** 4) Export 버튼 */}
      <Button
        variant="outline"
        size="sm"
        onClick={() =>
          exportTableToExcel(table, {
            filename: "vendors",
            excludeColumns: ["select", "actions"],
          })
        }
        className="gap-2"
      >
        <Download className="size-4" aria-hidden="true" />
        <span className="hidden sm:inline">Export</span>
      </Button>

      {/* QM 담당자 변경 다이얼로그 */}
      {selectedInvestigation && (
        <ChangeQMManagerDialog
          isOpen={showChangeQMDialog}
          onClose={() => setShowChangeQMDialog(false)}
          investigationId={selectedInvestigation.investigationId}
          currentQMManagerId={selectedInvestigation.qmManagerId}
          currentQMManagerName={selectedInvestigation.qmManagerName}
          onSuccess={handleChangeQMSuccess}
        />
      )}
    </div>
  )
}