Blame view

src/components/inspection/machineSelect2.vue 2.53 KB
48ea9c43   wuxw   巡检开发完成
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
  <template>
    <el-select v-model="selectedMachine" :placeholder="$t('machineSelect2.placeholder')" filterable remote
      :remote-method="remoteMethod" :loading="loading" style="width: 100%;" @change="handleChange">
      <el-option v-for="item in machineOptions" :key="item.id" :label="item.text" :value="item.id" />
    </el-select>
  </template>
  
  <script>
  import { listEquipmentAccount } from '@/api/inspection/inspectionPointApi'
  import { getCommunityId } from '@/api/community/communityApi'
  
  export default {
    name: 'MachineSelect2',
    props: {
      parentModal: {
        type: Boolean,
        default: false
      }
    },
    data() {
      return {
        selectedMachine: '',
        machineOptions: [],
        loading: false,
        communityId: ''
      }
    },
    created() {
      this.communityId = getCommunityId()
    },
    methods: {
      remoteMethod(query) {
        if (query !== '') {
          this.loading = true
          this.searchMachines(query)
        } else {
          this.machineOptions = []
        }
      },
  
      async searchMachines(query) {
        try {
          const params = {
            machineNameLike: query,
            page: 1,
            row: 100,
            communityId: this.communityId
          }
  
          const response = await listEquipmentAccount(params)
          this.machineOptions = this._filterMachineData(response.data)
        } catch (error) {
          console.error('搜索设备失败:', error)
          this.$message.error(this.$t('machineSelect2.fetchError'))
        } finally {
          this.loading = false
        }
      },
  
      _filterMachineData(machines) {
        if (!machines) return []
  
        return machines.map(machine => ({
          id: machine.machineId,
          text: `${machine.machineName}(${machine.machineCode})`
        }))
      },
  
      handleChange(value) {
        const selected = this.machineOptions.find(item => item.id === value)
        if (selected) {
          this.$emit('notify', {
            machineId: selected.id,
            machineName: selected.text.split('(')[0]
          })
        } else {
          this.$emit('notify', {
            machineId: '',
            machineName: ''
          })
        }
      },
  
      setMachine(machine) {
        if (machine && machine.machineId) {
          this.selectedMachine = machine.machineId
          this.machineOptions = [{
            id: machine.machineId,
            text: `${machine.machineName}(${machine.machineCode || ''})`
          }]
        }
      },
  
      clearMachine() {
        this.selectedMachine = ''
        this.machineOptions = []
        this.$emit('notify', {
          machineId: '',
          machineName: ''
        })
      }
    }
  }
  </script>