selectEmp.vue 7.8 KB
<template>
  <div class="select-emp-panel">
    <div class="scrm-p10 emp-tree-panel">
      <div class="popper-arrow"></div>
      <div
        class="emp-scroll-panel"
        :style="{
          height: treeHeight + 'px'
        }"
      >
        <div class="tree-title">所在门店/部门</div>
        <el-tree v-loading="loading" :data="listTree" :expand-on-click-node="false" highlight-current :props="defaultProps" @node-click="handleNodeClick"></el-tree>
      </div>
    </div>
    <el-col
      class="scrm-p10 emp-table-panel"
      :style="{
        height: treeHeight + 20 + 'px'
      }"
    >
      <GLTable :key="key" ref="saleTable" v-loading="loading" :tableData="tableEmpData" :tableParams="tableParams" :hasNum="false" :hasPage="false" @selectionTable="selectionTable">
        <div class="table-btns" slot="optionBtns" slot-scope="scope">
          <el-button type="text" v-show="!group.staffName" @click="selectGroup(scope.scope)">设为群主</el-button>
          <el-button type="text" v-show="group.staffName && scope.scope.staffName == group.staffName" @click="cancelGroup">取消群主</el-button>
        </div>
      </GLTable>
    </el-col>
  </div>
</template>
<script>
export default {
  name: 'selectEmp',
  props: ['selectData', 'key'],
  data() {
    return {
      treeHeight: 0,
      staffName: '',
      listTree: [],
      isFirst: true,
      defaultProps: {
        children: 'children',
        label: 'departmentName'
      },
      // 表格参数
      tableParams: {
        checkSelectKey: 'id',
        checkSelectVal: [],
        listName: 'list',
        // 行数据的key
        mutiSelectUniqueKey: 'id',
        // 多选ref的名称
        mutiSelectName: 'salelist',
        btnId: new Date().getTime() + parseInt(Math.random() * 1000),
        tableColumns: [
          {
            label: '姓名',
            prop: 'staffName'
          },
          {
            label: '职务',
            prop: 'staffTitle'
          },
          {
            label: '',
            prop: 'optionBtns',
            addHtml: 'optionBtns'
          }
        ]
      },
      tableEmpData: [],
      group: {},
      allGroup: [],
      employeeList: [],
      loading: true,
      currentArr: []
    }
  },
  watch: {
    selectData: {
      handler(val) {
        console.log(this.group)
        console.log(val)
        if (val.length == 0) {
          this.clearSelection()
        } else {
          this.$nextTick(() => {
            val.forEach(i => {
              const row = this.getEmployee(this.listTree).find(j => j.staffId == i.staffId)
              console.log(row)
              this.clearSelect(row)
            })
          })
          this.group = val.find(i => i.masterStatus == 1)
        }
      },
      immediate: true
    }
  },
  methods: {
    selectionTable(val) {
      console.log(val, this.group, this.isFirst)
      this.$emit('selectTable', val)
      this.allGroup = val
      const hasGroup = val.find(item => item.staffId == this.group.staffId)
      if (!hasGroup) {
        this.cancelGroup()
      }
      // if (val && !this.isFirst) {
      //   val.forEach(i => {
      //     const row = this.getEmployee(this.listTree).find(j => j.staffId == i.staffId)
      //     console.log(row)
      //     this.clearSelect(row)
      //   })
      // }
    },
    listDepartmentTree() {
      this.$apis.groupGuest.listDepartmentTree({ merchantId: 10001001, addChildrensFlag: true }).then(res => {
        this.listTree = res
        this.loading = false
        this.isFirst = false
        this.listTree[0].entmicroStaffResponses = this.getEmployee(this.listTree)
        console.log(this.listTree, this.selectData)
        this.currentArr = this.$common.utils.deepClone(this.listTree[0].entmicroStaffResponses)
        this.gruop = this.selectData.find(i => i.masterStatus == 1)
        this.$nextTick(() => {
          this.selectData.forEach(i => {
            const row = this.getEmployee(this.listTree).find(j => j.staffId == i.staffId)
            console.log(row)
            this.clearSelect(row)
          })
          document.getElementsByClassName('el-tree-node__expand-icon')[0].click()
          document.getElementsByClassName('el-tree-node__content')[0].click()
          if (this.selectData.find(i => i.masterStatus == 1)) {
            this.group = this.selectData.find(i => i.masterStatus == 1)
            this.$emit('group', this.group)
          }
        })
      })
    },
    clearSelect(row) {
      this.$refs.saleTable.$refs.salelistMutiTable.toggleRowSelection(row)
    },
    clearSelection() {
      this.$refs.saleTable.$refs.salelistMutiTable.clearSelection()
    },
    handleNodeClick(data) {
      this.departmentId = data.id
      this.tableEmpData = data.entmicroStaffResponses
      this.currentArr = this.$common.utils.deepClone(data.entmicroStaffResponses)
      // this.listEntmicroStaff()
    },
    selectGroup(data, flag = false) {
      console.log(data)
      this.group = data
      // 标识是否从父组件修改
      if (!flag) {
        const obj = this.allGroup.find(item => item.staffId == data.staffId)
        if (!obj) this.$refs.saleTable.$refs.salelistMutiTable.toggleRowSelection(data)
        this.$emit('group', this.group)
      }
    },
    cancelGroup() {
      this.group = {}
      this.$emit('group', this.group)
    },
    getEmployee(d, arr = []) {
      d.forEach(i => {
        if (i.entmicroStaffResponses) {
          i.entmicroStaffResponses.forEach(j => {
            arr.push(j)
          })
        }
        if (i.children) {
          this.getEmployee(i.children, arr)
        }
      })
      return arr
    },
    searchTable(search) {
      let arr = []
      if (search) {
        this.currentArr.forEach(item => {
          console.log(item.staffName.indexOf(search) != -1)
          if (item.staffName.indexOf(search) != -1) {
            console.log(item)
            arr.push(item)
          }
        })
      } else {
        arr = this.$common.utils.deepClone(this.listTree[0].entmicroStaffResponses)
      }
      this.tableEmpData = arr
    }
  },
  created() {
    this.listDepartmentTree()
  },
  mounted() {
    let clientHeight = document.documentElement.clientHeight
    this.treeHeight = clientHeight - 800
    console.log(this.selectData, '.....')
  }
}
</script> 
<style scoped lang="scss">
.select-emp-panel {
  display: flex;
  .emp-tree-panel {
    margin-top: 10px;
    width: 200px;
    position: relative;
    background: #ffffff;
    box-shadow: 0px 2px 12px 0px rgba(0, 0, 0, 0.06);
    border: 1px solid #e4e7ed;
    .emp-scroll-panel {
      min-height: 200px;
      overflow: auto;
      box-sizing: border-box;
    }
    .popper-arrow {
      border-top-width: 0;
      border-width: 6px;
      position: absolute;
      top: -12px;
      left: 30px;
      display: block;
      width: 0;
      height: 0;
      border-color: transparent;
      border-style: solid;
      border-bottom-color: #e4e7ed;
      &:after {
        content: ' ';
        border-width: 6px;
        top: 1px;
        margin-left: -6px;
        border-top-width: 0;
        position: absolute;
        display: block;
        width: 0;
        height: 0;
        border-color: transparent;
        border-style: solid;
        border-bottom-color: #fff;
      }
    }
  }
  .emp-table-panel {
    flex: 1;
    min-height: 200px;
    overflow: auto;
    margin-top: 10px;
    background: #ffffff;
    box-shadow: 0px 2px 12px 0px rgba(0, 0, 0, 0.06);
    border: 1px solid #e4e7ed;
    /deep/ .el-checkbox__input.is-disabled .el-checkbox__inner {
      background-color: #999999;
    }
  }
  .emp-tree-panel {
    margin-right: 10px;
    font-size: 15px;
    /deep/ .el-tree-node:focus > .el-tree-node__content {
      color: #5850ff;
      background: #f2f5fa;
    }
  }
  .tree-title {
    font-size: 16px;
    font-weight: 500;
    color: #333333;
    line-height: 14px;
    padding: 10px;
  }
}
</style>