From 95bbe9c583ff841220da1267630e7b2025fc36dc Mon Sep 17 00:00:00 2001
From: dujinkim <dujin.kim@dtsolution.co.kr>
Date: Thu, 19 Jun 2025 09:44:28 +0000
Subject: (대표님) 20250619 1844 KST 작업사항
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 components/form-data/form-data-table.tsx | 117 +++++++++++++++++++++++++++++--
 1 file changed, 110 insertions(+), 7 deletions(-)

(limited to 'components/form-data/form-data-table.tsx')

diff --git a/components/form-data/form-data-table.tsx b/components/form-data/form-data-table.tsx
index 9a438957..61e9897f 100644
--- a/components/form-data/form-data-table.tsx
+++ b/components/form-data/form-data-table.tsx
@@ -28,7 +28,8 @@ import {
   Send,
   GitCompareIcon,
   RefreshCcw,
-  Trash2
+  Trash2,
+  Eye // 새로 추가된 아이콘
 } from "lucide-react";
 import { toast } from "sonner";
 import {
@@ -55,9 +56,9 @@ import { exportExcelData } from "./export-excel-form";
 import { SEDPConfirmationDialog, SEDPStatusDialog } from "./sedp-components";
 import { SEDPCompareDialog } from "./sedp-compare-dialog";
 import { getSEDPToken } from "@/lib/sedp/sedp-token";
-import { DeleteFormDataDialog } from "./delete-form-data-dialog"; // 새로 추가
-
+import { DeleteFormDataDialog } from "./delete-form-data-dialog";
 
+// 기존 fetchTagDataFromSEDP 함수
 async function fetchTagDataFromSEDP(projectCode: string, formCode: string): Promise<any> {
   try {
     // Get the token
@@ -98,6 +99,47 @@ async function fetchTagDataFromSEDP(projectCode: string, formCode: string): Prom
   }
 }
 
+// 새로 추가된 fetchTemplateFromSEDP 함수
+async function fetchTemplateFromSEDP(projectCode: string, formCode: string): Promise<any> {
+  try {
+    // Get the token
+    const apiKey = await getSEDPToken();
+
+    // Define the API base URL
+    const SEDP_API_BASE_URL = process.env.SEDP_API_BASE_URL || 'http://sedpwebapi.ship.samsung.co.kr/api';
+
+    // Make the API call
+    const response = await fetch(
+      `${SEDP_API_BASE_URL}/Data/GetPubData`,
+      {
+        method: 'POST',
+        headers: {
+          'Content-Type': 'application/json',
+          'accept': '*/*',
+          'ApiKey': apiKey,
+          'ProjectNo': projectCode
+        },
+        body: JSON.stringify({
+          WithContent: true,
+          ProjectNo: projectCode,
+          REG_TYPE_ID: formCode
+        })
+      }
+    );
+
+    if (!response.ok) {
+      const errorText = await response.text();
+      throw new Error(`SEDP Template API request failed: ${response.status} ${response.statusText} - ${errorText}`);
+    }
+
+    const data = await response.json();
+    return data;
+  } catch (error: any) {
+    console.error('Error calling SEDP Template API:', error);
+    throw new Error(`Failed to fetch template from SEDP API: ${error.message || 'Unknown error'}`);
+  }
+}
+
 interface GenericData {
   [key: string]: any;
 }
@@ -158,9 +200,10 @@ export default function DynamicTable({
   const [isSaving, setIsSaving] = React.useState(false);
   const [isSendingSEDP, setIsSendingSEDP] = React.useState(false);
   const [isLoadingTags, setIsLoadingTags] = React.useState(false);
+  const [isLoadingTemplate, setIsLoadingTemplate] = React.useState(false); // 새로 추가
 
   // Any operation in progress
-  const isAnyOperationPending = isSyncingTags || isImporting || isExporting || isSaving || isSendingSEDP || isLoadingTags;
+  const isAnyOperationPending = isSyncingTags || isImporting || isExporting || isSaving || isSendingSEDP || isLoadingTags || isLoadingTemplate;
 
   // SEDP dialogs state
   const [sedpConfirmOpen, setSedpConfirmOpen] = React.useState(false);
@@ -177,6 +220,10 @@ export default function DynamicTable({
   const [sedpCompareOpen, setSedpCompareOpen] = React.useState(false);
   const [projectCode, setProjectCode] = React.useState<string>('');
 
+  // 새로 추가된 Template 다이얼로그 상태
+  const [templateDialogOpen, setTemplateDialogOpen] = React.useState(false);
+  const [templateData, setTemplateData] = React.useState<any>(null);
+
   const [tempUpDialog, setTempUpDialog] = React.useState(false);
   const [reportData, setReportData] = React.useState<GenericData[]>([]);
   const [batchDownDialog, setBatchDownDialog] = React.useState(false);
@@ -267,6 +314,35 @@ export default function DynamicTable({
     }));
   }, [columnsJSON]);
 
+  // 새로 추가된 Template 가져오기 함수
+  const handleGetTemplate = async () => {
+    if (selectedRowCount !== 1) {
+      toast.error("Please select exactly one row to view template");
+      return;
+    }
+
+    if (!projectCode) {
+      toast.error("Project code is not available");
+      return;
+    }
+
+    try {
+      setIsLoadingTemplate(true);
+
+      const templateResult = await fetchTemplateFromSEDP(projectCode, formCode);
+
+      setTemplateData(templateResult);
+      setTemplateDialogOpen(true);
+
+      toast.success("Template data loaded successfully");
+    } catch (error) {
+      console.error("Error fetching template:", error);
+      toast.error("Failed to fetch template from SEDP");
+    } finally {
+      setIsLoadingTemplate(false);
+    }
+  };
+
   // IM 모드: 태그 동기화 함수
   async function handleSyncTags() {
     try {
@@ -644,6 +720,21 @@ export default function DynamicTable({
           </Button>
         )}
 
+        {/* 새로 추가된 Template 보기 버튼 */}
+        <Button
+          variant="outline"
+          size="sm"
+          onClick={handleGetTemplate}
+          disabled={isAnyOperationPending || selectedRowCount !== 1}
+        >
+          {isLoadingTemplate ? (
+            <Loader className="mr-2 size-4 animate-spin" />
+          ) : (
+            <Eye className="mr-2 size-4" />
+          )}
+          View In Spread
+        </Button>
+
         {/* 버튼 그룹 */}
         <div className="flex items-center gap-2">
           {/* 태그 관리 드롭다운 */}
@@ -736,6 +827,8 @@ export default function DynamicTable({
             Export
           </Button>
 
+
+
           {/* COMPARE WITH SEDP 버튼 */}
           <Button
             variant="outline"
@@ -769,7 +862,6 @@ export default function DynamicTable({
         </div>
       </ClientDataTable>
 
-      {/* Modal dialog for tag update */}
       {/* Modal dialog for tag update */}
       <UpdateTagSheet
         open={rowAction?.type === "update"}
@@ -780,7 +872,7 @@ export default function DynamicTable({
         rowData={rowAction?.row.original ?? null}
         formCode={formCode}
         contractItemId={contractItemId}
-        editableFieldsMap={editableFieldsMap} // 새로 추가
+        editableFieldsMap={editableFieldsMap}
         onUpdateSuccess={(updatedValues) => {
           // Update the specific row in tableData when a single row is updated
           if (rowAction?.row.original?.TAG_NO) {
@@ -793,6 +885,7 @@ export default function DynamicTable({
           }
         }}
       />
+
       <DeleteFormDataDialog
         formData={deleteTarget}
         formCode={formCode}
@@ -818,6 +911,15 @@ export default function DynamicTable({
         onOpenChange={setAddTagDialogOpen}
       />
 
+      {/* 새로 추가된 Template 다이얼로그 */}
+      <TemplateViewDialog
+        isOpen={templateDialogOpen}
+        onClose={() => setTemplateDialogOpen(false)}
+        templateData={templateData}
+        selectedRow={selectedRowsData[0]}
+        formCode={formCode}
+      />
+
       {/* SEDP Confirmation Dialog */}
       <SEDPConfirmationDialog
         isOpen={sedpConfirmOpen}
@@ -887,4 +989,5 @@ export default function DynamicTable({
       )}
     </>
   );
-}
\ No newline at end of file
+}
+
-- 
cgit v1.2.3

