wlzboy
2025-10-27 559b2e34c983f615b6d6747f52c801022c561803
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
<template>
  <scroll-view class="create-emergency-task-container" scroll-y="true">
    <view class="form-header">
      <view class="back-btn" @click="goBack">
        <uni-icons type="arrowleft" size="20"></uni-icons>
      </view>
      <view class="title">创建转运任务</view>
    </view>
    
    <view class="form-section">
      <view class="form-item">
        <view class="form-label required">任务车辆</view>
        <picker mode="selector" :range="vehicles" :value="selectedVehicleIndex" @change="onVehicleChange">
          <view class="form-input picker-input">
            {{ selectedVehicle || '请选择任务车辆' }}
            <uni-icons type="arrowright" size="16" color="#999"></uni-icons>
          </view>
        </picker>
      </view>
        <view class="form-item">
        <view class="form-label">归属机构</view>
        <picker mode="selector" :range="organizations" @change="onOrganizationChange">
          <view class="form-input picker-input">
            {{ selectedOrganization || '请选择归属机构' }}
            <uni-icons type="arrowright" size="16" color="#999"></uni-icons>
          </view>
        </picker>
      </view>
      
      <view class="form-item">
        <view class="form-label required">任务类型</view>
        <picker mode="selector" :range="emergencyTaskTypeOptions" range-key="text" @change="onEmergencyTaskTypeChange">
          <view class="form-input picker-input">
            {{ selectedEmergencyTaskType || '请选择任务类型' }}
            <uni-icons type="arrowright" size="16" color="#999"></uni-icons>
          </view>
        </picker>
      </view>
      
      <view class="form-item">
        <view class="form-label required">单据类型</view>
        <picker mode="selector" :range="documentTypeOptions" range-key="text" @change="onDocumentTypeChange">
          <view class="form-input picker-input">
            {{ selectedDocumentType || '请选择单据类型' }}
            <uni-icons type="arrowright" size="16" color="#999"></uni-icons>
          </view>
        </picker>
      </view>
      <view class="form-item">
        <view class="form-label">执行任务人员</view>
        <view class="staff-list">
          <view class="staff-item" v-for="(staff, index) in selectedStaff" :key="staff.userId">
            <view class="staff-info">
              <text class="staff-name">{{ staff.nickName }}</text>
              <text class="staff-role">({{ getUserTypeName(staff.type) || '未知职位' }})</text>
            </view>
            <uni-icons 
              v-if="index > 0" 
              type="closeempty" 
              size="20" 
              color="#ff4d4f"
              @click="removeStaff(index)"
            ></uni-icons>
            <uni-icons 
              v-else
              type="checkmarkempty" 
              size="20" 
              color="#007AFF"
            ></uni-icons>
          </view>
          <view class="add-staff" @click="showStaffSelector">
            <uni-icons type="plusempty" size="20" color="#007AFF"></uni-icons>
            <text>添加人员</text>
          </view>
        </view>
      </view>
      
    
      
      <view class="form-item">
        <view class="form-label">转运时间</view>
        <uni-datetime-picker 
          v-model="taskForm.transferTime" 
          type="datetime" 
          :placeholder="'请选择转运时间'"
          class="form-input"
        />
      </view>
      
      <view class="form-section-title">患者信息</view>
      <view class="form-item">
        <view class="form-label required">联系人</view>
        <input 
          class="form-input" 
          placeholder="请输入联系人" 
          v-model="taskForm.patient.contact"
        />
      </view>
      
      <view class="form-item">
        <view class="form-label required">联系电话</view>
        <input 
          class="form-input" 
          type="number" 
          placeholder="请输入联系电话" 
          v-model="taskForm.patient.phone"
        />
      </view>
      
      <view class="form-item">
        <view class="form-label required">患者姓名</view>
        <input 
          class="form-input" 
          placeholder="请输入患者姓名" 
          v-model="taskForm.patient.name"
        />
      </view>
      
      <view class="form-item">
        <view class="form-label">性别</view>
        <view class="radio-group">
          <label class="radio-item">
            <radio value="male" :checked="taskForm.patient.gender === 'male'" @click="taskForm.patient.gender = 'male'" />
            <text>男</text>
          </label>
          <label class="radio-item">
            <radio value="female" :checked="taskForm.patient.gender === 'female'" @click="taskForm.patient.gender = 'female'" />
            <text>女</text>
          </label>
        </view>
      </view>
      
      <view class="form-item">
        <view class="form-label">患者身份证</view>
        <input 
          class="form-input" 
          type="idcard" 
          placeholder="请输入患者身份证号" 
          v-model="taskForm.patient.idCard"
        />
      </view>
      
      <view class="form-item">
        <view class="form-label">病情</view>
        <view class="disease-container">
          <view class="disease-tags" v-if="selectedDiseases.length > 0">
            <view 
              class="disease-tag" 
              v-for="(disease, index) in selectedDiseases" 
              :key="index"
            >
              <text class="disease-name">{{ disease.icdName }}</text>
              <uni-icons 
                type="closeempty" 
                size="16" 
                color="#fff"
                @click="removeDisease(index)"
              ></uni-icons>
            </view>
          </view>
          <view class="add-disease-btn" @click="showDiseaseSelector">
            <uni-icons type="plusempty" size="20" color="#007AFF"></uni-icons>
            <text>添加病情</text>
          </view>
          <textarea 
            class="form-textarea" 
            placeholder="其他病情描述(选填)" 
            v-model="taskForm.patient.condition"
            style="margin-top: 20rpx;"
          />
        </view>
      </view>
      
      <view class="form-section-title">转出医院信息</view>
      <view class="form-item">
        <view class="form-label required">医院名称</view>
        <view class="hospital-search-container">
          <input 
            class="form-input" 
            placeholder="请输入医院名称或地址搜索" 
            v-model="hospitalOutSearchKeyword"
            @input="onHospitalOutSearch"
            @focus="onHospitalOutFocus"
          />
          <view class="search-results" v-if="showHospitalOutResults && hospitalOutResults.length > 0">
            <view 
              class="search-result-item" 
              v-for="hospital in hospitalOutResults" 
              :key="hospital.hospId"
              @click="selectHospitalOut(hospital)"
            >
              <view class="hospital-name">{{ hospital.hospName }}</view>
              <view class="hospital-address">{{ buildFullAddress(hospital) }}</view>
            </view>
          </view>
        </view>
      </view>
      
      <view class="form-item">
        <view class="form-label required">科室</view>
        <picker mode="selector" :range="departmentOptions" range-key="text" @change="onHospitalOutDepartmentChange">
          <view class="form-input picker-input">
            {{ taskForm.hospitalOut.department || '请选择科室' }}
            <uni-icons type="arrowright" size="16" color="#999"></uni-icons>
          </view>
        </picker>
      </view>
      
      <view class="form-item">
        <view class="form-label">床号</view>
        <input 
          class="form-input" 
          placeholder="请输入床号" 
          v-model="taskForm.hospitalOut.bedNumber"
        />
      </view>
      
      <view class="form-item">
        <view class="form-label">转出地址</view>
        <view class="form-input picker-input disabled">
          {{ taskForm.hospitalOut.address || '选择医院后自动填充' }}
        </view>
      </view>
      
      <view class="form-section-title">转入医院信息</view>
      <view class="form-item">
        <view class="form-label required">医院名称</view>
        <view class="hospital-search-container">
          <input 
            class="form-input" 
            placeholder="请输入医院名称或地址搜索" 
            v-model="hospitalInSearchKeyword"
            @input="onHospitalInSearch"
            @focus="onHospitalInFocus"
          />
          <view class="search-results" v-if="showHospitalInResults && hospitalInResults.length > 0">
            <view 
              class="search-result-item" 
              v-for="hospital in hospitalInResults" 
              :key="hospital.hospId"
              @click="selectHospitalIn(hospital)"
            >
              <view class="hospital-name">{{ hospital.hospName }}</view>
              <view class="hospital-address">{{ buildFullAddress(hospital) }}</view>
            </view>
          </view>
        </view>
      </view>
      
      <view class="form-item">
        <view class="form-label required">科室</view>
        <picker mode="selector" :range="departmentOptions" range-key="text" @change="onHospitalInDepartmentChange">
          <view class="form-input picker-input">
            {{ taskForm.hospitalIn.department || '请选择科室' }}
            <uni-icons type="arrowright" size="16" color="#999"></uni-icons>
          </view>
        </picker>
      </view>
      
      <view class="form-item">
        <view class="form-label">床号</view>
        <input 
          class="form-input" 
          placeholder="请输入床号" 
          v-model="taskForm.hospitalIn.bedNumber"
        />
      </view>
      
      <view class="form-item">
        <view class="form-label">转入地址</view>
        <view class="form-input picker-input disabled">
          {{ taskForm.hospitalIn.address || '选择医院后自动填充' }}
        </view>
      </view>
      
      <view class="form-item">
        <view class="form-label">转运公里数</view>
        <input 
          class="form-input" 
          type="digit" 
          placeholder="请输入转运公里数" 
          v-model="taskForm.transferDistance"
        />
      </view>
      
      <view class="form-item">
        <view class="form-label">成交价</view>
        <input 
          class="form-input" 
          type="digit" 
          placeholder="请输入成交价" 
          v-model="taskForm.price"
        />
      </view>
      
      <view class="form-actions">
        <button class="submit-btn" @click="submitTask" :disabled="loading">
          {{ loading ? '保存中...' : '保存' }}
        </button>
      </view>
    </view>
    
    <!-- 人员选择弹窗 -->
    <uni-popup ref="staffPopup" type="bottom" :safe-area="true">
      <view class="staff-selector-popup">
        <view class="popup-header">
          <view class="popup-title">选择执行人员</view>
          <view class="popup-close" @click="closeStaffSelector">
            <uni-icons type="closeempty" size="24" color="#333"></uni-icons>
          </view>
        </view>
        
        <view class="search-box">
          <uni-icons type="search" size="18" color="#999"></uni-icons>
          <input 
            class="search-input" 
            placeholder="搜索姓名、手机号" 
            v-model="staffSearchKeyword"
            @input="onStaffSearch"
          />
        </view>
        
        <view class="staff-filter">
          <view 
            class="filter-item" 
            :class="{ active: staffFilterType === 'driver' }"
            @click="filterStaff('driver')"
          >司机</view>
          <view 
            class="filter-item" 
            :class="{ active: staffFilterType === 'doctor' }"
            @click="filterStaff('doctor')"
          >医生</view>
          <view 
            class="filter-item" 
            :class="{ active: staffFilterType === 'nurse' }"
            @click="filterStaff('nurse')"
          >护士</view>
        </view>
        
        <scroll-view class="staff-list-popup" scroll-y="true">
          <view 
            class="staff-item-popup" 
            v-for="staff in filteredStaffList" 
            :key="staff.userId"
            @click="toggleStaffSelection(staff)"
          >
            <view class="staff-info">
              <view class="staff-name-row">
                <text class="staff-name">{{ staff.nickName }}</text>
                <text class="staff-phone">{{ staff.phonenumber }}</text>
              </view>
              <view class="staff-detail-row">
                <text class="staff-dept">{{ staff.deptName }}</text>
                <text class="staff-post">{{ staff.postName || staff.roleName  || '未知职位' }}</text>
              </view>
            </view>
            <uni-icons 
              v-if="isStaffSelected(staff.userId)" 
              type="checkmarkempty" 
              size="24" 
              color="#007AFF"
            ></uni-icons>
            <view v-else class="checkbox-empty"></view>
          </view>
          
          <view class="no-data" v-if="filteredStaffList.length === 0">
            <uni-icons type="info" size="40" color="#ccc"></uni-icons>
            <text>暂无人员数据</text>
          </view>
        </scroll-view>
        
        <view class="popup-footer">
          <button class="cancel-btn" @click="closeStaffSelector">取消</button>
          <button class="confirm-btn" @click="confirmStaffSelection">确定(已选{{ selectedStaff.length }})</button>
        </view>
      </view>
    </uni-popup>
    
    <!-- 病情选择弹窗 -->
    <uni-popup ref="diseasePopup" type="bottom" :safe-area="true">
      <view class="disease-selector-popup">
        <view class="popup-header">
          <view class="popup-title">选择病情(ICD-10)</view>
          <view class="popup-close" @click="closeDiseaseSelector">
            <uni-icons type="closeempty" size="24" color="#333"></uni-icons>
          </view>
        </view>
        
        <view class="search-box">
          <uni-icons type="search" size="18" color="#999"></uni-icons>
          <input 
            class="search-input" 
            placeholder="搜索疾病名称、编码或助记码" 
            v-model="diseaseSearchKeyword"
            @input="onDiseaseSearch"
          />
        </view>
        
        <scroll-view class="disease-list-popup" scroll-y="true">
          <view 
            class="disease-item-popup" 
            v-for="disease in diseaseSearchResults" 
            :key="disease.id"
            @click="toggleDiseaseSelection(disease)"
          >
            <view class="disease-info">
              <view class="disease-name-row">
                <text class="disease-name">{{ disease.icdName }}</text>
                <text class="disease-code">[{{ disease.icdCode }}]</text>
              </view>
              <view class="disease-detail-row" v-if="disease.sm">
                <text class="disease-desc">{{ disease.sm }}</text>
              </view>
            </view>
            <uni-icons 
              v-if="isDiseaseSelected(disease.id)" 
              type="checkmarkempty" 
              size="24" 
              color="#007AFF"
            ></uni-icons>
            <view v-else class="checkbox-empty"></view>
          </view>
          
          <view class="no-data" v-if="diseaseSearchResults.length === 0">
            <uni-icons type="info" size="40" color="#ccc"></uni-icons>
            <text>{{ diseaseSearchKeyword ? '未找到相关疾病' : '暂无病情数据' }}</text>
          </view>
        </scroll-view>
        
        <view class="popup-footer">
          <button class="cancel-btn" @click="closeDiseaseSelector">取消</button>
          <button class="confirm-btn" @click="confirmDiseaseSelection">确定(已选{{ tempSelectedDiseases.length }})</button>
        </view>
      </view>
    </uni-popup>
  </scroll-view>
