summaryrefslogtreecommitdiff
path: root/components/form-data/spreadJS-dialog copy 3.tsx
blob: 1ea8232beca1fbbe01771b3f9fd4046515afbcc5 (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
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
"use client";

import * as React from "react";
import dynamic from "next/dynamic";
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter, DialogDescription } from "@/components/ui/dialog";
import { Button } from "@/components/ui/button";
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
import { GenericData } from "./export-excel-form";
import * as GC from "@mescius/spread-sheets";
import { toast } from "sonner";
import { updateFormDataInDB } from "@/lib/forms/services";
import { Loader, Save, AlertTriangle } from "lucide-react";
import '@mescius/spread-sheets/styles/gc.spread.sheets.excel2016colorful.css';
import { DataTableColumnJSON, ColumnType } from "./form-data-table-columns";


// SpreadSheets를 동적으로 import (SSR 비활성화)
const SpreadSheets = dynamic(
  () => import("@mescius/spread-sheets-react").then(mod => mod.SpreadSheets),
  {
    ssr: false,
    loading: () => (
      <div className="flex items-center justify-center h-full">
        <Loader className="mr-2 h-4 w-4 animate-spin" />
        Loading SpreadSheets...
      </div>
    )
  }
);

// 라이센스 키 설정을 클라이언트에서만 실행
if (typeof window !== 'undefined' && process.env.NEXT_PUBLIC_SPREAD_LICENSE) {
  GC.Spread.Sheets.LicenseKey = process.env.NEXT_PUBLIC_SPREAD_LICENSE;
}

interface TemplateItem {
  TMPL_ID: string;
  NAME: string;
  TMPL_TYPE: string;
  SPR_LST_SETUP: {
    ACT_SHEET: string;
    HIDN_SHEETS: Array<string>;
    CONTENT?: string;
    DATA_SHEETS: Array<{
      SHEET_NAME: string;
      REG_TYPE_ID: string;
      MAP_CELL_ATT: Array<{
        ATT_ID: string;
        IN: string;
      }>;
    }>;
  };
  GRD_LST_SETUP: {
    REG_TYPE_ID: string;
    SPR_ITM_IDS: Array<string>;
    ATTS: Array<{}>;
  };
  SPR_ITM_LST_SETUP: {
    ACT_SHEET: string;
    HIDN_SHEETS: Array<string>;
    CONTENT?: string;
    DATA_SHEETS: Array<{
      SHEET_NAME: string;
      REG_TYPE_ID: string;
      MAP_CELL_ATT: Array<{
        ATT_ID: string;
        IN: string;
      }>;
    }>;
  };
}

interface ValidationError {
  cellAddress: string;
  attId: string;
  value: any;
  expectedType: ColumnType;
  message: string;
}

interface CellMapping {
  attId: string;
  cellAddress: string;
  isEditable: boolean;
  dataRowIndex?: number;
}

interface TemplateViewDialogProps {
  isOpen: boolean;
  onClose: () => void;
  templateData: TemplateItem[] | any;
  selectedRow?: GenericData; // SPREAD_ITEM용
  tableData?: GenericData[]; // SPREAD_LIST용
  formCode: string;
  columnsJSON: DataTableColumnJSON[]
  contractItemId: number;
  editableFieldsMap?: Map<string, string[]>;
  onUpdateSuccess?: (updatedValues: Record<string, any> | GenericData[]) => void;
}

