wlzboy
2026-01-26 739d4c2f64fcfd4ddcce6292978ad6aeb2c7bdc7
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
<template>
  <view class="form-item">
    <view class="form-label" :class="{ required: required }">{{ label }}</view>
    <!-- 调试信息 -->
    <view v-if="false" style="font-size: 24rpx; color: #999; margin-bottom: 10rpx;">
      isHome: {{ isHome }}, options.length: {{ departmentOptions.length }}
    </view>
    <picker 
      v-if="!isHome"
      mode="selector" 
      :range="departmentOptions" 
      range-key="text" 
      :value="selectedIndex"
      @change="onDepartmentChange"
      :disabled="departmentOptions.length === 0"
    >
      <view class="form-input picker-input" :class="{ disabled: departmentOptions.length === 0 }">
        {{ displayText }}
        <uni-icons type="arrowright" size="16" color="#999"></uni-icons>
      </view>
    </picker>
    <view v-else class="form-input picker-input disabled">
      其它
    </view>
  </view>
</template>
 
<script>
import { getHospitalDepartments } from "@/api/dictionary"
 
export default {
  name: 'DepartmentSelector',
  props: {
    // 标签文本
    label: {
      type: String,
      default: '科室'
    },
    // 是否必填
    required: {
      type: Boolean,
      default: false
    },
    // 当前选中的科室名称
    value: {
      type: String,
      default: ''
    },
    // 当前选中的科室ID
    departmentId: {
      type: [Number, String],
      default: null
    },
    // 是否为"家中"(如果是家中,显示"其它"且不可选择)
    isHome: {
      type: Boolean,
      default: false
    },
    // 占位符
    placeholder: {
      type: String,
      default: '请选择科室'
    }
  },
  data() {
    return {
      departmentOptions: [], // 科室选项列表
      selectedIndex: -1
    }
  },
  computed: {
    displayText() {
      if (this.departmentOptions.length === 0) {
        return '加载中...'
      }
      if (this.selectedIndex >= 0 && this.selectedIndex < this.departmentOptions.length) {
        return this.departmentOptions[this.selectedIndex].text
      }
      return this.value || this.placeholder
    }
  },
  watch: {
    value: {
      immediate: true,
      handler(newVal) {
        console.log('DepartmentSelector: value changed:', newVal)
        if (newVal) {
          this.updateSelectedIndex(newVal)
        } else {
          this.selectedIndex = -1
        }
      }
    },
    departmentId: {
      immediate: true,
      handler(newVal) {
        console.log('DepartmentSelector: departmentId changed:', newVal)
        if (newVal) {
          this.updateSelectedIndexById(newVal)
        }
      }
    },
    departmentOptions: {
      immediate: true,
      handler() {
        console.log('DepartmentSelector: departmentOptions changed, length=', this.departmentOptions.length)
        console.log('DepartmentSelector: 当前显示条件 - isHome:', this.isHome, 'departmentOptions.length:', this.departmentOptions.length, '显示picker:', !this.isHome && this.departmentOptions.length > 0)
        // 优先使用科室ID进行匹配,如果没有科室ID再使用科室名称
        if (this.departmentId) {
          this.updateSelectedIndexById(this.departmentId)
        } else if (this.value) {
          this.updateSelectedIndex(this.value)
        }
      }
    },
    isHome: {
      immediate: true,
      handler(newVal) {
        console.log('DepartmentSelector: isHome changed:', newVal)
        // 当变为"家中"时,自动选择"其它"科室
        if (newVal && this.departmentOptions.length > 0) {
          this.autoSelectOtherDepartment()
        }
      }
    }
  },
  mounted() {
    console.log('DepartmentSelector: 组件已挂载, isHome=', this.isHome, 'value=', this.value, 'departmentId=', this.departmentId)
    this.loadDepartments()
  },
  methods: {
    // 加载科室数据
    loadDepartments() {
      console.log('DepartmentSelector: 开始加载科室数据')
      getHospitalDepartments().then(response => {
        console.log('DepartmentSelector: 科室接口响应:', response)
        const list = response.data || []
        console.log('DepartmentSelector: 科室数据条数:', list.length)
        if (list.length > 0) {
          console.log('DepartmentSelector: 科室数据示例:', list.slice(0, 3))
        }
        this.departmentOptions = list.map(item => ({
          id: item.vID,
          text: item.vtext,
          dictValue: item.vtext
        }))
        console.log('DepartmentSelector: 处理后的科室选项:', this.departmentOptions.length)
        
        // 如果当前是"家中",自动选择"其它"
        if (this.isHome) {
          this.autoSelectOtherDepartment()
        }
      }).catch(error => {
        console.error('DepartmentSelector: 加载科室数据失败:', error)
        this.departmentOptions = []
      })
    },
    
    // 根据科室名称更新选中索引
    updateSelectedIndex(departmentName) {
      if (!departmentName || this.departmentOptions.length === 0) {
        this.selectedIndex = -1
        return
      }
      
      const index = this.departmentOptions.findIndex(d => d.text === departmentName)
      this.selectedIndex = index !== -1 ? index : -1
    },
    
    // 根据科室ID更新选中索引
    updateSelectedIndexById(departmentId) {
      if (!departmentId || this.departmentOptions.length === 0) {
        this.selectedIndex = -1
        return
      }
      
      const index = this.departmentOptions.findIndex(d => d.id == departmentId)
      this.selectedIndex = index !== -1 ? index : -1
    },
    
    // 科室选择变化(picker)
    onDepartmentChange(e) {
      const index = e.detail.value
      
      // 安全检查:确保索引有效且选项存在
      if (index < 0 || index >= this.departmentOptions.length) {
        console.warn('科室选择索引越界:', index, '数组长度:', this.departmentOptions.length)
        return
      }
      
      const selected = this.departmentOptions[index]
      
      // 二次检查:确保选中项存在且有text属性
      if (!selected || !selected.text) {
        console.warn('科室选项数据异常:', selected)
        return
      }
      
      this.selectedIndex = index
      
      this.$emit('input', selected.text)
      this.$emit('change', {
        department: selected.text,
        departmentId: selected.id
      })
    },
    
    // 科室输入(手动输入)
    onDepartmentInput(e) {
      const department = e.detail.value
      this.$emit('input', department)
      this.$emit('change', {
        department: department,
        departmentId: null
      })
    },
    
    // 自动选择"其它"科室(当isHome为true时)
    autoSelectOtherDepartment() {
      // 查找"其它"科室
      const otherDept = this.departmentOptions.find(d => d.text === '其它')
      if (otherDept) {
        console.log('DepartmentSelector: 自动选择"其它"科室, ID:', otherDept.id)
        // 更新选中索引
        const index = this.departmentOptions.findIndex(d => d.id === otherDept.id)
        this.selectedIndex = index
        
        // 触发change事件,传递departmentId
        this.$emit('input', otherDept.text)
        this.$emit('change', {
          department: otherDept.text,
          departmentId: otherDept.id
        })
      } else {
        console.warn('DepartmentSelector: 未找到"其它"科室')
      }
    }
  }
}
</script>
 
<style lang="scss" scoped>
.form-item {
  margin-bottom: 40rpx;
  
  .form-label {
    font-size: 28rpx;
    margin-bottom: 15rpx;
    color: #333;
    
    &.required::before {
      content: '*';
      color: #ff0000;
      margin-right: 5rpx;
    }
  }
  
  .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>