From 24e0b8c83f7d68156e5a63ba85a541c04036f00b Mon Sep 17 00:00:00 2001
From: joonhoekim <26rote@gmail.com>
Date: Fri, 28 Nov 2025 14:13:46 +0900
Subject: (김준회) dolce: API 추가건 대응 처리(상세도면 및 파일 삭제
 기능-Standby 상태)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../add-and-modify-detail-drawing-dialog.tsx       | 139 ++++++++++++++++++---
 1 file changed, 123 insertions(+), 16 deletions(-)

(limited to 'lib/dolce/dialogs/add-and-modify-detail-drawing-dialog.tsx')

diff --git a/lib/dolce/dialogs/add-and-modify-detail-drawing-dialog.tsx b/lib/dolce/dialogs/add-and-modify-detail-drawing-dialog.tsx
index 0253228b..54ec79a7 100644
--- a/lib/dolce/dialogs/add-and-modify-detail-drawing-dialog.tsx
+++ b/lib/dolce/dialogs/add-and-modify-detail-drawing-dialog.tsx
@@ -1,6 +1,6 @@
 "use client";
 
-import { useState, useEffect } from "react";
+import { useState, useEffect, useCallback } from "react";
 import {
   Dialog,
   DialogContent,
@@ -20,7 +20,7 @@ import {
 } from "@/components/ui/select";
 import { Alert, AlertDescription } from "@/components/ui/alert";
 import { Textarea } from "@/components/ui/textarea";
-import { Upload, X, FileIcon, Info } from "lucide-react";
+import { Upload, X, FileIcon, Info, Trash2 } from "lucide-react";
 import { toast } from "sonner";
 import { useTranslation } from "@/i18n/client";
 import { UnifiedDwgReceiptItem, DetailDwgReceiptItem, editDetailDwgReceipt } from "../actions";
@@ -34,6 +34,16 @@ import {
   getB4DrawingUsageOptions,
   getB4RegisterKindOptions
 } from "../utils/code-translator";
+import {
+  AlertDialog,
+  AlertDialogAction,
+  AlertDialogCancel,
+  AlertDialogContent,
+  AlertDialogDescription,
+  AlertDialogFooter as AlertDialogFooterComponent,
+  AlertDialogHeader,
+  AlertDialogTitle,
+} from "@/components/ui/alert-dialog";
 
 interface AddAndModifyDetailDrawingDialogProps {
   open: boolean;
@@ -73,6 +83,10 @@ export function AddAndModifyDetailDrawingDialog({
   const [isSubmitting, setIsSubmitting] = useState(false);
 
   const [showConfirmation, setShowConfirmation] = useState(false);
+  
+  // 삭제 관련 상태
+  const [showDeleteConfirmation, setShowDeleteConfirmation] = useState(false);
+  const [isDeleting, setIsDeleting] = useState(false);
 
   // Edit 모드일 때 초기값 설정
   useEffect(() => {
@@ -150,7 +164,7 @@ export function AddAndModifyDetailDrawingDialog({
   };
 
   // 폼 초기화
-  const resetForm = () => {
+  const resetForm = useCallback(() => {
     setDrawingUsage("");
     setRegisterKind("");
     setRevision("");
@@ -158,7 +172,7 @@ export function AddAndModifyDetailDrawingDialog({
     setComment("");
     clearFiles();
     setShowConfirmation(false);
-  };
+  }, [clearFiles]);
 
   // 제출 (확인 단계 포함)
   const handleSubmit = async () => {
@@ -346,6 +360,59 @@ export function AddAndModifyDetailDrawingDialog({
     }
   };
 
+  // 상세도면 삭제 핸들러
+  const handleDelete = async () => {
+    if (!detailDrawing) return;
+
+    try {
+      setIsDeleting(true);
+      
+      const result = await editDetailDwgReceipt({
+        dwgList: [
+          {
+            Mode: "DEL",
+            Status: detailDrawing.Status,
+            RegisterId: detailDrawing.RegisterId,
+            ProjectNo: detailDrawing.ProjectNo,
+            Discipline: detailDrawing.Discipline,
+            DrawingKind: detailDrawing.DrawingKind,
+            DrawingNo: detailDrawing.DrawingNo,
+            DrawingName: detailDrawing.DrawingName,
+            RegisterGroupId: detailDrawing.RegisterGroupId,
+            RegisterSerialNo: detailDrawing.RegisterSerialNo,
+            RegisterKind: detailDrawing.RegisterKind, // 기존 값 유지
+            DrawingRevNo: detailDrawing.DrawingRevNo, // 기존 값 유지
+            Category: detailDrawing.Category,
+            Receiver: detailDrawing.Receiver,
+            Manager: detailDrawing.Manager,
+            RegisterDesc: detailDrawing.RegisterDesc,
+            UploadId: detailDrawing.UploadId,
+            RegCompanyCode: detailDrawing.RegCompanyCode || vendorCode,
+          },
+        ],
+        userId,
+        userNm: userName,
+        vendorCode,
+        email: userEmail,
+      });
+
+      if (result > 0) {
+        toast.success("Detail drawing deleted successfully");
+        setShowDeleteConfirmation(false);
+        resetForm();
+        onComplete();
+        onOpenChange(false);
+      } else {
+        toast.error("Failed to delete detail drawing");
+      }
+    } catch (error) {
+      console.error("상세도면 삭제 실패:", error);
+      toast.error("An error occurred while deleting");
+    } finally {
+      setIsDeleting(false);
+    }
+  };
+
   // DrawingUsage가 변경되면 RegisterKind 초기화
   const handleDrawingUsageChange = (value: string) => {
     setDrawingUsage(value);
@@ -631,19 +698,59 @@ export function AddAndModifyDetailDrawingDialog({
           </div>
         )}
 
-        <DialogFooter>
-          <Button variant="outline" onClick={handleCancel} disabled={isSubmitting}>
-            {showConfirmation ? t("addDetailDialog.backButton", "뒤로") : t("addDetailDialog.cancelButton")}
-          </Button>
-          <Button onClick={handleSubmit} disabled={isSubmitting || !isFormValid}>
-            {isSubmitting 
-              ? t("addDetailDialog.processingButton") 
-              : showConfirmation
-                ? t("addDetailDialog.confirmSubmit", "제출")
-                : t("addDetailDialog.nextButton", "다음")
-            }
-          </Button>
+        <DialogFooter className={mode === "edit" && !showConfirmation ? "sm:justify-between" : ""}>
+          {mode === "edit" && !showConfirmation && (
+            <Button
+              type="button"
+              variant="destructive"
+              onClick={() => setShowDeleteConfirmation(true)}
+              disabled={isSubmitting}
+            >
+              <Trash2 className="h-4 w-4 mr-2" />
+              {t("editDetailDialog.deleteButton", "Delete")}
+            </Button>
+          )}
+          <div className="flex gap-2 justify-end sm:w-auto w-full">
+            <Button variant="outline" onClick={handleCancel} disabled={isSubmitting}>
+              {showConfirmation ? t("addDetailDialog.backButton", "뒤로") : t("addDetailDialog.cancelButton")}
+            </Button>
+            <Button onClick={handleSubmit} disabled={isSubmitting || !isFormValid}>
+              {isSubmitting 
+                ? t("addDetailDialog.processingButton") 
+                : showConfirmation
+                  ? t("addDetailDialog.confirmSubmit", "제출")
+                  : t("addDetailDialog.nextButton", "다음")
+              }
+            </Button>
+          </div>
         </DialogFooter>
+
+        {/* Delete Confirmation Dialog */}
+        <AlertDialog open={showDeleteConfirmation} onOpenChange={setShowDeleteConfirmation}>
+          <AlertDialogContent>
+            <AlertDialogHeader>
+              <AlertDialogTitle>Delete Detail Drawing</AlertDialogTitle>
+              <AlertDialogDescription>
+                Are you sure you want to delete this detail drawing? This action cannot be undone.
+                {detailDrawing && (
+                  <span className="block mt-2 font-medium text-foreground">
+                    {detailDrawing.DrawingNo} (Rev. {detailDrawing.DrawingRevNo})
+                  </span>
+                )}
+              </AlertDialogDescription>
+            </AlertDialogHeader>
+            <AlertDialogFooterComponent>
+              <AlertDialogCancel disabled={isDeleting}>Cancel</AlertDialogCancel>
+              <AlertDialogAction 
+                onClick={handleDelete} 
+                disabled={isDeleting}
+                className="bg-destructive text-destructive-foreground hover:bg-destructive/90"
+              >
+                {isDeleting ? "Deleting..." : "Delete"}
+              </AlertDialogAction>
+            </AlertDialogFooterComponent>
+          </AlertDialogContent>
+        </AlertDialog>
       </DialogContent>
     </Dialog>
   );
-- 
cgit v1.2.3