export function TemplateViewDialog({
  isOpen,
  onClose,
  templateData,
  selectedRow,
  tableData = [],
  formCode,
  contractItemId,
  columnsJSON,
  editableFieldsMap = new Map(),
  onUpdateSuccess
}: TemplateViewDialogProps) {
  const [hostStyle, setHostStyle] = React.useState({
    width: '100%',
    height: '100%'
  });

  const [isPending, setIsPending] = React.useState(false);
  const [hasChanges, setHasChanges] = React.useState(false);
  const [currentSpread, setCurrentSpread] = React.useState<any>(null);
  const [cellMappings, setCellMappings] = React.useState<CellMapping[]>([]);
  const [isClient, setIsClient] = React.useState(false);
  const [templateType, setTemplateType] = React.useState<'SPREAD_LIST' | 'SPREAD_ITEM' | 'GRD_LIST' | null>(null);
  const [validationErrors, setValidationErrors] = React.useState<ValidationError[]>([]);
  const [selectedTemplateId, setSelectedTemplateId] = React.useState<string>("");
  const [availableTemplates, setAvailableTemplates] = React.useState<TemplateItem[]>([]);

  const determineTemplateType = React.useCallback((template: TemplateItem): 'SPREAD_LIST' | 'SPREAD_ITEM' | 'GRD_LIST' | null => {
    // 1. SPREAD_LIST: TMPL_TYPE이 SPREAD_LIST이고 SPR_LST_SETUP.CONTENT가 있음
    if (template.TMPL_TYPE === "SPREAD_LIST" && (template.SPR_LST_SETUP?.CONTENT || template.SPR_ITM_LST_SETUP?.CONTENT)) {
      return 'SPREAD_LIST';
    }

    // 2. SPREAD_ITEM: TMPL_TYPE이 SPREAD_ITEM이고 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';
    }

    // 3. GRD_LIST: GRD_LST_SETUP이 있고 columnsJSON이 있음 (동적 테이블)
    if (template.GRD_LST_SETUP && columnsJSON.length > 0) {
      return 'GRD_LIST';
    }

    return null; // 유효하지 않은 템플릿
  }, [columnsJSON]);

  const isValidTemplate = React.useCallback((template: TemplateItem): boolean => {
    return determineTemplateType(template) !== null;
  }, [determineTemplateType]);


  // 클라이언트 사이드에서만 렌더링되도록 보장
  React.useEffect(() => {
    setIsClient(true);
  }, []);

  // 사용 가능한 템플릿들을 필터링하고 설정
  React.useEffect(() => {
    if (!templateData) return;

    let templates: TemplateItem[];
    if (Array.isArray(templateData)) {
      templates = templateData as TemplateItem[];
    } else {
      templates = [templateData as TemplateItem];
    }

    // 유효한 템플릿들만 필터링
    const validTemplates = templates.filter(isValidTemplate);

    setAvailableTemplates(validTemplates);

    // 첫 번째 유효한 템플릿을 기본으로 선택
    if (validTemplates.length > 0 && !selectedTemplateId) {
      const firstTemplate = validTemplates[0];
      const templateTypeToSet = determineTemplateType(firstTemplate);

      setSelectedTemplateId(firstTemplate.TMPL_ID);
      setTemplateType(templateTypeToSet);
    }
  }, [templateData, selectedTemplateId, isValidTemplate, determineTemplateType]);


  // 선택된 템플릿 변경 처리
  const handleTemplateChange = (templateId: string) => {
    const template = availableTemplates.find(t => t.TMPL_ID === templateId);
    if (template) {
      const templateTypeToSet = determineTemplateType(template);

      setSelectedTemplateId(templateId);
      setTemplateType(templateTypeToSet);
      setHasChanges(false);
      setValidationErrors([]);

      // SpreadSheets 재초기화
      if (currentSpread && template) {
        initSpread(currentSpread, template);
      }
    }
  };
  // 현재 선택된 템플릿 가져오기
  const selectedTemplate = React.useMemo(() => {
    return availableTemplates.find(t => t.TMPL_ID === selectedTemplateId);
  }, [availableTemplates, selectedTemplateId]);


  // 편집 가능한 필드 목록 계산
  const editableFields = React.useMemo(() => {
    // SPREAD_ITEM인 경우: selectedRow의 TAG_NO로 확인
    if (templateType === 'SPREAD_ITEM' && selectedRow?.TAG_NO) {
      if (!editableFieldsMap.has(selectedRow.TAG_NO)) {
        return [];
      }
      return editableFieldsMap.get(selectedRow.TAG_NO) || [];
    }

    // SPREAD_LIST 또는 GRD_LIST인 경우: 첫 번째 행의 TAG_NO를 기준으로 처리
    if ((templateType === 'SPREAD_LIST' || templateType === 'GRD_LIST') && tableData.length > 0) {
      const firstRowTagNo = tableData[0]?.TAG_NO;
      if (firstRowTagNo && editableFieldsMap.has(firstRowTagNo)) {
        return editableFieldsMap.get(firstRowTagNo) || [];
      }
    }

    return [];
  }, [templateType, selectedRow?.TAG_NO, tableData, editableFieldsMap]);

  // 필드가 편집 가능한지 판별하는 함수
  const isFieldEditable = React.useCallback((attId: string, rowData?: GenericData) => {
    // columnsJSON에서 해당 attId의 shi 값 확인
    const columnConfig = columnsJSON.find(col => col.key === attId);
    if (columnConfig?.shi === true) {
      return false; // columnsJSON에서 shi가 true이면 편집 불가
    }

    // TAG_NO와 TAG_DESC는 기본적으로 편집 가능 (columnsJSON의 shi가 false인 경우)
    if (attId === "TAG_NO" || attId === "TAG_DESC") {
      return false;
    }

    if (attId === "status") {
      return false;
    }

    // SPREAD_ITEM인 경우: editableFields 체크
    // if (templateType === 'SPREAD_ITEM') {
    //   return editableFields.includes(attId);
    // }

    // SPREAD_LIST 또는 GRD_LIST인 경우: 개별 행의 편집 가능성도 고려
    if (templateType === 'SPREAD_LIST' || templateType === 'GRD_LIST') {
      // 기본적으로 editableFields에 포함되어야 함
      // if (!editableFields.includes(attId)) {
      //   return false;
      // }

      // rowData가 제공된 경우 해당 행의 shi 상태도 확인
      if (rowData && rowData.shi === true) {
        return false;
      }

      return true;
    }

    return true;
  }, [templateType, columnsJSON, editableFields]);

  // 편집 가능한 필드 개수 계산
  const editableFieldsCount = React.useMemo(() => {
    return cellMappings.filter(m => m.isEditable).length;
  }, [cellMappings]);

  // 셀 주소를 행과 열로 변환하는 함수
  const parseCellAddress = (address: string): { row: number, col: number } | null => {
    if (!address || address.trim() === "") return null;

    const match = address.match(/^([A-Z]+)(\d+)$/);
    if (!match) return null;

    const [, colStr, rowStr] = match;

    let col = 0;
    for (let i = 0; i < colStr.length; i++) {
      col = col * 26 + (colStr.charCodeAt(i) - 65 + 1);
    }
    col -= 1;

    const row = parseInt(rowStr) - 1;

    return { row, col };
  };

  // 행과 열을 셀 주소로 변환하는 함수 (GRD_LIST용)
  const getCellAddress = (row: number, col: number): string => {
    let colStr = '';
    let colNum = col;
    while (colNum >= 0) {
      colStr = String.fromCharCode((colNum % 26) + 65) + colStr;
      colNum = Math.floor(colNum / 26) - 1;
    }
    return colStr + (row + 1);
  };

  // 데이터 타입 검증 함수
  const validateCellValue = (value: any, columnType: ColumnType, options?: string[]): string | null => {
    if (value === undefined || value === null || value === "") {
      return null; // 빈 값은 별도 required 검증에서 처리
    }

    switch (columnType) {
      case "NUMBER":
        if (isNaN(Number(value))) {
          return "Value must be a valid number";
        }
        break;
      case "LIST":
        if (options && !options.includes(String(value))) {
          return `Value must be one of: ${options.join(", ")}`;
        }
        break;
      case "STRING":
        // STRING 타입은 대부분의 값을 허용
        break;
      default:
        // 커스텀 타입의 경우 추가 검증 로직이 필요할 수 있음
        break;
    }

    return null;
  };

  // 전체 데이터 검증 함수
  const validateAllData = React.useCallback(() => {
    if (!currentSpread || !selectedTemplate) return [];

    const activeSheet = currentSpread.getActiveSheet();
    const errors: ValidationError[] = [];

    cellMappings.forEach(mapping => {
      const columnConfig = columnsJSON.find(col => col.key === mapping.attId);
      if (!columnConfig) return;

      const cellPos = parseCellAddress(mapping.cellAddress);
      if (!cellPos) return;

      if (templateType === 'SPREAD_ITEM') {
        // 단일 행 검증
        const cellValue = activeSheet.getValue(cellPos.row, cellPos.col);
        const errorMessage = validateCellValue(cellValue, columnConfig.type, columnConfig.options);

        if (errorMessage) {
          errors.push({
            cellAddress: mapping.cellAddress,
            attId: mapping.attId,
            value: cellValue,
            expectedType: columnConfig.type,
            message: errorMessage
          });
        }
      } else if (templateType === 'SPREAD_LIST' || templateType === 'GRD_LIST') {
        // 복수 행 검증 - 각 매핑은 이미 개별 행을 가리킴
        const cellValue = activeSheet.getValue(cellPos.row, cellPos.col);
        const errorMessage = validateCellValue(cellValue, columnConfig.type, columnConfig.options);

        if (errorMessage) {
          errors.push({
            cellAddress: mapping.cellAddress,
            attId: mapping.attId,
            value: cellValue,
            expectedType: columnConfig.type,
            message: errorMessage
          });
        }
      }
    });

    setValidationErrors(errors);
    return errors;
  }, [currentSpread, selectedTemplate, cellMappings, columnsJSON, templateType]);

  // ═══════════════════════════════════════════════════════════════════════════════
  // 🛠️ 헬퍼 함수들
  // ═══════════════════════════════════════════════════════════════════════════════

  // 🎨 셀 스타일 생성
  const createCellStyle = React.useCallback((isEditable: boolean) => {
    const style = new GC.Spread.Sheets.Style();
    if (isEditable) {
      style.backColor = "#f0fdf4"; // 연한 초록 (편집 가능)
    } else {
      style.backColor = "#f9fafb"; // 연한 회색 (읽기 전용)
      style.foreColor = "#6b7280";
    }
    return style;
  }, []);

  // 🎯 드롭다운 설정
  const setupOptimizedListValidation = React.useCallback((activeSheet: any, cellPos: { row: number, col: number }, options: string[], rowCount: number) => {
    try {
      console.log(`🎯 Setting up dropdown for ${rowCount} rows with options:`, options);

      // ✅ options 정규화
      const safeOptions = options
        .filter(opt => opt !== null && opt !== undefined && opt !== '')
        .map(opt => String(opt).trim())
        .filter(opt => opt.length > 0)
        .filter((opt, index, arr) => arr.indexOf(opt) === index)
        .slice(0, 20);

      if (safeOptions.length === 0) {
        console.warn(`⚠️ No valid options found, skipping`);
        return;
      }

      console.log(`📋 Safe options:`, safeOptions);

      // ✅ DataValidation용 문자열 준비
      const optionsString = safeOptions.join(',');

      // 🔑 핵심 수정: 각 셀마다 개별 ComboBox 인스턴스 생성!
      for (let i = 0; i < rowCount; i++) {
        try {
          const targetRow = cellPos.row + i;

          // ✅ 각 셀마다 새로운 ComboBox 인스턴스 생성
          const comboBoxCellType = new GC.Spread.Sheets.CellTypes.ComboBox();
          comboBoxCellType.items(safeOptions); // 배열로 전달
          comboBoxCellType.editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.text);

          // ✅ 각 셀마다 새로운 DataValidation 인스턴스 생성
          const cellValidator = GC.Spread.Sheets.DataValidation.createListValidator(optionsString);

          // ComboBox + DataValidation 둘 다 적용
          activeSheet.setCellType(targetRow, cellPos.col, comboBoxCellType);
          activeSheet.setDataValidator(targetRow, cellPos.col, cellValidator);

          // 셀 잠금 해제
          const cell = activeSheet.getCell(targetRow, cellPos.col);
          cell.locked(false);

          console.log(`✅ Individual dropdown applied to [${targetRow}, ${cellPos.col}]`);

        } catch (cellError) {
          console.warn(`⚠️ Failed to apply to row ${cellPos.row + i}:`, cellError);
        }
      }

      console.log(`✅ Safe dropdown setup completed for ${rowCount} cells`);

    } catch (error) {
      console.error('❌ Dropdown setup failed:', error);
    }
  }, []);

  // 🛡️ 안전한 시트 검증 함수 추가
const validateActiveSheet = React.useCallback((activeSheet: any, functionName: string = 'unknown') => {
  console.log(`🔍 Validating activeSheet for ${functionName}:`);
  
  if (!activeSheet) {
    console.error(`❌ activeSheet is null/undefined in ${functionName}`);
    return false;
  }
  
  console.log(`✅ activeSheet exists (type: ${typeof activeSheet})`);
  console.log(`✅ constructor: ${activeSheet.constructor?.name}`);
  
  // 핵심 메서드들 존재 여부 확인
  const requiredMethods = ['getRowCount', 'getColumnCount', 'setRowCount', 'setColumnCount', 'getCell', 'getValue', 'setStyle'];
  const missingMethods = requiredMethods.filter(method => typeof activeSheet[method] !== 'function');
  
  if (missingMethods.length > 0) {
    console.error(`❌ Missing methods in ${functionName}:`, missingMethods);
    console.log(`📋 Available methods:`, Object.getOwnPropertyNames(activeSheet).filter(prop => typeof activeSheet[prop] === 'function').slice(0, 20));
    return false;
  }
  
  console.log(`✅ All required methods available for ${functionName}`);
  return true;
}, []);
// 🛡️ 안전한 ActiveSheet 가져오기 함수
const getSafeActiveSheet = React.useCallback((spread: any, functionName: string = 'unknown') => {
  console.log(`🔍 Getting safe activeSheet for ${functionName}`);
  
  if (!spread) {
    console.error(`❌ Spread is null/undefined in ${functionName}`);
    return null;
  }
  
  try {
    // 현재 활성 시트 가져오기
    let activeSheet = spread.getActiveSheet();
    
    if (!activeSheet) {
      console.warn(`⚠️ ActiveSheet is null, attempting to get first sheet in ${functionName}`);
      
      // 첫 번째 시트 시도
      const sheetCount = spread.getSheetCount();
      console.log(`📊 Total sheets: ${sheetCount}`);
      
      if (sheetCount > 0) {
        activeSheet = spread.getSheet(0);
        if (activeSheet) {
          spread.setActiveSheetIndex(0);
          console.log(`✅ Successfully got first sheet in ${functionName}`);
        }
      }
    }
    
    if (!activeSheet) {
      console.error(`❌ Failed to get any valid sheet in ${functionName}`);
      return null;
    }
    
    // 시트 유효성 검증
    const validation = validateActiveSheet(activeSheet, functionName);
    if (!validation) {
      console.error(`❌ Sheet validation failed in ${functionName}`);
      return null;
    }
    
    console.log(`✅ Got valid activeSheet for ${functionName}: ${activeSheet.name?.() || 'unnamed'}`);
    return activeSheet;
    
  } catch (error) {
    console.error(`❌ Error getting activeSheet in ${functionName}:`, error);
    return null;
  }
}, [validateActiveSheet]);

