<template>
  <w-dialog :border="false" closeFree width="600px" @ok="selectConfirm" :title="title" v-model="visible">
    <div>
      <div class="picker">
        <div class="candidate" v-loading="loading">
          <div style="padding: 5px 8px;">
            <el-input v-model="filterText" style="width: 100%;" size="small"
                      clearable placeholder="输入关键字进行过滤" prefix-icon="el-icon-search"/>
            <div style="margin-top: 5px">
              <el-radio-group v-model="radio" size="mini" @input="radioChange">
                <el-radio-button :label="0">角色</el-radio-button>
                <el-radio-button :label="1">部门</el-radio-button>
              </el-radio-group>
            </div>
          </div>
          <!-- 人员选择 -->
          <el-empty :image-size="100" description="似乎没有数据" v-show="dataList.length === 0"/>
          <el-scrollbar style="height:317px">
            <el-tree :data="dataList" ref="tree" :props="defaultProps" empty-text="" node-key="value"
                     :default-expanded-keys="expandedKeys" lazy accordion
                     @node-click="handleChange"
            >
              <!-- :style="data.avatar!==null ? {'height':'50px'}:''"-->
              <div class="tree-node" slot-scope="{  node,data }">
                <div v-if="data.type === 0" style="display: flex;align-items: center;">
                  <el-avatar :src="data.avatar"></el-avatar>
                  {{ node.label }}
                </div>
                <div v-else-if="data.type === 1">
                  <el-icon class="el-icon-user-solid"/>
                  {{ node.label }}
                </div>
                <div v-else>
                  <el-icon class="el-icon-folder-opened"/>
                  {{ node.label }}
                </div>
              </div>
            </el-tree>
          </el-scrollbar>
        </div>

        <div class="selected">
          <div class="count">
            <span>已选 {{ selectList.length }} 项</span>
            <span @click="clearSelected">清空</span>
          </div>
          <div class="org-items" style="height: 350px;">
            <el-empty :image-size="100" description="请点击左侧列表选择数据" v-show="selectList.length === 0"/>
            <div v-for="(selectItem, selectIndex) in selectList" :key="selectIndex" class="org-item">
              <el-avatar :src="selectItem.avatar" style="margin-right: 5px;"></el-avatar>
              {{ selectItem.name }}
              <i class="el-icon-close" @click="noSelected(selectItem)"></i>
            </div>
          </div>
        </div>
      </div>
    </div>
  </w-dialog>
</template>

<script>
import {getUserTree} from "@/api/org";

