zjf
2023-03-08 51468f93275c2bcfcc7ad25bf05f3d6a079ff764
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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
<template>
  <el-popover
    ref="popover"
    :placement="placement"
    :width="popoverWidth"
    v-model="popoverVisible"
    popper-class="project-com-popover"
    trigger="click">
    <div v-if="popoverContentShow">
      <div class="title-icon">
        <span>{{ title }}</span>
        <span
          class="el-icon-close rt"
          @click="popoverVisible = false"/>
      </div>
      <div class="popover-content-box">
        <div class="select-input">
          <!-- 搜索员工列表 -->
          <el-tabs
            v-model="activeTabName"
            :stretch="true"
            @tab-click="tabClick">
            <el-tab-pane
              v-loading="userLoading"
              v-if="!closeUser"
              label="员工"
              name="user">
              <el-input
                v-model="searchUserInput"
                placeholder="搜索员工"
                size="mini"
                prefix-icon="el-icon-search"
                @input="userSearchChange"/>
              <div class="search-list">
                <div
                  v-for="(user, index) in userList"
                  v-if="!user.hidden"
                  :key="index"
                  class="colleagues-list">
                  <el-checkbox
                    v-model="user.isCheck"
                    @change="userCheckboxChange(user, index)">
                    <div
                      v-photo="user"
                      v-lazy:background-image="$options.filters.filterUserLazyImg(user.img)"
                      class="div-photo search-img header-circle"/>
                    <span>{{ user.realname }}</span>
                  </el-checkbox>
                </div>
              </div>
            </el-tab-pane>
            <el-tab-pane
              v-loading="depLoading"
              v-if="!closeDep"
              label="部门"
              name="dep">
              <el-input
                v-model="searchDepInput"
                placeholder="搜索部门"
                size="mini"
                style="margin-bottom:10px;"
                prefix-icon="el-icon-search"
                @input="depSearchChange"/>
              <div class="search-list">
                <el-breadcrumb separator-class="el-icon-arrow-right">
                  <el-breadcrumb-item
                    v-for="(item, index) in breadcrumbList"
                    :key="index">
                    <a
                      href="javascript:;"
                      @click="breadcrumbBtn(item, index)">{{ item.label }}</a>
                  </el-breadcrumb-item>
                </el-breadcrumb>
                <div
                  v-for="(depItem, index) in depShowList"
                  v-if="!depItem.hidden"
                  :key="index"
                  class="checkout-box">
                  <el-checkbox
                    v-model="depItem.isCheck"
                    @change="depCheckboxChange(depItem, index)"/>
                  <div @click="enterDepChildren(depItem)">
                    <span>{{ depItem.name }}</span>
                    <span
                      v-if="depItem.children"
                      class="el-icon-arrow-right"/>
                  </div>
                </div>
              </div>
            </el-tab-pane>
          </el-tabs>
        </div>
        <div class="checked-content">
          <div class="checked-top">
            <span class="title">已选择</span>
            <span
              v-if="!closeUser"
              class="title">员工 ({{ userSelectCount }})</span>
            <span
              v-if="!closeDep"
              class="title">部门 ({{ depSelectCount }})</span>
            <el-button
              type="text"
              class="rt"
              @click="emptyClick">清空</el-button>
          </div>
          <div class="border-content">
            <div
              v-for="(item, index) in checkedUserDepList"
              :key="index"
              class="checked-list">
              <div
                v-photo="item"
                v-lazy:background-image="$options.filters.filterUserLazyImg(item.img)"
                v-if="item.type === 'user'"
                class="div-photo"/>
              <span v-if="item.type === 'user'"> {{ item.realname }} </span>
              <span v-else> {{ item.name }} </span>
              <span
                class="rt el-icon-close"
                @click="selectDelect(item, index)"/>
            </div>
          </div>
        </div>
      </div>
      <div class="popover-footer">
        <el-button
          type="primary"
          @click="popoverSubmit">确 定</el-button>
        <el-button @click="popoverVisible = false">取 消</el-button>
      </div>
    </div>
    <div
      slot="reference"
      :style="{'display':contentBlock ? 'block' : 'inline-block'}"
      @click="showContentClick">
      <slot name="membersDep"/>
    </div>
  </el-popover>
 