// 🛡️ 수정된 ensureRowCapacity 함수
const ensureRowCapacity = React.useCallback((activeSheet: any, requiredRowCount: number) => {
  try {
    // 🔍 상세한 null/undefined 체크
    if (!activeSheet) {
      console.error('❌ activeSheet is null/undefined in ensureRowCapacity');
      return false;
    }

    console.log('🔍 ActiveSheet validation in ensureRowCapacity:');
    console.log('  - Type:', typeof activeSheet);
    console.log('  - Constructor:', activeSheet.constructor?.name);
    console.log('  - Is null:', activeSheet === null);
    console.log('  - Is undefined:', activeSheet === undefined);
    
    // 🔍 메서드 존재 여부 확인
    if (typeof activeSheet.getRowCount !== 'function') {
      console.error('❌ getRowCount method does not exist on activeSheet');
      console.log('📋 Available properties:', Object.getOwnPropertyNames(activeSheet).slice(0, 20));
      return false;
    }

    // 🔍 시트 상태 확인
    const currentRowCount = activeSheet.getRowCount();
    console.log(`📊 Current row count: ${currentRowCount} (type: ${typeof currentRowCount})`);

    if (typeof currentRowCount !== 'number' || isNaN(currentRowCount)) {
      console.error('❌ getRowCount returned invalid value:', currentRowCount);
      return false;
    }

    if (requiredRowCount > currentRowCount) {
      // 🔍 setRowCount 메서드 확인
      if (typeof activeSheet.setRowCount !== 'function') {
        console.error('❌ setRowCount method does not exist on activeSheet');
        return false;
      }

      const newRowCount = requiredRowCount + 10;
      activeSheet.setRowCount(newRowCount);
      console.log(`📈 Expanded sheet: ${currentRowCount} → ${newRowCount} rows`);
      
      // 🔍 설정 후 검증
      const verifyRowCount = activeSheet.getRowCount();
      console.log(`✅ Verified new row count: ${verifyRowCount}`);
      
      return true;
    } else {
      console.log(`✅ Sheet already has sufficient rows: ${currentRowCount} >= ${requiredRowCount}`);
      return true;
    }

  } catch (error) {
    console.error('❌ Error in ensureRowCapacity:', error);
    console.error('❌ Error stack:', error.stack);
    return false;
  }
}, []);

// 🛡️ 안전한 컬럼 용량 확보 함수
const ensureColumnCapacity = React.useCallback((activeSheet: any, requiredColumnCount: number) => {
  try {
    // 🔍 상세한 null/undefined 체크
    if (!activeSheet) {
      console.error('❌ activeSheet is null/undefined in ensureColumnCapacity');
      return false;
    }

    console.log('🔍 ActiveSheet validation in ensureColumnCapacity:');
    console.log('  - Type:', typeof activeSheet);
    console.log('  - Constructor:', activeSheet.constructor?.name);
    console.log('  - Is null:', activeSheet === null);
    console.log('  - Is undefined:', activeSheet === undefined);

    // 🔍 메서드 존재 여부 확인
    if (typeof activeSheet.getColumnCount !== 'function') {
      console.error('❌ getColumnCount method does not exist on activeSheet');
      console.log('📋 Available properties:', Object.getOwnPropertyNames(activeSheet).slice(0, 20));
      return false;
    }

    const currentColumnCount = activeSheet.getColumnCount();
    console.log(`📊 Current column count: ${currentColumnCount} (type: ${typeof currentColumnCount})`);

    if (typeof currentColumnCount !== 'number' || isNaN(currentColumnCount)) {
      console.error('❌ getColumnCount returned invalid value:', currentColumnCount);
      return false;
    }

    if (requiredColumnCount > currentColumnCount) {
      if (typeof activeSheet.setColumnCount !== 'function') {
        console.error('❌ setColumnCount method does not exist on activeSheet');
        return false;
      }

      const newColumnCount = requiredColumnCount + 10;
      activeSheet.setColumnCount(newColumnCount);
      console.log(`📈 Expanded columns: ${currentColumnCount} → ${newColumnCount}`);
      
      // 🔍 설정 후 검증
      const verifyColumnCount = activeSheet.getColumnCount();
      console.log(`✅ Verified new column count: ${verifyColumnCount}`);
      
      return true;
    } else {
      console.log(`✅ Sheet already has sufficient columns: ${currentColumnCount} >= ${requiredColumnCount}`);
      return true;
    }

  } catch (error) {
    console.error('❌ Error in ensureColumnCapacity:', error);
    console.error('❌ Error stack:', error.stack);
    return false;
  }
}, []);


// 🎯 텍스트 너비 계산 함수들 (createGrdListTable 함수 위에 추가)
const measureTextWidth = React.useCallback((text: string, fontSize: number = 12, fontFamily: string = 'Arial'): number => {
  // Canvas를 사용한 정확한 텍스트 너비 측정
  const canvas = document.createElement('canvas');
  const context = canvas.getContext('2d');
  if (!context) return text.length * 8; // fallback
  
  context.font = `${fontSize}px ${fontFamily}`;
  const metrics = context.measureText(text || '');
  return Math.ceil(metrics.width);
}, []);

const calculateColumnWidth = React.useCallback((
  headerText: string, 
  dataValues: any[] = [], 
  minWidth: number = 80, 
  maxWidth: number = 300,
  padding: number = 20
): number => {
  // 헤더 텍스트 너비 계산
  const headerWidth = measureTextWidth(headerText, 12, 'Arial');
  
  // 데이터 값들의 최대 너비 계산
  let maxDataWidth = 0;
  if (dataValues.length > 0) {
    maxDataWidth = Math.max(
      ...dataValues
        .slice(0, 10) // 성능을 위해 처음 10개만 샘플링
        .map(value => measureTextWidth(String(value || ''), 11, 'Arial'))
    );
  }
  
  // 헤더와 데이터 중 더 큰 너비 + 패딩 적용
  const calculatedWidth = Math.max(headerWidth, maxDataWidth) + padding;
  
  // 최소/최대 너비 제한 적용
  return Math.min(Math.max(calculatedWidth, minWidth), maxWidth);
}, [measureTextWidth]);

const setOptimalColumnWidths = React.useCallback((activeSheet: any, columns: any[], startCol: number, tableData: any[]) => {
  console.log('🎨 Setting optimal column widths...');
  
  columns.forEach((column, colIndex) => {
    const targetCol = startCol + colIndex;
    
    // 해당 컬럼의 데이터 값들 추출
    const dataValues = tableData.map(row => row[column.key]).filter(val => val != null);
    
    // 최적 너비 계산
    const optimalWidth = calculateColumnWidth(
      column.label || column.key, 
      dataValues,
      column.type === 'NUMBER' ? 100 : 80, // 숫자는 좀 더 넓게
      column.type === 'STRING' ? 250 : 200, // 문자열은 더 넓게
      column.type === 'LIST' ? 30 : 20 // 드롭다운은 여유 패딩
    );
    
    // 컬럼 너비 설정
    activeSheet.setColumnWidth(targetCol, optimalWidth);
    
    console.log(`📏 Column ${targetCol} (${column.key}): width set to ${optimalWidth}px`);
  });
}, [calculateColumnWidth]);

  // 🔍 컬럼 그룹 분석 함수
  const analyzeColumnGroups = React.useCallback((columns: DataTableColumnJSON[]) => {
    const groups: Array<{
      head: string;
      isGroup: boolean;
      columns: DataTableColumnJSON[];
    }> = [];

    let i = 0;
    while (i < columns.length) {
      const currentCol = columns[i];

      // head가 없거나 빈 문자열인 경우 단일 컬럼으로 처리
      if (!currentCol.head || !currentCol.head.trim()) {
        groups.push({
          head: '',
          isGroup: false,
          columns: [currentCol]
        });
        i++;
        continue;
      }

      // 같은 head를 가진 연속된 컬럼들을 찾기
      const groupHead = currentCol.head.trim();
      const groupColumns: DataTableColumnJSON[] = [currentCol];
      let j = i + 1;

      while (j < columns.length && columns[j].head && columns[j].head.trim() === groupHead) {
        groupColumns.push(columns[j]);
        j++;
      }

      // 그룹 추가
      groups.push({
        head: groupHead,
        isGroup: groupColumns.length > 1,
        columns: groupColumns
      });

      i = j; // 다음 그룹으로 이동
    }

    return { groups };
  }, []);


