wlzboy
6 天以前 09e6dc3fb7266620fafb5e341808a8eb36e080a1
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
<template>
  <view class="hospital-selector">
    <view class="form-item">
      <view class="form-label" :class="{ required: required }" v-if="showLabel">{{ label }}</view>
      <view class="hospital-search-container">
        <input 
          class="form-input" 
          :placeholder="placeholder" 
          v-model="searchKeyword"
          @input="onSearch"
          @focus="onFocus"
        />
        <view class="search-results" v-if="showResults && searchResults.length > 0">
          <view 
            class="search-result-item" 
            v-for="hospital in searchResults" 
            :key="hospital.hospId"
            @click="selectHospital(hospital)"
          >
            <view class="hospital-name">
              {{ hospital.hospName }}
              <text class="hospital-short" v-if="hospital.hospShort">{{ hospital.hospShort }}</text>
            </view>
            <view class="hospital-address">{{ buildFullAddress(hospital) }}</view>
          </view>
        </view>
      </view>
    </view>
    
    <view class="form-item">
      <view class="form-label" :class="{ required: required }">{{ addressLabel }}</view>
      <view class="address-input-container" v-if="selectedHospitalName === '家中'">
        <input 
          class="form-input" 
          placeholder="请输入详细地址" 
          :value="addressValue"
          @input="onAddressInput"
          @focus="onAddressFocus"
        />
        <view class="address-suggestions" v-if="showAddressSuggestions && addressSuggestions.length > 0">
          <view 
            class="address-suggestion-item" 
            v-for="(item, index) in addressSuggestions" 
            :key="index"
            @click="selectAddressSuggestion(item)"
          >
            <view class="suggestion-name">{{ item.name }}</view>
            <view class="suggestion-address">{{ item.district }}{{ item.address }}</view>
          </view>
        </view>
      </view>
      <view v-else class="form-input picker-input disabled">
        {{ addressValue || '选择医院后自动填充' }}
      </view>
    </view>
  </view>
</template>
 
<script>
import { searchHospitals } from "@/api/hospital"
import { baiduPlaceSuggestion } from "@/api/map"
 
export default {
  name: 'HospitalSelector',
  props: {
    // 标签文本
    label: {
      type: String,
      default: '医院名称'
    },
    // 是否显示标签
    showLabel: {
      type: Boolean,
      default: true
    },
    // 是否必填
    required: {
      type: Boolean,
      default: false
    },
    // 地址标签
    addressLabel: {
      type: String,
      default: '医院地址'
    },
    // 占位符
    placeholder: {
      type: String,
      default: '请输入医院名称或地址搜索'
    },
    // 当前选中的医院信息
    value: {
      type: Object,
      default: () => ({
        id: null,
        name: '',
        department: '',
        departmentId: null,
        bedNumber: '',
        address: ''
      })
    },
    // 部门ID(用于区域过滤)
    deptId: {
      type: [Number, String],
      default: null
    },
    // 当前区域(用于地址搜索)
    region: {
      type: String,
      default: '广州'
    }
  },
  data() {
    return {
      searchKeyword: '',
      searchResults: [],
      showResults: false,
      searchTimer: null,
      selectedHospitalName: '',
      // 地址建议相关
      addressSuggestions: [],
      showAddressSuggestions: false,
      addressSearchTimer: null,
      // 默认医院列表(用于首次加载)
      defaultHospitals: [],
      hasLoadedDefault: false  // 标记是否已加载过默认列表
    }
  },
  computed: {
    addressValue() {
      return this.value.address || ''
    }
  },
  watch: {
    'value.name': {
      immediate: true,
      handler(newVal) {
        if (newVal) {
          this.searchKeyword = newVal
          this.selectedHospitalName = newVal
        }
      }
    },
    // 监听 deptId 变化,清除已加载的默认列表(但不自动加载)
    deptId(newVal, oldVal) {
      if (newVal !== oldVal && newVal) {
        this.hasLoadedDefault = false
        this.defaultHospitals = []
        // 不自动加载,等待用户点击输入框时再加载
      }
    }
  },
  methods: {
    // 搜索输入监听
    onSearch(e) {
      const keyword = e.detail.value
      this.searchKeyword = keyword
      
      if (this.searchTimer) {
        clearTimeout(this.searchTimer)
      }
      
      if (!keyword || keyword.trim() === '') {
        // 关键词为空时,显示默认医院列表
        if (this.defaultHospitals.length > 0) {
          this.searchResults = this.defaultHospitals
          this.showResults = true
        } else {
          this.searchResults = []
          this.showResults = false
        }
        return
      }
      
      // 防抖处理
      this.searchTimer = setTimeout(() => {
        this.searchHospital(keyword)
      }, 300)
    },
    
    // 搜索医院
    searchHospital(keyword) {
      searchHospitals(keyword, this.deptId).then(response => {
        this.searchResults = response.data || []
        this.showResults = true
      }).catch(error => {
        console.error('搜索医院失败:', error)
        this.searchResults = []
      })
    },
    
    // 输入框获得焦点
    onFocus() {
      // 如果已经有搜索结果,直接显示
      if (this.searchResults.length > 0) {
        this.showResults = true
        return
      }
      
      // 如果还没有加载过默认医院列表,则加载
      if (!this.hasLoadedDefault) {
        this.loadDefaultHospitals()
      } else if (this.defaultHospitals.length > 0) {
        // 如果已经加载过,直接显示默认列表
        this.searchResults = this.defaultHospitals
        this.showResults = true
      }
    },
    
    // 加载默认医院列表
    loadDefaultHospitals() {
      searchHospitals('', this.deptId).then(response => {
        this.defaultHospitals = response.data || []
        this.searchResults = this.defaultHospitals
        this.showResults = true
        this.hasLoadedDefault = true
        console.log('加载默认医院列表,数量:', this.defaultHospitals.length)
      }).catch(error => {
        console.error('加载默认医院列表失败:', error)
        this.defaultHospitals = []
      })
    },
    
    // 选择医院
    selectHospital(hospital) {
      // 添加null检查
      if (!hospital) {
        console.warn('选择的医院对象为空');
        return;
      }
      
      this.selectedHospitalName = hospital.hospName || '';
      this.searchKeyword = hospital.hospName || '';
      
      const hospitalData = {
        id: hospital.hospId || null,
        name: hospital.hospName || '',
        address: hospital.hospName === '家中' ? '' : this.buildFullAddress(hospital)
      }
      
      this.showResults = false;
      this.searchResults = [];
      
      // 触发更新事件
      this.$emit('input', hospitalData);
      this.$emit('change', hospitalData);
    },
    
 
    
    // 合并医院地址(省 + 市 + 区 + 详细地址)
    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('')
    },
    
    // 地址输入(仅在选择“家中”时可用)
    onAddressInput(e) {
      const address = e.detail.value
      this.$emit('input', {
        ...this.value,
        address: address
      })
      this.$emit('address-change', address)
          
      // 防抖处理地址搜索
      if (this.addressSearchTimer) {
        clearTimeout(this.addressSearchTimer)
      }
          
      if (!address || address.trim() === '') {
        this.addressSuggestions = []
        this.showAddressSuggestions = false
        return
      }
          
      this.addressSearchTimer = setTimeout(() => {
        this.searchAddress(address)
      }, 300)
    },
        
    // 搜索地址建议
    searchAddress(query) {
      baiduPlaceSuggestion(query, this.region).then(response => {
        if (response.code === 200 && response.data) {
          this.addressSuggestions = response.data
          this.showAddressSuggestions = true
        } else {
          this.addressSuggestions = []
          this.showAddressSuggestions = false
        }
      }).catch(error => {
        console.error('搜索地址失败:', error)
        this.addressSuggestions = []
        this.showAddressSuggestions = false
      })
    },
        
    // 地址输入框获得焦点
    onAddressFocus() {
      if (this.addressValue && this.addressSuggestions.length > 0) {
        this.showAddressSuggestions = true
      }
    },
        
    // 选择地址建议
    selectAddressSuggestion(item) {
      const fullAddress = item.district + item.address
      this.$emit('input', {
        ...this.value,
        address: fullAddress
      })
      this.$emit('address-selected', {
        address: fullAddress,
        location: item.location
      })
          
      this.showAddressSuggestions = false
      this.addressSuggestions = []
    }
  }
}
</script>
 
