From b9f575f6110faabc7b062f84bf491d69d88a10a5 Mon Sep 17 00:00:00 2001
From: joonhoekim <26rote@gmail.com>
Date: Mon, 17 Nov 2025 15:42:29 +0900
Subject: (김준회) swp: GetExternalInboxList를 메인 테이블로 변경
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 lib/swp/table/swp-inbox-document-detail-dialog.tsx | 450 +++++++++++++++++++++
 1 file changed, 450 insertions(+)
 create mode 100644 lib/swp/table/swp-inbox-document-detail-dialog.tsx

(limited to 'lib/swp/table/swp-inbox-document-detail-dialog.tsx')

diff --git a/lib/swp/table/swp-inbox-document-detail-dialog.tsx b/lib/swp/table/swp-inbox-document-detail-dialog.tsx
new file mode 100644
index 00000000..ca7fcf1b
--- /dev/null
+++ b/lib/swp/table/swp-inbox-document-detail-dialog.tsx
@@ -0,0 +1,450 @@
+"use client";
+
+import React, { useState, useMemo } from "react";
+import {
+  Dialog,
+  DialogContent,
+  DialogHeader,
+  DialogTitle,
+  DialogDescription,
+} from "@/components/ui/dialog";
+import { Button } from "@/components/ui/button";
+import { Badge } from "@/components/ui/badge";
+import { 
+  ChevronDown, 
+  ChevronRight, 
+  Download, 
+  FileIcon,
+  XCircle,
+  AlertCircle,
+} from "lucide-react";
+import { 
+  cancelVendorFile,
+  downloadVendorFile,
+} from "@/lib/swp/vendor-actions";
+import type { SwpFileApiResponse } from "@/lib/swp/api-client";
+import type { InboxDocumentItem } from "./swp-inbox-table";
+import { toast } from "sonner";
+
+interface SwpInboxDocumentDetailDialogProps {
+  open: boolean;
+  onOpenChange: (open: boolean) => void;
+  document: InboxDocumentItem | null;
+  projNo: string;
+  vendorCode: string;
+  userId: string;
+}
+
+// 리비전별 그룹 타입
+interface RevisionGroup {
+  revNo: string;
+  stage: string;
+  activities: ActivityGroup[];
+  totalFiles: number;
+}
+
+// Activity별 그룹 타입 (activity가 null일 수 있음)
+interface ActivityGroup {
+  actvNo: string | null;
+  files: SwpFileApiResponse[];
+}
+
+export function SwpInboxDocumentDetailDialog({
+  open,
+  onOpenChange,
+  document,
+  projNo,
+}: SwpInboxDocumentDetailDialogProps) {
+  const [expandedRevisions, setExpandedRevisions] = useState<Set<string>>(new Set());
+  const [expandedActivities, setExpandedActivities] = useState<Set<string>>(new Set());
+  const [isAllExpanded, setIsAllExpanded] = useState(true);
+
+  // 파일들을 리비전 > Activity 구조로 그룹핑
+  const revisionGroups = useMemo(() => {
+    if (!document) return [];
+
+    const revMap = new Map<string, SwpFileApiResponse[]>();
+
+    document.files.forEach((file) => {
+      const revKey = `${file.REV_NO}|${file.STAGE}`;
+      if (!revMap.has(revKey)) {
+        revMap.set(revKey, []);
+      }
+      revMap.get(revKey)!.push(file);
+    });
+
+    const result: RevisionGroup[] = [];
+
+    revMap.forEach((revFiles, revKey) => {
+      const [revNo, stage] = revKey.split("|");
+
+      // Activity별로 그룹핑 (null 가능)
+      const actMap = new Map<string | null, SwpFileApiResponse[]>();
+      
+      revFiles.forEach((file) => {
+        const actvNo = file.ACTV_NO || null;
+        if (!actMap.has(actvNo)) {
+          actMap.set(actvNo, []);
+        }
+        actMap.get(actvNo)!.push(file);
+      });
+
+      const activities: ActivityGroup[] = [];
+      actMap.forEach((files, actvNo) => {
+        activities.push({ actvNo, files });
+      });
+
+      // Activity가 없는 것을 먼저, 있는 것을 나중에 정렬
+      activities.sort((a, b) => {
+        if (a.actvNo === null && b.actvNo !== null) return -1;
+        if (a.actvNo !== null && b.actvNo === null) return 1;
+        if (a.actvNo === null && b.actvNo === null) return 0;
+        return (a.actvNo || "").localeCompare(b.actvNo || "");
+      });
+
+      result.push({
+        revNo,
+        stage,
+        activities,
+        totalFiles: revFiles.length,
+      });
+    });
+
+    // 리비전 번호로 정렬 (최신이 위로)
+    return result.sort((a, b) => b.revNo.localeCompare(a.revNo));
+  }, [document]);
+
+  // Dialog가 열릴 때 모두 펼치기
+  React.useEffect(() => {
+    if (open && revisionGroups.length > 0) {
+      const allRevKeys = new Set<string>();
+      const allActKeys = new Set<string>();
+      
+      revisionGroups.forEach((revision) => {
+        const revKey = revision.revNo;
+        allRevKeys.add(revKey);
+        
+        revision.activities.forEach((activity) => {
+          const actKey = `${revKey}|${activity.actvNo || "NO_ACTIVITY"}`;
+          allActKeys.add(actKey);
+        });
+      });
+      
+      setExpandedRevisions(allRevKeys);
+      setExpandedActivities(allActKeys);
+      setIsAllExpanded(true);
+    }
+  }, [open, revisionGroups]);
+
+  const toggleRevision = (revKey: string) => {
+    setExpandedRevisions((prev) => {
+      const newSet = new Set(prev);
+      if (newSet.has(revKey)) {
+        newSet.delete(revKey);
+      } else {
+        newSet.add(revKey);
+      }
+      return newSet;
+    });
+  };
+
+  const toggleActivity = (actKey: string) => {
+    setExpandedActivities((prev) => {
+      const newSet = new Set(prev);
+      if (newSet.has(actKey)) {
+        newSet.delete(actKey);
+      } else {
+        newSet.add(actKey);
+      }
+      return newSet;
+    });
+  };
+
+  // 일괄 열기/닫기
+  const handleToggleAll = () => {
+    if (isAllExpanded) {
+      // 모두 닫기
+      setExpandedRevisions(new Set());
+      setExpandedActivities(new Set());
+      setIsAllExpanded(false);
+    } else {
+      // 모두 열기
+      const allRevKeys = new Set<string>();
+      const allActKeys = new Set<string>();
+      
+      revisionGroups.forEach((revision) => {
+        const revKey = revision.revNo;
+        allRevKeys.add(revKey);
+        
+        revision.activities.forEach((activity) => {
+          const actKey = `${revKey}|${activity.actvNo || "NO_ACTIVITY"}`;
+          allActKeys.add(actKey);
+        });
+      });
+      
+      setExpandedRevisions(allRevKeys);
+      setExpandedActivities(allActKeys);
+      setIsAllExpanded(true);
+    }
+  };
+
+  const handleCancelFile = async (boxSeq: string, actvSeq: string, fileName: string) => {
+    try {
+      await cancelVendorFile(boxSeq, actvSeq);
+      toast.success(`파일 취소 완료: ${fileName}`);
+      
+      // Dialog를 닫고 부모 컴포넌트가 새로고침하도록 함
+      onOpenChange(false);
+    } catch (error) {
+      console.error("파일 취소 실패:", error);
+      toast.error("파일 취소에 실패했습니다");
+    }
+  };
+
+  const handleDownloadFile = async (fileName: string, ownDocNo: string) => {
+    try {
+      toast.info("파일 다운로드 중...");
+      const result = await downloadVendorFile(projNo, ownDocNo, fileName);
+      
+      if (!result.success || !result.data) {
+        toast.error(result.error || "파일 다운로드 실패");
+        return;
+      }
+
+      // Blob 생성 및 다운로드
+      const blob = new Blob([Buffer.from(result.data)], { type: result.mimeType });
+      const url = URL.createObjectURL(blob);
+      const link = window.document.createElement("a");
+      link.href = url;
+      link.download = result.fileName || fileName;
+      window.document.body.appendChild(link);
+      link.click();
+      window.document.body.removeChild(link);
+      URL.revokeObjectURL(url);
+
+      toast.success(`파일 다운로드 완료: ${fileName}`);
+    } catch (error) {
+      console.error("파일 다운로드 실패:", error);
+      toast.error("파일 다운로드에 실패했습니다");
+    }
+  };
+
+  return (
+    <Dialog open={open} onOpenChange={onOpenChange}>
+      <DialogContent className="max-w-6xl max-h-[90vh] overflow-y-auto">
+        <DialogHeader>
+          <DialogTitle>업로드 파일 상세</DialogTitle>
+          {document && (
+            <DialogDescription>
+              {document.ownDocNo}
+            </DialogDescription>
+          )}
+        </DialogHeader>
+
+        {document && (
+          <div className="space-y-4">
+            {/* 문서 정보 */}
+            <div className="grid grid-cols-1 md:grid-cols-4 gap-4 p-4 bg-muted/30 rounded-lg">
+              <div>
+                <span className="text-sm font-semibold">OWN_DOC_NO:</span>
+                <div className="text-sm font-mono">{document.ownDocNo}</div>
+              </div>
+              <div>
+                <span className="text-sm font-semibold">최신 스테이지:</span>
+                <div className="text-sm">{document.latestStage || "-"}</div>
+              </div>
+              <div>
+                <span className="text-sm font-semibold">최신 리비전:</span>
+                <div className="text-sm">{document.latestRevNo || "-"}</div>
+              </div>
+              <div>
+                <span className="text-sm font-semibold">최신 REV 파일:</span>
+                <div className="text-sm">{document.latestRevFileCount}개</div>
+              </div>
+            </div>
+
+            {/* 리비전 및 액티비티 트리 */}
+            {revisionGroups.length > 0 ? (
+              <div className="space-y-2">
+                {/* 일괄 열기/닫기 버튼 */}
+                <div className="flex justify-end">
+                  <Button
+                    variant="outline"
+                    size="sm"
+                    onClick={handleToggleAll}
+                  >
+                    {isAllExpanded ? (
+                      <>
+                        <ChevronDown className="h-4 w-4 mr-2" />
+                        일괄 닫기
+                      </>
+                    ) : (
+                      <>
+                        <ChevronRight className="h-4 w-4 mr-2" />
+                        일괄 열기
+                      </>
+                    )}
+                  </Button>
+                </div>
+                {revisionGroups.map((revision) => {
+                  const revKey = revision.revNo;
+                  const isRevExpanded = expandedRevisions.has(revKey);
+
+                  return (
+                    <div key={revKey} className="border rounded-lg">
+                      {/* 리비전 헤더 */}
+                      <div
+                        className="flex items-center justify-between p-3 bg-muted/50 cursor-pointer hover:bg-muted"
+                        onClick={() => toggleRevision(revKey)}
+                      >
+                        <div className="flex items-center gap-3">
+                          {isRevExpanded ? (
+                            <ChevronDown className="h-4 w-4" />
+                          ) : (
+                            <ChevronRight className="h-4 w-4" />
+                          )}
+                          <Badge variant="secondary" className="font-mono">
+                            REV {revision.revNo}
+                          </Badge>
+                          <Badge variant="outline" className={
+                            revision.stage === "IFC" ? "bg-green-100 text-green-800" :
+                            revision.stage === "IFA" ? "bg-blue-100 text-blue-800" :
+                            "bg-gray-100 text-gray-800"
+                          }>
+                            {revision.stage}
+                          </Badge>
+                          <span className="text-sm text-muted-foreground">
+                            {revision.activities.length}개 그룹 / {revision.totalFiles}개 파일
+                          </span>
+                        </div>
+                      </div>
+
+                      {/* 액티비티 목록 (또는 Activity 없는 파일들) */}
+                      {isRevExpanded && (
+                        <div className="p-2 space-y-2">
+                          {revision.activities.map((activity) => {
+                            const actKey = `${revKey}|${activity.actvNo || "NO_ACTIVITY"}`;
+                            const isActExpanded = expandedActivities.has(actKey);
+
+                            return (
+                              <div key={actKey} className="border rounded-md">
+                                {/* 액티비티 헤더 */}
+                                <div
+                                  className="flex items-center justify-between p-2 bg-muted/30 cursor-pointer hover:bg-muted/50"
+                                  onClick={() => toggleActivity(actKey)}
+                                >
+                                  <div className="flex items-center gap-2">
+                                    {isActExpanded ? (
+                                      <ChevronDown className="h-4 w-4" />
+                                    ) : (
+                                      <ChevronRight className="h-4 w-4" />
+                                    )}
+                                    {activity.actvNo ? (
+                                      <>
+                                        <Badge variant="outline" className="bg-blue-100 text-blue-800">
+                                          Activity
+                                        </Badge>
+                                        <span className="text-xs text-muted-foreground font-mono">
+                                          {activity.actvNo}
+                                        </span>
+                                      </>
+                                    ) : (
+                                      <Badge variant="outline" className="bg-gray-100 text-gray-800">
+                                        Activity 없음
+                                      </Badge>
+                                    )}
+                                    <span className="text-xs text-muted-foreground">
+                                      {activity.files.length}개 파일
+                                    </span>
+                                  </div>
+                                </div>
+
+                                {/* 파일 목록 */}
+                                {isActExpanded && (
+                                  <div className="p-2 space-y-1">
+                                    {activity.files.map((file, idx) => (
+                                      <div
+                                        key={idx}
+                                        className="flex items-center justify-between p-2 border rounded bg-background hover:bg-muted/30"
+                                      >
+                                        <div className="flex items-center gap-2 flex-1">
+                                          <FileIcon className="h-4 w-4 text-blue-500" />
+                                          <span className="text-sm font-mono">{file.FILE_NM}</span>
+                                          {file.FILE_SZ && (
+                                            <span className="text-xs text-muted-foreground">
+                                              ({formatFileSize(file.FILE_SZ)})
+                                            </span>
+                                          )}
+                                          {file.STAT && (
+                                            <Badge variant="outline" className={
+                                              file.STAT === "SCW03" || file.STAT === "SCW08" ? "bg-green-100 text-green-800" :      // Complete, Checked
+                                              file.STAT === "SCW02" ? "bg-blue-100 text-blue-800" :                                 // Processing
+                                              file.STAT === "SCW01" ? "bg-yellow-100 text-yellow-800" :                             // Standby
+                                              file.STAT === "SCW04" || file.STAT === "SCW05" || file.STAT === "SCW06" ? "bg-red-100 text-red-800" : // Reject, Error Zip, Error Meta
+                                              file.STAT === "SCW07" ? "bg-purple-100 text-purple-800" :                             // Send for Eng Verification
+                                              file.STAT === "SCW09" ? "bg-gray-100 text-gray-800" :                                 // Cancelled
+                                              file.STAT === "SCW00" ? "bg-orange-100 text-orange-800" :                             // Upload
+                                              "bg-gray-100 text-gray-800"
+                                            }>
+                                              {file.STAT_NM || file.STAT}
+                                            </Badge>
+                                          )}
+                                        </div>
+                                        <div className="flex items-center gap-1">
+                                          {file.STAT === "SCW01" && file.BOX_SEQ && file.ACTV_SEQ && (
+                                            <Button
+                                              variant="outline"
+                                              size="sm"
+                                              onClick={() => handleCancelFile(file.BOX_SEQ!, file.ACTV_SEQ!, file.FILE_NM)}
+                                            >
+                                              <XCircle className="h-4 w-4 mr-1" />
+                                              취소
+                                            </Button>
+                                          )}
+                                          {file.FLD_PATH && (
+                                            <Button
+                                              variant="outline"
+                                              size="sm"
+                                              onClick={() => handleDownloadFile(file.FILE_NM, document.ownDocNo)}
+                                            >
+                                              <Download className="h-4 w-4 mr-1" />
+                                              다운로드
+                                            </Button>
+                                          )}
+                                        </div>
+                                      </div>
+                                    ))}
+                                  </div>
+                                )}
+                              </div>
+                            );
+                          })}
+                        </div>
+                      )}
+                    </div>
+                  );
+                })}
+              </div>
+            ) : (
+              <div className="p-8 text-center text-muted-foreground">
+                <AlertCircle className="h-12 w-12 mx-auto mb-2 opacity-50" />
+                <p>파일 정보가 없습니다</p>
+              </div>
+            )}
+          </div>
+        )}
+      </DialogContent>
+    </Dialog>
+  );
+}
+
+function formatFileSize(sizeStr: string): string {
+  const bytes = parseInt(sizeStr, 10);
+  if (isNaN(bytes)) return sizeStr;
+
+  const kb = bytes / 1024;
+  const mb = kb / 1024;
+
+  return mb >= 1 ? `${mb.toFixed(2)} MB` : `${kb.toFixed(2)} KB`;
+}
+
-- 
cgit v1.2.3

