gongchunyi
4 天以前 6f1acfd2086a1f8c2cb80afd3db4e534e44d545e
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
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
<template>
  <div class="app-container safety-facility-page">
    <el-tabs v-model="activeTab"
             class="facility-tabs"
             @tab-click="handleTabClick">
      <!-- 安全设施台账 -->
      <el-tab-pane label="安全设施台账" name="ledger">
        <div class="facility-toolbar">
          <div class="toolbar-filters">
            <span class="search_title">设施编号:</span>
            <el-input v-model="ledgerSearchForm.facilityCode"
                      style="width: 200px"
                      placeholder="请输入设施编号"
                      @change="handleLedgerQuery"
                      clearable
                      :prefix-icon="Search" />
            <span class="search_title ml10">设施名称:</span>
            <el-input v-model="ledgerSearchForm.facilityName"
                      style="width: 200px"
                      placeholder="请输入设施名称"
                      @change="handleLedgerQuery"
                      clearable
                      :prefix-icon="Search" />
            <span class="search_title ml10">设施类型:</span>
            <el-select v-model="ledgerSearchForm.facilityType"
                       clearable
                       @change="handleLedgerQuery"
                       style="width: 150px">
              <el-option label="消防器材" value="消防器材" />
              <el-option label="安全设备" value="安全设备" />
              <el-option label="防护用品" value="防护用品" />
              <el-option label="监控设备" value="监控设备" />
            </el-select>
            <span class="search_title ml10">状态:</span>
            <el-select v-model="ledgerSearchForm.status"
                       clearable
                       @change="handleLedgerQuery"
                       style="width: 120px">
              <el-option label="正常" value="正常" />
              <el-option label="异常" value="异常" />
              <el-option label="报废" value="报废" />
            </el-select>
            <el-button type="primary"
                       @click="handleLedgerQuery">
              搜索
            </el-button>
            <el-button @click="resetLedgerQuery">重置</el-button>
          </div>
          <div class="toolbar-actions">
            <el-button type="danger"
                       plain
                       :disabled="ledgerSelectedIds.length === 0"
                       @click="handleBatchDeleteLedger">批量删除</el-button>
            <el-button type="primary"
                       @click="openLedgerForm('add')">新增设施</el-button>
          </div>
        </div>
 
        <div class="table_list">
          <PIMTable rowKey="id"
                    :column="ledgerTableColumn"
                    :tableData="ledgerTableData"
                    :page="ledgerPage"
                    :isSelection="true"
                    @selection-change="handleLedgerSelectionChange"
                    :tableLoading="ledgerTableLoading"
                    @pagination="ledgerPagination"
                    :total="ledgerPage.total"></PIMTable>
        </div>
      </el-tab-pane>
 
      <!-- 巡检任务 -->
      <el-tab-pane label="巡检任务" name="task">
        <div class="facility-toolbar">
          <div class="toolbar-filters">
            <span class="search_title">巡检任务名称:</span>
            <el-input v-model="taskSearchForm.inspectionName"
                      style="width: 220px"
                      placeholder="请输入巡检任务名称"
                      @change="handleTaskQuery"
                      clearable
                      :prefix-icon="Search" />
            <span class="search_title ml10">设施名称:</span>
            <el-input v-model="taskSearchForm.facilityName"
                      style="width: 200px"
                      placeholder="请输入设施名称"
                      @change="handleTaskQuery"
                      clearable
                      :prefix-icon="Search" />
            <span class="search_title ml10">是否启用:</span>
            <el-select v-model="taskSearchForm.isEnabled"
                       clearable
                       @change="handleTaskQuery"
                       style="width: 150px">
              <el-option label="是" :value="1" />
              <el-option label="否" :value="0" />
            </el-select>
            <el-button type="primary"
                       @click="handleTaskQuery">
              搜索
            </el-button>
            <el-button @click="resetTaskQuery">重置</el-button>
          </div>
          <div class="toolbar-actions">
            <el-button type="danger"
                       plain
                       :disabled="taskSelectedIds.length === 0"
                       @click="handleBatchDeleteTask">删除</el-button>
            <el-button type="primary"
                       @click="openTaskDialog('add')">新增巡检任务</el-button>
          </div>
        </div>
 
        <div class="table_list">
          <PIMTable rowKey="id"
                    :column="taskTableColumn"
                    :tableData="taskTableData"
                    :page="taskPage"
                    :isSelection="true"
                    @selection-change="handleTaskSelectionChange"
                    :tableLoading="taskTableLoading"
                    @pagination="taskPagination"
                    :total="taskPage.total"></PIMTable>
        </div>
      </el-tab-pane>
 
      <!-- 巡检记录 -->
      <el-tab-pane label="巡检记录" name="inspection">
        <div class="facility-toolbar">
          <div class="toolbar-filters">
            <span class="search_title">巡检编号:</span>
            <el-input v-model="inspectionSearchForm.inspectionCode"
                      style="width: 200px"
                      placeholder="请输入巡检编号"
                      @change="handleInspectionQuery"
                      clearable
                      :prefix-icon="Search" />
            <span class="search_title ml10">状态:</span>
            <el-select v-model="inspectionSearchForm.status"
                       clearable
                       @change="handleInspectionQuery"
                       style="width: 150px">
              <el-option label="待巡检" value="待巡检" />
              <el-option label="已巡检" value="已巡检" />
            </el-select>
            <el-button type="primary"
                       @click="handleInspectionQuery">
              搜索
            </el-button>
            <el-button @click="resetInspectionQuery">重置</el-button>
          </div>
        </div>
 
        <div class="table_list">
          <PIMTable rowKey="id"
                    :column="inspectionTableColumn"
                    :tableData="inspectionTableData"
                    :page="inspectionPage"
                    :isSelection="false"
                    :tableLoading="inspectionTableLoading"
                    @pagination="inspectionPagination"
                    :total="inspectionPage.total"></PIMTable>
        </div>
      </el-tab-pane>
 
      <!-- 整改跟踪 -->
      <el-tab-pane label="整改跟踪" name="rectification">
        <div class="facility-toolbar">
          <div class="toolbar-filters">
            <span class="search_title">状态:</span>
            <el-select v-model="rectificationSearchForm.status"
                       clearable
                       @change="handleRectificationQuery"
                       style="width: 150px">
              <el-option label="待整改" value="待整改" />
              <el-option label="整改中" value="整改中" />
              <el-option label="已整改" value="已整改" />
              <el-option label="已验收" value="已验收" />
            </el-select>
            <el-button type="primary"
                       @click="handleRectificationQuery">
              搜索
            </el-button>
            <el-button @click="resetRectificationQuery">重置</el-button>
          </div>
        </div>
 
        <div class="table_list">
          <PIMTable rowKey="id"
                    :column="rectificationTableColumn"
                    :tableData="rectificationTableData"
                    :page="rectificationPage"
                    :isSelection="false"
                    :tableLoading="rectificationTableLoading"
                    @pagination="rectificationPagination"
                    :total="rectificationPage.total"></PIMTable>
        </div>
      </el-tab-pane>
    </el-tabs>
 
    <!-- 设施台账表单弹窗 -->
    <el-dialog v-model="ledgerDialogVisible"
               :title="ledgerDialogTitle"
               width="800px"
               :close-on-click-modal="false">
      <el-form ref="ledgerFormRef"
               :model="ledgerForm"
               :rules="ledgerRules"
               label-width="120px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="设施编号"
                          prop="facilityCode">
              <el-input v-model="ledgerForm.facilityCode"
                        placeholder="请输入设施编号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设施名称"
                          prop="facilityName">
              <el-input v-model="ledgerForm.facilityName"
                        placeholder="请输入设施名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="设施类型"
                          prop="facilityType">
              <el-select v-model="ledgerForm.facilityType"
                         placeholder="请选择设施类型"
                         style="width: 100%">
                <el-option label="消防器材" value="消防器材" />
                <el-option label="安全设备" value="安全设备" />
                <el-option label="防护用品" value="防护用品" />
                <el-option label="监控设备" value="监控设备" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="规格型号"
                          prop="facilitySpec">
              <el-input v-model="ledgerForm.facilitySpec"
                        placeholder="请输入规格型号" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="安装位置"
                          prop="installLocation">
              <el-input v-model="ledgerForm.installLocation"
                        placeholder="请输入安装位置" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="安装时间"
                          prop="installTime">
              <el-date-picker v-model="ledgerForm.installTime"
                              type="date"
                              placeholder="请选择安装时间"
                              format="YYYY-MM-DD"
                              value-format="YYYY-MM-DD"
                              style="width: 100%" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="生产日期"
                          prop="productionDate">
              <el-date-picker v-model="ledgerForm.productionDate"
                              type="date"
                              placeholder="请选择生产日期"
                              format="YYYY-MM-DD"
                              value-format="YYYY-MM-DD"
                              style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态"
                          prop="status">
              <el-select v-model="ledgerForm.status"
                         placeholder="请选择状态"
                         style="width: 100%">
                <el-option label="正常" value="正常" />
                <el-option label="异常" value="异常" />
                <el-option label="报废" value="报废" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="备注"
                      prop="remark">
          <el-input v-model="ledgerForm.remark"
                    type="textarea"
                    :rows="3"
                    placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button type="primary"
                     @click="submitLedgerForm">确定</el-button>
          <el-button @click="ledgerDialogVisible = false">取消</el-button>
        </span>
      </template>
    </el-dialog>
 
    <!-- 设施台账详情弹窗 -->
    <el-dialog v-model="ledgerDetailVisible"
               title="设施详情"
               width="760px"
               class="detail-dialog">
      <el-descriptions :column="2" border>
        <el-descriptions-item label="设施编号">{{ ledgerDetail.facilityCode || '-' }}</el-descriptions-item>
        <el-descriptions-item label="设施名称">{{ ledgerDetail.facilityName || '-' }}</el-descriptions-item>
        <el-descriptions-item label="设施类型">{{ ledgerDetail.facilityType || '-' }}</el-descriptions-item>
        <el-descriptions-item label="规格型号">{{ ledgerDetail.facilitySpec || '-' }}</el-descriptions-item>
        <el-descriptions-item label="安装位置">{{ ledgerDetail.installLocation || '-' }}</el-descriptions-item>
        <el-descriptions-item label="安装时间">{{ ledgerDetail.installTime || '-' }}</el-descriptions-item>
        <el-descriptions-item label="生产日期">{{ ledgerDetail.productionDate || '-' }}</el-descriptions-item>
        <el-descriptions-item label="状态">
          <el-tag :type="ledgerStatusType(ledgerDetail.status)">{{ ledgerDetail.status || '-' }}</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="备注" :span="2">{{ ledgerDetail.remark || '-' }}</el-descriptions-item>
      </el-descriptions>
      <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="ledgerDetailVisible = false">关闭</el-button>
        </span>
      </template>
    </el-dialog>
 
    <!-- 巡检任务表单弹窗 -->
    <el-dialog v-model="taskDialogVisible"
               :title="taskDialogTitle"
               width="920px"
               :close-on-click-modal="false"
               @closed="resetTaskForm">
      <el-form ref="taskFormRef"
               :model="taskForm"
               :rules="taskRules"
               label-width="120px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="巡检任务名称"
                          prop="inspectionName">
              <el-input v-model="taskForm.inspectionName"
                        placeholder="请输入巡检任务名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="安全设施"
                          prop="facilityId">
              <el-select v-model="taskForm.facilityId"
                         placeholder="请选择安全设施"
                         filterable
                         clearable
                         style="width: 100%"
                         @change="handleTaskFacilityChange">
                <el-option v-for="item in facilityOptions"
                           :key="item.id"
                           :label="`${item.facilityName}(${item.facilityCode || '-'})`"
                           :value="item.id" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="巡检人"
                          prop="inspectorId">
              <el-select v-model="taskForm.inspectorId"
                         placeholder="请选择巡检人"
                         filterable
                         clearable
                         style="width: 100%">
                <el-option v-for="item in userList"
                           :key="item.userId"
                           :label="item.nickName"
                           :value="item.userId" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否启用"
                          prop="isEnabled">
              <el-radio-group v-model="taskForm.isEnabled">
                <el-radio :label="1">是</el-radio>
                <el-radio :label="0">否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="巡检项目"
                          prop="inspectionProject">
              <el-input v-model="taskForm.inspectionProject"
                        type="textarea"
                        :autosize="{ minRows: 3, maxRows: 6 }"
                        placeholder="请输入巡检项目" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注">
              <el-input v-model="taskForm.remark"
                        type="textarea"
                        :autosize="{ minRows: 3, maxRows: 6 }"
                        placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="任务频率"
                          prop="frequencyType">
              <el-select v-model="taskForm.frequencyType"
                         placeholder="请选择"
                         clearable
                         style="width: 100%"
                         @change="handleFrequencyTypeChange">
                <el-option label="每日" value="DAILY" />
                <el-option label="每周" value="WEEKLY" />
                <el-option label="每月" value="MONTHLY" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col v-if="taskForm.frequencyType === 'DAILY'" :span="12">
            <el-form-item class="frequency-detail-item"
                          label="执行时间"
                          prop="frequencyDetail">
              <el-time-picker v-model="taskForm.frequencyDetail"
                              placeholder="选择时间"
                              format="HH:mm"
                              value-format="HH:mm"
                              style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col v-if="taskForm.frequencyType === 'WEEKLY'" :span="12">
            <el-form-item class="frequency-detail-item"
                          label="执行时间"
                          prop="frequencyDetail">
              <div class="frequency-row">
                <el-select v-model="taskForm.week"
                           class="frequency-day-select"
                           placeholder="请选择星期"
                           clearable>
                  <el-option v-for="item in weekOptions"
                             :key="item.value"
                             :label="item.label"
                             :value="item.value" />
                </el-select>
                <el-time-picker v-model="taskForm.time"
                                class="frequency-time-picker"
                                placeholder="选择时间"
                                format="HH:mm"
                                value-format="HH:mm" />
              </div>
            </el-form-item>
          </el-col>
          <el-col v-if="taskForm.frequencyType === 'MONTHLY'" :span="12">
            <el-form-item class="frequency-detail-item"
                          label="执行时间"
                          prop="frequencyDetail">
              <div class="frequency-row">
                <el-select v-model="taskForm.day"
                           class="frequency-day-select"
                           placeholder="请选择日期"
                           clearable>
                  <el-option v-for="item in dayOptions"
                             :key="item.value"
                             :label="item.label"
                             :value="item.value" />
                </el-select>
                <el-time-picker v-model="taskForm.time"
                                class="frequency-time-picker"
                                placeholder="选择时间"
                                format="HH:mm"
                                value-format="HH:mm" />
              </div>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button type="primary"
                     @click="submitTaskForm">保存</el-button>
          <el-button @click="taskDialogVisible = false">取消</el-button>
        </span>
      </template>
    </el-dialog>
 
    <!-- 巡检任务详情 -->
    <el-dialog v-model="taskDetailVisible"
               title="巡检任务详情"
               width="860px">
      <el-descriptions :column="2" border>
        <el-descriptions-item label="巡检任务名称">{{ taskDetail.inspectionName || '-' }}</el-descriptions-item>
        <el-descriptions-item label="安全设施">{{ taskDetail.facilityName || '-' }}</el-descriptions-item>
        <el-descriptions-item label="设施编号">{{ taskDetail.facilityCode || '-' }}</el-descriptions-item>
        <el-descriptions-item label="巡检人">{{ taskDetail.inspectorName || '-' }}</el-descriptions-item>
        <el-descriptions-item label="是否启用">{{ taskDetail.isEnabled === 1 ? '是' : '否' }}</el-descriptions-item>
        <el-descriptions-item label="频次">{{ frequencyTypeMap[taskDetail.frequencyType] || taskDetail.frequencyType || '-' }}</el-descriptions-item>
        <el-descriptions-item label="执行时间">{{ formatFrequencyDetail(taskDetail.frequencyDetail) }}</el-descriptions-item>
        <el-descriptions-item label="下次执行时间">{{ taskDetail.nextExecutionTime || '-' }}</el-descriptions-item>
        <el-descriptions-item label="巡检项目" :span="2">{{ taskDetail.inspectionProject || '-' }}</el-descriptions-item>
        <el-descriptions-item label="备注" :span="2">{{ taskDetail.remark || '-' }}</el-descriptions-item>
      </el-descriptions>
      <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="taskDetailVisible = false">关闭</el-button>
        </span>
      </template>
    </el-dialog>
 
    <!-- 历史巡检记录 -->
    <el-dialog v-model="taskHistoryVisible"
               title="历史巡检记录"
               width="1100px">
      <el-descriptions class="history-summary" :column="3" border>
        <el-descriptions-item label="巡检任务名称">{{ taskHistoryTask.inspectionName || '-' }}</el-descriptions-item>
        <el-descriptions-item label="安全设施">{{ taskHistoryTask.facilityName || '-' }}</el-descriptions-item>
        <el-descriptions-item label="频次">{{ frequencyTypeMap[taskHistoryTask.frequencyType] || taskHistoryTask.frequencyType || '-' }}</el-descriptions-item>
      </el-descriptions>
      <PIMTable rowKey="id"
                :column="taskHistoryTableColumn"
                :tableData="taskHistoryTableData"
                :page="taskHistoryPage"
                :isSelection="false"
                :tableLoading="taskHistoryTableLoading"
                @pagination="taskHistoryPagination"
                :total="taskHistoryPage.total"></PIMTable>
      <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="taskHistoryVisible = false">关闭</el-button>
        </span>
      </template>
    </el-dialog>
 
    <!-- 巡检表单弹窗 -->
    <el-dialog v-model="inspectionDialogVisible"
               :title="inspectionDialogTitle"
               width="720px"
               class="facility-form-dialog"
               :close-on-click-modal="false">
      <el-form ref="inspectionFormRef"
               :model="inspectionForm"
               :rules="inspectionRules"
               label-position="top"
               class="inspection-form">
        <div v-if="inspectionForm.facilityName || inspectionForm.facilityCode"
             class="dialog-summary">
          <div class="summary-item">
            <span>设施名称</span>
            <strong>{{ inspectionForm.facilityName || '-' }}</strong>
          </div>
          <div class="summary-item">
            <span>设施编号</span>
            <strong>{{ inspectionForm.facilityCode || '-' }}</strong>
          </div>
        </div>
        <div class="inspection-form-grid">
          <el-form-item label="巡检编号"
                        prop="inspectionCode">
            <el-input v-model="inspectionForm.inspectionCode"
                      placeholder="请输入巡检编号"
                      :disabled="isInspectionBaseReadonly" />
          </el-form-item>
          <el-form-item label="巡检类型"
                        prop="inspectionType">
            <el-select v-model="inspectionForm.inspectionType"
                       placeholder="请选择巡检类型"
                       style="width: 100%"
                       :disabled="isInspectionBaseReadonly">
              <el-option label="定期巡检" value="定期巡检" />
              <el-option label="临时巡检" value="临时巡检" />
            </el-select>
          </el-form-item>
          <el-form-item label="计划巡检时间"
                        prop="planTime"
                        class="form-grid-full">
            <el-date-picker v-model="inspectionForm.planTime"
                            type="datetime"
                            placeholder="请选择计划巡检时间"
                            format="YYYY-MM-DD HH:mm:ss"
                            value-format="YYYY-MM-DD HH:mm:ss"
                            style="width: 100%"
                            :disabled="isInspectionBaseReadonly" />
          </el-form-item>
          <el-form-item label="巡检项目"
                        class="form-grid-full">
            <el-input v-model="inspectionForm.inspectionProject"
                      type="textarea"
                      :rows="3"
                      disabled />
          </el-form-item>
        </div>
        <template v-if="inspectionDialogType !== 'add'">
          <el-form-item label="检查结果"
                        prop="checkResult">
            <el-radio-group v-model="inspectionForm.checkResult" :disabled="isInspectionView">
              <el-radio label="正常">正常</el-radio>
              <el-radio label="异常">异常</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="检查说明"
                        prop="checkDesc">
            <el-input v-model="inspectionForm.checkDesc"
                      type="textarea"
                      :rows="3"
                      placeholder="请输入检查说明"
                      :disabled="isInspectionView" />
          </el-form-item>
          <el-form-item v-if="inspectionForm.checkResult === '异常'" label="现场照片">
            <ImageUpload v-model:file-list="inspectionForm.storageBlobDTOs"
                         :limit="6"
                         button-text="上传照片"
                         :disabled="isInspectionView" />
          </el-form-item>
        </template>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button v-if="!isInspectionView"
                     type="primary"
                     @click="submitInspectionForm">确定</el-button>
          <el-button @click="inspectionDialogVisible = false">{{ isInspectionView ? '关闭' : '取消' }}</el-button>
        </span>
      </template>
    </el-dialog>
 
    <!-- 巡检详情弹窗 -->
    <el-dialog v-model="inspectionDetailVisible"
               title="巡检详情"
               width="920px"
               class="detail-dialog">
      <el-descriptions :column="3" border>
        <el-descriptions-item label="巡检编号">{{ inspectionDetail.inspectionCode || '-' }}</el-descriptions-item>
        <el-descriptions-item label="设施名称">{{ inspectionDetail.facilityName || '-' }}</el-descriptions-item>
        <el-descriptions-item label="巡检人">{{ inspectionDetail.inspectorName || '-' }}</el-descriptions-item>
        <el-descriptions-item label="计划巡检时间">{{ inspectionDetail.planTime || '-' }}</el-descriptions-item>
        <el-descriptions-item label="实际巡检时间">{{ inspectionDetail.actualTime || '-' }}</el-descriptions-item>
        <el-descriptions-item label="状态">
          <el-tag :type="inspectionStatusType(inspectionDetail.status)">{{ inspectionDetail.status || '-' }}</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="检查结果">
          <el-tag :type="inspectionResultType(inspectionDetail.checkResult)">{{ inspectionDetail.checkResult || '未填写' }}</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="巡检项目" :span="2">{{ inspectionDetail.inspectionProject || '-' }}</el-descriptions-item>
        <el-descriptions-item label="检查说明" :span="3">{{ inspectionDetail.checkDesc || '-' }}</el-descriptions-item>
      </el-descriptions>
      <div v-if="inspectionDetailImageUrls.length" class="detail-section">
        <div class="detail-section-title">现场照片</div>
        <div class="detail-image-list">
          <el-image v-for="(url, index) in inspectionDetailImageUrls"
                    :key="`${url}-${index}`"
                    :src="url"
                    :preview-src-list="inspectionDetailImageUrls"
                    :initial-index="index"
                    fit="cover"
                    class="detail-image" />
        </div>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="inspectionDetailVisible = false">关闭</el-button>
        </span>
      </template>
    </el-dialog>
 
    <!-- 整改表单弹窗 -->
    <el-dialog v-model="rectificationDialogVisible"
               :title="rectificationDialogTitle"
               width="600px"
               :close-on-click-modal="false">
      <el-form ref="rectificationFormRef"
               :model="rectificationForm"
               :rules="rectificationRules"
               label-width="120px">
        <div v-if="rectificationForm.facilityName || rectificationForm.inspectionCode"
             class="dialog-summary">
          <div class="summary-item">
            <span>设施名称</span>
            <strong>{{ rectificationForm.facilityName || '-' }}</strong>
          </div>
          <div class="summary-item">
            <span>巡检编号</span>
            <strong>{{ rectificationForm.inspectionCode || '-' }}</strong>
          </div>
        </div>
        <el-form-item label="问题描述"
                      :prop="isRectificationCreate ? 'problemDesc' : undefined">
          <el-input v-model="rectificationForm.problemDesc"
                    type="textarea"
                    :rows="3"
                    placeholder="请输入问题描述"
                    :disabled="!isRectificationCreate" />
        </el-form-item>
        <el-form-item v-if="rectificationInspectionImageUrls.length"
                      label="巡检异常图片">
          <div class="rectification-image-list">
            <el-image v-for="(url, index) in rectificationInspectionImageUrls"
                      :key="`${url}-${index}`"
                      :src="url"
                      :preview-src-list="rectificationInspectionImageUrls"
                      :initial-index="index"
                      fit="cover"
                      class="rectification-image" />
          </div>
        </el-form-item>
        <el-form-item label="问题等级"
                      :prop="isRectificationCreate ? 'problemLevel' : undefined">
          <el-select v-model="rectificationForm.problemLevel"
                     placeholder="请选择问题等级"
                     style="width: 100%"
                     :disabled="!isRectificationCreate">
            <el-option label="一般" value="一般" />
            <el-option label="重大" value="重大" />
          </el-select>
        </el-form-item>
        <el-form-item label="计划整改时间"
                      :prop="isRectificationCreate ? 'planTime' : undefined">
          <el-date-picker v-model="rectificationForm.planTime"
                          type="datetime"
                          placeholder="请选择计划整改时间"
                          format="YYYY-MM-DD HH:mm:ss"
                          value-format="YYYY-MM-DD HH:mm:ss"
                          style="width: 100%"
                          :disabled="!isRectificationCreate" />
        </el-form-item>
        <el-form-item label="整改责任人"
                      :prop="isRectificationCreate ? 'rectifyUserId' : undefined">
          <el-select v-if="isRectificationCreate"
                     v-model="rectificationForm.rectifyUserId"
                     placeholder="请选择整改责任人"
                     filterable
                     clearable
                     style="width: 100%">
            <el-option v-for="item in userList"
                       :key="item.userId"
                       :label="item.nickName"
                       :value="item.userId" />
          </el-select>
          <el-input v-else
                    :model-value="rectificationForm.rectifyUserName || getUserName(rectificationForm.rectifyUserId) || '-'"
                    disabled />
        </el-form-item>
        <el-form-item v-if="showVerifyUser"
                      label="验收人"
                      :prop="isRectificationFix ? 'verifyUserId' : undefined">
          <el-select v-if="isRectificationFix"
                     v-model="rectificationForm.verifyUserId"
                     placeholder="请选择验收人"
                     filterable
                     clearable
                     style="width: 100%">
            <el-option v-for="item in userList"
                       :key="item.userId"
                       :label="item.nickName"
                       :value="item.userId" />
          </el-select>
          <el-input v-else
                    :model-value="rectificationForm.verifyUserName || getUserName(rectificationForm.verifyUserId) || '-'"
                    disabled />
        </el-form-item>
        <el-form-item v-if="showRectifyDesc"
                      label="整改说明"
                      :prop="isRectificationFix ? 'rectifyDesc' : undefined">
          <el-input v-model="rectificationForm.rectifyDesc"
                    type="textarea"
                    :rows="3"
                    placeholder="请输入整改说明"
                    :disabled="!isRectificationFix" />
        </el-form-item>
        <el-form-item v-if="showVerifyDesc"
                      label="验收说明"
                      :prop="isRectificationVerify ? 'verifyDesc' : undefined">
          <el-input v-model="rectificationForm.verifyDesc"
                    type="textarea"
                    :rows="3"
                    placeholder="请输入验收说明"
                    :disabled="!isRectificationVerify" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button v-if="!isRectificationView"
                     type="primary"
                     @click="submitRectificationForm">确定</el-button>
          <el-button @click="rectificationDialogVisible = false">{{ isRectificationView ? '关闭' : '取消' }}</el-button>
        </span>
      </template>
    </el-dialog>
 
    <!-- 整改详情弹窗 -->
    <el-dialog v-model="rectificationDetailVisible"
               title="整改详情"
               width="920px"
               class="detail-dialog">
      <el-descriptions :column="3" border>
        <el-descriptions-item label="巡检编号">{{ rectificationDetail.inspectionCode || '-' }}</el-descriptions-item>
        <el-descriptions-item label="设施名称">{{ rectificationDetail.facilityName || '-' }}</el-descriptions-item>
        <el-descriptions-item label="问题等级">
          <el-tag :type="rectificationLevelType(rectificationDetail.problemLevel)">
            {{ rectificationDetail.problemLevel || '-' }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="整改责任人">{{ rectificationDetail.rectifyUserName || getUserName(rectificationDetail.rectifyUserId) || '-' }}</el-descriptions-item>
        <el-descriptions-item label="计划整改时间">{{ rectificationDetail.planTime || '-' }}</el-descriptions-item>
        <el-descriptions-item label="实际整改时间">{{ rectificationDetail.actualTime || '-' }}</el-descriptions-item>
        <el-descriptions-item label="状态">
          <el-tag :type="rectificationStatusType(rectificationDetail.status)">{{ rectificationDetail.status || '-' }}</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="验收人">{{ rectificationDetail.verifyUserName || getUserName(rectificationDetail.verifyUserId) || '-' }}</el-descriptions-item>
        <el-descriptions-item label="验收时间">{{ rectificationDetail.verifyTime || '-' }}</el-descriptions-item>
        <el-descriptions-item label="问题描述" :span="3">{{ rectificationDetail.problemDesc || '-' }}</el-descriptions-item>
        <el-descriptions-item label="整改说明" :span="3">{{ rectificationDetail.rectifyDesc || '-' }}</el-descriptions-item>
        <el-descriptions-item label="验收说明" :span="3">{{ rectificationDetail.verifyDesc || '-' }}</el-descriptions-item>
      </el-descriptions>
      <div v-if="rectificationDetailImageUrls.length" class="detail-section">
        <div class="detail-section-title">巡检异常图片</div>
        <div class="detail-image-list">
          <el-image v-for="(url, index) in rectificationDetailImageUrls"
                    :key="`${url}-${index}`"
                    :src="url"
                    :preview-src-list="rectificationDetailImageUrls"
                    :initial-index="index"
                    fit="cover"
                    class="detail-image" />
        </div>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="rectificationDetailVisible = false">关闭</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
 
<script setup>
import { Search } from "@element-plus/icons-vue";
import { onMounted, ref, reactive, toRefs, computed, provide } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import PIMTable from "@/components/PIMTable/PIMTable.vue";
import ImageUpload from "@/components/AttachmentUpload/image/index.vue";
import { userListNoPage } from "@/api/system/user.js";
import useUserStore from "@/store/modules/user";
import {
  getFacilityLedgerList,
  addFacilityLedger,
  updateFacilityLedger,
  deleteFacilityLedger,
  getFacilityInspectionTaskList,
  addFacilityInspectionTask,
  updateFacilityInspectionTask,
  deleteFacilityInspectionTask,
  getFacilityInspectionList,
  updateFacilityInspection,
  deleteFacilityInspection,
  getFacilityRectificationList,
  addFacilityRectification,
  updateFacilityRectification
} from "@/api/safeProduction/safetyFacility";
 
const userStore = useUserStore();
const userList = ref([]);
const currentUserId = computed(() => Number(userStore.id) || null);
const ENABLED = 1;
 
const weekOptions = [
  { label: "周一", value: "MON" },
  { label: "周二", value: "TUE" },
  { label: "周三", value: "WED" },
  { label: "周四", value: "THU" },
  { label: "周五", value: "FRI" },
  { label: "周六", value: "SAT" },
  { label: "周日", value: "SUN" }
];
 
const dayOptions = Array.from({ length: 31 }, (_, index) => {
  const value = String(index + 1).padStart(2, "0");
  return { label: `${index + 1}日`, value };
});
 
const frequencyTypeMap = {
  DAILY: "每日",
  WEEKLY: "每周",
  MONTHLY: "每月"
};
 
const weekLabelMap = weekOptions.reduce((map, item) => {
  map[item.value] = item.label;
  return map;
}, {});
 
const createTaskForm = () => ({
  id: null,
  inspectionName: "",
  facilityId: null,
  facilityCode: "",
  facilityName: "",
  inspectionProject: "",
  inspectorId: "",
  frequencyType: "",
  frequencyDetail: "",
  week: "",
  day: "",
  time: "",
  isEnabled: ENABLED,
  remark: ""
});
 
const validateFrequencyDetail = (rule, value, callback) => {
  if (!taskForm.value.frequencyType) {
    callback(new Error("请选择任务频率"));
    return;
  }
  if (taskForm.value.frequencyType === "DAILY" && !taskForm.value.frequencyDetail) {
    callback(new Error("请选择执行时间"));
    return;
  }
  if (taskForm.value.frequencyType === "WEEKLY" && (!taskForm.value.week || !taskForm.value.time)) {
    callback(new Error("请选择星期和时间"));
    return;
  }
  if (taskForm.value.frequencyType === "MONTHLY" && (!taskForm.value.day || !taskForm.value.time)) {
    callback(new Error("请选择日期和时间"));
    return;
  }
  callback();
};
 
// 表单验证规则
const ledgerRules = {
  facilityCode: [{ required: true, message: "请输入设施编号", trigger: "blur" }],
  facilityName: [{ required: true, message: "请输入设施名称", trigger: "blur" }],
  facilityType: [{ required: true, message: "请选择设施类型", trigger: "change" }],
  installLocation: [{ required: true, message: "请输入安装位置", trigger: "blur" }]
};
 
const taskRules = {
  inspectionName: [{ required: true, message: "请输入巡检任务名称", trigger: "blur" }],
  facilityId: [{ required: true, message: "请选择安全设施", trigger: "change" }],
  inspectionProject: [{ required: true, message: "请输入巡检项目", trigger: "blur" }],
  inspectorId: [{ required: true, message: "请选择巡检人", trigger: "change" }],
  frequencyType: [{ required: true, message: "请选择任务频率", trigger: "change" }],
  frequencyDetail: [{ required: true, validator: validateFrequencyDetail, trigger: "change" }]
};
 
const inspectionRules = {
  inspectionCode: [{ required: true, message: "请输入巡检编号", trigger: "blur" }],
  inspectionType: [{ required: true, message: "请选择巡检类型", trigger: "change" }],
  planTime: [{ required: true, message: "请选择计划巡检时间", trigger: "change" }],
  checkResult: [{ required: true, message: "请选择检查结果", trigger: "change" }]
};
 
const rectificationRules = {
  problemDesc: [{ required: true, message: "请输入问题描述", trigger: "blur" }],
  problemLevel: [{ required: true, message: "请选择问题等级", trigger: "change" }],
  planTime: [{ required: true, message: "请选择计划整改时间", trigger: "change" }],
  rectifyUserId: [{ required: true, message: "请选择整改责任人", trigger: "change" }],
  verifyUserId: [{ required: true, message: "请选择验收人", trigger: "change" }],
  rectifyDesc: [{ required: true, message: "请输入整改说明", trigger: "blur" }],
  verifyDesc: [{ required: true, message: "请输入验收说明", trigger: "blur" }]
};
 
// 响应式数据
const data = reactive({
  activeTab: "ledger",
  // 设施台账
  ledgerSearchForm: { facilityCode: "", facilityName: "", facilityType: "", status: "" },
  ledgerTableLoading: false,
  ledgerPage: { current: 1, size: 20, total: 0 },
  ledgerTableData: [],
  ledgerSelectedIds: [],
  ledgerForm: {
    id: null, facilityCode: "", facilityName: "", facilityType: "", facilitySpec: "",
    installLocation: "", installTime: "", productionDate: "",
    status: "正常", remark: ""
  },
  ledgerDialogVisible: false,
  ledgerDialogTitle: "",
  ledgerDialogType: "add",
  ledgerDetailVisible: false,
  ledgerDetail: {},
  // 巡检任务
  taskSearchForm: { inspectionName: "", facilityName: "", isEnabled: "" },
  taskTableLoading: false,
  taskPage: { current: 1, size: 20, total: 0 },
  taskTableData: [],
  taskSelectedIds: [],
  taskForm: createTaskForm(),
  taskDialogVisible: false,
  taskDialogTitle: "新增巡检任务",
  taskDetailVisible: false,
  taskDetail: {},
  taskHistoryVisible: false,
  taskHistoryTask: {},
  taskHistoryTableLoading: false,
  taskHistoryPage: { current: 1, size: 10, total: 0 },
  taskHistoryTableData: [],
  facilityOptions: [],
  // 巡检记录
  inspectionSearchForm: { inspectionCode: "", status: "" },
  inspectionTableLoading: false,
  inspectionPage: { current: 1, size: 20, total: 0 },
  inspectionTableData: [],
  inspectionForm: {
    id: null, scheduleTaskId: null, facilityId: null, inspectionCode: "", inspectionType: "定期巡检",
    facilityCode: "", facilityName: "", inspectionProject: "", planTime: "", checkResult: "", checkDesc: "", storageBlobDTOs: []
  },
  inspectionDialogVisible: false,
  inspectionDialogTitle: "",
  inspectionDialogType: "do",
  inspectionDetailVisible: false,
  inspectionDetail: {},
  // 整改跟踪
  rectificationSearchForm: { status: "" },
  rectificationTableLoading: false,
  rectificationPage: { current: 1, size: 20, total: 0 },
  rectificationTableData: [],
  rectificationForm: {
    id: null, inspectionId: null, facilityId: null, problemDesc: "",
    problemLevel: "", rectifyUserId: "", rectifyUserName: "", verifyUserId: "",
    verifyUserName: "", facilityName: "", facilityCode: "", inspectionCode: "",
    planTime: "", actualTime: "", rectifyDesc: "", verifyTime: "", verifyDesc: "",
    status: "", storageBlobVOs: []
  },
  rectificationDialogVisible: false,
  rectificationDialogTitle: "",
  rectificationDialogType: "add",
  rectificationDetailVisible: false,
  rectificationDetail: {}
});
 
const {
  activeTab,
  ledgerSearchForm, ledgerTableLoading, ledgerPage, ledgerTableData, ledgerSelectedIds,
  ledgerForm, ledgerDialogVisible, ledgerDialogTitle, ledgerDialogType, ledgerDetailVisible, ledgerDetail,
  taskSearchForm, taskTableLoading, taskPage, taskTableData, taskSelectedIds,
  taskForm, taskDialogVisible, taskDialogTitle, taskDetailVisible, taskDetail,
  taskHistoryVisible, taskHistoryTask, taskHistoryTableLoading, taskHistoryPage, taskHistoryTableData,
  facilityOptions,
  inspectionSearchForm, inspectionTableLoading, inspectionPage, inspectionTableData,
  inspectionForm, inspectionDialogVisible, inspectionDialogTitle, inspectionDialogType, inspectionDetailVisible, inspectionDetail,
  rectificationSearchForm, rectificationTableLoading, rectificationPage, rectificationTableData,
  rectificationForm, rectificationDialogVisible, rectificationDialogTitle, rectificationDialogType,
  rectificationDetailVisible, rectificationDetail
} = toRefs(data);
 
// 表单引用
const ledgerFormRef = ref();
const taskFormRef = ref();
const inspectionFormRef = ref();
const rectificationFormRef = ref();
const isInspectionView = computed(() => inspectionDialogType.value === "view");
const isInspectionBaseReadonly = computed(() => inspectionDialogType.value !== "add");
const isRectificationView = computed(() => rectificationDialogType.value === "view");
const isRectificationCreate = computed(() => rectificationDialogType.value === "add");
const isRectificationFix = computed(() => rectificationDialogType.value === "do");
const isRectificationVerify = computed(() => rectificationDialogType.value === "verify");
const showRectifyDesc = computed(
  () =>
    isRectificationFix.value ||
    isRectificationVerify.value ||
    (isRectificationView.value && !!rectificationForm.value.rectifyDesc)
);
const showVerifyDesc = computed(
  () =>
    isRectificationVerify.value ||
    (isRectificationView.value &&
      (rectificationForm.value.status === "已验收" || !!rectificationForm.value.verifyDesc))
);
const showVerifyUser = computed(
  () =>
    isRectificationFix.value ||
    isRectificationVerify.value ||
    isRectificationView.value ||
    !!rectificationForm.value.verifyUserId
);
const getAttachmentUrl = item => {
  if (!item) return "";
  if (typeof item === "string") return item;
  return item.url || item.previewURL || item.previewUrl || item.downloadURL || "";
};
const rectificationInspectionImageUrls = computed(() => {
  const list = rectificationForm.value.storageBlobVOs || rectificationForm.value.storageBlobDTOs || [];
  return list.map(getAttachmentUrl).filter(Boolean);
});
const inspectionDetailImageUrls = computed(() => {
  const list = inspectionDetail.value.storageBlobVOs || inspectionDetail.value.storageBlobDTOs || [];
  return list.map(getAttachmentUrl).filter(Boolean);
});
const rectificationDetailImageUrls = computed(() => {
  const list = rectificationDetail.value.storageBlobVOs || rectificationDetail.value.storageBlobDTOs || [];
  return list.map(getAttachmentUrl).filter(Boolean);
});
 
const ledgerStatusType = value => {
  const map = { 正常: "success", 异常: "danger", 报废: "info" };
  return map[value] || "info";
};
const inspectionResultType = value => {
  const map = { 正常: "success", 异常: "danger", 未填写: "info" };
  return map[value || "未填写"] || "info";
};
const inspectionStatusType = value => value === "待巡检" ? "info" : "success";
const rectificationLevelType = value => value === "重大" ? "danger" : "warning";
const rectificationStatusType = value => {
  const map = { 待整改: "danger", 整改中: "warning", 已整改: "success", 已验收: "info" };
  return map[value] || "info";
};
 
const padTime = value => String(value).padStart(2, "0");
const formatDateTime = (date = new Date()) => {
  const target = date instanceof Date ? date : new Date(date);
  return `${target.getFullYear()}-${padTime(target.getMonth() + 1)}-${padTime(target.getDate())} ${padTime(target.getHours())}:${padTime(target.getMinutes())}:${padTime(target.getSeconds())}`;
};
const addDaysDateTime = days => {
  const target = new Date();
  target.setDate(target.getDate() + days);
  target.setHours(18, 0, 0, 0);
  return formatDateTime(target);
};
const getUserName = userId => {
  const user = userList.value.find(item => Number(item.userId) === Number(userId));
  return user?.nickName || "";
};
const formatFrequencyDetail = value => {
  if (!value) return "--";
  if (!value.includes(",")) return value;
  const [firstPart, timePart] = value.split(",");
  if (weekLabelMap[firstPart]) {
    return `${weekLabelMap[firstPart]} ${timePart || ""}`.trim();
  }
  if (firstPart) {
    return `${Number(firstPart)}日 ${timePart || ""}`.trim();
  }
  return value;
};
const isPendingInspection = row => row?.status === "待巡检";
const isCheckedInspection = row => row?.status === "已巡检";
const isAbnormalInspection = row => row?.checkResult === "异常";
const isPendingRectification = row => row?.status === "待整改" || row?.status === "整改中";
const isRectified = row => row?.status === "已整改";
const isNotAssignedVerifier = row =>
  row?.verifyUserId && Number(row.verifyUserId) !== Number(currentUserId.value);
 
provide("parentMethods", {
  openLedgerDetail: row => openLedgerDetail(row),
  openTaskDetail: row => openTaskDetail(row),
  openInspectionDetail: row => openInspectionDetail(row)
});
 
// 设施台账表格列配置
const ledgerTableColumn = ref([
  {
    label: "设施编号",
    prop: "facilityCode",
    dataType: "link",
    linkMethod: "openLedgerDetail",
    showOverflowTooltip: true,
    minWidth: 120,
    align: "center",
  },
  { label: "设施名称", prop: "facilityName", showOverflowTooltip: true, minWidth: 150 , align: "center"},
  { label: "设施类型", prop: "facilityType", showOverflowTooltip: true, minWidth: 120 , align: "center"},
  { label: "安装位置", prop: "installLocation", showOverflowTooltip: true, minWidth: 150 , align: "center"},
  { label: "安装时间", prop: "installTime", minWidth: 120 , align: "center"},
  {
    label: "状态", prop: "status", minWidth: 100, dataType: "tag", align: "center",
    formatType: params => {
      const map = { 正常: "success", 异常: "danger", 报废: "info" };
      return map[params] || "info";
    }
  },
  {
    dataType: "action", label: "操作", align: "center", fixed: "right", width: 140,
    operation: [
      { name: "编辑", type: "text", clickFun: row => openLedgerForm("edit", row) },
      { name: "删除", type: "text", clickFun: row => handleDeleteLedger(row) }
    ]
  }
]);
 
// 巡检任务表格列配置
const taskTableColumn = ref([
  {
    label: "巡检任务名称",
    prop: "inspectionName",
    dataType: "link",
    linkMethod: "openTaskDetail",
    showOverflowTooltip: true,
    minWidth: 170,
    align: "left"
  },
  { label: "设施名称", prop: "facilityName", showOverflowTooltip: true, minWidth: 155, align: "left" },
  { label: "巡检项目", prop: "inspectionProject", showOverflowTooltip: true, minWidth: 190, align: "left" },
  { label: "执行巡检人", prop: "inspectorName", minWidth: 120, align: "center" },
  {
    label: "是否启用",
    prop: "isEnabled",
    minWidth: 90,
    align: "center",
    dataType: "tag",
    formatData: value => value === 1 ? "是" : "否",
    formatType: value => value === 1 ? "success" : "info"
  },
  {
    label: "频次",
    prop: "frequencyType",
    minWidth: 90,
    align: "center",
    formatData: value => frequencyTypeMap[value] || value || "--"
  },
  {
    label: "执行时间",
    prop: "frequencyDetail",
    minWidth: 120,
    align: "center",
    formatData: value => formatFrequencyDetail(value)
  },
  { label: "下次执行时间", prop: "nextExecutionTime", minWidth: 170, showOverflowTooltip: true, align: "center" },
  {
    dataType: "action", label: "操作", align: "center", fixed: "right", width: 220,
    operation: [
      { name: "历史巡检", type: "text", clickFun: row => openTaskHistory(row) },
      { name: "编辑", type: "text", clickFun: row => openTaskDialog("edit", row) },
      { name: "删除", type: "text", clickFun: row => handleDeleteTask(row) }
    ]
  }
]);
 
const taskHistoryTableColumn = ref([
  {
    label: "巡检编号",
    prop: "inspectionCode",
    dataType: "link",
    linkMethod: "openInspectionDetail",
    showOverflowTooltip: true,
    minWidth: 180,
    align: "center"
  },
  { label: "设施名称", prop: "facilityName", showOverflowTooltip: true, minWidth: 150, align: "left" },
  { label: "巡检项目", prop: "inspectionProject", showOverflowTooltip: true, minWidth: 180, align: "left" },
  { label: "计划巡检时间", prop: "planTime", minWidth: 170, align: "center" },
  { label: "巡检人", prop: "inspectorName", minWidth: 110, align: "center" },
  { label: "实际巡检时间", prop: "actualTime", minWidth: 170, align: "center" },
  {
    label: "检查结果", prop: "checkResult", minWidth: 100, align: "center", dataType: "tag",
    formatData: params => params || "未填写",
    formatType: params => {
      const map = { 正常: "success", 异常: "danger", 未填写: "info" };
      return map[params] || "info";
    }
  },
  {
    label: "状态", prop: "status", minWidth: 100, align: "center", dataType: "tag",
    formatType: params => params === "待巡检" ? "info" : "success"
  }
]);
 
// 巡检记录表格列配置
const inspectionTableColumn = ref([
  {
    label: "巡检编号",
    dataType: "link",
    linkMethod: "openInspectionDetail",
    width: 200,
    align: "center",
    prop: "inspectionCode",
    showOverflowTooltip: true,
    minWidth: 150
  },
  {
    label: "设施名称",
    prop: "facilityName",
    showOverflowTooltip: true,
    minWidth: 150
  },
  {
    label: "巡检项目",
    prop: "inspectionProject",
    showOverflowTooltip: true,
    minWidth: 180
  },
  {
    label: "计划巡检时间",
    prop: "planTime",
    minWidth: 180
  },
  {
    label: "巡检人",
    prop: "inspectorName",
    minWidth: 100
  },
  {
    label: "实际巡检时间",
    prop: "actualTime",
    minWidth: 180
  },
  {
    label: "检查结果", prop: "checkResult", minWidth: 100, dataType: "tag",
    formatData: params => params || "未填写",
    formatType: params => {
      const map = { 正常: "success", 异常: "danger", 未填写: "info" };
      return map[params] || "info";
    }
  },
  {
    label: "状态", prop: "status", minWidth: 100, dataType: "tag",
    formatType: params => params === "待巡检" ? "info" : "success"
  },
  {
    dataType: "action", label: "操作", align: "center", fixed: "right", width: 200,
    operation: [
      {
        name: "巡检", type: "text",
        clickFun: row => openInspectionForm("do", row),
        show: isPendingInspection
      },
      {
        name: "查看", type: "text",
        clickFun: row => openInspectionDetail(row),
        show: isCheckedInspection
      },
      {
        name: "整改", type: "text",
        clickFun: row => openRectificationFromInspection(row),
        show: row => isCheckedInspection(row) && isAbnormalInspection(row)
      },
      { name: "删除", type: "text", clickFun: row => handleDeleteInspection(row), show: isPendingInspection }
    ]
  }
]);
 
// 整改跟踪表格列配置
const rectificationTableColumn = ref([
  { label: "巡检编号", prop: "inspectionCode", showOverflowTooltip: true, minWidth: 150 },
  { label: "设施名称", prop: "facilityName", showOverflowTooltip: true, minWidth: 150 },
  { label: "问题描述", prop: "problemDesc", showOverflowTooltip: true, minWidth: 200 },
  {
    label: "问题等级", prop: "problemLevel", minWidth: 100, dataType: "tag",
    formatType: params => params === "重大" ? "danger" : "warning"
  },
  { label: "整改责任人", prop: "rectifyUserName", minWidth: 120 },
  { label: "计划整改时间", prop: "planTime", minWidth: 180 },
  {
    label: "状态", prop: "status", minWidth: 100, dataType: "tag",
    formatType: params => {
      const map = { 待整改: "danger", 整改中: "warning", 已整改: "success", 已验收: "info" };
      return map[params] || "info";
    }
  },
  {
    dataType: "action", label: "操作", align: "center", fixed: "right", width: 180,
    operation: [
      {
        name: "整改", type: "text",
        clickFun: row => openRectificationForm("do", row),
        show: isPendingRectification
      },
      {
        name: "验收", type: "text",
        clickFun: row => openRectificationForm("verify", row),
        show: isRectified,
        disabled: isNotAssignedVerifier
      },
      { name: "查看", type: "text", clickFun: row => openRectificationDetail(row) }
    ]
  }
]);
 
// 生命周期
onMounted(() => {
  getLedgerList();
  getTaskList();
  getInspectionList();
  getRectificationList();
  getFacilityOptionList();
  getUserList();
});
 
// 标签页切换
const handleTabClick = () => {
  if (activeTab.value === "ledger") getLedgerList();
  else if (activeTab.value === "task") getTaskList();
  else if (activeTab.value === "inspection") getInspectionList();
  else if (activeTab.value === "rectification") getRectificationList();
};
 
const getUserList = () => {
  userListNoPage().then(res => {
    userList.value = res.data || [];
  });
};
 
const getFacilityOptionList = () => {
  getFacilityLedgerList({ current: 1, size: 1000 }).then(res => {
    facilityOptions.value = (res.data?.records || []).filter(item => item.status !== "报废");
  });
};
 
// ============ 设施台账方法 ============
 
const handleLedgerQuery = () => {
  ledgerPage.value.current = 1;
  getLedgerList();
};
 
const resetLedgerQuery = () => {
  Object.assign(ledgerSearchForm.value, {
    facilityCode: "",
    facilityName: "",
    facilityType: "",
    status: ""
  });
  handleLedgerQuery();
};
 
const getLedgerList = () => {
  ledgerTableLoading.value = true;
  getFacilityLedgerList({ ...ledgerPage.value, ...ledgerSearchForm.value })
    .then(res => {
      ledgerTableLoading.value = false;
      ledgerTableData.value = res.data?.records || [];
      ledgerPage.value.total = res.data?.total || 0;
    })
    .catch(() => { ledgerTableLoading.value = false; });
};
 
const ledgerPagination = obj => {
  ledgerPage.value.current = obj.page;
  ledgerPage.value.size = obj.limit;
  getLedgerList();
};
 
const handleLedgerSelectionChange = selection => {
  ledgerSelectedIds.value = selection.map(item => item.id);
};
 
const openLedgerDetail = row => {
  ledgerDetail.value = { ...row };
  ledgerDetailVisible.value = true;
};
 
const openLedgerForm = (type, row = null) => {
  ledgerDialogType.value = type;
  if (type === "add") {
    ledgerDialogTitle.value = "新增设施";
    Object.assign(ledgerForm.value, {
      id: null, facilityCode: "", facilityName: "", facilityType: "", facilitySpec: "",
      installLocation: "", installTime: "", productionDate: "",
      status: "正常", remark: ""
    });
  } else {
    ledgerDialogTitle.value = "编辑设施";
    Object.assign(ledgerForm.value, { ...row });
  }
  ledgerDialogVisible.value = true;
};
 
const submitLedgerForm = async () => {
  try {
    await ledgerFormRef.value.validate();
    const api = ledgerDialogType.value === "add" ? addFacilityLedger : updateFacilityLedger;
    const res = await api({ ...ledgerForm.value });
    if (res.code === 200) {
      ElMessage.success(ledgerDialogType.value === "add" ? "添加成功" : "更新成功");
      ledgerDialogVisible.value = false;
      getLedgerList();
      getFacilityOptionList();
    } else {
      ElMessage.error(res.msg || "操作失败");
    }
  } catch (error) {
    console.error("表单验证失败:", error);
  }
};
 
const handleDeleteLedger = row => {
  ElMessageBox.confirm("确认删除该设施吗?", "删除", {
    confirmButtonText: "确认", cancelButtonText: "取消", type: "warning"
  }).then(async () => {
    const res = await deleteFacilityLedger([row.id]);
    if (res.code === 200) {
      ElMessage.success("删除成功");
      getLedgerList();
      getFacilityOptionList();
    } else {
      ElMessage.error(res.msg || "删除失败");
    }
  }).catch(() => {});
};
 
const handleBatchDeleteLedger = () => {
  if (ledgerSelectedIds.value.length === 0) {
    ElMessage.warning("请选择要删除的设施");
    return;
  }
  ElMessageBox.confirm("确认删除选中的设施吗?已产生巡检记录的设施不会被后端允许删除。", "批量删除", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning"
  }).then(async () => {
    const res = await deleteFacilityLedger(ledgerSelectedIds.value);
    if (res.code === 200) {
      ElMessage.success("删除成功");
      ledgerSelectedIds.value = [];
      getLedgerList();
      getFacilityOptionList();
    } else {
      ElMessage.error(res.msg || "删除失败");
    }
  }).catch(() => {});
};
 
