jinlin
2023-11-03 35435e8b1995e6775c82b86652381e07e3faff54
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
<template>
  <div class="cascader">
    <el-input v-model="text" :disabled="disabled" @click.native="open()" suffix-icon="el-icon-arrow-down"
              slot="reference"
              :placeholder="placeholder"/>
    <zt-dialog ref="dialog" :append-to-body="true" column="3" :title="title" @confirm="confirm">
      <el-row>
        <el-form v-if="!multiple" :inline="true" :model="form" label-width="70px" style="padding: 5px">
          <zt-form-item :label="$store.state.word.xianhao">
            <div class="">
              <el-input @focus="getShipInfoById" v-model="shipName" type="text" :readonly="true"
                        placeholder="请选择" :disabled="disabled || disabledShip"></el-input>
            </div>
          </zt-form-item>
          <zt-form-item label="系统">
            <div class="" @click="getList(shipId,shipName)">
              <el-input v-model="sysName" type="text" :readonly="true"
                        :disabled="disabledSys" placeholder="请选择"></el-input>
            </div>
          </zt-form-item>
          <zt-form-item label="分系统">
            <div>
              <el-input @focus="getList(sysId,sysName)" v-model="subName" type="text" :readonly="true"
                        :disabled="disabledSub"
                        placeholder="请选择"></el-input>
            </div>
          </zt-form-item>
          <zt-form-item label="设备">
            <div class="">
              <el-input v-model="devName" type="text" placeholder="请选择" :readonly="true"
                        :disabled="disabledDev"></el-input>
            </div>
          </zt-form-item>
          <zt-form-item class="searchList-top" style="margin-left:20px">
              <el-input v-model="searchWord" type="text" placeholder="请输入">
                <el-button slot="append" @click="query(value,text)" icon="el-icon-search"></el-button>
              </el-input>
          </zt-form-item>
        </el-form>
        <el-col :span="multiple?'20':'24'">
          <div>
            <el-button type="text" class="form-title">请选择{{ dataListName }}</el-button>
            <div class="list">
              <ul class="cascader-ul">
                <li @click="getList(data.id,data.name)"
                    v-for="data of dataList3" :key="data.id" :label="data.id"
                    :style="{'background-color' :
                    shipName!==undefined&&shipName===data.name
                    ||sysName!==undefined&&sysName===data.name
                    ||subName!==undefined&&subName===data.name
                    ||devName!==undefined&&devName===data.name ? 'rgba(123, 224, 244, 0.5)' : ''}"
                >{{ data.name }}
                </li>
              </ul>
            </div>
          </div>
        </el-col>
        <el-col :span="multiple?'4':'0'">
          <el-container>
            <el-main>
              <el-input class="input-selected" v-model="selectedText" size="small" readonly/>
              <div style="margin-top: 10px">
                已选择的设备:
                <label style="width: 80%;"> {{ devPathName }}</label>
              </div>
            </el-main>
          </el-container>
        </el-col>
      </el-row>
      <el-dialog title="节点列表"  append-to-body :visible.sync="innerVisible">
        <div class="list1 list">
          <ul class="cascader-ul">
            <li @click="selectedRow(list.id)"  v-for="list in SearchList" :key="list.id" :label="list.id">{{ list.name }}</li>
          </ul>
        </div>
      </el-dialog>
    </zt-dialog>
  </div>
</template>
 
