summaryrefslogtreecommitdiff
path: root/lib/general-contract-template/template/general-contract-template-toolbar-actions.tsx
blob: 38a83a7fa3761c98cc29b12f95b12df4d4660bcc (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
"use client"

import * as React from "react"
import { Trash2 } from "lucide-react"
import { toast } from "sonner"

import { Button } from "@/components/ui/button"
import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from "@/components/ui/dialog"
import { disposeTemplates } from "../actions"
import { AddGeneralContractTemplateDialog } from "./add-general-contract-template-dialog"

interface GeneralContractTemplateToolbarActionsProps {
  selectedRows?: any[]
  onSelectionChange?: (selectedRows: any[]) => void
}

export function GeneralContractTemplateToolbarActions({
  selectedRows = [],
  onSelectionChange
}: GeneralContractTemplateToolbarActionsProps) {
  const [isDisposeOpen, setIsDisposeOpen] = React.useState(false)

  // 폐기 버튼 클릭
  const handleDispose = () => {
    if (selectedRows.length === 0) {
      toast.error("폐기할 문서를 선택해주세요.")
      return
    }

    setIsDisposeOpen(true)
  }

  // 실제 폐기 처리
  const handleConfirmDispose = async () => {
    try {
      // 폐기할 템플릿 ID들 추출
      const templateIds = selectedRows.map(row => row.id)

      // Server Action 호출
      const result = await disposeTemplates(templateIds)

      toast.success(result.message)
      setIsDisposeOpen(false)

      // 페이지 새로고침 또는 테이블 업데이트
      window.location.reload()
    } catch (error) {
      console.error('폐기 처리 오류:', error)
      toast.error(error instanceof Error ? error.message : "폐기 처리 중 오류가 발생했습니다.")
    }
  }

  return (
    <>
      <div className="flex items-center gap-2">
        {/* 신규등록: 다이얼로그 사용 */}
        <AddGeneralContractTemplateDialog />

        {/* 폐기 버튼 */}
        <Button
          onClick={handleDispose}
          variant="outline"
          size="sm"
          className="border-red-600 text-red-600 hover:bg-red-50"
        >
          <Trash2 className="mr-2 h-4 w-4" />
          폐기
        </Button>
      </div>

      {/* 폐기 확인 다이얼로그 */}
      <Dialog open={isDisposeOpen} onOpenChange={setIsDisposeOpen}>
        <DialogContent>
          <DialogHeader>
            <DialogTitle>문서 폐기 확인</DialogTitle>
            <DialogDescription>
              선택된 {selectedRows.length}개의 문서를 폐기하시겠습니까?
              <br />
              폐기된 문서는 복구할 수 없습니다.
            </DialogDescription>
          </DialogHeader>
          <div className="flex justify-end gap-2">
            <Button
              variant="outline"
              onClick={() => setIsDisposeOpen(false)}
            >
              취소
            </Button>
            <Button
              variant="destructive"
              onClick={handleConfirmDispose}
            >
              폐기
            </Button>
          </div>
        </DialogContent>
      </Dialog>
    </>
  )
}