// ============ 巡检任务方法 ============
 
const handleTaskQuery = () => {
  taskPage.value.current = 1;
  getTaskList();
};
 
const resetTaskQuery = () => {
  Object.assign(taskSearchForm.value, {
    inspectionName: "",
    facilityName: "",
    isEnabled: ""
  });
  handleTaskQuery();
};
 
const getTaskList = () => {
  taskTableLoading.value = true;
  getFacilityInspectionTaskList({ ...taskPage.value, ...taskSearchForm.value })
    .then(res => {
      taskTableLoading.value = false;
      taskTableData.value = res.data?.records || [];
      taskPage.value.total = res.data?.total || 0;
    })
    .catch(() => { taskTableLoading.value = false; });
};
 
const taskPagination = obj => {
  taskPage.value.current = obj.page;
  taskPage.value.size = obj.limit;
  getTaskList();
};
 
const handleTaskSelectionChange = selection => {
  taskSelectedIds.value = selection.map(item => item.id);
};
 
const openTaskDetail = row => {
  taskDetail.value = { ...row };
  taskDetailVisible.value = true;
};
 
const openTaskDialog = (type, row = null) => {
  resetTaskForm();
  taskDialogTitle.value = type === "add" ? "新增巡检任务" : "编辑巡检任务";
  if (type === "edit" && row) {
    Object.assign(taskForm.value, {
      id: row.id,
      inspectionName: row.inspectionName,
      facilityId: row.facilityId,
      facilityCode: row.facilityCode,
      facilityName: row.facilityName,
      inspectionProject: row.inspectionProject,
      inspectorId: row.inspectorId,
      frequencyType: row.frequencyType,
      frequencyDetail: row.frequencyDetail,
      isEnabled: row.isEnabled ?? ENABLED,
      remark: row.remark
    });
    parseFrequencyDetail(row.frequencyType, row.frequencyDetail);
  }
  taskDialogVisible.value = true;
};
 
