riku
2025-02-21 c35074e0e33054bb6c5ada22f8104422ae953b17
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
<template>
  <el-form-item label="设备">
    <el-select
      :model-value="modelValue"
      @update:model-value="handleChange"
      placeholder="设备"
      size="small"
      class="w-120"
    >
      <el-option
        v-for="(s, i) in deviceOptions"
        :key="i"
        :label="s.label"
        :value="s.value"
      />
    </el-select>
  </el-form-item>
</template>
 
<script>
// import { deviceList } from '@/constant/device-type';
import { mapStores } from 'pinia';
import { useDeviceStore } from '@/stores/device';
 
export default {
  props: {
    type: String,
    modelValue: String
  },
  emits: ['update:modelValue', 'initOver'],
  data() {
    return {};
  },
  computed: {
    ...mapStores(useDeviceStore),
    deviceOptions() {
      return this.deviceStore.getDevice(this.type).map((v) => {
        return {
          label: v.deviceName,
          value: v.deviceCode
        };
      });
    }
  },
  watch: {
    // type(nV, oV) {
    //   if (nV != oV) {
    //     this.refreshOptions();
    //   }
    // }
    deviceOptions(nV, oV) {
      if (nV != oV) {
        if (nV.length > 0) {
          this.handleChange(nV[0].value);
        }
      }
    }
  },
  methods: {
    fetchDevice() {
      this.deviceStore.fetchDevice().then((res) => {
        if (res.success && res.data.length > 0) {
          this.handleChange(this.deviceOptions[0].value);
          this.$emit('initOver');
        }
      });
    },
    // refreshOptions() {
    //   this.deviceOptions =
    // },
    handleChange(value) {
      this.$emit('update:modelValue', value);
    }
  },
  mounted() {
    if (this.deviceStore.deviceList.length == 0) {
      this.fetchDevice();
    } else {
      this.handleChange(this.deviceOptions[0].value);
    }
  }
};
</script>
<style scoped></style>