</template>
 
<script>
import { mapState } from 'vuex'
import uniDatetimePicker from '@/uni_modules/uni-datetime-picker/components/uni-datetime-picker/uni-datetime-picker.vue'
import uniPopup from '@/uni_modules/uni-popup/components/uni-popup/uni-popup.vue'
import { addTask } from "@/api/task"
import { listAvailableVehicles, getUserBoundVehicle } from "@/api/vehicle"
import { calculateDistance } from "@/api/map"
import { searchHospitals } from "@/api/hospital"
import { listUser } from "@/api/system/user"
import { searchIcd10 } from "@/api/icd10"
 
import { getDicts } from "@/api/dict"
import { getServiceOrdAreaTypes, getServiceOrderTypes, getHospitalDepartments } from "@/api/dictionary"
import { listBranchCompany } from "@/api/system/dept"
import MapSelector from '@/components/map-selector.vue'
 
export default {
  components: {
    uniDatetimePicker,
    uniPopup,
    MapSelector
  },
  data() {
    return {
      selectedVehicle: '',
      selectedVehicleId: null,
      selectedOrganization: '',
      selectedOrganizationId: null, // 归属机构ID(部门ID)
      selectedRegion: '', // 从归属机构中提取的地域信息(如:广州、深圳等)
      selectedEmergencyTaskType: '', // 选中的任务类型文本
      selectedEmergencyTaskTypeId: null, // 选中的任务类型ID
      selectedDocumentType: '', // 选中的单据类型文本
      selectedDocumentTypeId: null, // 选中的单据类型ID
      mapSelectorType: '',
      // 医院搜索相关
      hospitalOutSearchKeyword: '',
      hospitalOutResults: [],
      showHospitalOutResults: false,
      hospitalInSearchKeyword: '',
      hospitalInResults: [],
      showHospitalInResults: false,
      searchTimer: null,
      defaultHospitals: [], // 默认的100条医院数据
      // 人员选择相关
      selectedStaff: [], // 已选择的人员列表
      allStaffList: [], // 所有人员列表
      filteredStaffList: [], // 过滤后的人员列表
      staffSearchKeyword: '', // 人员搜索关键词
      staffFilterType: 'driver', // 人员筛选类型:driver/doctor/nurse,默认选中司机
      // 病情选择相关
      selectedDiseases: [], // 已选择的病情列表
      tempSelectedDiseases: [], // 临时选择的病情列表(用于弹窗)
      diseaseSearchKeyword: '', // 病情搜索关键词
      diseaseSearchResults: [], // 病情搜索结果
      diseaseSearchTimer: null, // 病情搜索防抖定时器
      taskForm: {
        transferTime: '',
        patient: {
          contact: '',
          phone: '',
          name: '',
          gender: 'male',
          idCard: '',
          condition: ''
        },
        hospitalOut: {
          id: null,  // 医院ID
          name: '',
          department: '',
          departmentId: null,  // 科室ID
          bedNumber: '',
          address: ''
        },
        hospitalIn: {
          id: null,  // 医院ID
          name: '',
          department: '',
          departmentId: null,  // 科室ID
          bedNumber: '',
          address: ''
        },
        transferDistance: '',
        price: ''
      },
      vehicles: [],
      vehicleOptions: [],
      organizations: [], // 归属机构列表(从后台加载分公司数据)
      organizationOptions: [], // 归属机构选项(用于picker显示)
      emergencyTaskTypes: [], // 任务类型列表(从 SQL Server 动态加载)
      emergencyTaskTypeOptions: [], // 任务类型选项(用于picker显示)
      documentTypes: [], // 单据类型列表
      documentTypeOptions: [], // 单据类型选项(用于picker显示)
      departmentOptions: [], // 科室字典数据
      loading: false,
      addressCoordinates: {
        hospitalOutAddress: null,
        hospitalInAddress: null
      }
    }
  },
  computed: {
    ...mapState({
      currentUser: state => ({
        userId: state.user.userId,
        name: state.user.nickName || '张三',
        nickName: state.user.nickName || '张三',
        position: '司机',
        deptId: state.user.deptId || 100,
        phonenumber: state.user.phonenumber || '',
        branchCompanyId: state.user.branchCompanyId,
        branchCompanyName: state.user.branchCompanyName
      })
    }),
    // 计算车辆在picker中的索引,用于默认选中
    selectedVehicleIndex() {
      if (!this.selectedVehicle || this.vehicles.length === 0) {
        return 0
      }
      const index = this.vehicles.findIndex(v => v === this.selectedVehicle)
      return index !== -1 ? index : 0
    }
  },
  onLoad(options) {
    // 先加载车辆列表,然后加载绑定车辆信息
    this.getAvailableVehicles().then(() => {
      this.getUserBoundVehicleInfo()
    })
    this.initSelectedStaff()
    this.loadDeptStaff()
    // 加载分公司数据(会自动设置默认分公司并加载医院列表)
    this.loadBranchCompanies()
    // 加载科室字典数据
    this.loadDepartments()
    // 加载任务类型数据
    this.loadEmergencyTaskTypes()
    // 加载单据类型数据
    this.loadDocumentTypes()
  },
  methods: {
    // 获取用户绑定的车辆信息
    getUserBoundVehicleInfo() {
      const userId = this.currentUser.userId
      if (!userId) {
        console.warn('用户ID不存在,无法获取绑定车辆')
        return
      }
      
      getUserBoundVehicle(userId).then(response => {
        const boundVehicle = response.data
        
        if (boundVehicle && boundVehicle.vehicleId) {
          const boundVehicleNo = boundVehicle.vehicleNo
          const boundVehicleId = boundVehicle.vehicleId
          
          console.log('用户绑定的车辆:', boundVehicleNo, 'ID:', boundVehicleId)
          
          // 在车辆列表中查找绑定的车辆
          const vehicleIndex = this.vehicleOptions.findIndex(v => 
            v.id === boundVehicleId || v.name === boundVehicleNo
          )
          
          if (vehicleIndex !== -1) {
            // 设置默认选中的车辆
            this.selectedVehicle = this.vehicleOptions[vehicleIndex].name
            this.selectedVehicleId = this.vehicleOptions[vehicleIndex].id
            console.log('默认选中车辆:', this.selectedVehicle)
          } else {
            console.warn('绑定的车辆不在可用车辆列表中')
          }
        } else {
          console.log('用户未绑定车辆')
        }
      }).catch(error => {
        console.error('获取用户绑定车辆信息失败:', error)
      })
    },
    
    getAvailableVehicles() {
      const deptId = this.currentUser.deptId
      return listAvailableVehicles(deptId, 'EMERGENCY').then(response => {
        const vehicleList = response.data || response.rows || []
        this.vehicleOptions = vehicleList.map(vehicle => ({
          id: vehicle.vehicleId,
          name: vehicle.vehicleNo,
          type: vehicle.vehicleType,
          status: vehicle.status
        }))
        this.vehicles = this.vehicleOptions.map(v => v.name)
      }).catch(() => {
        this.vehicles = []
      })
    },
    
    onVehicleChange(e) {
      const index = e.detail.value
      this.selectedVehicle = this.vehicles[index]
      this.selectedVehicleId = this.vehicleOptions[index]?.id
    },
    
    onOrganizationChange(e) {
      const index = e.detail.value
      const selected = this.organizationOptions[index]
      this.selectedOrganization = selected.deptName
      this.selectedOrganizationId = selected.deptId // 保存部门ID
      // 从归属机构中提取地域关键词(去除"分公司"后缀)
      // 例如:"广州分公司" -> "广州"
      this.selectedRegion = selected.deptName.replace(/分公司$/g, '').trim()
      // 重新加载医院列表(带地域过滤)
      this.loadDefaultHospitals()
    },
    
    // 加载分公司数据(parent_id=100的部门)
    loadBranchCompanies() {
      listBranchCompany().then(response => {
        const list = response.data || []
        // 过滤出 parent_id = 100 的部门(分公司)
        this.organizationOptions = list.filter(dept => dept.parentId === 100)
        // 生成picker的数据源(只显示名称)
        this.organizations = this.organizationOptions.map(dept => dept.deptName)
        
        // 默认设置为当前用户的分公司
        if (this.currentUser.branchCompanyName) {
          const index = this.organizationOptions.findIndex(
            dept => dept.deptName === this.currentUser.branchCompanyName
          )
          if (index !== -1) {
            this.selectedOrganization = this.currentUser.branchCompanyName
            this.selectedOrganizationId = this.organizationOptions[index].deptId // 保存部门ID
            // 提取地域关键词
            this.selectedRegion = this.selectedOrganization.replace(/分公司$/g, '').trim()
            console.log('默认选中归属机构:', this.selectedOrganization, '部门ID:', this.selectedOrganizationId, '地域:', this.selectedRegion)
            // 加载医院列表(带地域过滤)
            this.loadDefaultHospitals()
          }
        }
      }).catch(error => {
        console.error('加载分公司数据失败:', error)
        this.organizationOptions = []
        this.organizations = []
      })
    },
    
    // 加载科室数据(从 SQL Server 动态加载)
    loadDepartments() {
      getHospitalDepartments().then(response => {
        const list = response.data || [];
        this.departmentOptions = list.map(item => ({
          id: item.vID,
          text: item.vtext,
          dictValue: item.vtext  // 为了保持兼容性,保留dictValue字段
        }));
        // console.log('科室数据加载成功:', this.departmentOptions);
      }).catch(error => {
        console.error('加载科室数据失败:', error)
        this.departmentOptions = []
      })
    },
    
    // 加载任务类型数据(从 SQL Server)
    loadEmergencyTaskTypes() {
      getServiceOrderTypes().then(response => {
        const list = response.data || []
        this.emergencyTaskTypes = list
        this.emergencyTaskTypeOptions = list.map(item => ({
          id: item.vID,
          text: item.vtext
        }))
        
        // 默认选中第一个任务类型
        if (this.emergencyTaskTypeOptions.length > 0) {
          this.selectedEmergencyTaskType = this.emergencyTaskTypeOptions[0].text
          this.selectedEmergencyTaskTypeId = this.emergencyTaskTypeOptions[0].id
          console.log('默认选中任务类型:', this.selectedEmergencyTaskType)
        }
      }).catch(error => {
        console.error('加载任务类型失败:', error)
        this.emergencyTaskTypes = []
        this.emergencyTaskTypeOptions = []
      })
    },
    
    // 任务类型选择
    onEmergencyTaskTypeChange(e) {
      const index = e.detail.value
      const selected = this.emergencyTaskTypeOptions[index]
      this.selectedEmergencyTaskType = selected.text
      this.selectedEmergencyTaskTypeId = selected.id
    },
    getUserTypeName(staffType){
      switch(staffType){
        case "nurse":
          return "护士";
        case "doctor":
          return "医生";
        case "driver":
          return "司机";
        default:
          return "司机";
      }
    },
    
    // 加载单据类型数据
    loadDocumentTypes() {
      getServiceOrdAreaTypes().then(response => {
        const list = response.data || []
        this.documentTypes = list
        this.documentTypeOptions = list.map(item => ({
          id: item.vID,
          text: item.vtext
        }))
        
        // 默认选中第一个单据类型
        if (this.documentTypeOptions.length > 0) {
          this.selectedDocumentType = this.documentTypeOptions[0].text
          this.selectedDocumentTypeId = this.documentTypeOptions[0].id
          console.log('默认选中单据类型:', this.selectedDocumentType)
        }
      }).catch(error => {
        console.error('加载单据类型失败:', error)
        this.documentTypes = []
        this.documentTypeOptions = []
      })
    },
    
    // 单据类型选择
    onDocumentTypeChange(e) {
      const index = e.detail.value
      const selected = this.documentTypeOptions[index]
      this.selectedDocumentType = selected.text
      this.selectedDocumentTypeId = selected.id
    },
    
    // 转出医院科室选择
    onHospitalOutDepartmentChange(e) {
      const index = e.detail.value
      const selected = this.departmentOptions[index]
      this.taskForm.hospitalOut.department = selected.text  // 保存科室名称
      this.taskForm.hospitalOut.departmentId = selected.id  // 保存科室ID
    },
    
    // 转入医院科室选择
    onHospitalInDepartmentChange(e) {
      const index = e.detail.value
      const selected = this.departmentOptions[index]
      this.taskForm.hospitalIn.department = selected.text  // 保存科室名称
      this.taskForm.hospitalIn.departmentId = selected.id  // 保存科室ID
    },
    
    // 加载默认医院列表(前100条)
    loadDefaultHospitals() {
      // 转出医院:只加载当前区域的医院(带地域过滤)
      searchHospitals('', this.selectedRegion).then(response => {
        this.hospitalOutResults = response.data || []
        console.log('加载转出医院(当前区域):', this.selectedRegion, '数量:', this.hospitalOutResults.length)
      }).catch(error => {
        console.error('加载转出医院列表失败:', error)
        this.hospitalOutResults = []
      })
      
      // 转入医院:加载所有医院(不带地域过滤,后续会按地域排序)
      searchHospitals('', this.selectedRegion).then(response => {
        const allHospitals = response.data || []
        // 将医院按地域排序:本地区域优先
        this.hospitalInResults = this.sortHospitalsByRegion(allHospitals)
        console.log('加载转入医院(全部区域):', '数量:', this.hospitalInResults.length)
      }).catch(error => {
        console.error('加载转入医院列表失败:', error)
        this.hospitalInResults = []
      })
    },
    
    // 按地域排序医院:本地区域优先
    sortHospitalsByRegion(hospitals) {
      if (!this.selectedRegion || !hospitals || hospitals.length === 0) {
        return hospitals
      }
      
      const region = this.selectedRegion
      const localHospitals = []
      const otherHospitals = []
      
      hospitals.forEach(hospital => {
        // 判断医院是否在本地区域(省、市、区任一包含地域关键词)
        const isLocal = 
          (hospital.hopsProvince && hospital.hopsProvince.includes(region)) ||
          (hospital.hopsCity && hospital.hopsCity.includes(region)) ||
          (hospital.hopsArea && hospital.hopsArea.includes(region))
        
        if (isLocal) {
          localHospitals.push(hospital)
        } else {
          otherHospitals.push(hospital)
        }
      })
      
      // 本地医院在前,其他医院在后
      return [...localHospitals, ...otherHospitals]
    },
    
    // 转出医院输入框获得焦点
    onHospitalOutFocus() {
      // 如果没有搜索关键词,只显示当前区域的医院
      if (!this.hospitalOutSearchKeyword || this.hospitalOutSearchKeyword.trim() === '') {
        searchHospitals('', this.selectedRegion).then(response => {
          this.hospitalOutResults = response.data || []
        }).catch(error => {
          console.error('加载转出医院失败:', error)
          this.hospitalOutResults = []
        })
      }
      this.showHospitalOutResults = true
    },
    
    // 转出医院搜索
    onHospitalOutSearch(e) {
      const keyword = e.detail.value
      this.hospitalOutSearchKeyword = keyword
      
      // 防抖处理
      if (this.searchTimer) {
        clearTimeout(this.searchTimer)
      }
      
      // 如果关键词为空,只显示当前区域的医院
      if (!keyword || keyword.trim() === '') {
        searchHospitals('', this.selectedRegion).then(response => {
          this.hospitalOutResults = response.data || []
        }).catch(error => {
          console.error('加载转出医院失败:', error)
          this.hospitalOutResults = []
        })
        this.showHospitalOutResults = true
        return
      }
      
      // 有关键词时,去服务端搜索(仅限当前区域)
      this.searchTimer = setTimeout(() => {
        this.searchHospitalOut(keyword)
      }, 300)
    },
    
    // 搜索转出医院(仅限当前区域)
    searchHospitalOut(keyword) {
      // 传入关键词和地域过滤,只搜索当前区域的医院
      searchHospitals(keyword, this.selectedRegion).then(response => {
        this.hospitalOutResults = response.data || []
        this.showHospitalOutResults = true
        console.log('搜索转出医院:', keyword, '区域:', this.selectedRegion, '结果数:', this.hospitalOutResults.length)
      }).catch(error => {
        console.error('搜索转出医院失败:', error)
        this.hospitalOutResults = []
      })
    },
    
    // 选择转出医院
    selectHospitalOut(hospital) {
      this.taskForm.hospitalOut.id = hospital.hospId  // 保存医院ID
      this.taskForm.hospitalOut.name = hospital.hospName
      // 合并省市区 + 详细地址
      const fullAddress = this.buildFullAddress(hospital)
      this.taskForm.hospitalOut.address = fullAddress
      this.hospitalOutSearchKeyword = hospital.hospName
      this.showHospitalOutResults = false
      this.hospitalOutResults = []
      
      // 如果有GPS坐标,保存下来
      // 注意:HospData表中可能没有GPS坐标,需要根据地址进行地理编码
      // 这里先置为null,后续可以通过地址解析获取
      this.addressCoordinates.hospitalOutAddress = null
      
      // 如果两个医院都已选择,自动计算距离
      if (this.taskForm.hospitalIn.address) {
        // 这里可以调用地址解析和距离计算
        // 暂时留空,由用户手动输入距离
      }
    },
    
    // 转入医院输入框获得焦点
    onHospitalInFocus() {
      // 如果没有搜索关键词,显示所有医院(本地区域优先)
      if (!this.hospitalInSearchKeyword || this.hospitalInSearchKeyword.trim() === '') {
        searchHospitals('', '').then(response => {
          const allHospitals = response.data || []
          // 按地域排序:本地区域优先
          this.hospitalInResults = this.sortHospitalsByRegion(allHospitals)
        }).catch(error => {
          console.error('加载转入医院失败:', error)
          this.hospitalInResults = []
        })
      }
      this.showHospitalInResults = true
    },
    
    // 转入医院搜索
    onHospitalInSearch(e) {
      const keyword = e.detail.value
      this.hospitalInSearchKeyword = keyword
      
      // 防抖处理
      if (this.searchTimer) {
        clearTimeout(this.searchTimer)
      }
      
      // 如果关键词为空,显示所有医院(本地区域优先)
      if (!keyword || keyword.trim() === '') {
        searchHospitals('', '').then(response => {
          const allHospitals = response.data || []
          // 按地域排序:本地区域优先
          this.hospitalInResults = this.sortHospitalsByRegion(allHospitals)
        }).catch(error => {
          console.error('加载转入医院失败:', error)
          this.hospitalInResults = []
        })
        this.showHospitalInResults = true
        return
      }
      
      // 有关键词时,去服务端搜索(不限区域,但结果按地域排序)
      this.searchTimer = setTimeout(() => {
        this.searchHospitalIn(keyword)
      }, 300)
    },
    
    // 搜索转入医院(不限区域,但本地区域优先)
    searchHospitalIn(keyword) {
      // 传入关键词,不传地域过滤(搜索所有区域)
      searchHospitals(keyword, '').then(response => {
        const allHospitals = response.data || []
        // 按地域排序:本地区域优先
        this.hospitalInResults = this.sortHospitalsByRegion(allHospitals)
        this.showHospitalInResults = true
        console.log('搜索转入医院:', keyword, '结果数:', this.hospitalInResults.length)
      }).catch(error => {
        console.error('搜索转入医院失败:', error)
        this.hospitalInResults = []
      })
    },
    
    // 选择转入医院
    selectHospitalIn(hospital) {
      this.taskForm.hospitalIn.id = hospital.hospId  // 保存医院ID
      this.taskForm.hospitalIn.name = hospital.hospName
      // 合并省市区 + 详细地址
      const fullAddress = this.buildFullAddress(hospital)
      this.taskForm.hospitalIn.address = fullAddress
      this.hospitalInSearchKeyword = hospital.hospName
      this.showHospitalInResults = false
      this.hospitalInResults = []
      
      // 如果有GPS坐标,保存下来
      this.addressCoordinates.hospitalInAddress = null
      
      // 如果两个医院都已选择,自动计算距离
      if (this.taskForm.hospitalOut.address) {
        // 这里可以调用地址解析和距离计算
        // 暂时留空,由用户手动输入距离
      }
    },
    
    // 初始化选中的人员(默认包含当前用户)
    initSelectedStaff() {
      // 构建当前用户对象,包含完整的角色信息
      const currentUserStaff = {
        userId: this.currentUser.userId,
        nickName: this.currentUser.nickName,
        phonenumber: this.currentUser.phonenumber,
        postName: this.currentUser.position,
        deptId: this.currentUser.deptId,
        posts: this.currentUser.posts || [],
        roles: this.currentUser.roles || [],
        dept: this.currentUser.dept || null
      }
      
      // 为当前用户设置角色类型
      currentUserStaff.type = this.getUserType(currentUserStaff)
      
      this.selectedStaff = [currentUserStaff]
    },
    
    // 加载当前用户所在分公司的所有人员
    loadDeptStaff() {
      const deptId = this.currentUser.deptId
      if (!deptId) {
        console.error('无法获取当前用户所在部门')
        this.$modal.showToast('无法获取所在部门信息')
        return
      }
      
      // 直接查询当前用户部门下的所有用户
      // 后端SQL会自动处理:如果传入的是子部门,会查找其所属的分公司及其所有子部门的用户
      const queryParams = {
        deptId: deptId,
        status: '0', // 只查询正常状态的用户
        pageNum: 1,
        pageSize: 10000 // 设置足够大的页面大小,获取所有用户
      }
      
      listUser(queryParams).then(response => {
        const userList = response.rows || response.data || []
        this.allStaffList = userList.map(user => ({
          userId: user.userId,
          nickName: user.nickName,
          phonenumber: user.phonenumber,
          deptName: user.dept?.deptName || '',
          postName: user.posts && user.posts.length > 0 ? user.posts[0].postName : '',
          roleName: user.roles && user.roles.length > 0 ? user.roles[0].roleName : '',
          // 根据岗位名称或角色名称判断类型
          type: this.getUserType(user)
        }))
        
        // 初始化过滤列表
        this.filterStaffList()
      }).catch(error => {
        console.error('加载人员列表失败:', error)
        this.$modal.showToast('加载人员列表失败')
      })
    },
    
    // 根据用户的岗位或角色判断类型
    getUserType(user) {
      const postName = user.posts && user.posts.length > 0 ? user.posts[0].postName : ''
      const roleName = user.roles && user.roles.length > 0 ? user.roles[0].roleName : ''
      const deptName = user.dept?.deptName || ''
      // console.log("获取用户类型:", postName, roleName,user)
      // 判断是否为司机
      if (postName.includes('司机') || roleName.includes('司机') || deptName.includes('车队') || deptName.includes('司机')) {
        return 'driver'
      }
      // 判断是否为医生
      if (postName.includes('医生') || roleName.includes('医生') || deptName.includes('医生')) {
        return 'doctor'
      }
      // 判断是否为护士
      if (postName.includes('护士') || roleName.includes('护士') || deptName.includes('护士')) {
        return 'nurse'
      }
      // 其他类型,默认为司机
      return 'driver'
    },
    
    // 显示人员选择弹窗
    showStaffSelector() {
      this.$refs.staffPopup.open()
      this.filterStaffList()
    },
    
    // 关闭人员选择弹窗
    closeStaffSelector() {
      this.$refs.staffPopup.close()
      this.staffSearchKeyword = ''
      this.staffFilterType = 'driver' // 重置为默认的司机类型
    },
    
    // 人员搜索
    onStaffSearch(e) {
      this.staffSearchKeyword = e.detail.value
      this.filterStaffList()
    },
    
    // 筛选人员类型
    filterStaff(type) {
      this.staffFilterType = type
      this.filterStaffList()
    },
    
    // 过滤人员列表
    filterStaffList() {
      let list = [...this.allStaffList]
      
      // 按类型过滤
      if (this.staffFilterType === 'driver') {
        list = list.filter(staff => staff.type === 'driver')
      } else if (this.staffFilterType === 'doctor') {
        list = list.filter(staff => staff.type === 'doctor')
      } else if (this.staffFilterType === 'nurse') {
        list = list.filter(staff => staff.type === 'nurse')
      }
      
      // 按关键词搜索
      if (this.staffSearchKeyword && this.staffSearchKeyword.trim() !== '') {
        const keyword = this.staffSearchKeyword.trim().toLowerCase()
        list = list.filter(staff => {
          return staff.nickName.toLowerCase().includes(keyword) || 
                 (staff.phonenumber && staff.phonenumber.includes(keyword))
        })
      }
      
      this.filteredStaffList = list
    },
    
    // 切换人员选中状态
    toggleStaffSelection(staff) {
      const index = this.selectedStaff.findIndex(s => s.userId === staff.userId)
      
      if (index > -1) {
        // 如果是第一个(当前用户),不允许移除
        if (index === 0) {
          this.$modal.showToast('当前用户不能移除')
          return
        }
        // 已选中,移除
        this.selectedStaff.splice(index, 1)
      } else {
        // 未选中,添加
        this.selectedStaff.push(staff)
      }
    },
    
    // 判断人员是否已选中
    isStaffSelected(userId) {
      return this.selectedStaff.some(staff => staff.userId === userId)
    },
    
    // 确认人员选择
    confirmStaffSelection() {
      if (this.selectedStaff.length === 0) {
        this.$modal.showToast('请至少选择一名人员')
        return
      }
      this.closeStaffSelector()
    },
    
    // 移除人员
    removeStaff(index) {
      if (index === 0) {
        this.$modal.showToast('当前用户不能移除')
        return
      }
      this.selectedStaff.splice(index, 1)
    },
    
    addStaff() {
      this.showStaffSelector()
    },
    
    // ==================== 病情选择相关方法 ====================
    
    // 显示病情选择弹窗
    showDiseaseSelector() {
      // 初始化临时选择列表(复制当前已选择的病情)
      this.tempSelectedDiseases = [...this.selectedDiseases]
      this.diseaseSearchKeyword = ''
      // 默认加载所有病情
      this.loadAllDiseases()
      this.$refs.diseasePopup.open()
    },
    
    // 关闭病情选择弹窗
    closeDiseaseSelector() {
      this.$refs.diseasePopup.close()
      this.diseaseSearchKeyword = ''
      this.diseaseSearchResults = []
      this.tempSelectedDiseases = []
    },
    
    // 病情搜索
    onDiseaseSearch(e) {
      const keyword = e.detail.value
      this.diseaseSearchKeyword = keyword
      
      // 防抖处理
      if (this.diseaseSearchTimer) {
        clearTimeout(this.diseaseSearchTimer)
      }
      
      // 如果关键词为空,加载所有病情
      if (!keyword || keyword.trim() === '') {
        this.loadAllDiseases()
        return
      }
      
      // 有关键词时进行搜索
      this.diseaseSearchTimer = setTimeout(() => {
        this.searchDiseaseByKeyword(keyword)
      }, 300)
    },
    
    // 加载所有病情(默认显示)
    loadAllDiseases() {
      // 使用空字符串或特殊标识符来获取所有病情
      // 如果后端不支持空查询,可以传入一个通配符如'%'或者修改后端接口
      searchIcd10('').then(response => {
        this.diseaseSearchResults = response.data || []
      }).catch(error => {
        console.error('加载病情列表失败:', error)
        this.diseaseSearchResults = []
      })
    },
    
    // 根据关键词搜索病情
    searchDiseaseByKeyword(keyword) {
      searchIcd10(keyword).then(response => {
        this.diseaseSearchResults = response.data || []
      }).catch(error => {
        console.error('搜索病情失败:', error)
        this.diseaseSearchResults = []
      })
    },
    
    // 切换病情选中状态
    toggleDiseaseSelection(disease) {
      const index = this.tempSelectedDiseases.findIndex(d => d.id === disease.id)
      
      if (index > -1) {
        // 已选中,移除
        this.tempSelectedDiseases.splice(index, 1)
      } else {
        // 未选中,添加
        this.tempSelectedDiseases.push({
          id: disease.id,
          icdCode: disease.icdCode,
          icdName: disease.icdName,
          sm: disease.sm
        })
      }
    },
    
    // 判断病情是否已选中
    isDiseaseSelected(diseaseId) {
      return this.tempSelectedDiseases.some(d => d.id === diseaseId)
    },
    
    // 确认病情选择
    confirmDiseaseSelection() {
      // 将临时选择的病情复制到正式列表
      this.selectedDiseases = [...this.tempSelectedDiseases]
      this.closeDiseaseSelector()
    },
    
    // 移除病情
    removeDisease(index) {
      this.selectedDiseases.splice(index, 1)
    },
    
    validateForm() {
      if (!this.selectedVehicleId) {
        this.$modal.showToast('请选择任务车辆')
        return false
      }
      
      if (!this.selectedEmergencyTaskType) {
        this.$modal.showToast('请选择任务类型')
        return false
      }
      
      if (!this.selectedDocumentType) {
        this.$modal.showToast('请选择单据类型')
        return false
      }
      
      if (!this.taskForm.patient.contact) {
        this.$modal.showToast('请输入联系人')
        return false
      }
      
      if (!this.taskForm.patient.name) {
        this.$modal.showToast('请输入患者姓名')
        return false
      }
      
      if (!this.taskForm.patient.phone) {
        this.$modal.showToast('请输入患者联系电话')
        return false
      }
      
      if (!this.taskForm.hospitalOut.name) {
        this.$modal.showToast('请输入转出医院名称')
        return false
      }
      
      if (!this.taskForm.hospitalOut.department) {
        this.$modal.showToast('请选择转出医院科室')
        return false
      }
      
      if (!this.taskForm.hospitalIn.name) {
        this.$modal.showToast('请输入转入医院名称')
        return false
      }
      
      if (!this.taskForm.hospitalIn.department) {
        this.$modal.showToast('请选择转入医院科室')
        return false
      }
      
      return true
    },
    
    buildSubmitData() {
      // 合并病情信息:选中的ICD-10疾病 + 其他描述
      let conditionText = ''
      if (this.selectedDiseases.length > 0) {
        const diseaseNames = this.selectedDiseases.map(d => `${d.icdName}(${d.icdCode})`).join('、')
        conditionText = diseaseNames
      }
      if (this.taskForm.patient.condition && this.taskForm.patient.condition.trim()) {
        if (conditionText) {
          conditionText += '\n其他:' + this.taskForm.patient.condition
        } else {
          conditionText = this.taskForm.patient.condition
        }
      }
      
      const submitData = {
        taskType: 'EMERGENCY_TRANSFER',
        deptId: this.selectedOrganizationId, // 归属机构ID(部门ID)
        vehicleIds: this.selectedVehicleId ? [this.selectedVehicleId] : [],
        assigneeIds: this.selectedStaff.map(staff => staff.userId), // 执行人员ID列表
        assigneeId: this.selectedStaff.length > 0 ? this.selectedStaff[0].userId : null, // 主要执行人
        // 执行人员详细信息(包含角色类型)
        assignees: this.selectedStaff.map(staff => ({
          userId: staff.userId,
          userName: staff.nickName,
          userType: staff.type // driver/doctor/nurse
        })),
        transferTime: this.taskForm.transferTime, // 转运时间
        plannedStartTime: this.taskForm.transferTime, // 计划开始时间
        documentTypeId: this.selectedDocumentTypeId, // 单据类型ID
        taskTypeId: this.selectedEmergencyTaskTypeId, // 任务类型ID
        // 病情ID列表(用于同步调度单的OrdICD_ID参数)
        diseaseIds: this.selectedDiseases.map(d => d.id),
        // 将转出医院地址作为出发地,转入医院地址作为目的地
        departureAddress: this.taskForm.hospitalOut.address || '',
        destinationAddress: this.taskForm.hospitalIn.address || '',
        patient: {
          ...this.taskForm.patient,
          condition: conditionText, // 使用合并后的病情信息
          diseases: this.selectedDiseases.map(d => ({
            icdId: d.id,
            icdCode: d.icdCode,
            icdName: d.icdName
          }))
        },
        // 医院信息(包含医院ID、科室名称、科室ID等完整信息)
        hospitalOut: this.taskForm.hospitalOut,  // 包含: id, name, department, departmentId, bedNumber, address
        hospitalIn: this.taskForm.hospitalIn,    // 包含: id, name, department, departmentId, bedNumber, address
        transferDistance: this.taskForm.transferDistance ? parseFloat(this.taskForm.transferDistance) : null,
        price: this.taskForm.price ? parseFloat(this.taskForm.price) : null
      }
      
      if (this.addressCoordinates.hospitalOutAddress) {
        // 转出医院GPS坐标写入扩展表
        if (!submitData.hospitalOut) submitData.hospitalOut = {}
        submitData.hospitalOut.longitude = this.addressCoordinates.hospitalOutAddress.lng
        submitData.hospitalOut.latitude = this.addressCoordinates.hospitalOutAddress.lat
        
        // 同时写入主任务表的出发地经纬度
        submitData.departureLongitude = this.addressCoordinates.hospitalOutAddress.lng
        submitData.departureLatitude = this.addressCoordinates.hospitalOutAddress.lat
      }
      
      if (this.addressCoordinates.hospitalInAddress) {
        // 转入医院GPS坐标写入扩展表
        if (!submitData.hospitalIn) submitData.hospitalIn = {}
        submitData.hospitalIn.longitude = this.addressCoordinates.hospitalInAddress.lng
        submitData.hospitalIn.latitude = this.addressCoordinates.hospitalInAddress.lat
        
        // 同时写入主任务表的目的地经纬度
        submitData.destinationLongitude = this.addressCoordinates.hospitalInAddress.lng
        submitData.destinationLatitude = this.addressCoordinates.hospitalInAddress.lat
      }
      
      return submitData
    },
    
    submitTask() {
      if (!this.validateForm()) {
        return
      }
      
      this.$modal.confirm('确定要保存任务吗?').then(() => {
        this.loading = true
        const submitData = this.buildSubmitData()
        
        addTask(submitData).then(response => {
          this.loading = false
          this.$modal.showToast('任务创建成功')
          setTimeout(() => {
            uni.switchTab({
              url: '/pages/task/index'
            })
          }, 1500)
        }).catch(error => {
          this.loading = false
          console.error('任务创建失败:', error)
          this.$modal.showToast('任务创建失败,请重试')
        })
      }).catch(() => {})
    },
    
    goBack() {
      uni.navigateBack()
    },
    
    // 合并医院地址(省 + 市 + 区 + 详细地址)
    buildFullAddress(hospital) {
      const parts = []
      if (hospital.hopsProvince) {
        parts.push(hospital.hopsProvince)
      }
      if (hospital.hopsCity) {
        parts.push(hospital.hopsCity)
      }
      if (hospital.hopsArea) {
        parts.push(hospital.hopsArea)
      }
      if (hospital.hospAddress) {
        parts.push(hospital.hospAddress)
      }
      return parts.join('')
    }
  }
}
</script>
 