<style lang="scss" scoped>
.hospital-selector {
  .form-item {
    margin-bottom: 40rpx;
    
    .form-label {
      font-size: 28rpx;
      margin-bottom: 15rpx;
      color: #333;
      
      &.required::before {
        content: '*';
        color: #ff0000;
        margin-right: 5rpx;
      }
    }
    
    .hospital-search-container {
      position: relative;
      
      .form-input {
        width: 100%;
        height: 70rpx;
        padding: 0 20rpx;
        border: 1rpx solid #eee;
        border-radius: 10rpx;
        font-size: 28rpx;
        box-sizing: border-box;
      }
      
      .search-results {
        position: absolute;
        top: 75rpx;
        left: 0;
        right: 0;
        max-height: 400rpx;
        overflow-y: auto;
        background-color: white;
        border: 1rpx solid #eee;
        border-radius: 10rpx;
        box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.1);
        z-index: 100;
        
        .search-result-item {
          padding: 20rpx;
          border-bottom: 1rpx solid #f0f0f0;
          
          &:last-child {
            border-bottom: none;
          }
          
          &:active {
            background-color: #f5f5f5;
          }
          
          .hospital-name {
            font-size: 28rpx;
            color: #333;
            margin-bottom: 8rpx;
            
            .hospital-short {
              margin-left: 10rpx;
              font-size: 24rpx;
              color: #999;
            }
          }
          
          .hospital-address {
            font-size: 24rpx;
            color: #999;
          }
        }
      }
    }
    
    .address-input-container {
      position: relative;
      
      .form-input {
        width: 100%;
        height: 70rpx;
        padding: 0 20rpx;
        border: 1rpx solid #eee;
        border-radius: 10rpx;
        font-size: 28rpx;
        box-sizing: border-box;
      }
      
      .address-suggestions {
        position: absolute;
        top: 75rpx;
        left: 0;
        right: 0;
        max-height: 400rpx;
        overflow-y: auto;
        background-color: white;
        border: 1rpx solid #eee;
        border-radius: 10rpx;
        box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.1);
        z-index: 100;
        
        .address-suggestion-item {
          padding: 20rpx;
          border-bottom: 1rpx solid #f0f0f0;
          
          &:last-child {
            border-bottom: none;
          }
          
          &:active {
            background-color: #f5f5f5;
          }
          
          .suggestion-name {
            font-size: 28rpx;
            color: #333;
            margin-bottom: 8rpx;
          }
          
          .suggestion-address {
            font-size: 24rpx;
            color: #999;
          }
        }
      }
    }
    
    .form-input {
      width: 100%;
      height: 70rpx;
      padding: 0 20rpx;
      border: 1rpx solid #eee;
      border-radius: 10rpx;
      font-size: 28rpx;
      box-sizing: border-box;
      
      &.picker-input {
        display: flex;
        align-items: center;
        justify-content: space-between;
      }
      
      &.disabled {
        background-color: #f5f5f5;
        color: #999;
      }
    }
  }
}
</style>