const resetTaskForm = () => {
  Object.assign(taskForm.value, createTaskForm());
  taskFormRef.value?.clearValidate?.();
};
 
const handleTaskFacilityChange = facilityId => {
  const facility = facilityOptions.value.find(item => Number(item.id) === Number(facilityId));
  taskForm.value.facilityCode = facility?.facilityCode || "";
  taskForm.value.facilityName = facility?.facilityName || "";
};
 
const handleFrequencyTypeChange = () => {
  Object.assign(taskForm.value, { frequencyDetail: "", week: "", day: "", time: "" });
  taskFormRef.value?.clearValidate?.("frequencyDetail");
};
 
const parseFrequencyDetail = (frequencyType, frequencyDetail) => {
  if (!frequencyDetail) return;
  if (frequencyType === "DAILY") {
    taskForm.value.frequencyDetail = frequencyDetail;
    return;
  }
  const [firstPart, timePart] = frequencyDetail.split(",");
  taskForm.value.frequencyDetail = "";
  taskForm.value.time = timePart || "";
  if (frequencyType === "WEEKLY") {
    taskForm.value.week = firstPart || "";
  }
  if (frequencyType === "MONTHLY") {
    taskForm.value.day = String(firstPart || "").padStart(2, "0");
  }
};
 
const buildFrequencyDetail = () => {
  if (taskForm.value.frequencyType === "DAILY") {
    return taskForm.value.frequencyDetail;
  }
  if (taskForm.value.frequencyType === "WEEKLY") {
    return `${taskForm.value.week},${taskForm.value.time}`;
  }
  if (taskForm.value.frequencyType === "MONTHLY") {
    return `${taskForm.value.day},${taskForm.value.time}`;
  }
  return "";
};
 