<script>
  import {debounce} from 'lodash'
 
  export default {
    name: 'zt-equipment-select',
    props: {
      isShow: {
        type: String,
        default: true
      },
      value: [String, Array],
      shipId: {
        String,
        default: ''
      },
      showLevel: String,
      title: String,
      idField: {
        type: String,
        default: 'id'
      },
      textField: {
        type: String,
        default: 'name'
      },
      parentIdField: {
        type: String,
        default: 'pid'
      },
      url: String,
      // datas: [String, Array],
      simple: { // 简单模式
        type: Boolean,
        default: false
      },
      multiple: {
        type: Boolean,
        default: false
      },
      leafOnly: { // 是否只能选择叶子节点
        type: Boolean,
        default: false
      },
      checkStrictly: {
        type: Boolean,
        default: false
      },
      lazy: { // 懒加载子节点
        type: Boolean,
        default: false
      },
      expandLevel: { // 懒加载展开层级
        type: Number,
        default: 0
      },
      disabledFilter: Function, // 可选的节点
      disabled: {
        type: Boolean,
        default: false
      },
      area: {
        type: String,
        default: ''
      },
      placeholder: String
    },
    inject: {
      elForm: {
        default: ''
      }
    },
    data() {
      return {
        innerVisible:false,
        dataListName: '',
        dataList3: [],
        SearchList: [
          {id:'1280000000000017917',name:'/系统3-4-2',level:'system1'},
          {id:'1280000000000017919',name:'/系统3-4-2/设备3-4-2-2',level:'equipment'},
          {id:'1280000000000017937',name:'/系统3-4-2/设备3-4-2-20',level:'equipment'},
          {id:'1280000000000017938',name:'/系统3-4-2/设备3-4-2-21',level:'equipment'},
 
          {id:'1280000000000017998',name:'/系统3-4-3',level:'system1'},
          {id:'1280000000000018018',name:'/系统3-4-3/设备3-4-3-20',level:'equipment'},
          {id:'1280000000000018019',name:'/系统3-4-3/设备3-4-3-21',level:'equipment'},
        ],
        searchWord:'',
 
        shipId: '',
        sysId: '',
        subId: '',
        devId: '',
 
        shipName: '',
        devName: '',
        sysName: '',
        subName: '',
 
        disabledShip: false,
        disabledSys: false, // 目前未用,注意加上控制
        disabledSub: false,
        disabledDev: false, // 目前未用
 
 
        devPathName: '',// 多选 设备路径集合
 
        // devTimer: null,
        // tmpSubVal: '',
        // tmpDevVal: '',
        // dataListSubId: '',
        // seldetailed: false,
        selectValue: '',
 
        // datas: [],
        treeValue: this.value,
        filterText: '',
        text: '',
        // selectTxt2: 'ok',
        selectedData: []
      }
    },
    computed: {
      selectedText() {
        return `已选择(${this.selectedData.length})`
      }
    },
    watch: {
      value(val, oldval) { // 传递给tree-selector
        this.treeValue = val
        this.$nextTick(() => {
          //alert('111')
          this.setTextOnetime()
        })
 
      },
      // 'selectValueShip': function (val, oldval) {
      //   console.log(val, oldval, 'val oldval')
      //   //this.getShipId()
      // },
      filterText: debounce(function (val) {
        this.$refs.tree.filter(val)
      }, 1000)
    },
    mounted() {
      // this.getShipId()
 
    },
    methods: {
     async query(){
       if(this.searchWord){
         let res= await this.$http.get(`/ztProduct/searchNodes?pid=${this.shipId}&name=${this.searchWord}`)
          console.log(res,'query res')
         if(res.data.length>0){
           this.SearchList=res.data
           this.innerVisible= true
         }else {
           this.$alert('没有数据!')
         }
       }else{
         this.$alert('请输入查询的关键字!')
       }
      },
      selectedRow(id){
        this.value = id
        this.innerVisible= false
      },
      async getShipInfoById() {
        //alert("getShipId")
        console.log(this.value,'getShipId this.value')
        console.log(this.shipId,'getShipId this.shipId')
        console.log(this.dataList3,'getShipId dataList3')
        if (!this.value) {
          if (!this.shipId ) {
            let res = await this.$http.get('/ztProduct/getShipList', {params: {area: this.area}})
            // this.dataListShip = res.data
            this.dataList3 = res.data
          }
          this.dataListName = this.$store.state.word.xianhao
          // console.log(this.dataListShip, 'this.dataListShip this.dataListShip')
          if (this.shipId) {
            this.disabledShip = true
            // this.selectValueShip = this.shipId
            this.getList(this.shipId)
            this.dataList3.filter(item => {
                // console.log(item.id, 'item.id item.id')
                if (item.id === this.shipId) {
                  this.shipName = item.name
                }
              }
            )
            console.log(this.shipName, 'this.shipName this.shipName')
          } else {
            this.disabledShip = false
          }
          this.$nextTick(() => {
            this.setTextOnetime()
          })
        }
      },
      open() {
        let that = this
        that.searchWord=''
        //setTimeout(that.setDefault, 2000)
        if (this.disabled || (this.elForm || {}).disabled) {
          // 只读
        } else {
          console.log(this.value, 'this.value')
          this.getShipInfoById()
          this.$refs.dialog.open()
        }
      },
      // 选择列表数据
      async getList(dataId, dataName) {
        console.time('select')
        console.log(dataName, 'dataName')
        let res = await this.$http.get(`/ztProduct/${dataId}`, null)
        console.log(res.data, 'res4 res4')
        if (res.data) {
          if (res.data.shipProductId != null) { // 有舷号
            this.shipName = res.data.shipName
            //this.isshipName = true
          } else {
            this.shipName = ''
          }
 
          this.shipId = res.data.shipProductId
          if (res.data.sysProductId != null) { // 有系统
            this.sysName = res.data.sysName
            //this.issysName = true
          } else {
            this.sysName = ''
          }
 
          if (res.data.subSysProductId != null) { // 又有分系统
            this.subName = res.data.subSysName
            //this.issubName = true
            this.disabledSub = false
            //this.sysId = res.data.sysProductId
            console.log(this.devId, 'getSysList this.dataList3')
          } else {
            this.subName = ''
            //this.issubName = false
            this.disabledSub = true
            //this.devId = res.data.sysProductId
            console.log(this.devId, 'getSysList this.dataList3')
          }
 
          if (res.data.deviceProductId != null) { // 有设备
            this.devName = res.data.deviceName
          } else {
            this.devName = ''
          }
        }
        console.timeEnd('select')
 
        console.time('select2')
        // console.log(this.datas, ' datas 系统数据')
        res = await this.$http.get(`/ztProduct/getSubList?pid=${dataId}`, null) //根据选中列的id获得下级数据
        if (res.data && res.data.length > 0) { // 下级数据有数据说明还有子级
          this.dataList3 = res.data
          console.log(this.dataList3, ' dataList3 系统数据')
          if (this.dataList3.length > 0) {
            if (this.dataList3[0].level == 'side') {
              this.dataListName = this.$store.state.word.xianhao
            }
            if (this.dataList3[0].level == 'system1') {
              this.dataListName = '系统'
            }
            if (this.dataList3[0].level == 'system2') {
              this.dataListName = '分系统或设备'
            }
            if (this.dataList3[0].level == 'equipment') {
              this.dataListName = '设备'
            }
          }
        } else { // 没有下级数据说明此级为最后一级节点
          if (!this.multiple) { // 单选
            this.selectedData = []
            let text = this.dataList3.find(item => item.id === dataId)['name']
            this.selectedData.push({id: dataId, name: text})
            this.selectedData = this.selectedData.filter(item => item.id !== this.dataListSubId)
            console.log(this.selectedData, 'this.selectedData的数据')
            this.devName = text
            this.text = text
            console.log(this.text, 'this.text的数据')
          } else { // 多选
            // alert(55)
            let hasItem = false
            // this.dataListSubText = ''
            // this.devName = ''
            this.selectedData.forEach(d => {
              if (d.id === dataId) {
                hasItem = true
                return
              }
            })
            console.log(hasItem, 'hasItem')
            if (!hasItem) {
              // this.dataListSubText = ''
              let text = this.dataList3.find(item => item.id === dataId)['name']
 
              this.selectedData.push({id: dataId, name: text})
              this.selectedData = this.selectedData.filter(item => item.id !== this.dataListSubId)
              console.log(this.selectedData, 'this.selectedData的数据')
              this.text = this.text + (this.text === '' ? '' : ',') + text
            }
            // this.selectValueDev = this.text
            // this.seldetailed = false
            this.devName = this.text
            console.log(this.text, 'this.text WT wente')
          }
        }
        console.timeEnd('select2')
      },
      async setTextOnetime() { // 组件没有初始化时设置text
        // alert("setTextOnetime")
        console.time('valueTime')
        this.selectedData = []
        if (this.value) {
          let res = await this.$http.get(`/ztProduct/${this.value}`, null)
          console.log(res, 'setTextOnetimee /ztProduct/${this.value} res res res res res')
          if (res.data.id != null) {
            this.shipName = res.data.shipName
            this.sysName = res.data.sysName
            this.subName = res.data.subSysName
            this.devName = res.data.deviceName
 
            let res2 = await this.$http.get(`/ztProduct/getSubList?pid=${res.data.pid}`, null)
            this.dataList3 = res2.data
            if (this.dataList3.length > 0) {
              if (this.dataList3[0].level == 'side') {
                this.dataListName = this.$store.state.word.xianhao
              }
              if (this.dataList3[0].level == 'system1') {
                this.dataListName = '系统'
              }
              if (this.dataList3[0].level == 'system2') {
                this.dataListName = '分系统或设备'
              }
              if (this.dataList3[0].level == 'equipment') {
                this.dataListName = '设备'
              }
            }
            this.selectValue = this.value
            if (this.devName) {
              this.selectedData.push({id: res.data.deviceProductId, name: this.devName})
            }
          }else {
            this.value = null
            this.selectValue = this.value
          }
          if (!this.value){
            this.$tip.alert('选择的设备已经不存在')
          }
          console.timeEnd('valueTime')
        } else {
          console.time('NoValueTime')
          this.sysName = ''
          this.subName = ''
          this.devName = ''
          console.timeEnd('NoValueTime')
        }
        console.time('TextTime')
        // this.datas.forEach(d => {
        //   console.log(this.selectedData.findIndex(item => item.id === d.id), 'findIndex')
        //   if (this.selectedData.findIndex(item => item.id === d.id) == -1) {
        //     this.selectedData.push({id: d.id, name: d.name})
        //   }
        // })
        // let text = ''
        // this.selectedData.forEach(d => {
        //   text = text + (text === '' ? '' : ':') + d.name
        // })
        this.text = this.devName
        console.timeEnd('TextTime')
      },
      // cancelSelected(data) {
      //   this.selectedData.some((item, i) => {
      //     if (item.id === data.id) {
      //       this.selectedData.splice(i, 1)
      //       //在数组的some方法中,如果return true,就会立即终止这个数组的后续循环
      //       return true
      //     }
      //   })
      //   let text = ''
      //   this.selectedData.forEach(d => {
      //     text = text + (text === '' ? '' : ',') + d.name
      //   })
      //   this.text = text
      // },
      confirm() {
        this.selectValue = ''
        this.selectedData.forEach(d => {
          this.selectValue = this.selectValue + (this.selectValue === '' ? '' : ',') + d.id
          return
        })
        console.log(this.text, 'confirm this.text')
        console.log(this.selectValue, 'confirm this.selectValue')
        if (this.selectValue  && this.selectValue != 'undefined') {
          this.$emit('input', this.selectValue, this.text)
          this.$emit('confirm', this.selectValue, this.text)
          this.$refs.dialog.close()
        }else{
          this.$tip.alert('请先选择设备再确定')
        }
      }
    }
  }
