summaryrefslogtreecommitdiff
path: root/components/form-data-plant/form-data-table.tsx
blob: c6c79a69b8dc4f862d3ad7cbfc2c8ad781fb6a82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
"use client";

import * as React from "react";
import { useParams, useRouter, usePathname } from "next/navigation";
import { useTranslation } from "@/i18n/client";

import { ClientDataTable } from "../client-data-table/data-table";
import {
  getColumns,
  DataTableRowAction,
  DataTableColumnJSON,
  ColumnType,
  Register,
} from "./form-data-table-columns";
import type { DataTableAdvancedFilterField } from "@/types/table";
import { Button } from "../ui/button";
import {
  Download,
  Loader,
  Upload,
  Plus,
  Tag,
  TagsIcon,
  FileOutput,
  Clipboard,
  Send,
  GitCompareIcon,
  RefreshCcw,
  Trash2,
  Eye,
  FileText,
  Target,
  CheckCircle2,
  AlertCircle,
  Clock
} from "lucide-react";
import { toast } from "sonner";
import {
  getPackageCodeById,
  getProjectById,
  getReportTempList,
  sendFormDataToSEDP,
  syncMissingTags, excludeFormDataByTags, getRegisters 
} from "@/lib/forms-plant/services";
import { UpdateTagSheet } from "./update-form-sheet";
import { FormDataReportTempUploadDialog } from "./form-data-report-temp-upload-dialog";
import { FormDataReportDialog } from "./form-data-report-dialog";
import { FormDataReportBatchDialog } from "./form-data-report-batch-dialog";
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import { AddFormTagDialog } from "./add-formTag-dialog";
import { importExcelData } from "./import-excel-form";
import { exportExcelData } from "./export-excel-form";
import { SEDPConfirmationDialog, SEDPStatusDialog } from "./sedp-components";
import { SEDPCompareDialog } from "./sedp-compare-dialog";
import { DeleteFormDataDialog } from "./delete-form-data-dialog";
import { TemplateViewDialog } from "./spreadJS-dialog";
import { fetchTemplateFromSEDP } from "@/lib/forms-plant/sedp-actions";
import { FormStatusByVendor, getFormStatusByVendor } from "@/lib/forms-plant/stat";
import {
  Card,
  CardContent,
  CardHeader,
  CardTitle
} from "@/components/ui/card";
import { XCircle } from "lucide-react"; // 기존 import 리스트에 추가

interface GenericData {
  [key: string]: unknown;
}

export interface DynamicTableProps {
  dataJSON: GenericData[];
  columnsJSON: DataTableColumnJSON[];
  projectCode: string;
  packageCode: string;
  formCode: string;
  formId: number;
  projectId: number;
  formName?: string;
  objectCode?: string;
  mode: "IM" | "ENG"; // 모드 속성
  editableFieldsMap?: Map<string, string[]>; // 새로 추가
}

