wlzboy
5 天以前 7de1396e315896dbc72a9d54e44f77434ea90f18
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
<template>
  <view class="task-type-selector">
    <view class="form-label" v-if="label">{{ label }}</view>
    <picker 
      mode="selector" 
      :range="taskTypeLabels" 
      :value="selectedIndex"
      @change="onTaskTypeChange"
      :disabled="disabled"
    >
      <view class="form-input picker-input" :class="{ disabled: disabled }">
        {{ displayText }}
        <uni-icons type="arrowright" size="16" color="#999"></uni-icons>
      </view>
    </picker>
  </view>
</template>
 
<script>
import { getDicts } from "@/api/dict"
 
export default {
  name: 'TaskTypeSelector',
  props: {
    // 标签文本
    label: {
      type: String,
      default: '任务类型'
    },
    // 当前选中的任务类型值
    value: {
      type: String,
      default: ''
    },
    // 字典类型(sys_task_type)
    dictType: {
      type: String,
      default: 'sys_task_type'
    },
    // 占位符
    placeholder: {
      type: String,
      default: '请选择任务类型'
    },
    // 是否禁用
    disabled: {
      type: Boolean,
      default: false
    },
    // 过滤条件(可选)- 只显示特定的任务类型
    filter: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      taskTypeOptions: [],
      taskTypeLabels: [],
      selectedIndex: -1
    }
  },
  computed: {
    displayText() {
      if (this.selectedIndex >= 0 && this.selectedIndex < this.taskTypeLabels.length) {
        return this.taskTypeLabels[this.selectedIndex]
      }
      return this.placeholder
    }
  },
  watch: {
    value: {
      immediate: true,
      handler(newVal) {
        if (newVal && this.taskTypeOptions.length > 0) {
          const index = this.taskTypeOptions.findIndex(item => item.value === newVal)
          this.selectedIndex = index
        }
      }
    }
  },
  mounted() {
    this.loadTaskTypes()
  },
  methods: {
    // 加载任务类型字典
    loadTaskTypes() {
      return getDicts(this.dictType).then(response => {
        const dictData = response.data || []
        let options = dictData.map(item => ({
          label: item.dictLabel,
          value: item.dictValue
        }))
        
        // 如果有过滤条件,只保留指定的类型
        if (this.filter && this.filter.length > 0) {
          options = options.filter(item => this.filter.includes(item.value))
        }
        
        this.taskTypeOptions = options
        this.taskTypeLabels = options.map(item => item.label)
        
        // 如果有预设值,设置选中索引
        if (this.value) {
          const index = this.taskTypeOptions.findIndex(item => item.value === this.value)
          this.selectedIndex = index
        }
        
        return options
      }).catch(error => {
        console.error('加载任务类型字典失败:', error)
        // 使用默认值
        this.taskTypeOptions = [
          { label: '维修保养', value: 'MAINTENANCE' },
          { label: '加油任务', value: 'FUEL' },
          { label: '其他', value: 'OTHER' }
        ]
        this.taskTypeLabels = this.taskTypeOptions.map(item => item.label)
        return this.taskTypeOptions
      })
    },
    
    // 任务类型选择变化
    onTaskTypeChange(e) {
      const index = e.detail.value
      this.selectedIndex = index
      const selectedOption = this.taskTypeOptions[index]
      
      if (selectedOption) {
        this.$emit('input', selectedOption.value)
        this.$emit('change', {
          value: selectedOption.value,
          label: selectedOption.label
        })
      }
    },
    
    // 重新加载任务类型(供外部调用)
    reload() {
      return this.loadTaskTypes()
    },
    
    // 获取当前选中的任务类型对象
    getSelected() {
      if (this.selectedIndex >= 0 && this.selectedIndex < this.taskTypeOptions.length) {
        return this.taskTypeOptions[this.selectedIndex]
      }
      return null
    }
  }
}
</script>
 
<style lang="scss" scoped>
.task-type-selector {
  .form-label {
    font-size: 28rpx;
    margin-bottom: 15rpx;
    color: #333;
  }
  
  .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;
    }
  }
}
</style>