</script>
 
<style>
  .cascader ul li {
    list-style: none;
  }
 
  .cascader .el-form {
    margin-top: 20px;
  }
 
  .cascader .el-form-item {
    margin-top: 100px;
  }
 
  /*.cascader .input {*/
  /*margin-top: 200px;*/
  /*}*/
 
  .list {
    width: 100%;
    font-size: 16px;
    border: 1px solid #EBEEF5;
    color: #303133;
    -webkit-transition: .3s;
    transition: .3s;
    background: #ffffff;
    box-shadow: 0 3px 6px 0 rgba(72, 119, 232, 0.14);
    height: 500px;
    overflow: auto;
  }
 
  .cascader-ul {
    margin: 0;
    padding: 0;
  }
 
  .list ul li {
    float: left;
    width: 25%;
    height: 40px;
    text-align: left;
    cursor: pointer;
    line-height: 40px;
    padding-left: 10px;
    /*margin-left: 50px;*/
    list-style: none;
  }
  .list1.list ul li {
    width: 50%;
  }
  .list ul li:hover {
    background-color: rgba(123, 224, 244, 0.5);
  }
 
  .list ul li:active {
    background-color: rgba(123, 224, 244, 0.5);
  }
 
  .cascader .el-textarea__inner {
    font-size: 30px;
  }
  .searchList-top .el-form-item__content .el-input-group{
    vertical-align: baseline;
  }
</style>