<template>
  <div class="el-tree-select">
    <el-row :span="24">
      <el-col :span="6">
        <el-input
            v-model="filterTxt"
            style="width: 240px;margin-bottom: 10px"
            placeholder="搜索"
            @input="filterInput()"
        />
        <el-tree
            id="tree-option"
            ref="treeData"
            show-checkbox
            :accordion="accordion"
            :data="treeList"
            :props="objMap"
            :node-key="objMap.value"
            :default-expand-all="true"
            :expand-on-click-node="false"

            :default-expanded-keys="defaultExpandedKey"
            :filter-node-method="filterNode"
            @check="handleNodeClick"
        >
        </el-tree>
      </el-col>
      <el-col :span="16">
        <el-input
            v-model="nickName"
            style="width: 240px;margin-bottom: 10px;margin-left: 19px"
            placeholder="搜索"
            @input="getUserList()"
        />
        <el-table  @selection-change="handleSelectionChange" ref="userRef" :data="userData" row-key="id" style="width: 100%" stripe>
          <el-table-column type="selection" width="55" />
          <el-table-column fixed prop="nickname" label="名称" :show-overflow-tooltip="true"/>
          <el-table-column fixed prop="mobile" label="联系方式" :show-overflow-tooltip="true"/>
        </el-table>

      </el-col>
    </el-row>


  </div>
</template>

<script setup name="userData">

import {getDeptTree,getUserData} from "@/api/knowledge/system/system";

const { proxy } = getCurrentInstance();
const nickName =ref('');
const props = defineProps({
  /* 配置项 */
  objMap: {
    type: Object,
    default: () => {
      return {
        value: 'id', // ID字段名
        label: 'label', // 显示名称
        children: 'children' // 子级字段名
      }
    }
  },
  selfStyle:{
    type: Boolean,
    default: () => {
      return true
    }
  },
  /* 自动收起 */
  accordion: {
    type: Boolean,
    default: () => {
      return false
    }
  },
  needCheck: {
    type: Boolean,
    default: () => {
      return false
    }
  },
  /**当前双向数据绑定的值 */
  value: {
    type: [String, Number],
    default: ''
  },
  /**输入框内部的文字 */
  placeholder: {
    type: String,
    default: ''
  }
})

const emit = defineEmits(['update:value']);
const treeList = ref([])
const filterTxt = ref('');
const deptIds = ref('');
const selectId = ref('');
const valueId = computed({
  get: () => props.value,
  set: (val) => {
    emit('update:value', val)
  }
});
const valueTitle = ref('');
const defaultExpandedKey = ref([]);
const selectUser=ref([])

function handleSelectionChange(selectData){
  selectUser.value = selectData
}
function initHandle() {
  nextTick(() => {
    const selectedValue = valueId.value;
    if(selectedValue !== null && typeof (selectedValue) !== 'undefined') {
      const node = proxy.$refs.treeData.getNode(selectedValue)
      if (node) {
        valueTitle.value = node.data[props.objMap.label]
        proxy.$refs.treeData.setCurrentKey(selectedValue) // 设置默认选中
        defaultExpandedKey.value = [selectedValue] // 设置默认展开
      }
    } else {
      clearHandle()
    }
  })
}
function filterInput(){
  selectFilterData(filterTxt.value)
}
defineExpose({
  getTreeList,selectUser
});

function getTreeList() {
  getDeptTree().then((res) => {
    treeList.value = res.data.data
  })
}
const userData =ref([])
function getUserList(){
  getUserData(deptIds.value,nickName.value).then((res) => {
    userData.value = res.data.data
  })
}
function handleNodeClick() {
  let data =  proxy.$refs['treeData'].getCheckedNodes()
  let tempData=[]
  for (let i = 0; i < data.length; i++) {
    tempData.push(data[i].id)
  }
  deptIds.value = tempData.join(",")
  getUserList()
  proxy.$forceUpdate()
}
function selectFilterData(val) {
  proxy.$refs.treeData.filter(val)
}
function filterNode(value, data) {
  if (!value) return true
  return data[props.objMap['label']].indexOf(value) !== -1
}
function clearHandle() {
  valueTitle.value = ''
  valueId.value = ''
  defaultExpandedKey.value = [];
  clearSelected()
}
function clearSelected() {
  const allNode = document.querySelectorAll('#tree-option .el-tree-node')
  allNode.forEach((element) => element.classList.remove('is-current'))
}

onMounted(() => {
  initHandle()
})

watch(valueId, () => {
  initHandle();
})
</script>

<style lang='scss' scoped>
@import "@/assets/styles/variables.module.scss";
.el-scrollbar .el-scrollbar__view .el-select-dropdown__item {
  padding: 0;
  background-color: #fff;
  height: auto;
}

.el-select-dropdown__item.selected {
  font-weight: normal;
}

ul li .el-tree .el-tree-node__content {
  height: auto;
  padding: 0 20px;
  box-sizing: border-box;
}

:deep(.el-tree-node__content:hover),
:deep(.el-tree-node__content:active),
:deep(.is-current > div:first-child),
:deep(.el-tree-node__content:focus) {
  background-color: mix(#fff, $--color-primary, 90%);
  color: $--color-primary;
}
.f-box{
  width: 13px;
  height: 13px;
  border-radius: 2px;
  border: 1px solid #a3a7a8;
  margin-right: 5px;
}
.custom-tree-node{
  display: flex;
  align-items: center;
}
.f-box-y{
  width: 13px;
  height: 13px;
  border-radius: 2px;
  border: 1px solid #1890ff;
  margin-right: 5px;
  background: #1890ff;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
}

</style>