const submitTaskForm = async () => {
  try {
    await taskFormRef.value.validate();
    const payload = {
      id: taskForm.value.id,
      inspectionName: taskForm.value.inspectionName,
      facilityId: taskForm.value.facilityId,
      facilityCode: taskForm.value.facilityCode,
      facilityName: taskForm.value.facilityName,
      inspectionProject: taskForm.value.inspectionProject,
      inspectorId: taskForm.value.inspectorId,
      frequencyType: taskForm.value.frequencyType,
      frequencyDetail: buildFrequencyDetail(),
      isEnabled: taskForm.value.isEnabled,
      remark: taskForm.value.remark
    };
    const api = payload.id ? updateFacilityInspectionTask : addFacilityInspectionTask;
    const res = await api(payload);
    if (res.code === 200) {
      ElMessage.success(payload.id ? "修改成功" : "新增成功");
      taskDialogVisible.value = false;
      activeTab.value = "task";
      getTaskList();
    } else {
      ElMessage.error(res.msg || "操作失败");
    }
  } catch (error) {
    console.error("任务表单校验失败:", error);
  }
};
 
const handleDeleteTask = row => {
  ElMessageBox.confirm("确认删除该定时巡检任务吗?", "删除", {
    confirmButtonText: "确认", cancelButtonText: "取消", type: "warning"
  }).then(async () => {
    const res = await deleteFacilityInspectionTask([row.id]);
    if (res.code === 200) {
      ElMessage.success("删除成功");
      getTaskList();
    } else {
      ElMessage.error(res.msg || "删除失败");
    }
  }).catch(() => {});
};
 