// 🆕 수정된 createGrdListTable 함수
// 🆕 개선된 GRD_LIST용 동적 테이블 생성 함수
const createGrdListTable = React.useCallback((activeSheet: any, template: TemplateItem) => {
  console.log('🏗️ Creating GRD_LIST table');

  // columnsJSON의 visible 컬럼들을 seq 순서로 정렬하여 사용
  const visibleColumns = columnsJSON
    .filter(col => col.hidden !== true)
    .sort((a, b) => {
      const seqA = a.seq !== undefined ? a.seq : 999999;
      const seqB = b.seq !== undefined ? b.seq : 999999;
      return seqA - seqB;
    });

  console.log('📊 Using columns:', visibleColumns.map(c => `${c.key}(seq:${c.seq})`));
  console.log(`📊 Total visible columns: ${visibleColumns.length}`);

  if (visibleColumns.length === 0) {
    console.warn('❌ No visible columns found in columnsJSON');
    return [];
  }

  // ⭐ 컬럼 용량 확보
  const startCol = 1;
  const requiredColumnCount = startCol + visibleColumns.length;
  ensureColumnCapacity(activeSheet, requiredColumnCount);

  // 테이블 생성 시작
  const mappings: CellMapping[] = [];

  // 🔍 그룹 헤더 분석
  const groupInfo = analyzeColumnGroups(visibleColumns);
  const hasGroups = groupInfo.groups.length > 0;

  // 헤더 행 계산: 그룹이 있으면 2행, 없으면 1행
  const groupHeaderRow = 0;
  const columnHeaderRow = hasGroups ? 1 : 0;
  const dataStartRow = hasGroups ? 2 : 1;

  // 🎨 헤더 스타일 생성
  const groupHeaderStyle = new GC.Spread.Sheets.Style();
  groupHeaderStyle.backColor = "#1e40af";
  groupHeaderStyle.foreColor = "#ffffff";
  groupHeaderStyle.font = "bold 13px Arial";
  groupHeaderStyle.hAlign = GC.Spread.Sheets.HorizontalAlign.center;
  groupHeaderStyle.vAlign = GC.Spread.Sheets.VerticalAlign.center;

  const columnHeaderStyle = new GC.Spread.Sheets.Style();
  columnHeaderStyle.backColor = "#3b82f6";
  columnHeaderStyle.foreColor = "#ffffff";
  columnHeaderStyle.font = "bold 12px Arial";
  columnHeaderStyle.hAlign = GC.Spread.Sheets.HorizontalAlign.center;
  columnHeaderStyle.vAlign = GC.Spread.Sheets.VerticalAlign.center;

  let currentCol = startCol;

  // 🏗️ 그룹 헤더 및 컬럼 헤더 생성
  if (hasGroups) {
    // 그룹 헤더가 있는 경우
    groupInfo.groups.forEach(group => {
      if (group.isGroup) {
        // 그룹 헤더 생성 및 병합
        const groupStartCol = currentCol;
        const groupEndCol = currentCol + group.columns.length - 1;

        // 그룹 헤더 셀 설정
        const groupHeaderCell = activeSheet.getCell(groupHeaderRow, groupStartCol);
        groupHeaderCell.value(group.head);

        // 그룹 헤더 병합
        if (group.columns.length > 1) {
          activeSheet.addSpan(groupHeaderRow, groupStartCol, 1, group.columns.length);
        }

        // 그룹 헤더 스타일 적용
        for (let col = groupStartCol; col <= groupEndCol; col++) {
          activeSheet.setStyle(groupHeaderRow, col, groupHeaderStyle);
          activeSheet.getCell(groupHeaderRow, col).locked(true);
        }

        console.log(`📝 Group Header [${groupHeaderRow}, ${groupStartCol}-${groupEndCol}]: ${group.head}`);

        // 그룹 내 개별 컬럼 헤더 생성
        group.columns.forEach((column, index) => {
          const colIndex = groupStartCol + index;
          const columnHeaderCell = activeSheet.getCell(columnHeaderRow, colIndex);
          columnHeaderCell.value(column.label);
          activeSheet.setStyle(columnHeaderRow, colIndex, columnHeaderStyle);
          columnHeaderCell.locked(true);

          console.log(`📝 Column Header [${columnHeaderRow}, ${colIndex}]: ${column.label}`);
        });

        currentCol += group.columns.length;
      } else {
        // 그룹이 아닌 단일 컬럼
        const column = group.columns[0];

        // 그룹 헤더 행에는 빈 셀
        const groupHeaderCell = activeSheet.getCell(groupHeaderRow, currentCol);
        groupHeaderCell.value("");
        activeSheet.setStyle(groupHeaderRow, currentCol, groupHeaderStyle);
        groupHeaderCell.locked(true);

        // 컬럼 헤더 생성
        const columnHeaderCell = activeSheet.getCell(columnHeaderRow, currentCol);
        columnHeaderCell.value(column.label);
        activeSheet.setStyle(columnHeaderRow, currentCol, columnHeaderStyle);
        columnHeaderCell.locked(true);

        console.log(`📝 Single Column [${columnHeaderRow}, ${currentCol}]: ${column.label}`);
        currentCol++;
      }
    });
  } else {
    // 그룹이 없는 경우
    visibleColumns.forEach((column, colIndex) => {
      const targetCol = startCol + colIndex;
      const columnConfig = columnsJSON.find(col => col.key === column.key);
      
      // 📋 각 행마다 개별 셀 설정
      tableData.forEach((rowData, rowIndex) => {
        const targetRow = dataStartRow + rowIndex;
        const cell = activeSheet.getCell(targetRow, targetCol);
        const value = rowData[column.key];
        const cellEditable = isFieldEditable(column.key, rowData);
    
        // 🔧 새로 추가: 셀 타입 및 편집기 설정
        if (columnConfig) {
          setupCellTypeAndEditor(activeSheet, { row: targetRow, col: targetCol }, columnConfig, cellEditable, 1);
        }
    
        // 값 설정
        cell.value(value ?? null);
    
        // 스타일 설정
        const style = createCellStyle(cellEditable);
        activeSheet.setStyle(targetRow, targetCol, style);
    
        // 개별 매핑 추가
        mappings.push({
          attId: column.key,
          cellAddress: getCellAddress(targetRow, targetCol),
          isEditable: cellEditable,
          dataRowIndex: rowIndex
        });
      });
    });
  }

  // 🔄 데이터 행 및 매핑 생성 (SPREAD_LIST 방식과 동일한 로직)
  const dataRowCount = tableData.length;
  ensureRowCapacity(activeSheet, dataStartRow + dataRowCount);

  // 📋 각 컬럼별로 매핑 생성 (SPREAD_LIST와 동일한 방식)
  visibleColumns.forEach((column, colIndex) => {
    const targetCol = startCol + colIndex;
    
    console.log(`🔄 Processing column ${column.key} with ${dataRowCount} rows`);

    // 📋 각 행마다 개별 매핑 생성 (SPREAD_LIST와 동일)
    tableData.forEach((rowData, rowIndex) => {
      const targetRow = dataStartRow + rowIndex;
      const cellAddress = getCellAddress(targetRow, targetCol);
      
      // 🛡️ readonly 체크 (SPREAD_LIST와 동일한 로직)
      const cellEditable = isFieldEditable(column.key, rowData);
      
      // 개별 매핑 추가
      mappings.push({
        attId: column.key,
        cellAddress: cellAddress,
        isEditable: cellEditable,
        dataRowIndex: rowIndex
      });

      console.log(`📝 Mapping ${column.key} Row ${rowIndex}: ${cellAddress} (${cellEditable ? 'Editable' : 'ReadOnly'})`);
    });

    // 📋 LIST 타입 드롭다운 설정 (편집 가능한 행이 있는 경우만)
    if (column.type === "LIST" && column.options) {
      const hasEditableRows = tableData.some((rowData) => isFieldEditable(column.key, rowData));
      if (hasEditableRows) {
        const cellPos = { row: dataStartRow, col: targetCol };
        setupOptimizedListValidation(activeSheet, cellPos, column.options, dataRowCount);
        console.log(`📋 Dropdown set for ${column.key}: ${hasEditableRows ? 'Has editable rows' : 'All readonly'}`);
      }
    }
  });

  // 🎨 개별 셀 데이터 및 스타일 설정 (SPREAD_LIST와 동일한 방식)
  tableData.forEach((rowData, rowIndex) => {
    const targetRow = dataStartRow + rowIndex;
    
    visibleColumns.forEach((column, colIndex) => {
      const targetCol = startCol + colIndex;
      const cell = activeSheet.getCell(targetRow, targetCol);
      const value = rowData[column.key];

      // 값 설정
      cell.value(value ?? null);

      // 🛡️ 편집 권한 및 스타일 재확인 (SPREAD_LIST와 동일)
      const cellEditable = isFieldEditable(column.key, rowData);
      cell.locked(!cellEditable);
      const style = createCellStyle(cellEditable);
      activeSheet.setStyle(targetRow, targetCol, style);

      // 🔍 디버깅: readonly 상태 로깅
      if (!cellEditable) {
        const columnConfig = columnsJSON.find(col => col.key === column.key);
        const reasons = [];
        
        if (columnConfig?.shi === true) {
          reasons.push('column.shi=true');
        }
        if (rowData.shi === true) {
          reasons.push('row.shi=true');
        }
        if (!editableFields.includes(column.key) && column.key !== "TAG_NO" && column.key !== "TAG_DESC") {
          reasons.push('not in editableFields');
        }
        
        console.log(`🔒 ReadOnly [${targetRow}, ${targetCol}] ${column.key}: ${reasons.join(', ')}`);
      }
    });
  });

  // 🎨 컬럼 너비 자동 설정
  setOptimalColumnWidths(activeSheet, visibleColumns, startCol, tableData);

  console.log(`🏗️ GRD_LIST table created with ${mappings.length} mappings, hasGroups: ${hasGroups}`);
  console.log(`📊 Readonly analysis:`);
  console.log(`   Total cells: ${mappings.length}`);
  console.log(`   Editable cells: ${mappings.filter(m => m.isEditable).length}`);
  console.log(`   Readonly cells: ${mappings.filter(m => !m.isEditable).length}`);
  
  return mappings;
}, [tableData, columnsJSON, isFieldEditable, createCellStyle, ensureRowCapacity, ensureColumnCapacity, setupOptimizedListValidation, setOptimalColumnWidths, editableFields, getCellAddress, analyzeColumnGroups]);