<style lang="scss" scoped>
.create-emergency-task-container {
  padding: 20rpx;
  background-color: #f5f5f5;
  min-height: 100vh;
  
  .form-header {
    display: flex;
    align-items: center;
    padding: 20rpx 0;
    margin-bottom: 30rpx;
    
    .back-btn {
      width: 60rpx;
      height: 60rpx;
      border-radius: 50%;
      background-color: #f0f0f0;
      display: flex;
      align-items: center;
      justify-content: center;
      margin-right: 20rpx;
    }
    
    .title {
      font-size: 36rpx;
      font-weight: bold;
      color: #333;
    }
  }
  
  .form-section {
    background-color: white;
    border-radius: 15rpx;
    padding: 30rpx;
    box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
    
    .form-section-title {
      font-size: 32rpx;
      font-weight: bold;
      margin: 40rpx 0 20rpx 0;
      padding-bottom: 10rpx;
      border-bottom: 1rpx solid #f0f0f0;
    }
    
    .form-item {
      margin-bottom: 40rpx;
      
      .form-label {
        font-size: 28rpx;
        margin-bottom: 15rpx;
        color: #333;
        
        &.required::before {
          content: '*';
          color: #ff4d4f;
          margin-right: 4rpx;
          font-weight: bold;
        }
      }
      
      .hospital-search-container {
        position: relative;
        
        .search-results {
          position: absolute;
          top: 75rpx;
          left: 0;
          right: 0;
          max-height: 400rpx;
          background-color: white;
          border: 1rpx solid #eee;
          border-radius: 10rpx;
          box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.1);
          z-index: 100;
          overflow-y: auto;
          
          .search-result-item {
            padding: 20rpx 30rpx;
            border-bottom: 1rpx solid #f0f0f0;
            
            &:last-child {
              border-bottom: none;
            }
            
            &:active {
              background-color: #f5f5f5;
            }
            
            .hospital-name {
              font-size: 28rpx;
              color: #333;
              font-weight: bold;
              margin-bottom: 8rpx;
            }
            
            .hospital-address {
              font-size: 24rpx;
              color: #999;
            }
          }
        }
      }
      
      .form-input {
        height: 70rpx;
        padding: 0 20rpx;
        border: 1rpx solid #eee;
        border-radius: 10rpx;
        font-size: 28rpx;
        
        &.picker-input {
          display: flex;
          align-items: center;
          justify-content: space-between;
          
          &.disabled {
            background-color: #f5f5f5;
            color: #999;
          }
        }
      }
      
      .form-textarea {
        width: 100%;
        min-height: 150rpx;
        padding: 20rpx;
        border: 1rpx solid #eee;
        border-radius: 10rpx;
        font-size: 28rpx;
      }
      
      .disease-container {
        .disease-tags {
          display: flex;
          flex-wrap: wrap;
          gap: 15rpx;
          margin-bottom: 20rpx;
          
          .disease-tag {
            display: flex;
            align-items: center;
            padding: 10rpx 20rpx;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 30rpx;
            
            .disease-name {
              font-size: 26rpx;
              color: white;
              margin-right: 10rpx;
            }
          }
        }
        
        .add-disease-btn {
          display: flex;
          align-items: center;
          justify-content: center;
          padding: 20rpx;
          border: 1rpx dashed #007AFF;
          border-radius: 10rpx;
          color: #007AFF;
          font-size: 28rpx;
          
          text {
            margin-left: 10rpx;
          }
        }
      }
      
      .radio-group {
        display: flex;
        
        .radio-item {
          display: flex;
          align-items: center;
          margin-right: 30rpx;
          
          radio {
            margin-right: 10rpx;
          }
        }
      }
      
      .staff-list {
        .staff-item {
          display: flex;
          justify-content: space-between;
          align-items: center;
          padding: 20rpx;
          background-color: #f9f9f9;
          border-radius: 10rpx;
          margin-bottom: 20rpx;
          
          .staff-info {
            flex: 1;
            display: flex;
            align-items: center;
            
            .staff-name {
              font-size: 28rpx;
              color: #333;
              margin-right: 10rpx;
            }
            
            .staff-role {
              font-size: 24rpx;
              color: #999;
            }
          }
        }
        
        .add-staff {
          display: flex;
          align-items: center;
          justify-content: center;
          padding: 20rpx;
          border: 1rpx dashed #007AFF;
          border-radius: 10rpx;
          color: #007AFF;
        }
      }
    }
    
    .form-actions {
      margin-top: 50rpx;
      text-align: center;
      
      .submit-btn {
        width: 80%;
        height: 80rpx;
        background-color: #007AFF;
        color: white;
        border-radius: 10rpx;
        font-size: 32rpx;
        
        &[disabled] {
          background-color: #ccc;
          color: #999;
        }
      }
    }
  }
}
 