const handleBatchDeleteTask = () => {
  if (taskSelectedIds.value.length === 0) {
    ElMessage.warning("请选择要删除的巡检任务");
    return;
  }
  ElMessageBox.confirm("确认删除选中的定时巡检任务吗?", "批量删除", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning"
  }).then(async () => {
    const res = await deleteFacilityInspectionTask(taskSelectedIds.value);
    if (res.code === 200) {
      ElMessage.success("删除成功");
      taskSelectedIds.value = [];
      getTaskList();
    } else {
      ElMessage.error(res.msg || "删除失败");
    }
  }).catch(() => {});
};
 
const openTaskHistory = row => {
  taskHistoryTask.value = { ...row };
  taskHistoryPage.value.current = 1;
  taskHistoryVisible.value = true;
  getTaskHistoryList();
};
 
const getTaskHistoryList = () => {
  if (!taskHistoryTask.value?.id) {
    taskHistoryTableData.value = [];
    taskHistoryPage.value.total = 0;
    return;
  }
  taskHistoryTableLoading.value = true;
  getFacilityInspectionList({
    current: taskHistoryPage.value.current,
    size: taskHistoryPage.value.size,
    scheduleTaskId: taskHistoryTask.value.id
  })
    .then(res => {
      taskHistoryTableLoading.value = false;
      taskHistoryTableData.value = res.data?.records || [];
      taskHistoryPage.value.total = res.data?.total || 0;
    })
    .catch(() => { taskHistoryTableLoading.value = false; });
};
 
