From defda07c0bb4b0bd444ca8dc4fd3f89322bda0ce Mon Sep 17 00:00:00 2001
From: dujinkim <dujin.kim@dtsolution.co.kr>
Date: Fri, 3 Oct 2025 04:48:47 +0000
Subject: (대표님) edp, tbe, dolce 등
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 components/form-data/spreadJS-dialog.tsx | 71 ++++++++++++++++++++++++--------
 1 file changed, 54 insertions(+), 17 deletions(-)

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

diff --git a/components/form-data/spreadJS-dialog.tsx b/components/form-data/spreadJS-dialog.tsx
index af1a3dca..375c097c 100644
--- a/components/form-data/spreadJS-dialog.tsx
+++ b/components/form-data/spreadJS-dialog.tsx
@@ -176,13 +176,13 @@ export function TemplateViewDialog({
   }, []);
 
   const determineTemplateType = React.useCallback((template: TemplateItem): 'SPREAD_LIST' | 'SPREAD_ITEM' | 'GRD_LIST' | null => {
-    if (template.TMPL_TYPE === "SPREAD_LIST" && (template.SPR_LST_SETUP?.CONTENT || template.SPR_ITM_LST_SETUP?.CONTENT)) {
+    if (template?.TMPL_TYPE === "SPREAD_LIST" && (template?.SPR_LST_SETUP?.CONTENT || template?.SPR_ITM_LST_SETUP?.CONTENT)) {
       return 'SPREAD_LIST';
     }
-    if (template.TMPL_TYPE === "SPREAD_ITEM" && (template.SPR_LST_SETUP?.CONTENT || template.SPR_ITM_LST_SETUP?.CONTENT)) {
+    if (template?.TMPL_TYPE === "SPREAD_ITEM" && (template?.SPR_LST_SETUP?.CONTENT || template?.SPR_ITM_LST_SETUP?.CONTENT)) {
       return 'SPREAD_ITEM';
     }
-    if (template.GRD_LST_SETUP && columnsJSON.length > 0) {
+    if (template?.GRD_LST_SETUP && columnsJSON.length > 0) {
       return 'GRD_LIST';
     }
     return null;
@@ -221,10 +221,10 @@ export function TemplateViewDialog({
             DATA_SHEETS: []
           }
         };
-        
+
         setAvailableTemplates([defaultGrdTemplate]);
-        setSelectedTemplateId('DEFAULT_GRD_LIST');
-        setTemplateType('GRD_LIST');
+        // setSelectedTemplateId('DEFAULT_GRD_LIST');
+        // setTemplateType('GRD_LIST');
         console.log('📋 Created default GRD_LIST template');
       }
       return;
@@ -238,7 +238,7 @@ export function TemplateViewDialog({
     }
 
     const validTemplates = templates.filter(isValidTemplate);
-    
+
     // 유효한 템플릿이 없지만 columnsJSON이 있으면 기본 GRD_LIST 추가
     if (validTemplates.length === 0 && columnsJSON && columnsJSON.length > 0) {
       const defaultGrdTemplate: TemplateItem = {
@@ -261,11 +261,11 @@ export function TemplateViewDialog({
           DATA_SHEETS: []
         }
       };
-      
+
       validTemplates.push(defaultGrdTemplate);
       console.log('📋 Added default GRD_LIST template to empty template list');
     }
-    
+
     setAvailableTemplates(validTemplates);
 
     if (validTemplates.length > 0 && !selectedTemplateId) {
@@ -1251,13 +1251,13 @@ export function TemplateViewDialog({
                 }
               });
 
-                // 🔧 마지막에 activeSheetName으로 다시 전환
-            if (activeSheetName && spread.getSheetFromName(activeSheetName)) {
-              spread.setActiveSheet(activeSheetName);
-              activeSheet = spread.getActiveSheet();
-            }
+              // 🔧 마지막에 activeSheetName으로 다시 전환
+              if (activeSheetName && spread.getSheetFromName(activeSheetName)) {
+                spread.setActiveSheet(activeSheetName);
+                activeSheet = spread.getActiveSheet();
+              }
+
 
-            
             });
           }
         }
@@ -1292,6 +1292,32 @@ export function TemplateViewDialog({
     }
   }, [selectedTemplate, templateType, selectedRow, tableData, updateProgress, getSafeActiveSheet, createGrdListTableOptimized, setBatchValues, setBatchStyles, setupSheetProtectionAndEvents, setCellMappings, createCellStyle, isFieldEditable, columnsJSON, setupOptimizedListValidation, parseCellAddress, ensureRowCapacity, getCellAddress]);
 
+  React.useEffect(() => {
+    if (!selectedTemplateId) {
+      const only = availableTemplates[0];
+      const type = determineTemplateType(only);
+  
+      // 선택되어 있지 않다면 자동 선택
+      if (selectedTemplateId !== only.TMPL_ID) {
+        setSelectedTemplateId(only.TMPL_ID);
+        setTemplateType(type);
+      }
+  
+      // 이미 스프레드가 마운트되어 있다면 즉시 초기화(선택 변경만으로도 리렌더되지만 안전하게 보강)
+      if (currentSpread) {
+        initSpread(currentSpread, only);
+      }
+    }
+  }, [
+    availableTemplates,
+    selectedTemplateId,
+    currentSpread,
+    determineTemplateType,
+    initSpread,
+    setTemplateType,
+    setSelectedTemplateId
+  ]);
+
   const handleSaveChanges = React.useCallback(async () => {
     if (!currentSpread || !hasChanges) {
       toast.info("No changes to save");
@@ -1454,6 +1480,8 @@ export function TemplateViewDialog({
   const isDataValid = templateType === 'SPREAD_ITEM' ? !!selectedRow : tableData.length > 0;
   const dataCount = templateType === 'SPREAD_ITEM' ? 1 : tableData.length;
 
+
+
   return (
     <Dialog open={isOpen} onOpenChange={onClose}>
       <DialogContent
@@ -1463,7 +1491,8 @@ export function TemplateViewDialog({
           <DialogTitle>SEDP Template - {formCode}</DialogTitle>
           <DialogDescription>
             <div className="space-y-3">
-              {availableTemplates.length > 1 && (
+              {availableTemplates.length > 0 ? (
+                // 템플릿이 2개 이상일 때: Select 박스 표시
                 <div className="flex items-center gap-4">
                   <span className="text-sm font-medium">Template:</span>
                   <Select value={selectedTemplateId} onValueChange={handleTemplateChange}>
@@ -1479,7 +1508,15 @@ export function TemplateViewDialog({
                     </SelectContent>
                   </Select>
                 </div>
-              )}
+              ) : availableTemplates.length === 1 ? (
+                // 템플릿이 정확히 1개일 때: 템플릿 이름을 텍스트로 표시
+                <div className="flex items-center gap-4">
+                  <span className="text-sm font-medium">Template:</span>
+                  <span className="text-sm text-blue-600 font-medium">
+                    {availableTemplates[0].NAME} ({availableTemplates[0].TMPL_TYPE})
+                  </span>
+                </div>
+              ) : null}  
 
               {selectedTemplate && (
                 <div className="flex items-center gap-4 text-sm">
-- 
cgit v1.2.3