// 人员选择弹窗样式
.staff-selector-popup {
  background-color: white;
  border-radius: 20rpx 20rpx 0 0;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
  
  .popup-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 30rpx;
    border-bottom: 1rpx solid #f0f0f0;
    flex-shrink: 0;
    
    .popup-title {
      font-size: 32rpx;
      font-weight: bold;
      color: #333;
    }
    
    .popup-close {
      padding: 10rpx;
    }
  }
  
  .search-box {
    display: flex;
    align-items: center;
    margin: 20rpx 30rpx;
    padding: 15rpx 20rpx;
    background-color: #f5f5f5;
    border-radius: 10rpx;
    flex-shrink: 0;
    
    .search-input {
      flex: 1;
      margin-left: 10rpx;
      font-size: 28rpx;
    }
  }
  
  .staff-filter {
    display: flex;
    padding: 0 30rpx 20rpx;
    gap: 20rpx;
    flex-shrink: 0;
    
    .filter-item {
      flex: 1;
      text-align: center;
      padding: 15rpx 0;
      background-color: #f5f5f5;
      border-radius: 10rpx;
      font-size: 28rpx;
      color: #666;
      
      &.active {
        background-color: #007AFF;
        color: white;
      }
    }
  }
  
  .staff-list-popup {
    flex: 1;
    overflow-y: auto;
    padding: 0 30rpx;
    
    .staff-item-popup {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 25rpx 20rpx;
      border-bottom: 1rpx solid #f0f0f0;
      
      &:active {
        background-color: #f5f5f5;
      }
      
      .staff-info {
        flex: 1;
        
        .staff-name-row {
          display: flex;
          align-items: center;
          margin-bottom: 10rpx;
          
          .staff-name {
            font-size: 30rpx;
            font-weight: bold;
            color: #333;
            margin-right: 20rpx;
          }
          
          .staff-phone {
            font-size: 24rpx;
            color: #999;
          }
        }
        
        .staff-detail-row {
          display: flex;
          align-items: center;
          
          .staff-dept {
            font-size: 24rpx;
            color: #666;
            margin-right: 20rpx;
          }
          
          .staff-post {
            font-size: 24rpx;
            color: #007AFF;
          }
        }
      }
      
      .checkbox-empty {
        width: 40rpx;
        height: 40rpx;
        border: 2rpx solid #ddd;
        border-radius: 50%;
      }
    }
    
    .no-data {
      text-align: center;
      padding: 100rpx 0;
      color: #999;
      
      text {
        display: block;
        margin-top: 20rpx;
        font-size: 28rpx;
      }
    }
  }
  
  .popup-footer {
    display: flex;
    padding: 20rpx 30rpx;
    border-top: 1rpx solid #f0f0f0;
    gap: 20rpx;
    flex-shrink: 0;
    
    button {
      flex: 1;
      height: 80rpx;
      border-radius: 10rpx;
      font-size: 30rpx;
    }
    
    .cancel-btn {
      background-color: #f5f5f5;
      color: #666;
    }
    
    .confirm-btn {
      background-color: #007AFF;
      color: white;
    }
  }
}
 