const taskHistoryPagination = obj => {
  taskHistoryPage.value.current = obj.page;
  taskHistoryPage.value.size = obj.limit;
  getTaskHistoryList();
};
 
// ============ 巡检记录方法 ============
 
const handleInspectionQuery = () => {
  inspectionPage.value.current = 1;
  getInspectionList();
};
 
const resetInspectionQuery = () => {
  Object.assign(inspectionSearchForm.value, {
    inspectionCode: "",
    status: ""
  });
  handleInspectionQuery();
};
 
const getInspectionList = () => {
  inspectionTableLoading.value = true;
  getFacilityInspectionList({ ...inspectionPage.value, ...inspectionSearchForm.value })
    .then(res => {
      inspectionTableLoading.value = false;
      inspectionTableData.value = res.data?.records || [];
      inspectionPage.value.total = res.data?.total || 0;
    })
    .catch(() => { inspectionTableLoading.value = false; });
};
 
const inspectionPagination = obj => {
  inspectionPage.value.current = obj.page;
  inspectionPage.value.size = obj.limit;
  getInspectionList();
};
 
const openInspectionDetail = row => {
  inspectionDetail.value = {
    ...row,
    storageBlobDTOs: row.storageBlobVOs || row.storageBlobDTOs || []
  };
  inspectionDetailVisible.value = true;
};
 