// 🛡️ 추가: readonly 상태 확인 헬퍼 함수
const analyzeReadonlyStatus = React.useCallback((column: DataTableColumnJSON, rowData: GenericData) => {
  const reasons: string[] = [];
  
  // 1. 컬럼 자체가 readonly인지 확인
  if (column.shi === true) {
    reasons.push('Column marked as readonly (shi=true)');
  }
  
  // 2. 행 자체가 readonly인지 확인
  if (rowData.shi === true) {
    reasons.push('Row marked as readonly (shi=true)');
  }
  
  // 3. editableFields에 포함되지 않은 경우
  if (!editableFields.includes(column.key) && column.key !== "TAG_NO" && column.key !== "TAG_DESC") {
    reasons.push('Not in editable fields list');
  }
  
  // 4. 특수 필드 체크
  if (column.key === "TAG_NO" || column.key === "TAG_DESC") {
    // TAG_NO와 TAG_DESC는 기본 편집 가능하지만 다른 조건들은 적용됨
    if (column.shi === true || rowData.shi === true) {
      // 다른 readonly 조건이 있으면 적용
    } else {
      return { isEditable: true, reasons: ['Default editable field'] };
    }
  }
  
  const isEditable = reasons.length === 0;
  
  return {
    isEditable,
    reasons: isEditable ? ['Editable'] : reasons
  };
}, [editableFields]);



// 🛡️ 수정된 시트 보호 및 이벤트 설정 함수
const setupSheetProtectionAndEvents = React.useCallback((activeSheet: any, mappings: CellMapping[]) => {
  console.log(`🛡️ Setting up protection and events for ${mappings.length} mappings`);

  // 🔧 1단계: 먼저 시트 보호를 완전히 해제하고 강력한 잠금 해제 실행
  console.log('🔓 Step 1: Forcing unlock all editable cells...');
  activeSheet.options.isProtected = false;

  // 🔧 2단계: 모든 편집 가능한 셀에 대해 강제 잠금 해제 및 CellType 설정
  mappings.forEach((mapping, index) => {
    if (!mapping.isEditable) return;
    
    const cellPos = parseCellAddress(mapping.cellAddress);
    if (!cellPos) return;
    
    try {
      const cell = activeSheet.getCell(cellPos.row, cellPos.col);
      const columnConfig = columnsJSON.find(col => col.key === mapping.attId);
      
      // 강제 잠금 해제
      cell.locked(false);
      
      // CellType 명시적 설정
      if (columnConfig?.type === "LIST" && columnConfig.options) {
        // LIST 타입: ComboBox 설정
        const comboBox = new GC.Spread.Sheets.CellTypes.ComboBox();
        comboBox.items(columnConfig.options);
        comboBox.editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.text);
        activeSheet.setCellType(cellPos.row, cellPos.col, comboBox);
        console.log(`📋 ComboBox set for ${mapping.attId} at ${mapping.cellAddress}`);
      } else {
        // 다른 모든 타입: 기본 텍스트 편집기 설정
        const textCellType = new GC.Spread.Sheets.CellTypes.Text();
        activeSheet.setCellType(cellPos.row, cellPos.col, textCellType);
        console.log(`📝 Text editor set for ${mapping.attId} at ${mapping.cellAddress}`);
        
        // NUMBER 타입인 경우에만 validation 추가 (편집은 가능하게)
        if (columnConfig?.type === "NUMBER") {
          const numberValidator = GC.Spread.Sheets.DataValidation.createNumberValidator(
            GC.Spread.Sheets.ConditionalFormatting.ComparisonOperators.between,
            -999999999, 999999999, true
          );
          numberValidator.showInputMessage(false);
          numberValidator.showErrorMessage(false);
          activeSheet.setDataValidator(cellPos.row, cellPos.col, numberValidator);
        }
      }
      
      // 편집 가능 스타일 명확히 표시
      const editableStyle = createCellStyle(true);
      activeSheet.setStyle(cellPos.row, cellPos.col, editableStyle);
      
      console.log(`🔓 Forced unlock: ${mapping.attId} at ${mapping.cellAddress}`);
      
    } catch (error) {
      console.error(`❌ Error processing cell ${mapping.cellAddress}:`, error);
    }
  });

  // 🔧 3단계: 시트 보호 재설정 (편집 허용하는 설정으로)
  activeSheet.options.isProtected = true;
  activeSheet.options.protectionOptions = {
    allowSelectLockedCells: true,
    allowSelectUnlockedCells: true,
    allowSort: false,
    allowFilter: false,
    allowEditObjects: true,        // ✅ 편집 객체 허용
    allowResizeRows: false,
    allowResizeColumns: false,
    allowFormatCells: false,
    allowInsertRows: false,
    allowInsertColumns: false,
    allowDeleteRows: false,
    allowDeleteColumns: false
  };

  // 🔧 4단계: 편집 테스트 실행
  console.log('🧪 Testing cell editability...');
  const editableMapping = mappings.find(m => m.isEditable);
  if (editableMapping) {
    const cellPos = parseCellAddress(editableMapping.cellAddress);
    if (cellPos) {
      try {
        const cell = activeSheet.getCell(cellPos.row, cellPos.col);
        const testValue = 'TEST_' + Math.random().toString(36).substr(2, 5);
        const originalValue = cell.value();
        
        console.log(`🧪 Testing ${editableMapping.attId} at ${editableMapping.cellAddress}`);
        console.log(`🧪 Locked status: ${cell.locked()}`);
        
        // 직접 값 설정 테스트
        cell.value(testValue);
        const newValue = cell.value();
        
        if (newValue === testValue) {
          console.log('✅ Cell edit test PASSED');
          cell.value(originalValue); // 원래 값 복원
        } else {
          console.log(`❌ Cell edit test FAILED: ${newValue} !== ${testValue}`);
        }
      } catch (testError) {
        console.error('❌ Edit test error:', testError);
      }
    }
  }

  // 🎯 변경 감지 이벤트
  const changeEvents = [
    GC.Spread.Sheets.Events.CellChanged,
    GC.Spread.Sheets.Events.ValueChanged,
    GC.Spread.Sheets.Events.ClipboardPasted
  ];

  changeEvents.forEach(eventType => {
    activeSheet.bind(eventType, () => {
      console.log(`📝 ${eventType} detected`);
      setHasChanges(true);
    });
  });

  // 🚫 편집 시작 권한 확인 (수정됨)
  activeSheet.bind(GC.Spread.Sheets.Events.EditStarting, (event: any, info: any) => {
    console.log(`🎯 EditStarting: Row ${info.row}, Col ${info.col}`);

    // ✅ 정확한 매핑 찾기 (행/열 정확히 일치)
    const exactMapping = mappings.find(m => {
      const cellPos = parseCellAddress(m.cellAddress);
      return cellPos && cellPos.row === info.row && cellPos.col === info.col;
    });

    if (!exactMapping) {
      console.log(`ℹ️ No mapping found for [${info.row}, ${info.col}] - allowing edit`);
      return; // 매핑이 없으면 허용 (템플릿 영역 밖)
    }

    console.log(`📋 Found mapping: ${exactMapping.attId} at ${exactMapping.cellAddress}, isEditable: ${exactMapping.isEditable}`);

    // 🔍 추가 디버깅: 셀의 실제 상태 확인
    const cell = activeSheet.getCell(info.row, info.col);
    const isLocked = cell.locked();
    const cellValue = cell.value();
    
    console.log(`🔍 Cell state check:`, {
      attId: exactMapping.attId,
      isEditable: exactMapping.isEditable,
      isLocked: isLocked,
      currentValue: cellValue
    });

    // 🔧 추가: EditStarting 시점에서도 강제 잠금 해제 재시도
    if (exactMapping.isEditable && isLocked) {
      console.log(`🔓 Re-unlocking cell during EditStarting...`);
      cell.locked(false);
      
      // CellType도 재설정
      const columnConfig = columnsJSON.find(col => col.key === exactMapping.attId);
      if (columnConfig?.type !== "LIST") {
        const textCellType = new GC.Spread.Sheets.CellTypes.Text();
        activeSheet.setCellType(info.row, info.col, textCellType);
      }
    }

    // 기본 편집 권한 확인
    if (!exactMapping.isEditable) {
      console.log(`🚫 Field ${exactMapping.attId} is not editable`);
      toast.warning(`${exactMapping.attId} field is read-only`);
      info.cancel = true;
      return;
    }

    // SPREAD_LIST 또는 GRD_LIST 개별 행 SHI 확인
    if ((templateType === 'SPREAD_LIST' || templateType === 'GRD_LIST') && exactMapping.dataRowIndex !== undefined) {
      const dataRowIndex = exactMapping.dataRowIndex;

      console.log(`🔍 Checking SHI for data row ${dataRowIndex}`);

      if (dataRowIndex >= 0 && dataRowIndex < tableData.length) {
        const rowData = tableData[dataRowIndex];
        if (rowData?.shi === true) {
          console.log(`🚫 Row ${dataRowIndex} is in SHI mode`);
          toast.warning(`Row ${dataRowIndex + 1}: ${exactMapping.attId} field is read-only (SHI mode)`);
          info.cancel = true;
          return;
        }
      } else {
        console.warn(`⚠️ Invalid dataRowIndex: ${dataRowIndex} (tableData.length: ${tableData.length})`);
      }
    }

    console.log(`✅ Edit allowed for ${exactMapping.attId}`);
  });

  // ✅ 편집 완료 검증 (기존 로직 유지)
  activeSheet.bind(GC.Spread.Sheets.Events.EditEnded, (event: any, info: any) => {
    console.log(`🏁 EditEnded: Row ${info.row}, Col ${info.col}, New value: ${activeSheet.getValue(info.row, info.col)}`);

    // ✅ 정확한 매핑 찾기
    const exactMapping = mappings.find(m => {
      const cellPos = parseCellAddress(m.cellAddress);
      return cellPos && cellPos.row === info.row && cellPos.col === info.col;
    });

    if (!exactMapping) {
      console.log(`ℹ️ No mapping found for [${info.row}, ${info.col}] - skipping validation`);
      return;
    }

    const columnConfig = columnsJSON.find(col => col.key === exactMapping.attId);
    if (columnConfig) {
      const cellValue = activeSheet.getValue(info.row, info.col);
      console.log(`🔍 Validating ${exactMapping.attId}: "${cellValue}"`);

      const errorMessage = validateCellValue(cellValue, columnConfig.type, columnConfig.options);
      const cell = activeSheet.getCell(info.row, info.col);

      if (errorMessage) {
        console.log(`❌ Validation failed: ${errorMessage}`);

        // 🚨 에러 스타일 적용 (편집 가능 상태 유지)
        const errorStyle = new GC.Spread.Sheets.Style();
        errorStyle.backColor = "#fef2f2";
        errorStyle.foreColor = "#dc2626";
        errorStyle.borderLeft = new GC.Spread.Sheets.LineBorder("#dc2626", GC.Spread.Sheets.LineStyle.thick);
        errorStyle.borderRight = new GC.Spread.Sheets.LineBorder("#dc2626", GC.Spread.Sheets.LineStyle.thick);
        errorStyle.borderTop = new GC.Spread.Sheets.LineBorder("#dc2626", GC.Spread.Sheets.LineStyle.thick);
        errorStyle.borderBottom = new GC.Spread.Sheets.LineBorder("#dc2626", GC.Spread.Sheets.LineStyle.thick);

        activeSheet.setStyle(info.row, info.col, errorStyle);
        cell.locked(!exactMapping.isEditable); // 편집 가능 상태 유지

        toast.warning(`Invalid value in ${exactMapping.attId}: ${errorMessage}. Please correct the value.`, { duration: 5000 });
      } else {
        console.log(`✅ Validation passed`);

        // ✅ 정상 스타일 복원
        const normalStyle = createCellStyle(exactMapping.isEditable);
        activeSheet.setStyle(info.row, info.col, normalStyle);
        cell.locked(!exactMapping.isEditable);
      }
    }

    // 🔄 변경 상태 업데이트
    setHasChanges(true);
  });

  // 🔧 5단계: 설정 완료 후 1초 뒤에 추가 잠금 해제 실행 (안전장치)
  setTimeout(() => {
    console.log('🔄 Running safety unlock after 1 second...');
    mappings.forEach(mapping => {
      if (!mapping.isEditable) return;
      
      const cellPos = parseCellAddress(mapping.cellAddress);
      if (!cellPos) return;
      
      try {
        const cell = activeSheet.getCell(cellPos.row, cellPos.col);
        if (cell.locked()) {
          console.log(`🔓 Safety unlock: ${mapping.attId}`);
          cell.locked(false);
        }
      } catch (error) {
        console.error(`❌ Safety unlock error for ${mapping.cellAddress}:`, error);
      }
    });
  }, 1000);

  console.log(`🛡️ Protection and events configured for ${mappings.length} mappings`);
  console.log(`🔓 Editable cells: ${mappings.filter(m => m.isEditable).length}`);
  console.log(`🔒 Readonly cells: ${mappings.filter(m => !m.isEditable).length}`);
}, [templateType, tableData, createCellStyle, validateCellValue, columnsJSON]);

