From bc7d627f61a4d055b19d0679b3a4c128b7afcfda Mon Sep 17 00:00:00 2001
From: dujinkim <dujin.kim@dtsolution.co.kr>
Date: Wed, 28 May 2025 17:29:43 +0000
Subject: (대표님) admin / api / components
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 components/form-data/form-data-table.tsx | 60 ++++++++++++++++++++++++++++----
 1 file changed, 53 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 05278375..0a76e145 100644
--- a/components/form-data/form-data-table.tsx
+++ b/components/form-data/form-data-table.tsx
@@ -131,9 +131,14 @@ export default function DynamicTable({
     React.useState<DataTableRowAction<GenericData> | null>(null);
   const [tableData, setTableData] = React.useState<GenericData[]>(dataJSON);
 
+  // 배치 선택 관련 상태
+  const [selectedRows, setSelectedRows] = React.useState<Record<string, boolean>>({});
+
   // Update tableData when dataJSON changes
   React.useEffect(() => {
     setTableData(dataJSON);
+    // 데이터가 변경되면 선택 상태 초기화
+    setSelectedRows({});
   }, [dataJSON]);
   
   // 폴링 상태 관리를 위한 ref
@@ -207,9 +212,27 @@ export default function DynamicTable({
     }
   }, [projectId]);
 
+  // 선택된 행들의 실제 데이터 가져오기
+  const getSelectedRowsData = React.useCallback(() => {
+    const selectedIndices = Object.keys(selectedRows).filter(key => selectedRows[key]);
+    return selectedIndices.map(index => tableData[parseInt(index)]).filter(Boolean);
+  }, [selectedRows, tableData]);
+
+  // 선택된 행 개수 계산
+  const selectedRowCount = React.useMemo(() => {
+    return Object.values(selectedRows).filter(Boolean).length;
+  }, [selectedRows]);
+
   const columns = React.useMemo(
-    () => getColumns<GenericData>({ columnsJSON, setRowAction, setReportData, tempCount }),
-    [columnsJSON, setRowAction, setReportData, tempCount]
+    () => getColumns<GenericData>({ 
+      columnsJSON, 
+      setRowAction, 
+      setReportData, 
+      tempCount,
+      selectedRows,
+      onRowSelectionChange: setSelectedRows
+    }),
+    [columnsJSON, setRowAction, setReportData, tempCount, selectedRows]
   );
 
   function mapColumnTypeToAdvancedFilterType(
@@ -518,13 +541,22 @@ export default function DynamicTable({
     }
   }
 
-  // Handle batch document check
+  // Handle batch document with smart selection logic
   const handleBatchDocument = () => {
-    if (tempCount > 0) {
-      setBatchDownDialog(true);
-    } else {
+    if (tempCount === 0) {
       toast.error("업로드된 Template File이 없습니다.");
+      return;
     }
+
+    // 선택된 항목이 있으면 선택된 것만, 없으면 전체 사용
+    const selectedData = getSelectedRowsData();
+    if (selectedData.length > 0) {
+      toast.info(`선택된 ${selectedData.length}개 항목으로 배치 문서를 생성합니다.`);
+    } else {
+      toast.info(`전체 ${tableData.length}개 항목으로 배치 문서를 생성합니다.`);
+    }
+    
+    setBatchDownDialog(true);
   };
 
   return (
@@ -534,6 +566,15 @@ export default function DynamicTable({
         columns={columns}
         advancedFilterFields={advancedFilterFields}
       >
+        {/* 선택된 항목 수 표시 (선택된 항목이 있을 때만) */}
+        {selectedRowCount > 0 && (
+          <div className="mb-4 p-3 bg-blue-50 border border-blue-200 rounded-md">
+            <p className="text-sm text-blue-700">
+              {selectedRowCount}개 항목이 선택되었습니다. 배치 문서는 선택된 항목만으로 생성됩니다.
+            </p>
+          </div>
+        )}
+
         {/* 버튼 그룹 */}
         <div className="flex items-center gap-2">
           {/* 태그 관리 드롭다운 */}
@@ -583,6 +624,11 @@ export default function DynamicTable({
               <DropdownMenuItem onClick={handleBatchDocument} disabled={isAnyOperationPending}>
                 <FileOutput className="mr-2 h-4 w-4" />
                 Batch Document
+                {selectedRowCount > 0 && (
+                  <span className="ml-2 text-xs bg-blue-100 text-blue-700 px-2 py-1 rounded">
+                    {selectedRowCount}
+                  </span>
+                )}
               </DropdownMenuItem>
             </DropdownMenuContent>
           </DropdownMenu>
@@ -748,7 +794,7 @@ export default function DynamicTable({
           open={batchDownDialog}
           setOpen={setBatchDownDialog}
           columnsJSON={columnsJSON}
-          reportData={tableData}
+          reportData={selectedRowCount > 0 ? getSelectedRowsData() : tableData}
           packageId={contractItemId}
           formCode={formCode}
           formId={formId}
-- 
cgit v1.2.3

