"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 ( 결재선 삭제 {isSingleLine ? `"${line?.name}" 결재선을 삭제하시겠습니까?` : `${lines.length}개의 결재선을 삭제하시겠습니까?`}
{/* 사용량 정보 표시 */} {isSingleLine && usageInfo && (
사용 중인 결재선

이 결재선은 {usageInfo.templateCount}개의 템플릿에서 사용되고 있습니다.

)} {/* 삭제할 결재선 목록 */}

삭제할 결재선:

{lines.map((line) => (
{line.name}
{line.description && (
{line.description}
)}
결재자 {(line.aplns as any[])?.length || 0}명
))}
{/* 경고 메시지 */}
주의

삭제된 결재선은 복구할 수 없습니다. 이 작업은 되돌릴 수 없습니다.

) }