// 🔧 셀 타입 및 편집기 설정 함수 (initSpread 함수 내부에 추가)
const setupCellTypeAndEditor = React.useCallback((activeSheet: any, cellPos: { row: number, col: number }, columnConfig: DataTableColumnJSON, isEditable: boolean, rowCount: number = 1) => {
  console.log(`🔧 Setting up cell type for ${columnConfig.key} (${columnConfig.type}) at [${cellPos.row}, ${cellPos.col}]`);
  
  try {
    // 편집 가능한 셀에만 적절한 셀 타입 설정
    if (isEditable) {
      for (let i = 0; i < rowCount; i++) {
        const targetRow = cellPos.row + i;
        const cell = activeSheet.getCell(targetRow, cellPos.col);
        
        // 셀 잠금 해제
        cell.locked(false);
        
        switch (columnConfig.type) {
          case "LIST":
            // 드롭다운은 기존 setupOptimizedListValidation 함수에서 처리
            break;
            
          case "NUMBER":
            // 숫자 입력용 셀 타입 설정
            const numberCellType = new GC.Spread.Sheets.CellTypes.Text();
            activeSheet.setCellType(targetRow, cellPos.col, numberCellType);
            
            // 숫자 validation 설정 (선택사항)
            const numberValidator = GC.Spread.Sheets.DataValidation.createNumberValidator(
              GC.Spread.Sheets.ConditionalFormatting.ComparisonOperators.between,
              -999999999, 999999999, true
            );
            numberValidator.showInputMessage(true);
            numberValidator.inputTitle("Number Input");
            numberValidator.inputMessage("Please enter a valid number");
            activeSheet.setDataValidator(targetRow, cellPos.col, numberValidator);
            break;
            
          case "STRING":
          default:
            // 기본 텍스트 입력용 셀 타입 설정
            const textCellType = new GC.Spread.Sheets.CellTypes.Text();
            activeSheet.setCellType(targetRow, cellPos.col, textCellType);
            break;
        }
        
        console.log(`✅ Cell type set for [${targetRow}, ${cellPos.col}]: ${columnConfig.type}`);
      }
    } else {
      // 읽기 전용 셀 설정
      for (let i = 0; i < rowCount; i++) {
        const targetRow = cellPos.row + i;
        const cell = activeSheet.getCell(targetRow, cellPos.col);
        cell.locked(true);
      }
    }
    
  } catch (error) {
    console.error(`❌ Error setting cell type for ${columnConfig.key}:`, error);
  }
}, []);

  // ═══════════════════════════════════════════════════════════════════════════════
  // 🏗️ 메인 SpreadSheets 초기화 함수
  // ═══════════════════════════════════════════════════════════════════════════════

