"use client" import * as React from "react" import { Loader, Trash } from "lucide-react" import { toast } from "sonner" import { useMediaQuery } from "@/hooks/use-media-query" import { Button } from "@/components/ui/button" import { Dialog, DialogClose, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog" import { Drawer, DrawerClose, DrawerContent, DrawerDescription, DrawerFooter, DrawerHeader, DrawerTitle, DrawerTrigger, } from "@/components/ui/drawer" import { deleteFormDataByTags } from "@/lib/forms/services" interface GenericData { [key: string]: any TAG_NO?: string } interface DeleteFormDataDialogProps extends React.ComponentPropsWithoutRef { formData: GenericData[] formCode: string contractItemId: number showTrigger?: boolean onSuccess?: () => void triggerVariant?: "default" | "destructive" | "outline" | "secondary" | "ghost" | "link" } export function DeleteFormDataDialog({ formData, formCode, contractItemId, showTrigger = true, onSuccess, triggerVariant = "outline", ...props }: DeleteFormDataDialogProps) { const [isDeletePending, startDeleteTransition] = React.useTransition() const isDesktop = useMediaQuery("(min-width: 640px)") // TAG_NO가 있는 항목들만 필터링 const validItems = formData.filter(item => item.TAG_NO?.trim()) const tagNos = validItems.map(item => item.TAG_NO).filter(Boolean) as string[] function onDelete() { startDeleteTransition(async () => { if (tagNos.length === 0) { toast.error("No valid items to delete") return } const result = await deleteFormDataByTags({ formCode, contractItemId, tagNos, }) if (result.error) { toast.error(result.error) return } props.onOpenChange?.(false) // 성공 메시지 (개수는 같을 것으로 예상) const deletedCount = result.deletedCount || 0 const deletedTagsCount = result.deletedTagsCount || 0 if (deletedCount !== deletedTagsCount) { // 데이터 불일치 경고 console.warn(`Data inconsistency: FormEntries deleted: ${deletedCount}, Tags deleted: ${deletedTagsCount}`) toast.error( `Deleted ${deletedCount} form entries and ${deletedTagsCount} tags (data inconsistency detected)` ) } else { // 정상적인 삭제 완료 toast.success( `Successfully deleted ${deletedCount} item${deletedCount === 1 ? "" : "s"}` ) } onSuccess?.() }) } const itemCount = tagNos.length const hasValidItems = itemCount > 0 if (isDesktop) { return ( {showTrigger ? ( ) : null} Are you absolutely sure? This action cannot be undone. This will permanently delete{" "} {itemCount} {itemCount === 1 ? " item" : " items"} and related tag records from the database. {itemCount > 0 && ( <>

TAG_NO(s): {tagNos.slice(0, 3).join(", ")} {tagNos.length > 3 && ` and ${tagNos.length - 3} more...`} )}
) } return ( {showTrigger ? ( ) : null} Are you absolutely sure? This action cannot be undone. This will permanently delete{" "} {itemCount} {itemCount === 1 ? " item" : " items"} and related tag records from the database. {itemCount > 0 && ( <>

TAG_NO(s): {tagNos.slice(0, 3).join(", ")} {tagNos.length > 3 && ` and ${tagNos.length - 3} more...`} )}
) }