</template>
<script>
// import { usersList, depList } from '@/api/common'
export default {
  props: {
    // 弹出框宽度
    popoverWidth: {
      type: String,
      default: '600'
    },
    // 标题
    title: {
      type: String,
      default: '选择成员'
    },
    // 显示位置
    placement: {
      type: String,
      default: 'bottom-start'
    },
    // 内容框类型
    contentBlock: {
      type: Boolean,
      default: true
    },
    // 编辑时 -- 用户默认勾选的数据
    userCheckedData: {
      type: Array,
      default: () => {
        return []
      }
    },
    // 编辑时 -- 部门默认勾选的数据
    depCheckedData: {
      type: Array,
      default: () => {
        return []
      }
    },
    // 是否关闭某个类的选择
    closeUser: {
      type: Boolean,
      default: false
    },
    closeDep: {
      type: Boolean,
      default: false
    },
    userRequest: Function,
    userParams: Object
  },
  data () {
    return {
      activeTabName: 'user',
      // 筛选
      searchUserInput: '',
      searchDepInput: '',
      popoverVisible: false,
      // 内容展示
      popoverContentShow: false,
      // 加载动画
      userLoading: false,
      depLoading: false,
      // 所有用户
      userList: [],
      // 展示客户
      depShowList: [],
      // 面包屑数据
      breadcrumbList: [],
      // 选中的数据 -- 员工和部门一个数组用于页面展示
      checkedUserDepList: []
    }
  },
  computed: {
    userSelectCount () {
      return this.checkedUserDepList.filter(item => {
        return item.type === 'user'
      }).length
    },
    depSelectCount () {
      return this.checkedUserDepList.filter(item => {
        return item.type === 'dep'
      }).length
    }
  },
  watch: {
    userCheckedData: function () {
      this.updateCheckInfoByWatch()
    },
    depCheckedData: function () {
      this.updateCheckInfoByWatch()
    },
    popoverVisible: function (val) {
      if (val) {
        this.updateCheckInfoByWatch()
      }
    }
  },
  methods: {
    initInfo () {
      // 用户列表
      this.checkedUserDepList = this.userCheckedData
        .map(item => {
          item.type = 'user'
          return item
        })
        .concat(
          this.depCheckedData.map(item => {
            item.type = 'dep'
            return item
          })
        )
 
      if (!this.closeUser) {
        this.getUserList()
      } else if (!this.closeDep) {
        this.activeTabName = 'dep'
        this.getDepList()
      }
    },
    tabClick () {
      if (this.activeTabName === 'dep') {
        if (this.depShowList.length === 0) {
          this.getDepList()
        }
      }
    },
    userSearchChange (val) {
      this.userList = this.userList.map(item => {
        if (item.realname.indexOf(val) !== -1) {
          item.hidden = false
        } else {
          item.hidden = true
        }
        return item
      })
    },
    depSearchChange (val) {
      this.depShowList = this.depShowList.map(item => {
        if (item.name.indexOf(val) !== -1) {
          item.hidden = false
        } else {
          item.hidden = true
        }
        return item
      })
    },
    /**
     * 部门信息
     */
    // 部门列表数据
    getDepList () {
      this.depLoading = true
      // depList({ type: 'tree' })
      //   .then(res => {
      //     this.depShowList = res.data.map((item, index, array) => {
      //       item.type = 'dep'
      //       item.isCheck = this.getItemCheckInfo(item, 'dep')
      //       return item
      //     })
      //     this.breadcrumbList.push({ label: '全部', data: this.depShowList })
      //     this.depLoading = false
      //   })
      //   .catch(() => {
      //     this.depLoading = false
      //   })
    },
    // 部门下一级
    enterDepChildren (depItem) {
      if (depItem.children) {
        this.depShowList = []
        this.depShowList = depItem.children.map((item, index, array) => {
          item.type = 'dep'
          if (item.name.indexOf(this.searchDepInput) !== -1) {
            item.hidden = false
          } else {
            item.hidden = true
          }
          item.isCheck = this.getItemCheckInfo(item, 'dep')
          return item
        })
        this.breadcrumbList.push({
          label: depItem.label,
          data: this.depShowList
        })
      }
    },
    // 面包屑点击事件
    breadcrumbBtn (item, index) {
      if (index + 1 <= this.breadcrumbList.length - 1) {
        this.breadcrumbList.splice(index + 1, this.breadcrumbList.length - 1)
      }
      this.depShowList = []
      this.depShowList = item.data.map((item, index, array) => {
        if (item.name.indexOf(this.searchDepInput) !== -1) {
          item.hidden = false
        } else {
          item.hidden = true
        }
        item.isCheck = this.getItemCheckInfo(item, 'dep')
        return item
      })
    },
    // 部门勾选
    depCheckboxChange (item, aindex) {
      this.$set(this.depShowList, aindex, item)
      this.updateCheckedUserDepListByCheck(item, 'dep')
    },
    /**
     * 员工操作
     */
    getUserList () {
      this.userLoading = true
      // let request = usersList
      // let params = {}
      if (this.userRequest) {
        // request = this.userRequest
        // params = this.userParams || {}
      } else {
        // params = { pageType: 0 }
      }
      // request(params)
      //   .then(res => {
      //     this.userList = res.data.map(item => {
      //       item.type = 'user'
      //       item.isCheck = this.getItemCheckInfo(item, 'user')
      //       return item
      //     })
      //     this.userLoading = false
      //   })
      //   .catch(() => {
      //     this.userLoading = false
      //   })
    },
    // 员工勾选
    userCheckboxChange (item, aindex) {
      this.$set(this.userList, aindex, item)
      this.updateCheckedUserDepListByCheck(item, 'user')
    },
    // check 操作后的 存储数据刷新
    updateCheckedUserDepListByCheck (item, type) {
      var removeIndex = -1
      for (let index = 0; index < this.checkedUserDepList.length; index++) {
        const element = this.checkedUserDepList[index]
        if (element.type === 'user' && item.userId === element.userId) {
          removeIndex = index
        } else if (element.type === 'dep' && item.id === element.id) {
          removeIndex = index
        }
      }
      if (removeIndex === -1) {
        this.checkedUserDepList.push(item)
      } else if (removeIndex !== -1) {
        this.checkedUserDepList.splice(removeIndex, 1)
      }
    },
    // 获取事项标记信息
    getItemCheckInfo (item, type) {
      if (this.checkedUserDepList.length === 0) {
        return false
      }
      var hasItem = false
      for (let index = 0; index < this.checkedUserDepList.length; index++) {
        const element = this.checkedUserDepList[index]
        if (element.type === 'user' && item.userId === element.userId) {
          hasItem = true
          break
        } else if (element.type === 'dep' && item.id === element.id) {
          hasItem = true
          break
        }
      }
      return hasItem
    },
    /**
     * 删除 清空 等操作
     */
    // 删除一个选择员工或部门
    selectDelect (selectItem, index) {
      this.checkedUserDepList.splice(index, 1)
      if (selectItem.type === 'dep') {
        this.depShowList = this.depShowList.map((item, index, array) => {
          item.isCheck = this.getItemCheckInfo(item, 'dep')
          return item
        })
      } else {
        this.userList = this.userList.map((item, index, array) => {
          item.isCheck = this.getItemCheckInfo(item, 'user')
          return item
        })
      }
    },
    // 提交按钮
    popoverSubmit () {
      this.popoverVisible = false
      this.$emit(
        'popoverSubmit',
        this.checkedUserDepList.filter(item => {
          return item.type === 'user'
        }),
        this.checkedUserDepList.filter(item => {
          return item.type === 'dep'
        })
      )
    },
    // 清空按钮
    emptyClick () {
      this.checkedUserDepList = []
      for (let index = 0; index < this.userList.length; index++) {
        this.userList[index].isCheck = false
      }
 
      for (let index = 0; index < this.depShowList.length; index++) {
        this.depShowList[index].isCheck = false
      }
    },
    // 内容可见
    showContentClick () {
      if (!this.popoverContentShow) {
        this.popoverContentShow = true
        this.initInfo()
      }
    },
    // 监听父类改变更新check
    updateCheckInfoByWatch () {
      this.checkedUserDepList = this.userCheckedData
        .map(item => {
          item.type = 'user'
          return item
        })
        .concat(
          this.depCheckedData.map(item => {
            item.type = 'dep'
            return item
          })
        )
      this.userList = this.userList.map(item => {
        item.isCheck = this.getItemCheckInfo(item, 'user')
        return item
      })
      this.depShowList = this.depShowList.map((item, index, array) => {
        item.isCheck = this.getItemCheckInfo(item, 'dep')
        return item
      })
    }
  }
}
</script>
 