// 🛡️ 수정된 initSpread 함수 - activeSheet 참조 문제 해결
const initSpread = React.useCallback((spread: any, template?: TemplateItem) => {
  const workingTemplate = template || selectedTemplate;
  if (!spread || !workingTemplate) {
    console.error('❌ Invalid spread or template in initSpread');
    return;
  }

  try {
    // 🔄 초기 설정
    setCurrentSpread(spread);
    setHasChanges(false);
    setValidationErrors([]);

    // 성능을 위한 렌더링 일시 중단
    spread.suspendPaint();

    try {
      // ⚠️ 초기 activeSheet 가져오기
      let activeSheet = getSafeActiveSheet(spread, 'initSpread-initial');
      if (!activeSheet) {
        throw new Error('Failed to get initial activeSheet');
      }

      // 시트 보호 해제 (편집을 위해)
      activeSheet.options.isProtected = false;

      let mappings: CellMapping[] = [];

      // 🆕 GRD_LIST 처리
      if (templateType === 'GRD_LIST' && workingTemplate.GRD_LST_SETUP) {
        console.log('🏗️ Processing GRD_LIST template');

        // 기본 워크북 설정
        spread.clearSheets();
        spread.addSheet(0);
        const sheet = spread.getSheet(0);
        sheet.name('Data');
        spread.setActiveSheet('Data');

        // 동적 테이블 생성
        mappings = createGrdListTable(sheet, workingTemplate);

      } else {
        // 🔍 SPREAD_LIST 및 SPREAD_ITEM 처리
        let contentJson = null;
        let dataSheets = null;

        // SPR_LST_SETUP.CONTENT 우선 사용
        if (workingTemplate.SPR_LST_SETUP?.CONTENT) {
          contentJson = workingTemplate.SPR_LST_SETUP.CONTENT;
          dataSheets = workingTemplate.SPR_LST_SETUP.DATA_SHEETS;
          console.log('✅ Using SPR_LST_SETUP for template:', workingTemplate.NAME);
        }
        // SPR_ITM_LST_SETUP.CONTENT 대안 사용
        else if (workingTemplate.SPR_ITM_LST_SETUP?.CONTENT) {
          contentJson = workingTemplate.SPR_ITM_LST_SETUP.CONTENT;
          dataSheets = workingTemplate.SPR_ITM_LST_SETUP.DATA_SHEETS;
          console.log('✅ Using SPR_ITM_LST_SETUP for template:', workingTemplate.NAME);
        }

        if (!contentJson) {
          throw new Error(`No template content found for ${workingTemplate.NAME}`);
        }

        if (!dataSheets || dataSheets.length === 0) {
          throw new Error(`No data sheets configuration found for ${workingTemplate.NAME}`);
        }

        console.log('🔍 Template info:', {
          templateName: workingTemplate.NAME,
          templateType: templateType,
          dataSheetsCount: dataSheets.length,
          hasSelectedRow: !!selectedRow,
          tableDataLength: tableData.length
        });

        // 🏗️ SpreadSheets 템플릿 로드
        const jsonData = typeof contentJson === 'string' ? JSON.parse(contentJson) : contentJson;
        
        console.log('📥 Loading template JSON...');
        spread.fromJSON(jsonData);
        console.log('✅ Template JSON loaded');

        // ⚠️ 중요: 템플릿 로드 후 activeSheet 다시 가져오기
        activeSheet = getSafeActiveSheet(spread, 'initSpread-after-fromJSON');
        if (!activeSheet) {
          throw new Error('ActiveSheet became null after loading template');
        }

        console.log('🔍 Active sheet after template load:', {
          name: activeSheet.name?.() || 'unnamed',
          rowCount: activeSheet.getRowCount(),
          colCount: activeSheet.getColumnCount()
        });

        // 시트 보호 다시 해제 (템플릿 로드 후 다시 설정될 수 있음)
        activeSheet.options.isProtected = false;

        // 📊 데이터 매핑 및 로딩 처리
        console.log(`🔄 Processing ${dataSheets.length} data sheets`);

        dataSheets.forEach((dataSheet, sheetIndex) => {
          console.log(`📋 Processing data sheet ${sheetIndex}:`, {
            sheetName: dataSheet.SHEET_NAME,
            mappingCount: dataSheet.MAP_CELL_ATT?.length || 0
          });

          if (dataSheet.MAP_CELL_ATT && dataSheet.MAP_CELL_ATT.length > 0) {
            dataSheet.MAP_CELL_ATT.forEach((mapping, mappingIndex) => {
              const { ATT_ID, IN } = mapping;

              if (!ATT_ID || !IN || IN.trim() === "") {
                console.warn(`⚠️ Invalid mapping: ATT_ID=${ATT_ID}, IN=${IN}`);
                return;
              }

              const cellPos = parseCellAddress(IN);
              if (!cellPos) {
                console.warn(`⚠️ Invalid cell address: ${IN}`);
                return;
              }

              const columnConfig = columnsJSON.find(col => col.key === ATT_ID);

              // 🎯 템플릿 타입별 데이터 처리
              if (templateType === 'SPREAD_ITEM' && selectedRow) {
                console.log(`📝 Processing SPREAD_ITEM for ${ATT_ID}`);
                
                const isEditable = isFieldEditable(ATT_ID);
                const value = selectedRow[ATT_ID];
              
                // 매핑 정보 저장
                mappings.push({
                  attId: ATT_ID,
                  cellAddress: IN,
                  isEditable: isEditable,
                  dataRowIndex: 0
                });
              
                // ⚠️ 안전한 셀 참조 및 값 설정
                try {
                  const cell = activeSheet.getCell(cellPos.row, cellPos.col);
                  console.log(`🔄 Setting SPREAD_ITEM cell [${cellPos.row}, ${cellPos.col}] ${ATT_ID}: "${value}"`);
                  
                  // 🔧 새로 추가: 셀 타입 및 편집기 설정
                  setupCellTypeAndEditor(activeSheet, cellPos, columnConfig, isEditable, 1);
                  
                  // 값 설정
                  cell.value(value ?? null);
                  
                  // 스타일 설정
                  const style = createCellStyle(isEditable);
                  activeSheet.setStyle(cellPos.row, cellPos.col, style);
              
                  // LIST 타입 드롭다운 설정 (기존 코드 유지)
                  if (columnConfig?.type === "LIST" && columnConfig.options && isEditable) {
                    setupOptimizedListValidation(activeSheet, cellPos, columnConfig.options, 1);
                  }
                  
                  console.log(`✅ SPREAD_ITEM cell set successfully`);
                } catch (cellError) {
                  console.error(`❌ Error setting SPREAD_ITEM cell:`, cellError);
                }
              } else if (templateType === 'SPREAD_LIST' && tableData.length > 0) {
                console.log(`📊 Processing SPREAD_LIST for ${ATT_ID} with ${tableData.length} rows`);
                
                // 🚀 행 확장 - 안전한 방법으로
                const requiredRows = cellPos.row + tableData.length;
                console.log(`🚀 Ensuring ${requiredRows} rows for SPREAD_LIST`);
                
                // ⚠️ activeSheet 유효성 재검증
                const currentActiveSheet = getSafeActiveSheet(spread, 'ensureRowCapacity');
                if (!currentActiveSheet) {
                  console.error(`❌ ActiveSheet is null before ensureRowCapacity`);
                  return;
                }
                
                if (!ensureRowCapacity(currentActiveSheet, requiredRows)) {
                  console.error(`❌ Failed to ensure row capacity for ${requiredRows} rows`);
                  return;
                }

                // activeSheet 참조 업데이트
                activeSheet = currentActiveSheet;

                // 매핑 생성
                tableData.forEach((rowData, index) => {
                  const targetRow = cellPos.row + index;
                  const targetCellAddress = getCellAddress(targetRow, cellPos.col);
                  const cellEditable = isFieldEditable(ATT_ID, rowData);

                  mappings.push({
                    attId: ATT_ID,
                    cellAddress: targetCellAddress,
                    isEditable: cellEditable,
                    dataRowIndex: index
                  });
                });

                // LIST 타입 드롭다운 설정
                if (columnConfig?.type === "LIST" && columnConfig.options) {
                  const hasEditableRows = tableData.some((rowData) => isFieldEditable(ATT_ID, rowData));
                  if (hasEditableRows) {
                    setupOptimizedListValidation(activeSheet, cellPos, columnConfig.options, tableData.length);
                  }
                }
// 개별 셀 데이터 및 스타일 설정
tableData.forEach((rowData, index) => {
  const targetRow = cellPos.row + index;
  
  try {
    const cell = activeSheet.getCell(targetRow, cellPos.col);
    const value = rowData[ATT_ID];
    const cellEditable = isFieldEditable(ATT_ID, rowData);

    console.log(`🔄 Setting SPREAD_LIST Row ${index} ${ATT_ID}: "${value}"`);
    
    // 🔧 새로 추가: 각 셀에 대한 타입 및 편집기 설정
    setupCellTypeAndEditor(activeSheet, { row: targetRow, col: cellPos.col }, columnConfig, cellEditable, 1);
    
    // 값 설정
    cell.value(value ?? null);

    // 스타일 설정
    const style = createCellStyle(cellEditable);
    activeSheet.setStyle(targetRow, cellPos.col, style);
    
  } catch (cellError) {
    console.error(`❌ Error setting SPREAD_LIST cell Row ${index}:`, cellError);
  }
});


                console.log(`✅ SPREAD_LIST processing completed for ${ATT_ID}`);
              }
            });
          }
        });
      }

      // 💾 매핑 정보 저장 및 이벤트 설정
      setCellMappings(mappings);
      
      // ⚠️ 최종 activeSheet 재확인 후 이벤트 설정
      const finalActiveSheet = getSafeActiveSheet(spread, 'setupSheetProtectionAndEvents');
      if (finalActiveSheet) {
        setupSheetProtectionAndEvents(finalActiveSheet, mappings);
      } else {
        console.error('❌ Failed to get activeSheet for events setup');
      }

      console.log(`✅ Template initialization completed with ${mappings.length} mappings`);

    } finally {
      // 렌더링 재개
      spread.resumePaint();
    }

  } catch (error) {
    console.error('❌ Error initializing spread:', error);
    // toast.error(`Failed to load template: ${error.message}`);
    if (spread?.resumePaint) {
      spread.resumePaint();
    }
  }
}, [selectedTemplate, templateType, selectedRow, tableData, isFieldEditable, columnsJSON, createCellStyle, setupOptimizedListValidation, ensureRowCapacity, setupSheetProtectionAndEvents, createGrdListTable, getCellAddress, getSafeActiveSheet, validateActiveSheet]);
  // 변경사항 저장 함수
  const handleSaveChanges = React.useCallback(async () => {
    if (!currentSpread || !hasChanges) {
      toast.info("No changes to save");
      return;
    }

    // 저장 전 데이터 검증
    const errors = validateAllData();
    if (errors.length > 0) {
      toast.error(`Cannot save: ${errors.length} validation errors found. Please fix them first.`);
      return;
    }

    try {
      setIsPending(true);

      const activeSheet = currentSpread.getActiveSheet();

      if (templateType === 'SPREAD_ITEM' && selectedRow) {
        // 단일 행 저장
        const dataToSave = { ...selectedRow };

        cellMappings.forEach(mapping => {
          if (mapping.isEditable) {
            const cellPos = parseCellAddress(mapping.cellAddress);
            if (cellPos) {
              const cellValue = activeSheet.getValue(cellPos.row, cellPos.col);
              dataToSave[mapping.attId] = cellValue;
            }
          }
        });

        dataToSave.TAG_NO = selectedRow.TAG_NO;

        const { success, message } = await updateFormDataInDB(
          formCode,
          contractItemId,
          dataToSave
        );

        if (!success) {
          toast.error(message);
          return;
        }

        toast.success("Changes saved successfully!");
        onUpdateSuccess?.(dataToSave);

      } else if ((templateType === 'SPREAD_LIST' || templateType === 'GRD_LIST') && tableData.length > 0) {
        // 복수 행 저장 (SPREAD_LIST와 GRD_LIST 동일 처리)
        const updatedRows: GenericData[] = [];
        let saveCount = 0;

        for (let i = 0; i < tableData.length; i++) {
          const originalRow = tableData[i];
          const dataToSave = { ...originalRow };
          let hasRowChanges = false;

          // 각 매핑에 대해 해당 행의 값 확인
          cellMappings.forEach(mapping => {
            if (mapping.dataRowIndex === i && mapping.isEditable) {
              const columnConfig = columnsJSON.find(col => col.key === mapping.attId);
              const isColumnEditable = columnConfig?.shi !== true;
              const isRowEditable = originalRow.shi !== true;

              if (isColumnEditable && isRowEditable) {
                const cellPos = parseCellAddress(mapping.cellAddress);
                if (cellPos) {
                  const cellValue = activeSheet.getValue(cellPos.row, cellPos.col);

                  // 값이 변경되었는지 확인
                  if (cellValue !== originalRow[mapping.attId]) {
                    dataToSave[mapping.attId] = cellValue;
                    hasRowChanges = true;
                  }
                }
              }
            }
          });

          // 변경사항이 있는 행만 저장
          if (hasRowChanges) {
            dataToSave.TAG_NO = originalRow.TAG_NO; // TAG_NO는 절대 변경되지 않도록

            const { success } = await updateFormDataInDB(
              formCode,
              contractItemId,
              dataToSave
            );

            if (success) {
              updatedRows.push(dataToSave);
              saveCount++;
            }
          } else {
            updatedRows.push(originalRow); // 변경사항이 없으면 원본 유지
          }
        }

        if (saveCount > 0) {
          toast.success(`${saveCount} rows saved successfully!`);
          onUpdateSuccess?.(updatedRows);
        } else {
          toast.info("No changes to save");
        }
      }

      setHasChanges(false);
      setValidationErrors([]);

    } catch (error) {
      console.error("Error saving changes:", error);
      toast.error("An unexpected error occurred while saving");
    } finally {
      setIsPending(false);
    }
  }, [currentSpread, hasChanges, templateType, selectedRow, tableData, formCode, contractItemId, onUpdateSuccess, cellMappings, columnsJSON, validateAllData]);

  if (!isOpen) return null;

  // 데이터 유효성 검사
  const isDataValid = templateType === 'SPREAD_ITEM' ? !!selectedRow : tableData.length > 0;
  const dataCount = templateType === 'SPREAD_ITEM' ? 1 : tableData.length;
  

  return (
    <Dialog open={isOpen} onOpenChange={onClose}>
      <DialogContent
        className="w-[80%] max-w-none h-[80vh] flex flex-col"
        style={{ maxWidth: "80vw" }}
      >
        <DialogHeader className="flex-shrink-0">
          <DialogTitle>SEDP Template - {formCode}</DialogTitle>
          <DialogDescription>
            <div className="space-y-3">
              {/* 템플릿 선택 */}
              {availableTemplates.length > 1 && (
                <div className="flex items-center gap-4">
                  <span className="text-sm font-medium">Template:</span>
                  <Select value={selectedTemplateId} onValueChange={handleTemplateChange}>
                    <SelectTrigger className="w-64">
                      <SelectValue placeholder="Select a template" />
                    </SelectTrigger>
                    <SelectContent>
                      {availableTemplates.map(template => (
                        <SelectItem key={template.TMPL_ID} value={template.TMPL_ID}>
                          {template.NAME} ({
                           template.TMPL_TYPE
                          })
                        </SelectItem>
                      ))}
                    </SelectContent>
                  </Select>
                </div>
              )}

              {/* 템플릿 정보 */}
              {selectedTemplate && (
                <div className="flex items-center gap-4 text-sm">
                  <span className="font-medium text-blue-600">
                    Template Type: {
                      templateType === 'SPREAD_LIST' ? 'List View (SPREAD_LIST)' :
                        templateType === 'SPREAD_ITEM' ? 'Item View (SPREAD_ITEM)' :
                          'Grid List View (GRD_LIST)'
                    }
                  </span>
                  {templateType === 'SPREAD_ITEM' && selectedRow && (
                    <span>• Selected TAG_NO: {selectedRow.TAG_NO || 'N/A'}</span>
                  )}
                  {(templateType === 'SPREAD_LIST' || templateType === 'GRD_LIST') && (
                    <span>• {dataCount} rows</span>
                  )}
                  {hasChanges && (
                    <span className="text-orange-600 font-medium">
                      • Unsaved changes
                    </span>
                  )}
                  {validationErrors.length > 0 && (
                    <span className="text-red-600 font-medium flex items-center">
                      <AlertTriangle className="w-4 h-4 mr-1" />
                      {validationErrors.length} validation errors
                    </span>
                  )}
                </div>
              )}

              {/* 범례 */}
              <div className="flex items-center gap-4 text-xs">
                <span className="text-muted-foreground">
                  <span className="inline-block w-3 h-3 bg-green-100 border border-green-400 mr-1"></span>
                  Editable fields
                </span>
                <span className="text-muted-foreground">
                  <span className="inline-block w-3 h-3 bg-gray-100 border border-gray-300 mr-1"></span>
                  Read-only fields
                </span>
                <span className="text-muted-foreground">
                  <span className="inline-block w-3 h-3 bg-red-100 border border-red-300 mr-1"></span>
                  Validation errors
                </span>
                {cellMappings.length > 0 && (
                  <span className="text-blue-600">
                    {editableFieldsCount} of {cellMappings.length} fields editable
                  </span>
                )}
              </div>
            </div>
          </DialogDescription>
        </DialogHeader>

        {/* SpreadSheets 컴포넌트 영역 */}
        <div className="flex-1 overflow-hidden">
          {selectedTemplate && isClient && isDataValid ? (
            <SpreadSheets
              key={`${templateType}-${selectedTemplate.TMPL_ID}-${selectedTemplateId}`}
              workbookInitialized={initSpread}
              hostStyle={hostStyle}
            />
          ) : (
            <div className="flex items-center justify-center h-full text-muted-foreground">
              {!isClient ? (
                <>
                  <Loader className="mr-2 h-4 w-4 animate-spin" />
                  Loading...
                </>
              ) : !selectedTemplate ? (
                "No template available"
              ) : !isDataValid ? (
                `No ${templateType === 'SPREAD_ITEM' ? 'selected row' : 'data'} available`
              ) : (
                "Template not ready"
              )}
            </div>
          )}
        </div>

        <DialogFooter className="flex-shrink-0">
          <div className="flex items-center gap-2">
            <Button variant="outline" onClick={onClose}>
              Close
            </Button>

            {hasChanges && (
              <Button
                variant="default"
                onClick={handleSaveChanges}
                disabled={isPending || validationErrors.length > 0}
              >
                {isPending ? (
                  <>
                    <Loader className="mr-2 h-4 w-4 animate-spin" />
                    Saving...
                  </>
                ) : (
                  <>
                    <Save className="mr-2 h-4 w-4" />
                    Save Changes
                  </>
                )}
              </Button>
            )}

            {validationErrors.length > 0 && (
              <Button
                variant="outline"
                onClick={validateAllData}
                className="text-red-600 border-red-300 hover:bg-red-50"
              >
                <AlertTriangle className="mr-2 h-4 w-4" />
                Check Errors ({validationErrors.length})
              </Button>
            )}
          </div>
        </DialogFooter>
      </DialogContent>
    </Dialog>
  );
}