summaryrefslogtreecommitdiff
path: root/lib/approval-line/table/delete-approval-line-dialog.tsx
blob: aa1d894977ece23dcff3ffc7eb7d472dc50baa24 (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
"use client"

import * as React from "react"
import { Button } from "@/components/ui/button"
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
} from "@/components/ui/dialog"
import { Badge } from "@/components/ui/badge"
import { toast } from "sonner"
import { Loader2, Trash2, AlertTriangle } from "lucide-react"
import { deleteApprovalLine, getApprovalLineUsage } from "../service"
import { type ApprovalLine } from "../service"

interface DeleteApprovalLineDialogProps {
  open: boolean
  onOpenChange: (open: boolean) => void
  lines: ApprovalLine[]
  showTrigger?: boolean
  onSuccess?: () => void
}

export function DeleteApprovalLineDialog({
  open,
  onOpenChange,
  lines,
  showTrigger = true,
  onSuccess,
}: DeleteApprovalLineDialogProps) {
  const [isDeleting, setIsDeleting] = React.useState(false)
  const [usageInfo, setUsageInfo] = React.useState<{ templateCount: number } | null>(null)

  // 사용량 정보 조회
  React.useEffect(() => {
    if (open && lines.length === 1) {
      getApprovalLineUsage(lines[0].id).then(setUsageInfo)
    }
  }, [open, lines])

  const handleDelete = async () => {
    setIsDeleting(true)

    try {
      const deletePromises = lines.map((line) => deleteApprovalLine(line.id))
      const results = await Promise.all(deletePromises)

      const successCount = results.filter((r) => r.success).length
      const errorCount = results.length - successCount

      if (successCount > 0) {
        toast.success(`${successCount}개의 결재선이 삭제되었습니다.`)
        onSuccess?.()
        onOpenChange(false)
      }

      if (errorCount > 0) {
        toast.error(`${errorCount}개의 결재선 삭제에 실패했습니다.`)
      }
    } catch (error) {
      toast.error("삭제 중 오류가 발생했습니다.")
    } finally {
      setIsDeleting(false)
    }
  }

  const isSingleLine = lines.length === 1
  const line = isSingleLine ? lines[0] : null

  return (
    <Dialog open={open} onOpenChange={onOpenChange}>
      <DialogContent>
        <DialogHeader>
          <DialogTitle className="flex items-center gap-2">
            <Trash2 className="h-5 w-5 text-destructive" />
            결재선 삭제
          </DialogTitle>
          <DialogDescription>
            {isSingleLine
              ? `"${line?.name}" 결재선을 삭제하시겠습니까?`
              : `${lines.length}개의 결재선을 삭제하시겠습니까?`}
          </DialogDescription>
        </DialogHeader>

        <div className="space-y-4">
          {/* 사용량 정보 표시 */}
          {isSingleLine && usageInfo && (
            <div className="p-4 bg-yellow-50 border border-yellow-200 rounded-lg">
              <div className="flex items-center gap-2 text-yellow-700">
                <AlertTriangle className="w-4 h-4" />
                <span className="font-medium">사용 중인 결재선</span>
              </div>
              <p className="text-sm text-yellow-600 mt-1">
                이 결재선은 {usageInfo.templateCount}개의 템플릿에서 사용되고 있습니다.
              </p>
            </div>
          )}

          {/* 삭제할 결재선 목록 */}
          <div className="space-y-2">
            <h4 className="text-sm font-medium">삭제할 결재선:</h4>
            <div className="space-y-2 max-h-40 overflow-y-auto">
              {lines.map((line) => (
                <div
                  key={line.id}
                  className="flex items-center justify-between p-3 border rounded-lg"
                >
                  <div className="flex-1">
                    <div className="font-medium">{line.name}</div>
                    {line.description && (
                      <div className="text-sm text-gray-500">
                        {line.description}
                      </div>
                    )}
                    <div className="text-xs text-gray-400 mt-1">
                      결재자 {(line.aplns as any[])?.length || 0}명
                    </div>
                  </div>
                </div>
              ))}
            </div>
          </div>

          {/* 경고 메시지 */}
          <div className="p-4 bg-red-50 border border-red-200 rounded-lg">
            <div className="flex items-center gap-2 text-red-700">
              <AlertTriangle className="w-4 h-4" />
              <span className="font-medium">주의</span>
            </div>
            <p className="text-sm text-red-600 mt-1">
              삭제된 결재선은 복구할 수 없습니다. 이 작업은 되돌릴 수 없습니다.
            </p>
          </div>
        </div>

        <DialogFooter>
          <Button
            variant="outline"
            onClick={() => onOpenChange(false)}
            disabled={isDeleting}
          >
            취소
          </Button>
          <Button
            variant="destructive"
            onClick={handleDelete}
            disabled={isDeleting}
          >
            {isDeleting ? (
              <>
                <Loader2 className="w-4 h-4 mr-2 animate-spin" />
                삭제 중...
              </>
            ) : (
              <>
                <Trash2 className="w-4 h-4 mr-2" />
                삭제
              </>
            )}
          </Button>
        </DialogFooter>
      </DialogContent>
    </Dialog>
  )
}