export default {
  name: "UserPicker",
  props: {
    value: {
      type: Array,
      default: () => {
        return [];
      }
    },
    selected: {
      type: Array,
      default: () => {
        return [];
      }
    },
    multiple: { //是否多选
      default: true,
      type: Boolean
    },
    showCheckbox: { //是否显示左侧选择框
      default: true,
      type: Boolean
    }
  },
  data() {
    return {
      radio: 0,
      chooseId: 0,
      selectItem: {
        type: -1,
        value: '0',
      },
      activeNames: ['1'],
      visible: false,
      loading: false,
      title: "请选择",
      selectList: [],
      filterText: "",
      dataList: [],
      expandedKeys: [],
      defaultProps: {
        value: 'value',
        label: 'name',
        children: 'children',
        isLeaf: function (data, node) {
          return !!data.avatar
        }
      }
    }
  },
  watch: {
    filterText(val) {
      this.$refs.tree.filter(val);
    }
  },
  computed: {
    _value: {
      get() {
        return this.value
      },
      set(val) {
        this.$emit("input", val)
      }
    }
  },
  methods: {
    radioChange(e) {
      this.selectItem.type = -2
      this.chooseId = 0
      this.radio = e
      this.expandedKeys = []
      this.getList();
    },
    getList() {
      getUserTree(this.radio, this.chooseId).then(res => {
        console.log('人员', res.data)
        // if (res.data) {
        if (this.selectItem.type === -1 || this.selectItem.type === -2) {
          this.dataList = res.data
        } else if (this.selectItem.type === 1) {
          this.selectItem.children = res.data
        } else if (this.selectItem.type === 2) {
          this.selectItem.children = res.data
        }
        // }
      });
    },
    setData(source) {
      // for (let item of source) {
      //   this.$set(item, "value", this.selectItem.value + "-" + item.id)
      // }
      return source;
    },
    //通过关键字过滤树节点
    // filterNode(value, data) {
    //   if (!value) return true;
    //   return data.name.indexOf(value) !== -1;
    // },
    //用于用户选择
    showUserPicker() {
      this.selectItem = {
        type: -1,
        value: '0',
      }
      this.dataList = []
      // this.selectList = []
      this.chooseId = 0
      this.radio = 0
      this.visible = true;
      this.getList();

    },
    //渲染子节点用户或部门及用户数据
    handleChange(item, data, node) {
      this.selectItem = item
      this.expandedKeys.push(item.value)
      //不重复发送请求getList()
      if (node.expanded === false) {
        if (item.type !== 0) {
          this.chooseId = item.id
          this.getList()
          return
        }
      }
      //仅选择用户
      if (item.avatar !== null) {
        if (this.multiple) {
          //不添加重复的数据到右边
          for (let i = 0; i < this.selectList.length; i++) {
            if (this.selectList[i].id === item.id) {
              this.selectList.splice(i, 1);
              break;
            }
          }
          this.selectList.push(item);
        } else {
          this.selectList = [item];
        }
      }
      // this._value = this.selectList
    },
    //右侧的×
    noSelected(selectItem) {
      for (let i = 0; i < this.selectList.length; i++) {
        if (this.selectList[i].value === selectItem.value) {
          this.selectList.splice(i, 1);
          break;
        }
      }
      selectItem.selected = false;
    },
    //清空
    clearSelected() {
      this.$confirm("您确定要清空已选中的项?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.selectList = []
      });
    },
    //确定按钮
    selectConfirm() {
      this.$emit("ok", this.selectList);
      // this.dataList = []
      this.visible = false;
    }
  }
};
</script>

<style lang="less" scoped>
@containWidth: 278px;
/deep/ .el-tree-node {
  .el-tree-node__children {
    .el-tree-node {
      .el-tree-node__content {
        height: 42px;
      }
    }
  }
}

///deep/.el-tree-node__children{
//  .is-current{
//    .el-tree-node__content{
//      height: 42px;
//    }
//  }
//}
.tree-node {
  div {
    .el-avatar {
      width: 40px;
      height: 40px;
      margin-right: 5px;
    }
  }
}

/deep/ .el-tree-node {
  .is-leaf + .el-checkbox .el-checkbox__inner {
    display: inline-block;
  }

  .el-checkbox .el-checkbox__inner {
    display: none;
  }
}

/deep/ .el-dialog__body {
  padding: 10px 20px;
}

.picker {
  height: 402px;
  position: relative;
  text-align: left;

  .candidate {
    left: 0;
    top: 0;
  }
}

.candidate, .selected {
  position: absolute;
  display: inline-block;
  width: @containWidth;
  height: 400px;
  border: 1px solid #e8e8e8;
}

.selected {
  border-left: none;
  right: 0;
  top: 0;

  .count {
    width: calc(@containWidth - 20px);
    padding: 10px;
    display: inline-block;
    border-bottom: 1px solid #e8e8e8;
    margin-bottom: 5px;

    & > span:nth-child(2) {
      float: right;
      color: #c75450;
      cursor: pointer;
    }
  }
}

.org-items {
  overflow-y: auto;
  height: 350px;

  .el-icon-close {
    position: absolute;
    right: 5px;
    cursor: pointer;
    font-size: larger;
  }

  .org-item {
    margin: 0 5px;
    border-radius: 5px;
    position: relative;
    padding: 7px 5px;
    display: flex;
    align-items: center;

    cursor: pointer;

    &:hover {
      background: #f1f1f1;
    }

    > span {
      margin-left: 5px;
    }
  }
}

/deep/ .el-scrollbar .el-scrollbar__wrap {
  overflow-x: hidden;
}

::-webkit-scrollbar {
  float: right;
  width: 4px;
  height: 4px;
  background-color: white;
}

::-webkit-scrollbar-thumb {
  border-radius: 16px;
  background-color: #efefef;
}

</style>
