summaryrefslogtreecommitdiff
path: root/lib/tech-vendor-possible-items/table/delete-possible-items-dialog.tsx
blob: 6b1c777584c8e6899241b96cfb19d3594215bcf6 (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
169
170
171
172
173
174
175
"use client";

import * as React from "react";
import { Trash2, AlertTriangle } from "lucide-react";

import { Button } from "@/components/ui/button";
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
  DialogTrigger,
} from "@/components/ui/dialog";
import { Badge } from "@/components/ui/badge";
import { ScrollArea } from "@/components/ui/scroll-area";
import { useToast } from "@/hooks/use-toast";
import { deleteTechVendorPossibleItems } from "@/lib/tech-vendor-possible-items/service";

interface TechVendorPossibleItemsData {
  id: number;
  vendorId: number;
  vendorCode: string | null;
  vendorName: string;
  techVendorType: string;
  itemCode: string;
  itemList: string | null;
  workType: string | null;
  shipTypes: string | null;
  subItemList: string | null;
  createdAt: Date;
  updatedAt: Date;
}

interface DeletePossibleItemsDialogProps {
  selectedItems: TechVendorPossibleItemsData[];
  children?: React.ReactNode;
  onSuccess?: () => void;
}

export function DeletePossibleItemsDialog({ 
  selectedItems,
  children, 
  onSuccess 
}: DeletePossibleItemsDialogProps) {
  const { toast } = useToast();
  const [open, setOpen] = React.useState(false);
  const [isLoading, setIsLoading] = React.useState(false);

  const handleDelete = async () => {
    if (selectedItems.length === 0) return;

    try {
      setIsLoading(true);
      const selectedIds = selectedItems.map(item => item.id);
      
      const result = await deleteTechVendorPossibleItems(selectedIds);
      
      if (result.success) {
        toast({
          title: "성공",
          description: `${selectedIds.length}개의 아이템이 삭제되었습니다.`,
        });
        
        setOpen(false);
        onSuccess?.();
      } else {
        toast({
          title: "오류",
          description: result.error || "삭제 중 오류가 발생했습니다.",
          variant: "destructive",
        });
      }
    } catch (error) {
      console.error("Delete error:", error);
      toast({
        title: "오류",
        description: "삭제 중 오류가 발생했습니다.",
        variant: "destructive",
      });
    } finally {
      setIsLoading(false);
    }
  };

  const parseVendorTypes = (vendorType: string): string[] => {
    try {
      return JSON.parse(vendorType);
    } catch {
      return vendorType.split(',').map(t => t.trim());
    }
  };

  return (
    <Dialog open={open} onOpenChange={setOpen}>
      <DialogTrigger asChild>
        {children || (
          <Button 
            variant="destructive" 
            size="sm"
            disabled={selectedItems.length === 0}
          >
            <Trash2 className="mr-2 h-4 w-4" />
            삭제 ({selectedItems.length})
          </Button>
        )}
      </DialogTrigger>
      <DialogContent className="max-w-2xl">
        <DialogHeader>
          <DialogTitle className="flex items-center gap-2">
            <AlertTriangle className="h-5 w-5 text-destructive" />
            아이템 삭제 확인
          </DialogTitle>
          <DialogDescription>
            선택한 {selectedItems.length}개의 벤더-아이템 조합을 삭제하시겠습니까?
            이 작업은 되돌릴 수 없습니다.
          </DialogDescription>
        </DialogHeader>

        <div className="py-4">
          <div className="text-sm font-medium mb-3">삭제될 아이템 목록:</div>
          <ScrollArea className="max-h-[300px] border rounded-md">
            <div className="p-4 space-y-3">
              {selectedItems.map((item) => (
                <div key={item.id} className="border rounded-md p-3 bg-muted/50">
                  <div className="flex justify-between items-start">
                    <div className="space-y-1">
                      <div className="font-medium text-sm">
                        {item.vendorName} ({item.vendorCode})
                      </div>
                      <div className="text-sm text-muted-foreground">
                        아이템코드: {item.itemCode}
                      </div>
                      {item.itemList && (
                        <div className="text-xs text-muted-foreground">
                          아이템리스트: {item.itemList}
                        </div>
                      )}
                      {item.workType && (
                        <div className="text-xs text-muted-foreground">
                          공종: {item.workType}
                        </div>
                      )}
                    </div>
                    <div className="flex flex-wrap gap-1">
                      {parseVendorTypes(item.techVendorType).map((type, index) => (
                        <Badge key={index} variant="secondary" className="text-xs">
                          {type}
                        </Badge>
                      ))}
                    </div>
                  </div>
                </div>
              ))}
            </div>
          </ScrollArea>
        </div>

        <DialogFooter>
          <Button variant="outline" onClick={() => setOpen(false)}>
            취소
          </Button>
          <Button 
            variant="destructive" 
            onClick={handleDelete}
            disabled={isLoading}
          >
            {isLoading ? "삭제 중..." : `삭제 (${selectedItems.length})`}
          </Button>
        </DialogFooter>
      </DialogContent>
    </Dialog>
  );
}