summaryrefslogtreecommitdiff
path: root/lib/esg-check-list/table/esg-evaluation-delete-dialog.tsx
blob: ac66748321383a2584ba7d468dbf8086236a8715 (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 { EsgEvaluationsView } from "@/db/schema"
import React from "react"
import { toast } from "sonner"
import { useTransition } from "react"
import { Button } from "@/components/ui/button"
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
} from "@/components/ui/dialog"
import { ScrollArea } from "@/components/ui/scroll-area"
import { Badge } from "@/components/ui/badge"

// 서비스 함수 import
import { deleteEsgEvaluationsBatch, softDeleteEsgEvaluationsBatch } from "../service"

interface EsgEvaluationBatchDeleteDialogProps {
  open: boolean
  onOpenChange: (open: boolean) => void
  evaluations: EsgEvaluationsView[]
  onSuccess: () => void
  useSoftDelete?: boolean // 소프트 삭제 사용 여부
}

export function EsgEvaluationBatchDeleteDialog({
  open,
  onOpenChange,
  evaluations,
  onSuccess,
  useSoftDelete = false,
}: EsgEvaluationBatchDeleteDialogProps) {
  const [isPending, startTransition] = useTransition()

  const evaluationCount = evaluations.length
  const isSingle = evaluationCount === 1

  const handleDelete = async () => {
    if (evaluationCount === 0) return

    startTransition(async () => {
      try {
        const ids = evaluations.map((evaluation) => evaluation.id)
        
        let result
        if (useSoftDelete) {
          result = await softDeleteEsgEvaluationsBatch(ids)
        } else {
          result = await deleteEsgEvaluationsBatch(ids)
        }

        // 성공 메시지
        if (result.failed > 0) {
          toast.warning(
            `${result.deleted}개 삭제 완료, ${result.failed}개 실패했습니다.`
          )
        } else {
          toast.success(
            isSingle 
              ? '평가표가 삭제되었습니다.'
              : `${result.deleted}개의 평가표가 삭제되었습니다.`
          )
        }

        onSuccess()
        onOpenChange(false)
      } catch (error) {
        console.error('Error deleting evaluations:', error)
        toast.error(
          error instanceof Error ? error.message : '삭제 중 오류가 발생했습니다.'
        )
      }
    })
  }

  if (evaluationCount === 0) return null

  return (
    <Dialog open={open} onOpenChange={onOpenChange}>
      <DialogContent className="max-w-2xl">
        <DialogHeader>
          <DialogTitle>
            {isSingle ? '평가표 삭제' : `평가표 일괄 삭제 (${evaluationCount}개)`}
          </DialogTitle>
          <DialogDescription>
            {isSingle ? (
              <>
                정말로 이 ESG 평가표를 삭제하시겠습니까?
                <br />
                이 작업은 되돌릴 수 없으며, 연관된 모든 평가항목과 답변옵션들도 함께 삭제됩니다.
              </>
            ) : (
              <>
                선택된 {evaluationCount}개의 ESG 평가표를 삭제하시겠습니까?
                <br />
                이 작업은 되돌릴 수 없으며, 연관된 모든 평가항목과 답변옵션들도 함께 삭제됩니다.
              </>
            )}
          </DialogDescription>
        </DialogHeader>

        {/* 삭제될 평가표 목록 */}
        <div className="py-4">
          <h4 className="text-sm font-medium mb-3">
            {isSingle ? '삭제될 평가표:' : '삭제될 평가표 목록:'}
          </h4>
          
          <ScrollArea className={evaluationCount > 5 ? "h-[200px]" : "h-auto"}>
            <div className="space-y-3">
              {evaluations.map((evaluation, index) => (
                <div
                  key={evaluation.id}
                  className="p-3 border rounded-lg bg-muted/50"
                >
                  <div className="flex items-start justify-between gap-3">
                    <div className="flex-1 space-y-1">
                      <div className="flex items-center gap-2">
                        <Badge variant="outline" className="text-xs">
                          {evaluation.serialNumber}
                        </Badge>
                        <span className="text-sm font-medium">
                          {evaluation.category}
                        </span>
                      </div>
                      <p className="text-xs text-muted-foreground line-clamp-2">
                        {evaluation.inspectionItem}
                      </p>
                      <div className="flex gap-4 text-xs text-muted-foreground">
                        <span>평가항목: {evaluation.totalEvaluationItems}개</span>
                        <span>답변옵션: {evaluation.totalAnswerOptions}개</span>
                      </div>
                    </div>
                  </div>
                </div>
              ))}
            </div>
          </ScrollArea>
        </div>

        <DialogFooter>
          <Button
            variant="outline"
            onClick={() => onOpenChange(false)}
            disabled={isPending}
          >
            취소
          </Button>
          <Button
            variant="destructive"
            onClick={handleDelete}
            disabled={isPending}
          >
            {isPending 
              ? '삭제 중...' 
              : isSingle 
                ? '삭제' 
                : `${evaluationCount}개 삭제`
            }
          </Button>
        </DialogFooter>
      </DialogContent>
    </Dialog>
  )
}