<style scoped lang="scss">
@import '@/styles/mixin.scss';
.popover-content-box {
  display: flex;
  .select-input {
    flex: 1;
    margin-right: 30px;
    .select-input > .el-input {
      margin: 10px 0;
    }
  }
  .select-input /deep/ .el-tabs {
    .el-tabs__active-bar {
      display: none;
    }
    .el-tabs__nav-wrap::after {
      height: 0;
    }
    .el-tabs__content {
      border: 1px solid #e6e6e6;
      height: 300px;
      .el-checkbox {
        margin-left: 0;
        margin-right: 10px;
      }
    }
    .el-breadcrumb {
      margin-bottom: 15px;
    }
    .checkout-box {
      display: flex;
      margin-bottom: 10px;
    }
    .checkout-box > div {
      flex: 1;
      .el-icon-arrow-right {
        float: right;
      }
      span {
        cursor: pointer;
      }
    }
    .el-tab-pane {
      padding: 10px;
      height: 100%;
    }
    .el-tab-pane > .el-input {
      width: 90%;
      margin: auto 5%;
      .el-input__inner {
        border-radius: 15px;
      }
    }
  }
  .checked-content {
    flex: 1;
    .checked-top {
      height: 40px;
      line-height: 40px;
      margin-bottom: 15px;
      .title {
        color: #999999;
      }
    }
    .border-content {
      border: 1px solid #e6e6e6;
      height: 300px;
      overflow: auto;
      padding: 20px 0;
      @include scrollBar;
      .checked-list {
        height: 30px;
        line-height: 30px;
        padding: 0 20px;
        cursor: pointer;
        .el-icon-close {
          opacity: 0;
          margin-top: 8px;
          margin-right: 0;
        }
        .div-photo {
          width: 24px;
          height: 24px;
          border-radius: 12px;
          vertical-align: middle;
          margin-right: 8px;
        }
      }
      .checked-list:hover {
        -webkit-box-shadow: 0 0 8px 2px #eee;
        box-shadow: 0 0 8px 2px #eee;
        .el-icon-close {
          opacity: 1;
        }
      }
    }
  }
}
.title-icon {
  padding: 10px 20px 15px;
  border-bottom: 1px solid #e6e6e6;
  margin-bottom: 10px;
  font-size: 16px;
  .el-icon-close {
    font-size: 20px;
    color: #ccc;
    margin-right: 0;
  }
}
.popover-footer {
  float: right;
  margin: 20px 0 0;
}
/* 选择员工 */
.search-img {
  width: 24px;
  height: 24px;
  border-radius: 12px;
  vertical-align: middle;
  margin-right: 8px;
}
.search-list {
  margin: 5px;
  height: 248px;
  overflow: auto;
  margin-right: -10px;
  padding-right: 10px;
  @include scrollBar;
}
.colleagues-list {
  padding: 5px;
}
</style>