const openInspectionForm = (type, row = null) => {
  if (type === "view") {
    openInspectionDetail(row);
    return;
  }
  inspectionDialogType.value = type;
  if (type === "do") {
    inspectionDialogTitle.value = "巡检";
    Object.assign(inspectionForm.value, {
      id: row.id,
      scheduleTaskId: row.scheduleTaskId,
      facilityId: row.facilityId,
      facilityCode: row.facilityCode,
      facilityName: row.facilityName,
      inspectionCode: row.inspectionCode,
      inspectionType: row.inspectionType,
      inspectionProject: row.inspectionProject,
      planTime: row.planTime,
      checkResult: "",
      checkDesc: "",
      storageBlobDTOs: []
    });
  }
  inspectionDialogVisible.value = true;
};
 
const submitInspectionForm = async () => {
  try {
    await inspectionFormRef.value.validate();
    if (inspectionDialogType.value === "do") {
      if (!inspectionForm.value.checkResult) {
        ElMessage.warning("请选择检查结果");
        return;
      }
      if (inspectionForm.value.checkResult === "异常" && !inspectionForm.value.checkDesc) {
        ElMessage.warning("检查结果异常时请填写检查说明");
        return;
      }
      if (inspectionForm.value.checkResult === "异常" && !hasAttachments(inspectionForm.value.storageBlobDTOs)) {
        ElMessage.warning("检查结果异常时请上传现场照片");
        return;
      }
      inspectionForm.value.status = "已巡检";
      inspectionForm.value.actualTime = formatDateTime();
    }
    const res = await updateFacilityInspection({ ...inspectionForm.value });
    if (res.code === 200) {
      ElMessage.success("操作成功");
      inspectionDialogVisible.value = false;
      getInspectionList();
      if (inspectionDialogType.value === "do" && inspectionForm.value.checkResult === "异常") {
        ElMessageBox.confirm("本次巡检结果为异常,是否立即创建整改跟踪?", "创建整改", {
          confirmButtonText: "创建整改",
          cancelButtonText: "稍后处理",
          type: "warning"
        }).then(() => {
          openRectificationFromInspection({ ...inspectionForm.value });
        }).catch(() => {});
      }
    } else {
      ElMessage.error(res.msg || "操作失败");
    }
  } catch (error) {
    console.error("表单验证失败:", error);
  }
};
 
const hasAttachments = attachments => Array.isArray(attachments) && attachments.length > 0;
 
const handleDeleteInspection = row => {
  ElMessageBox.confirm("确认删除该巡检记录吗?", "删除", {
    confirmButtonText: "确认", cancelButtonText: "取消", type: "warning"
  }).then(async () => {
    const res = await deleteFacilityInspection([row.id]);
    if (res.code === 200) {
      ElMessage.success("删除成功");
      getInspectionList();
    } else {
      ElMessage.error(res.msg || "删除失败");
    }
  }).catch(() => {});
};
 
