zmc
2023-11-22 a969126ac530da96b1edb02152db87f0ad317f6e
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
<!-- 
  远程搜索 站点名称 输入框组件
  根据输入站点的内容提供对应的输入建议
  1.向父组件传入站点名字 和 设备编码
  2.可以选择建议或者任意输入
  3.父组件可以设置输入框有无默认的站点名称
  4.父组件可以设置是否需要根据选中的异常类型来给出站点建议
  ** 
  在父组件中设置
  <InputSearch :isNeedDefaultSite="1" @submit-value="(n)=>form.name=n"> </InputSearch>
 
  父组件通过注入 :isNeedDefaultSite="1"或  :isNeedDefaultSite="0"来代表该输入框有无默认的站点名称
  1代表需要默认值 ,0代表不需要默认值
 -->
 
 
 
<script>
import exceptionApi from '@/api/exceptionApi.js';
export default {
  props: {
    // 0代表不需要默认值,1代表需要默认值
    isNeedDefaultSite: {
      type: String,
      default: '0'
    },
    // 0代表不需要,1代表根据时段和异常来给出输入建议
    isNeedRealTimeAdvice:{
      type:String,
      default:'0'
    },
    exceptionType:{
      type:Array,
      default:()=>{
        return []
      }
    },
    beginTime:{
      type:String,
      default:''
    },
    endTime:{
      type:String,
      default:''
    },
    siteName:{
      type:String,
      default:'-1'
    }
  },
  emits: ['submitValue', 'submitSiteNums','submitMncode'],
  data() {
    return {
      // 用户选中的值
      state: '',
      // 保存输入建议的站点名称
      siteNames: [],
      // 保存输入建议的站点设备编号
      siteNamesAndMnCode:[]
    }
  },
  watch:{
    beginTime(val,oldVal){
      
      if(this.isNeedRealTimeAdvice =='1' && oldVal != ''){
        exceptionApi.getExceptionName(this.exceptionType,this.beginTime,this.endTime).then( res=>{
          this.siteNames = []
          const sites = res.data.data
          sites.filter((item) => {
          this.siteNames.push(item['name'])
          })
        })
      }
    },
    exceptionType(){
      if(this.isNeedRealTimeAdvice =='1' ){
        exceptionApi.getExceptionName(this.exceptionType,this.beginTime,this.endTime).then( res=>{
          this.siteNames = []
          const sites = res.data.data
          sites.filter((item) => {
          this.siteNames.push(item['name'])
          })
        })
      }
    },
    siteName(){
      if(this.siteName != '-1' && this.siteName != ''){
        this.state = this.siteName
        this.$emit('submitValue', this.state)
      }
    },
    state(){
      if(this.state != '' && this.state != null){
        // 查找对应的设备编号
        this.onSiteNameSelected(this.state)
      }
    }
  },
  mounted() {
    // 加载所有的站点名称
    this.loadAll()
    if (this.isNeedDefaultSite == 1) {
      this.state = '金山区金山新城JSC1-0401单元1-11-01地块项目09'
      this.$emit('submitValue', this.state)
    } else if (this.isNeedDefaultSite == 0) {
      this.state = ''
      this.$emit('submitValue', this.state)
    }
  },
  methods: {
    querySearch(queryString, cb) {
      const results = queryString
        ? this.siteNames.filter(this.createFilter(queryString))
        : this.siteNames
      cb(results)
    },
    createFilter(queryString) {
      return (restaurant) => {
        return (
          // toLowerCase() 方法用于把字符串转换为小写。
          restaurant.toLowerCase().indexOf(queryString.toLowerCase()) === 0
        )
      }
    },
    // 初始化加载所有的站点名字
    loadAll() {
      if(this.isNeedRealTimeAdvice == '1'){
        exceptionApi.getExceptionName(this.exceptionType,this.beginTime,this.endTime).then( res=>{
          this.siteNames = []
          const sites = res.data.data
          sites.filter((item) => {
          this.siteNames.push(item['name'])
          let temp = {}
          temp.name = item.name
          temp.mnCode = item.mnCode
          this.siteNamesAndMnCode.push(temp)
          })
        })
 
    }else if(this.isNeedRealTimeAdvice == '0'){
      this.$http.get('/dust/sitename').then((response) => {
        const sites = response.data.data
        sites.filter((item) => {
          this.siteNames.push(item['name'])
          let temp = {}
          temp.name = item.name
          temp.mnCode = item.mnCode
          this.siteNamesAndMnCode.push(temp)
        })
        this.$emit('submitSiteNums', this.siteNames.length)
      })
    }
    },
    // 根据选中的站点查询设备编号
    onSiteNameSelected(siteName) { 
      const selectedSite = this.siteNamesAndMnCode.find((site) => site.name == siteName); 
  if (selectedSite) { 
    this.$emit('submitMncode',selectedSite.mnCode)
  }else{
    this.$emit('submitMncode','')
  }
},
    
    // 点击选中建议项时触发
    handleSelect(item) {
      this.state = item
      this.$emit('submitValue', this.state)
    },
    // 在点击由 clearable 属性生成的清空按钮时触发
    clearSiteName() {
      this.state = ''
      this.$emit('submitValue', this.state)
    },
 
  }
}
</script>
 
<template>
  <div class="container">
    <span class="text">点位名称:</span>
    <el-autocomplete
      v-model="state"
      :fetch-suggestions="querySearch"
      popper-class="my-autocomplete"
      placeholder="请输入"
      @select="handleSelect"
      @change="handleSelect"
      clearable
      @clear="clearSiteName"
    >
      <template #default="{ item }">
        <div class="value">{{ item }}</div>
      </template>
    </el-autocomplete>
  </div>
</template>
 
<style>
.container {
  display: flex;
}
 
.text {
  font-weight: bold;
  font-size: 14px;
  color: #333333;
}
.el-autocomplete {
  width: 200px;
}
 
</style>