// 病情选择弹窗样式
.disease-selector-popup {
  background-color: white;
  border-radius: 20rpx 20rpx 0 0;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
  
  .popup-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 30rpx;
    border-bottom: 1rpx solid #f0f0f0;
    flex-shrink: 0;
    
    .popup-title {
      font-size: 32rpx;
      font-weight: bold;
      color: #333;
    }
    
    .popup-close {
      padding: 10rpx;
    }
  }
  
  .search-box {
    display: flex;
    align-items: center;
    margin: 20rpx 30rpx;
    padding: 15rpx 20rpx;
    background-color: #f5f5f5;
    border-radius: 10rpx;
    flex-shrink: 0;
    
    .search-input {
      flex: 1;
      margin-left: 10rpx;
      font-size: 28rpx;
    }
  }
  
  .disease-list-popup {
    flex: 1;
    overflow-y: auto;
    padding: 0 30rpx;
    
    .disease-item-popup {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 25rpx 20rpx;
      border-bottom: 1rpx solid #f0f0f0;
      
      &:active {
        background-color: #f5f5f5;
      }
      
      .disease-info {
        flex: 1;
        
        .disease-name-row {
          display: flex;
          align-items: center;
          margin-bottom: 8rpx;
          
          .disease-name {
            font-size: 30rpx;
            font-weight: bold;
            color: #333;
            margin-right: 15rpx;
          }
          
          .disease-code {
            font-size: 24rpx;
            color: #007AFF;
            background-color: #e6f2ff;
            padding: 4rpx 12rpx;
            border-radius: 6rpx;
          }
        }
        
        .disease-detail-row {
          .disease-desc {
            font-size: 24rpx;
            color: #999;
            line-height: 1.5;
          }
        }
      }
      
      .checkbox-empty {
        width: 40rpx;
        height: 40rpx;
        border: 2rpx solid #ddd;
        border-radius: 50%;
      }
    }
    
    .no-data {
      text-align: center;
      padding: 100rpx 0;
      color: #999;
      
      text {
        display: block;
        margin-top: 20rpx;
        font-size: 28rpx;
      }
    }
  }
  
  .popup-footer {
    display: flex;
    padding: 20rpx 30rpx;
    border-top: 1rpx solid #f0f0f0;
    gap: 20rpx;
    flex-shrink: 0;
    
    button {
      flex: 1;
      height: 80rpx;
      border-radius: 10rpx;
      font-size: 30rpx;
    }
    
    .cancel-btn {
      background-color: #f5f5f5;
      color: #666;
    }
    
    .confirm-btn {
      background-color: #007AFF;
      color: white;
    }
  }
}
</style>