// ============ 整改跟踪方法 ============
 
const handleRectificationQuery = () => {
  rectificationPage.value.current = 1;
  getRectificationList();
};
 
const resetRectificationQuery = () => {
  Object.assign(rectificationSearchForm.value, {
    status: ""
  });
  handleRectificationQuery();
};
 
const getRectificationList = () => {
  rectificationTableLoading.value = true;
  getFacilityRectificationList({ ...rectificationPage.value, ...rectificationSearchForm.value })
    .then(res => {
      rectificationTableLoading.value = false;
      rectificationTableData.value = res.data?.records || [];
      rectificationPage.value.total = res.data?.total || 0;
    })
    .catch(() => { rectificationTableLoading.value = false; });
};
 
const rectificationPagination = obj => {
  rectificationPage.value.current = obj.page;
  rectificationPage.value.size = obj.limit;
  getRectificationList();
};
 
const getExistingRectification = async inspectionId => {
  if (!inspectionId) return null;
  const res = await getFacilityRectificationList({ current: 1, size: 1, inspectionId });
  return res.data?.records?.[0] || null;
};
 
const getRectificationDialogType = row => {
  if (isPendingRectification(row)) return "do";
  if (isRectified(row)) return "verify";
  return "view";
};
 
const openRectificationDetail = row => {
  rectificationDetail.value = {
    ...row,
    storageBlobVOs: row.storageBlobVOs || row.storageBlobDTOs || []
  };
  rectificationDetailVisible.value = true;
};
 
const openRectificationFromInspection = async row => {
  const existing = await getExistingRectification(row.id);
  activeTab.value = "rectification";
  if (existing) {
    ElMessage.info("该巡检已存在整改记录,已为你打开对应处理窗口");
    existing.inspectionCode = existing.inspectionCode || row.inspectionCode;
    existing.facilityCode = existing.facilityCode || row.facilityCode;
    existing.facilityName = existing.facilityName || row.facilityName;
    openRectificationForm(getRectificationDialogType(existing), existing);
    return;
  }
  openRectificationForm("add", row);
};
 
const openRectificationForm = (type, row = null) => {
  if (type === "view") {
    openRectificationDetail(row);
    return;
  }
  rectificationDialogType.value = type;
  if (type === "add") {
    rectificationDialogTitle.value = "新增整改";
    Object.assign(rectificationForm.value, {
      id: null,
      inspectionId: row.id,
      facilityId: row.facilityId,
      facilityName: row.facilityName || "",
      facilityCode: row.facilityCode || "",
      inspectionCode: row.inspectionCode || "",
      problemDesc: row.checkDesc || "",
      problemLevel: "一般",
      rectifyUserId: currentUserId.value || "",
      rectifyUserName: getUserName(currentUserId.value),
      verifyUserId: "",
      verifyUserName: "",
      planTime: addDaysDateTime(1),
      actualTime: "",
      rectifyDesc: "",
      verifyTime: "",
      verifyDesc: "",
      status: "待整改",
      storageBlobVOs: row.storageBlobVOs || row.storageBlobDTOs || []
    });
  } else if (type === "do") {
    rectificationDialogTitle.value = "整改";
    Object.assign(rectificationForm.value, {
      ...row,
      storageBlobVOs: row.storageBlobVOs || row.storageBlobDTOs || [],
      rectifyDesc: row.rectifyDesc || "",
      verifyDesc: row.verifyDesc || ""
    });
  } else if (type === "verify") {
    rectificationDialogTitle.value = "验收";
    Object.assign(rectificationForm.value, {
      ...row,
      storageBlobVOs: row.storageBlobVOs || row.storageBlobDTOs || [],
      verifyDesc: row.verifyDesc || ""
    });
  } else {
    rectificationDialogTitle.value = "查看整改";
    Object.assign(rectificationForm.value, {
      ...row,
      storageBlobVOs: row.storageBlobVOs || row.storageBlobDTOs || []
    });
  }
  rectificationDialogVisible.value = true;
};
 
const buildRectificationPayload = () => {
  const {
    id,
    inspectionId,
    facilityId,
    problemDesc,
    problemLevel,
    problemImage,
    rectifyUserId,
    planTime,
    actualTime,
    rectifyDesc,
    rectifyImage,
    status,
    verifyUserId,
    verifyTime,
    verifyDesc
  } = rectificationForm.value;
  return {
    id,
    inspectionId,
    facilityId,
    problemDesc,
    problemLevel,
    problemImage,
    rectifyUserId,
    planTime,
    actualTime,
    rectifyDesc,
    rectifyImage,
    status,
    verifyUserId,
    verifyTime,
    verifyDesc
  };
};
 
const submitRectificationForm = async () => {
  if (rectificationDialogType.value === "view") {
    rectificationDialogVisible.value = false;
    return;
  }
  try {
    await rectificationFormRef.value.validate();
  } catch (error) {
    console.error("表单验证失败:", error);
    return;
  }
  if (rectificationDialogType.value === "do") {
    rectificationForm.value.status = "已整改";
    rectificationForm.value.actualTime = formatDateTime();
  } else if (rectificationDialogType.value === "verify") {
    rectificationForm.value.status = "已验收";
    rectificationForm.value.verifyTime = formatDateTime();
  }
  const api = rectificationForm.value.id ? updateFacilityRectification : addFacilityRectification;
  const res = await api(buildRectificationPayload());
  if (res.code === 200) {
    ElMessage.success("操作成功");
    rectificationDialogVisible.value = false;
    getRectificationList();
    getInspectionList();
  } else {
    ElMessage.error(res.msg || "操作失败");
  }
};
</script>
 
<style scoped>
.safety-facility-page {
  padding-top: 4px;
}
 
.facility-tabs {
  width: 100%;
}
 
.facility-tabs :deep(.el-tabs__header) {
  margin-bottom: 18px;
}
 
.facility-tabs :deep(.el-tabs__nav-wrap::after) {
  height: 1px;
  background-color: #d8e2ef;
}
 
.facility-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 14px 18px;
  min-height: 44px;
  margin-bottom: 18px;
}
 
.toolbar-filters,
.toolbar-actions {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px 12px;
}
 
.toolbar-actions {
  margin-left: auto;
}
 
.table_list {
  padding-top: 6px;
}
 
.history-summary {
  margin-bottom: 16px;
}
 
.table_list :deep(.link) {
  color: var(--el-color-primary);
  cursor: pointer;
  font-weight: 600;
}
 
.table_list :deep(.link:hover) {
  text-decoration: underline;
}
 
.search_title {
  font-size: 14px;
  font-weight: 600;
  color: #606266;
  white-space: nowrap;
}
 
.mb20 {
  margin-bottom: 20px;
}
 
.ml10 {
  margin-left: 10px;
}
 
.toolbar-filters .ml10 {
  margin-left: 0;
}
 
.facility-form-dialog :deep(.el-dialog__body) {
  padding: 24px 32px 10px;
}
 
.facility-form-dialog :deep(.el-dialog__footer) {
  padding: 8px 32px 26px;
}
 
.dialog-summary {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
  margin-bottom: 20px;
  padding: 14px 18px;
  border: 1px solid #e4eaf3;
  border-radius: 6px;
  background: #f7faff;
}
 
.summary-item {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}
 
.summary-item span {
  flex: 0 0 auto;
  color: #7a8599;
}
 
.summary-item strong {
  min-width: 0;
  overflow: hidden;
  color: #25324b;
  text-overflow: ellipsis;
  white-space: nowrap;
}
 
.inspection-form-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(220px, 0.8fr);
  column-gap: 20px;
}
 
.inspection-form-grid .form-grid-full {
  grid-column: 1 / -1;
}
 
.inspection-form :deep(.el-form-item) {
  margin-bottom: 20px;
}
 
.inspection-form :deep(.el-form-item__label) {
  margin-bottom: 8px;
  padding: 0;
  font-weight: 600;
  color: #25324b;
  line-height: 1.2;
}
 
.rectification-image-list {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}
 
.rectification-image {
  width: 96px;
  height: 96px;
  border: 1px solid #dbe4f0;
  border-radius: 6px;
  background: #f8fafc;
}
 
.detail-dialog :deep(.el-dialog__body) {
  padding: 24px 32px 8px;
}
 
.detail-dialog :deep(.el-descriptions__label) {
  width: 130px;
  color: #4d5b73;
  font-weight: 600;
  background: #f4f7fb;
}
 
.detail-dialog :deep(.el-descriptions__content) {
  color: #25324b;
  word-break: break-word;
}
 
.detail-section {
  margin-top: 18px;
}
 
.detail-section-title {
  margin-bottom: 10px;
  color: #25324b;
  font-weight: 600;
}
 
.detail-image-list {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}
 
.detail-image {
  width: 96px;
  height: 96px;
  border: 1px solid #dbe4f0;
  border-radius: 6px;
  background: #f8fafc;
}
 
.frequency-row {
  display: flex;
  width: 100%;
  align-items: center;
  gap: 12px;
}
 
.frequency-time-picker {
  min-width: 0;
  flex: 1;
}
 
.frequency-day-select {
  flex: 0 0 132px;
  width: 132px;
}
 
@media (max-width: 900px) {
  .inspection-form-grid,
  .dialog-summary {
    grid-template-columns: 1fr;
  }
}
</style>