export default function DynamicTable({
  dataJSON,
  columnsJSON,
  projectCode,
  packageCode,
  formCode,
  formId,
  projectId,
  mode = "IM", // 기본값 설정
  formName = `${formCode}`, // Default form name based on formCode
  editableFieldsMap = new Map(), // 새로 추가
}: DynamicTableProps) {
  const params = useParams();
  const router = useRouter();
  const lng = (params?.lng as string) || "ko";
  const { t } = useTranslation(lng, "engineering");


  const [rowAction, setRowAction] =
    React.useState<DataTableRowAction<GenericData> | null>(null);
  const [tableData, setTableData] = React.useState<GenericData[]>(dataJSON);

  // 배치 선택 관련 상태
  const [selectedRowsData, setSelectedRowsData] = React.useState<GenericData[]>([]);
  const [clearSelection, setClearSelection] = React.useState(false);
  // 삭제 관련 상태 간소화
  const [deleteDialogOpen, setDeleteDialogOpen] = React.useState(false);
  const [deleteTarget, setDeleteTarget] = React.useState<GenericData[]>([]);

  const [formStats, setFormStats] = React.useState<FormStatusByVendor | null>(null);
  const [isLoadingStats, setIsLoadingStats] = React.useState(true);

  const [activeFilter, setActiveFilter] = React.useState<string | null>(null);
  const [filteredTableData, setFilteredTableData] = React.useState<GenericData[]>(tableData);
  const [rowSelection, setRowSelection] = React.useState<Record<string, boolean>>({});

  const [isExcludingTags, setIsExcludingTags] = React.useState(false);

  const handleExcludeTags = async () => {
    const selectedRows = getSelectedRowsData();

    if (selectedRows.length === 0) {
      toast.error(t("messages.noTagsSelected"));
      return;
    }

    // 확인 다이얼로그
    const confirmMessage = t("messages.confirmExclude", {
      count: selectedRows.length
    }) || `선택한 ${selectedRows.length}개의 태그를 제외 처리하시겠습니까?`;

    if (!confirm(confirmMessage)) {
      return;
    }

    setIsExcludingTags(true);

    try {
      // TAG_NO 목록 추출
      const tagNumbers = selectedRows
        .map(row => row.TAG_NO)
        .filter(tagNo => tagNo !== null && tagNo !== undefined);

      if (tagNumbers.length === 0) {
        toast.error(t("messages.noValidTags"));
        return;
      }

      // 서버 액션 호출
      const result = await excludeFormDataByTags({
        formCode,
        projectCode,
        packageCode,
        tagNumbers,
      });

      if (result.success) {
        toast.success(
          t("messages.tagsExcluded", { count: result.excludedCount }) ||
          `${result.excludedCount}개의 태그가 제외되었습니다.`
        );

        // 로컬 상태 업데이트
        setTableData(prev =>
          prev.map(item => {
            if (tagNumbers.includes(item.TAG_NO)) {
              return {
                ...item,
                status: 'excluded',
                excludedAt: new Date().toISOString()
              };
            }
            return item;
          })
        );

        // 선택 상태 초기화
        setClearSelection(true);
        setTimeout(() => setClearSelection(false), 100);
      } else {
        toast.error(result.error || t("messages.excludeFailed"));
      }
    } catch (error) {
      console.error("Error excluding tags:", error);
      toast.error(t("messages.excludeError") || "태그 제외 중 오류가 발생했습니다.");
    } finally {
      setIsExcludingTags(false);
    }
  };

  // 필터링 로직
  React.useEffect(() => {
    if (!activeFilter) {
      setFilteredTableData(tableData);
      return;
    }

    const today = new Date();
    today.setHours(0, 0, 0, 0);
    const sevenDaysLater = new Date(today);
    sevenDaysLater.setDate(sevenDaysLater.getDate() + 7);

    let filtered = [...tableData];

    switch (activeFilter) {
      case 'completed':
        // 모든 필수 필드가 완료된 태그만 표시
        filtered = tableData.filter(item => {
          const tagEditableFields = editableFieldsMap.get(item.TAG_NO) || [];
          return columnsJSON
            .filter(col => (col.shi === 'IN' || col.shi === 'BOTH') && tagEditableFields.includes(col.key))
            .every(col => {
              const value = item[col.key];
              return value !== undefined && value !== null && value !== '';
            });
        });
        break;

      case 'remaining':
        // 미완료 필드가 있는 태그만 표시
        filtered = tableData.filter(item => {
          const tagEditableFields = editableFieldsMap.get(item.TAG_NO) || [];
          return columnsJSON
            .filter(col => (col.shi === 'IN' || col.shi === 'BOTH') && tagEditableFields.includes(col.key))
            .some(col => {
              const value = item[col.key];
              return value === undefined || value === null || value === '';
            });
        });
        break;

      case 'upcoming':
        // 7일 이내 임박한 태그만 표시
        filtered = tableData.filter(item => {
          const dueDate = item.DUE_DATE;
          if (!dueDate) return false;

          const target = new Date(dueDate);
          target.setHours(0, 0, 0, 0);

          // 미완료이면서 7일 이내인 경우
          const hasIncompleteFields = columnsJSON
            .filter(col => col.shi === 'IN' || col.shi === 'BOTH')
            .some(col => !item[col.key]);

          return hasIncompleteFields && target >= today && target <= sevenDaysLater;
        });
        break;

      case 'overdue':
        // 지연된 태그만 표시
        filtered = tableData.filter(item => {
          const dueDate = item.DUE_DATE;
          if (!dueDate) return false;

          const target = new Date(dueDate);
          target.setHours(0, 0, 0, 0);

          // 미완료이면서 지연된 경우
          const hasIncompleteFields = columnsJSON
            .filter(col => col.shi === 'IN' || col.shi === 'BOTH')
            .some(col => !item[col.key]);

          return hasIncompleteFields && target < today;
        });
        break;

      default:
        filtered = tableData;
    }

    setFilteredTableData(filtered);
  }, [activeFilter, tableData, columnsJSON, editableFieldsMap]);

  // 카드 클릭 핸들러
  const handleCardClick = (filterType: string | null) => {
    setActiveFilter(prev => prev === filterType ? null : filterType);
  };

  React.useEffect(() => {
    const fetchFormStats = async () => {
      try {
        setIsLoadingStats(true);
        // getFormStatusByVendor 서버 액션 직접 호출
        const data = await getFormStatusByVendor(projectId, projectCode, packageCode,formCode);

        if (data && data.length > 0) {
          setFormStats(data[0]);
        }
      } catch (error) {
        console.error("Failed to fetch form stats:", error);
        toast.error("통계 데이터를 불러오는데 실패했습니다.");
      } finally {
        setIsLoadingStats(false);
      }
    };

    if (projectId && formCode) {
      fetchFormStats();
    }
  }, [projectId, formCode]);

  // Update tableData when dataJSON changes
  React.useEffect(() => {
    setTableData(dataJSON);
  }, [dataJSON]);

  // 폴링 상태 관리를 위한 ref
  const pollingRef = React.useRef<NodeJS.Timeout | null>(null);

  // Separate loading states for different operations
  const [isSyncingTags, setIsSyncingTags] = React.useState(false);
  const [isImporting, setIsImporting] = React.useState(false);
  const [isExporting, setIsExporting] = React.useState(false);
  const [isSaving] = 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 || isLoadingTemplate || isExcludingTags;

  // SEDP dialogs state
  const [sedpConfirmOpen, setSedpConfirmOpen] = React.useState(false);
  const [sedpStatusOpen, setSedpStatusOpen] = React.useState(false);
  const [sedpStatusData, setSedpStatusData] = React.useState({
    status: 'success' as 'success' | 'error' | 'partial',
    message: '',
    successCount: 0,
    errorCount: 0,
    totalCount: 0
  });

  // SEDP compare dialog state 
  const [sedpCompareOpen, setSedpCompareOpen] = React.useState(false);
  const projectType = "plant";

  // 새로 추가된 Template 다이얼로그 상태
  const [templateDialogOpen, setTemplateDialogOpen] = React.useState(false);
  const [templateData, setTemplateData] = React.useState<unknown>(null);

  const [tempUpDialog, setTempUpDialog] = React.useState(false);
  const [reportData, setReportData] = React.useState<GenericData[]>([]);
  const [batchDownDialog, setBatchDownDialog] = React.useState(false);
  const [tempCount, setTempCount] = React.useState(0);
  const [addTagDialogOpen, setAddTagDialogOpen] = React.useState(false);

  const [registers, setRegisters] = React.useState<Register[]>([]);
const [isLoadingRegisters, setIsLoadingRegisters] = React.useState(false);


  // TAG_NO가 있는 첫 번째 행의 shi 값 확인
  const isAddTagDisabled = React.useMemo(() => {
    const firstRowWithTagNo = tableData.find(row => row.TAG_NO);
    return firstRowWithTagNo?.shi === true;
  }, [tableData]);

  // Clean up polling on unmount
  React.useEffect(() => {
    return () => {
      if (pollingRef.current) {
        clearInterval(pollingRef.current);
      }
    };
  }, []);

  React.useEffect(() => {
    const getTempCount = async () => {
      const tempList = await getReportTempList(projectCode, packageCode, formId);
      setTempCount(tempList.length);
    };

    getTempCount();
  }, [projectCode,packageCode, formId, tempUpDialog]);


  // 선택된 행들의 실제 데이터 가져오기
  const getSelectedRowsData = React.useCallback(() => {
    return selectedRowsData;
  }, [selectedRowsData]);

  // 선택된 행 개수 계산
  const selectedRowCount = React.useMemo(() => {
    return selectedRowsData.length;
  }, [selectedRowsData]);

  // 프로젝트 코드를 가져오는 useEffect (기존 코드 참고)
React.useEffect(() => {
  const fetchRegisters = async () => {
    if (!projectCode) return; // projectCode가 있는지 확인
    
    setIsLoadingRegisters(true);
    try {
      const registersData = await getRegisters(projectCode);
      setRegisters(registersData);
      console.log('✅ Registers loaded:', registersData.length);
    } catch (error) {
      console.error('❌ Failed to load registers:', error);
      toast.error('레지스터 정보를 불러오는데 실패했습니다.');
    } finally {
      setIsLoadingRegisters(false);
    }
  };

  fetchRegisters();
}, [projectCode]);


  const columns = React.useMemo(
    () =>
      getColumns({
        columnsJSON,
        setRowAction,
        setReportData,
        tempCount,
        onRowSelectionChange: setRowSelection,
        templateData, // 기존
        registers, // 새로 추가
      }),
    [columnsJSON, setRowAction, setReportData, tempCount,  templateData, registers]
  );

  function mapColumnTypeToAdvancedFilterType(
    columnType: ColumnType
  ): DataTableAdvancedFilterField<GenericData>["type"] {
    switch (columnType) {
      case "STRING":
        return "text";
      case "NUMBER":
        return "number";
      case "LIST":
        return "select";
      default:
        return "text";
    }
  }

  const advancedFilterFields = React.useMemo<
    DataTableAdvancedFilterField<GenericData>[]
  >(() => {
    return columnsJSON.map((col) => ({
      id: col.key,
      label: col.label,
      type: mapColumnTypeToAdvancedFilterType(col.type),
      options:
        col.type === "LIST"
          ? col.options?.map((v) => ({ label: v, value: v }))
          : undefined,
    }));
  }, [columnsJSON]);

  // 새로 추가된 Template 가져오기 함수
  const handleGetTemplate = async () => {

    if (!projectCode) {
      toast.error("Project code is not available");
      return;
    }

    try {
      setIsLoadingTemplate(true);

      const templateResult = await fetchTemplateFromSEDP(projectCode, formCode);

      // 🔍 전달되는 템플릿 데이터 로깅
      console.log('📊 Template data received from SEDP:', {
        count: Array.isArray(templateResult) ? templateResult.length : 'not array',
        isArray: Array.isArray(templateResult),
        data: templateResult
      });

      if (Array.isArray(templateResult)) {
        templateResult.forEach((tmpl, idx) => {
          console.log(`  [${idx}] TMPL_ID: ${tmpl?.TMPL_ID || 'MISSING'}, NAME: ${tmpl?.NAME || 'N/A'}, TYPE: ${tmpl?.TMPL_TYPE || 'N/A'}`);
        });
      }

      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 {
      setIsSyncingTags(true);
      const result = await syncMissingTags(projectCode,packageCode, formCode);

      // Prepare the toast messages based on what changed
      const changes = [];
      if (result.createdCount > 0)
        changes.push(`${result.createdCount}건 태그 생성`);
      if (result.updatedCount > 0)
        changes.push(`${result.updatedCount}건 태그 업데이트`);
      if (result.deletedCount > 0)
        changes.push(`${result.deletedCount}건 태그 삭제`);

      if (changes.length > 0) {
        // If any changes were made, show success message and reload
        toast.success(`동기화 완료: ${changes.join(", ")}`);
        router.refresh(); // Use router.refresh instead of location.reload
      } else {
        // If no changes were made, show an info message
        toast.info("변경사항이 없습니다. 모든 태그가 최신 상태입니다.");
      }
    } catch (err) {
      console.error(err);
      toast.error("태그 동기화 중 에러가 발생했습니다.");
    } finally {
      setIsSyncingTags(false);
    }
  }

  // ENG 모드: 태그 가져오기 함수
  const handleGetTags = async () => {
    try {
      setIsLoadingTags(true);

      // API 엔드포인트 호출 - 작업 시작만 요청
      const response = await fetch('/api/cron/form-tags-plant/start', {
        method: 'POST',
        body: JSON.stringify({ projectCode, formCode, packageCode })
      });

      if (!response.ok) {
        const errorData = await response.json();
        throw new Error(errorData.error || 'Failed to start tag import');
      }

      const data = await response.json();

      // 작업 ID 저장
      if (data.syncId) {
        toast.info('Tag import started. This may take a while...');

        // 상태 확인을 위한 폴링 시작
        startPolling(data.syncId);
      } else {
        throw new Error('No import ID returned from server');
      }
    } catch (error) {
      console.error('Error starting tag import:', error);
      toast.error(
        error instanceof Error
          ? error.message
          : 'An error occurred while starting tag import'
      );
      setIsLoadingTags(false);
    }
  };

  const startPolling = (id: string) => {
    // 이전 폴링이 있다면 제거
    if (pollingRef.current) {
      clearInterval(pollingRef.current);
    }

    // 5초마다 상태 확인
    pollingRef.current = setInterval(async () => {
      try {
        const response = await fetch(`/api/cron/form-tags-plant/status?id=${id}`);

        if (!response.ok) {
          throw new Error('Failed to get tag import status');
        }

        const data = await response.json();

        if (data.status === 'completed') {
          // 폴링 중지
          if (pollingRef.current) {
            clearInterval(pollingRef.current);
            pollingRef.current = null;
          }

          router.refresh();

          // 상태 초기화
          setIsLoadingTags(false);

          // 성공 메시지 표시
          toast.success(
            `Tags imported successfully! ${data.result?.processedCount || 0} items processed.`
          );

        } else if (data.status === 'failed') {
          // 에러 처리
          if (pollingRef.current) {
            clearInterval(pollingRef.current);
            pollingRef.current = null;
          }

          setIsLoadingTags(false);
          toast.error(data.error || 'Import failed');
        } else if (data.status === 'processing') {
          // 진행 상태 업데이트 (선택적)
          if (data.progress) {
            toast.info(`Import in progress: ${data.progress}%`, {
              id: `import-progress-${id}`,
            });
          }
        }
      } catch (error) {
        console.error('Error checking importing status:', error);
      }
    }, 5000); // 5초마다 체크
  };

  // Excel Import - Fixed version with proper loading state management
  async function handleImportExcel(e: React.ChangeEvent<HTMLInputElement>) {
    const file = e.target.files?.[0];
    if (!file) return;

    try {
      // Don't set setIsImporting here - let importExcelData handle it completely
      // setIsImporting(true); // Remove this line

      // Call the updated importExcelData function with editableFieldsMap
      const result = await importExcelData({
        file,
        tableData,
        columnsJSON,
        formCode,
        projectCode,
        packageCode,
        editableFieldsMap, // 추가: 편집 가능 필드 정보 전달
        onPendingChange: setIsImporting, // Let importExcelData handle loading state
        onDataUpdate: (newData) => {
          setTableData(Array.isArray(newData) ? newData : newData(tableData));
        }
      });

      // If import and save was successful, refresh the page
      if (result.success) {
        // Show additional info about skipped fields if any
        if (result.skippedFields && result.skippedFields.length > 0) {
          console.log("Import completed with some fields skipped:", result.skippedFields);
        }

        // Ensure loading state is cleared before refresh
        setIsImporting(false);

        // Add a small delay to ensure state update is processed
        setTimeout(() => {
          router.refresh();
        }, 100);
      }
    } catch (error) {
      console.error("Import failed:", error);
      toast.error("Failed to import Excel data");
      // Ensure loading state is cleared on error
      setIsImporting(false);
    } finally {
      // Always clear the file input value
      e.target.value = "";
      // Don't set setIsImporting(false) here since we handle it above
    }
  }
  // SEDP Send handler (with confirmation)
  function handleSEDPSendClick() {
    if (tableData.length === 0) {
      toast.error("No data to send to SEDP");
      return;
    }

    // Open confirmation dialog
    setSedpConfirmOpen(true);
  }

  // Handle SEDP compare button click
  function handleSEDPCompareClick() {
    if (tableData.length === 0) {
      toast.error("No data to compare with SEDP");
      return;
    }

    if (!projectCode) {
      toast.error("Project code is not available");
      return;
    }

    // Open compare dialog
    setSedpCompareOpen(true);
  }

  // Actual SEDP send after confirmation
  async function handleSEDPSendConfirmed() {
    try {
      setIsSendingSEDP(true);

      // Validate data
      const invalidData = tableData.filter((item) => {
        const tagNo = item.TAG_NO;
        return !tagNo || (typeof tagNo === 'string' && !tagNo.trim());
      });
      if (invalidData.length > 0) {
        toast.error(`태그 번호가 없는 항목이 ${invalidData.length}개 있습니다.`);
        setSedpConfirmOpen(false);
        return;
      }

      // Then send to SEDP - pass formCode instead of formName
      const sedpResult = await sendFormDataToSEDP(
        formCode,        // Send formCode instead of formName
        projectId,       // Project ID
        projectCode,
        packageCode,
        tableData.filter(v=>v.status !== 'excluded'),       // Table data
        columnsJSON      // Column definitions
      );

      // Close confirmation dialog
      setSedpConfirmOpen(false);

      // Set status data based on result
      if (sedpResult.success) {
        setSedpStatusData({
          status: 'success',
          message: "Data successfully sent to SEDP",
          successCount: tableData.length,
          errorCount: 0,
          totalCount: tableData.length
        });
      } else {
        setSedpStatusData({
          status: 'error',
          message: sedpResult.message || "Failed to send data to SEDP",
          successCount: 0,
          errorCount: tableData.length,
          totalCount: tableData.length
        });
      }

      // Open status dialog to show result
      setSedpStatusOpen(true);

      // Refresh the route to get fresh data
      router.refresh();

    } catch (err: unknown) {
      console.error("SEDP error:", err);

      // Set error status
      setSedpStatusData({
        status: 'error',
        message: err instanceof Error ? err.message : "An unexpected error occurred",
        successCount: 0,
        errorCount: tableData.length,
        totalCount: tableData.length
      });

      // Close confirmation and open status
      setSedpConfirmOpen(false);
      setSedpStatusOpen(true);

    } finally {
      setIsSendingSEDP(false);
    }
  }

  // Template Export
  async function handleExportExcel() {
    try {
      setIsExporting(true);
      await exportExcelData({
        tableData,
        columnsJSON,
        formCode,
        editableFieldsMap,
        onPendingChange: setIsExporting
      });
    } finally {
      setIsExporting(false);
    }
  }

  // Handle batch document with smart selection logic
  const handleBatchDocument = () => {
    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);
  };

  // 개별 행 삭제 핸들러
  const handleDeleteRow = (rowData: GenericData) => {
    setDeleteTarget([rowData]);
    setDeleteDialogOpen(true);
  };

  // 배치 삭제 핸들러
  const handleBatchDelete = () => {
    const selectedData = getSelectedRowsData();
    if (selectedData.length === 0) {
      toast.error("삭제할 항목을 선택해주세요.");
      return;
    }

    setDeleteTarget(selectedData);
    setDeleteDialogOpen(true);
  };

  // 삭제 성공 후 처리
  const handleDeleteSuccess = () => {
    // 로컬 상태에서 삭제된 항목들 제거
    const tagNosToDelete = deleteTarget
      .map(item => item.TAG_NO)
      .filter(Boolean);

    setTableData(prev =>
      prev.filter(item => !tagNosToDelete.includes(item.TAG_NO))
    );

    // 선택 상태 초기화
    setSelectedRowsData([]);
    setClearSelection(prev => !prev); // ClientDataTable의 선택 상태 초기화

    // 삭제 타겟 초기화
    setDeleteTarget([]);
  };

  // rowAction 처리 부분 수정
  React.useEffect(() => {
    if (rowAction?.type === "delete") {
      handleDeleteRow(rowAction.row.original);
      setRowAction(null); // 액션 초기화
    }
  }, [rowAction]);


  return (
    <>

      <div className="mb-6">
        <div className="grid gap-4 md:grid-cols-2 lg:grid-cols-5">
          {/* Total Tags Card - 클릭 시 전체 보기 */}
          <Card
            className={`cursor-pointer transition-all ${activeFilter === null ? 'ring-2 ring-primary' : 'hover:shadow-lg'
              }`}
            onClick={() => handleCardClick(null)}
          >
            <CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
              <CardTitle className="text-sm font-medium">
                Total Tags
              </CardTitle>
              <FileText className="h-4 w-4 text-muted-foreground" />
            </CardHeader>
            <CardContent>
              <div className="text-2xl font-bold">
                {isLoadingStats ? (
                  <span className="animate-pulse">-</span>
                ) : (
                  formStats?.tagCount || 0
                )}
              </div>
              <p className="text-xs text-muted-foreground">
                {activeFilter === null ? 'Showing all' : 'Click to show all'}
              </p>
            </CardContent>
          </Card>

          {/* Completed Fields Card */}
          <Card
            className={`cursor-pointer transition-all ${activeFilter === 'completed' ? 'ring-2 ring-green-600' : 'hover:shadow-lg'
              }`}
            onClick={() => handleCardClick('completed')}
          >
            <CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
              <CardTitle className="text-sm font-medium">
                Completed
              </CardTitle>
              <CheckCircle2 className="h-4 w-4 text-green-600" />
            </CardHeader>
            <CardContent>
              <div className="text-2xl font-bold text-green-600">
                {isLoadingStats ? (
                  <span className="animate-pulse">-</span>
                ) : (
                  formStats?.completedFields || 0
                )}
              </div>
              <p className="text-xs text-muted-foreground">
                {activeFilter === 'completed' ? 'Filtering active' : 'Click to filter'}
              </p>
            </CardContent>
          </Card>

          {/* Remaining Fields Card */}
          <Card
            className={`cursor-pointer transition-all ${activeFilter === 'remaining' ? 'ring-2 ring-blue-600' : 'hover:shadow-lg'
              }`}
            onClick={() => handleCardClick('remaining')}
          >
            <CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
              <CardTitle className="text-sm font-medium">
                Remaining
              </CardTitle>
              <Clock className="h-4 w-4 text-muted-foreground" />
            </CardHeader>
            <CardContent>
              <div className="text-2xl font-bold">
                {isLoadingStats ? (
                  <span className="animate-pulse">-</span>
                ) : (
                  (formStats?.totalFields || 0) - (formStats?.completedFields || 0)
                )}
              </div>
              <p className="text-xs text-muted-foreground">
                {activeFilter === 'remaining' ? 'Filtering active' : 'Click to filter'}
              </p>
            </CardContent>
          </Card>

          {/* Upcoming Card */}
          <Card
            className={`cursor-pointer transition-all ${activeFilter === 'upcoming' ? 'ring-2 ring-yellow-600' : 'hover:shadow-lg'
              }`}
            onClick={() => handleCardClick('upcoming')}
          >
            <CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
              <CardTitle className="text-sm font-medium">
                Upcoming
              </CardTitle>
              <AlertCircle className="h-4 w-4 text-yellow-600" />
            </CardHeader>
            <CardContent>
              <div className="text-2xl font-bold text-yellow-600">
                {isLoadingStats ? (
                  <span className="animate-pulse">-</span>
                ) : (
                  formStats?.upcomingCount || 0
                )}
              </div>
              <p className="text-xs text-muted-foreground">
                {activeFilter === 'upcoming' ? 'Filtering active' : 'Click to filter'}
              </p>
            </CardContent>
          </Card>

          {/* Overdue Card */}
          <Card
            className={`cursor-pointer transition-all ${activeFilter === 'overdue' ? 'ring-2 ring-red-600' : 'hover:shadow-lg'
              }`}
            onClick={() => handleCardClick('overdue')}
          >
            <CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
              <CardTitle className="text-sm font-medium">
                Overdue
              </CardTitle>
              <AlertCircle className="h-4 w-4 text-red-600" />
            </CardHeader>
            <CardContent>
              <div className="text-2xl font-bold text-red-600">
                {isLoadingStats ? (
                  <span className="animate-pulse">-</span>
                ) : (
                  formStats?.overdueCount || 0
                )}
              </div>
              <p className="text-xs text-muted-foreground">
                {activeFilter === 'overdue' ? 'Filtering active' : 'Click to filter'}
              </p>
            </CardContent>
          </Card>
        </div>
      </div>


      <ClientDataTable
        data={filteredTableData}  // tableData 대신 filteredTableData 사용
        columns={columns}
        advancedFilterFields={advancedFilterFields}
        autoSizeColumns
        onSelectedRowsChange={setSelectedRowsData}
        clearSelection={clearSelection}
      >
        {/* 필터 상태 표시 */}
        {activeFilter && (
          <div className="flex items-center gap-2 mr-auto">
            <span className="text-sm text-muted-foreground">
              Filter: {activeFilter === 'completed' ? 'Completed' :
                activeFilter === 'remaining' ? 'Remaining' :
                  activeFilter === 'upcoming' ? 'Upcoming (7 days)' :
                    activeFilter === 'overdue' ? 'Overdue' : 'All'}
            </span>
            <Button
              variant="ghost"
              size="sm"
              onClick={() => setActiveFilter(null)}
            >
              Clear filter
            </Button>
          </div>
        )}
        {/* 선택된 항목 수 표시 (선택된 항목이 있을 때만) */}
        {selectedRowCount > 0 && (
          <Button
            variant="destructive"
            size="sm"
            onClick={handleBatchDelete}
          >
            <Trash2 className="mr-2 size-4" />
            {t("buttons.delete")} ({selectedRowCount})
          </Button>
        )}

        {/* 버튼 그룹 */}
        <div className="flex items-center gap-2">

          {selectedRowCount > 0 && (
            <Button
              variant="outline"
              size="sm"
              onClick={handleExcludeTags}
              disabled={isAnyOperationPending}
              className="border-orange-500 text-orange-600 hover:bg-orange-50"
            >
              {isExcludingTags ? (
                <Loader className="mr-2 size-4 animate-spin" />
              ) : (
                <XCircle className="mr-2 size-4" />
              )}
              {t("buttons.excludeTags")} ({selectedRowCount})
            </Button>
          )}
          {/* 태그 관리 드롭다운 */}
          <DropdownMenu>
            <DropdownMenuTrigger asChild>
              <Button variant="outline" size="sm" disabled={isAnyOperationPending}>
                {(isSyncingTags || isLoadingTags) ? (
                  <Loader className="mr-2 size-4 animate-spin" aria-hidden="true" />
                ) :
                  <TagsIcon className="size-4" />}
                {t("buttons.tagOperations")}
              </Button>
            </DropdownMenuTrigger>
            <DropdownMenuContent align="end">
              {/* 모드에 따라 다른 태그 작업 표시 */}
              {mode === "IM" ? (
                <DropdownMenuItem onClick={handleSyncTags} disabled={isAnyOperationPending}>
                  <Tag className="mr-2 h-4 w-4" />
                  {t("buttons.syncTags")}
                </DropdownMenuItem>
              ) : (
                <DropdownMenuItem onClick={handleGetTags} disabled={isAnyOperationPending}>
                  <RefreshCcw className="mr-2 h-4 w-4" />
                  {t("buttons.getTags")}
                </DropdownMenuItem>
              )}
              <DropdownMenuItem
                onClick={() => setAddTagDialogOpen(true)}
                disabled={isAnyOperationPending || isAddTagDisabled}
              >
                <Plus className="mr-2 h-4 w-4" />
                {t("buttons.addTags")}
              </DropdownMenuItem>
            </DropdownMenuContent>
          </DropdownMenu>

          {/* 리포트 관리 드롭다운 */}
          <DropdownMenu>
            <DropdownMenuTrigger asChild>
              <Button variant="outline" size="sm" disabled={isAnyOperationPending}>
                <Clipboard className="size-4" />
                {t("buttons.reportOperations")}
              </Button>
            </DropdownMenuTrigger>
            <DropdownMenuContent align="end">
              <DropdownMenuItem onClick={() => setTempUpDialog(true)} disabled={isAnyOperationPending}>
                <Upload className="mr-2 h-4 w-4" />
                {t("buttons.uploadTemplate")}
              </DropdownMenuItem>
              <DropdownMenuItem onClick={handleBatchDocument} disabled={isAnyOperationPending}>
                <FileOutput className="mr-2 h-4 w-4" />
                {t("buttons.batchDocument")}
                {selectedRowCount > 0 && (
                  <span className="ml-2 text-xs bg-blue-100 text-blue-700 px-2 py-1 rounded">
                    {selectedRowCount}
                  </span>
                )}
              </DropdownMenuItem>
            </DropdownMenuContent>
          </DropdownMenu>

          {/* IMPORT 버튼 (파일 선택) */}
          <Button asChild variant="outline" size="sm" disabled={isAnyOperationPending}>
            <label>
              {isImporting ? (
                <Loader className="mr-2 size-4 animate-spin" aria-hidden="true" />
              ) : (
                <Upload className="size-4" />
              )}
              {t("buttons.import")}
              <input
                type="file"
                accept=".xlsx,.xls"
                onChange={handleImportExcel}
                style={{ display: "none" }}
                disabled={isAnyOperationPending}
              />
            </label>
          </Button>

          {/* EXPORT 버튼 */}
          <Button
            variant="outline"
            size="sm"
            onClick={handleExportExcel}
            disabled={isAnyOperationPending}
          >
            {isExporting ? (
              <Loader className="mr-2 size-4 animate-spin" />
            ) : (
              <Download className="mr-2 size-4" />
            )}
            {t("buttons.export")}
          </Button>

          {/* Template 보기 버튼 */}
          <Button
            variant="outline"
            size="sm"
            onClick={handleGetTemplate}
            disabled={isAnyOperationPending}
          >
            {isLoadingTemplate ? (
              <Loader className="mr-2 size-4 animate-spin" />
            ) : (
              <Eye className="mr-2 size-4" />
            )}
            {t("buttons.viewTemplate")}
          </Button>

          {/* COMPARE WITH SEDP 버튼 */}
          <Button
            variant="outline"
            size="sm"
            onClick={handleSEDPCompareClick}
            disabled={isAnyOperationPending}
          >
            <GitCompareIcon className="mr-2 size-4" />
            {t("buttons.compareWithSEDP")}
          </Button>

          {/* SEDP 전송 버튼 */}
          <Button
            variant="samsung"
            size="sm"
            onClick={handleSEDPSendClick}
            disabled={isAnyOperationPending}
          >
            {isSendingSEDP ? (
              <>
                <Loader className="mr-2 size-4 animate-spin" />
                {t("messages.sendingSEDP")}
              </>
            ) : (
              <>
                <Send className="size-4" />
                {t("buttons.sendToSHI")}
              </>
            )}
          </Button>
        </div>
      </ClientDataTable>

      {/* Modal dialog for tag update */}
      <UpdateTagSheet
        open={rowAction?.type === "update"}
        onOpenChange={(open) => {
          if (!open) setRowAction(null);
        }}
        columns={columnsJSON}
        rowData={rowAction?.row.original ?? null}
        formCode={formCode}
        projectCode={projectCode}
        packageCode={packageCode}
        editableFieldsMap={editableFieldsMap}
        onUpdateSuccess={(updatedValues) => {
          // Update the specific row in tableData when a single row is updated
          if (rowAction?.row.original?.TAG_NO) {
            const tagNo = rowAction.row.original.TAG_NO;
            setTableData(prev =>
              prev.map(item =>
                item.TAG_NO === tagNo ? updatedValues : item
              )
            );
          }
        }}
      />

      <DeleteFormDataDialog
        formData={deleteTarget}
        formCode={formCode}
        projectCode={projectCode}
        packageCode={packageCode}
        projectId={projectId}
        open={deleteDialogOpen}
        onOpenChange={(open) => {
          if (!open) {
            setDeleteDialogOpen(false);
            setDeleteTarget([]);
          }
        }}
        onSuccess={handleDeleteSuccess}
        showTrigger={false}
      />


      {/* 새로 추가된 Template 다이얼로그 */}
      <TemplateViewDialog
        isOpen={templateDialogOpen}
        onClose={() => setTemplateDialogOpen(false)}
        templateData={templateData}
        selectedRow={selectedRowsData[0]} // SPR_ITM_LST_SETUP용
        tableData={tableData} // SPR_LST_SETUP용 - 새로 추가
        formCode={formCode}
        projectCode={projectCode}
        packageCode={packageCode}
        editableFieldsMap={editableFieldsMap}
        columnsJSON={columnsJSON}
        onUpdateSuccess={(updatedValues) => {
          // 업데이트 로직도 수정해야 함 - 단일 행 또는 복수 행 처리
          if (Array.isArray(updatedValues)) {
            // SPR_LST_SETUP의 경우 - 복수 행 업데이트
            const updatedData = [...tableData];
            updatedValues.forEach(updatedItem => {
              const index = updatedData.findIndex(item => item.TAG_NO === updatedItem.TAG_NO);
              if (index !== -1) {
                updatedData[index] = updatedItem;
              }
            });
            setTableData(updatedData);
          } else {
            // SPR_ITM_LST_SETUP의 경우 - 단일 행 업데이트
            const tagNo = updatedValues.TAG_NO;
            if (tagNo) {
              setTableData(prev =>
                prev.map(item =>
                  item.TAG_NO === tagNo ? updatedValues : item
                )
              );
            }
          }
        }}
      />

      {/* SEDP Confirmation Dialog */}
      <SEDPConfirmationDialog
        isOpen={sedpConfirmOpen}
        onClose={() => setSedpConfirmOpen(false)}
        onConfirm={handleSEDPSendConfirmed}
        formName={formName}
        tagCount={tableData.filter(v=>v.status !=='excluded').length}
        isLoading={isSendingSEDP}
      />

      {/* SEDP Status Dialog */}
      <SEDPStatusDialog
        isOpen={sedpStatusOpen}
        onClose={() => setSedpStatusOpen(false)}
        status={sedpStatusData.status}
        message={sedpStatusData.message}
        successCount={sedpStatusData.successCount}
        errorCount={sedpStatusData.errorCount}
        totalCount={sedpStatusData.totalCount}
      />

      {/* SEDP Compare Dialog */}
      <SEDPCompareDialog
        isOpen={sedpCompareOpen}
        onClose={() => setSedpCompareOpen(false)}
        tableData={tableData}
        columnsJSON={columnsJSON}
        projectCode={projectCode}
        formCode={formCode}
        projectType={projectType}
        packageCode={packageCode}
      />

      {/* Other dialogs */}
      {tempUpDialog && (
        <FormDataReportTempUploadDialog
          columnsJSON={columnsJSON}
          open={tempUpDialog}
          setOpen={setTempUpDialog}
          projectCode={projectCode}
          packageCode={packageCode}
          formCode={formCode}
          formId={formId}
          uploaderType="vendor"
        />
      )}

      {reportData.length > 0 && (
        <FormDataReportDialog
          columnsJSON={columnsJSON}
          reportData={reportData}
          setReportData={setReportData}
          projectCode={projectCode}
          packageCode={packageCode}
          formCode={formCode}
          formId={formId}
        />
      )}

      {batchDownDialog && (
        <FormDataReportBatchDialog
          open={batchDownDialog}
          setOpen={setBatchDownDialog}
          columnsJSON={columnsJSON}
          reportData={selectedRowCount > 0 ? getSelectedRowsData() : tableData}
          projectCode={projectCode}
          packageCode={packageCode}
          formCode={formCode}
          formId={formId}
        />
      )}
    </>
  );
}