<template>
  <div>
    <el-dialog title="组织机构人员选择" width="50%" :close-on-click-modal="false" v-model="props.showDdld" @close="closeDialog">
      <div class="search">
        <el-input></el-input>
        <el-button type="primary">
          <el-icon>
            <Search />
          </el-icon>
          <span>搜索</span>
        </el-button>
      </div>
      <el-form :model="listQuery" :rules="rules" label-width="100px" class="demo-ruleForm" :size="formSize" status-icon>

      </el-form>
      <!-- 组织机构信息 -->
      <div class="box">
        <div class="treeBox">
          <el-tree ref="singleTableRef" class="filter-tree" :props="endProps" lazy node-key="id" :load="loadNode"
            :default-expanded-keys="treemrData" check-strictly @node-click="nodeClick" :filter-node-method="filterNode"
            @check-change="checkChange" :data="treeData" show-checkbox @check="handleCheck"
            :default-checked-keys="ids" />
        </div>
        <div class="cskbox">
          <!-- 穿梭按钮 -->
          <el-icon @click="pushRight">
            <DArrowRight />
          </el-icon>
          <el-icon @click="pushLeft">
            <DArrowLeft />
          </el-icon>
        </div>

        <div class="listbox">
          <!-- 选中的组员 -->
          <ul>
            <li :class="item.active ? 'activ' : ''" v-for="(item, index) in rightList" :key="item"
              @click="clicklist(item, index)">
              {{ item.orgName }}
            </li>
          </ul>
        </div>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="handleSave">
            <el-icon>
              <DocumentChecked />
            </el-icon>
            <span>确定</span>
          </el-button>
          <el-button type="primary" @click="clearlist">
            <el-icon>
              <DocumentRemove />
            </el-icon>
            <span>清空</span>
          </el-button>
          <el-button @click="handleClose" type="primary">
            <el-icon>
              <DocumentDelete />
            </el-icon>
            <span>关闭</span>
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
// 引入
import {
  defineProps,
  reactive,
  watch,
  onMounted,
  ref,
  defineEmits,
  proxyRefs
} from "vue";

import {
  getDept,
} from "@/api/user-manage";
const treeData = ref([]); // 树的数据
const node_had = ref([]);
const treemrData = ref([]);
const singleTableRef = ref();
const rightList = ref([]); //右边列表数据
const diIndex = ref(); //点击右边列表数据
const leftList = ref([]); //存放右边被点击的数据
const total = ref(0);
const tableData = ref([]);
const resolve_had = ref([]);
const listQuery = ref({
  deptcode: "",
  parentid: "",
  searchType: props.searchType
});

const props = defineProps({
  showDdld: {
    type: Boolean,
    default: true
  },
  zzlx: String,
  formInfo: {
    type: Object,
    default: {}
  },
  deptcode: {
    type: String,
    default: ""
  },
  // 查询层级 01.查询所有层级(默认) 02.查询所有省、市公司 03.查询所有省、市、县公司
  searchType: {
    type: String,
    default: ""
  }
});
const ids = ref([])
watch(
  () => props.formInfo,
  (val) => {
    if (val.length > 0) {
      props.formInfo.userList.map((item) => {
        ids.value.push(item.userId)
      })
    }

  }
)
//根据外部传入ID查询部门信息
watch(
  () => props.deptcode,
  (newVal) => {
    if (newVal) {
      listQuery.value.deptcode = newVal;
      getTreeData();
    }
  },
  { deep: true, immediate: true }
);
const currentRow = ref(-1); //选中的数据
const emits = defineEmits(["closezzjg", "listdata"]);
onMounted(() => {
  // geiUserData();
  getTreeData();
});
const endProps = {
  children: "childDeptList",
  value: "id",
  label: "orgName",
  isLeaf: "leaf"
};
//懒加载方法
async function loadNode(node, resolve) {
  listQuery.value.parentid = node.data.id;
  if (node.level === 0) {
    node_had.value = node;
    resolve_had.value = resolve;
    getTreeData();
  }
  if (node.level >= 1) {

    getDept(listQuery.value).then((res) => {
      for (let i = 0; i < res.length; i++) {
        res[i].leaf = !res[i].hasChildren;
      }
      resolve(res);
    });
  }
}
// 获取部门树的数据
function getTreeData() {
  getDept(listQuery.value).then((res) => {
    if (res.length == 1) {
      res.forEach(element => {
        treemrData.value.push(element.id)
      });
    }
    for (let i = 0; i < res.length; i++) {
      res[i].leaf = !res[i].hasChildren;
    }
    if (res.length > 0) treeData.value = res;
  });
}
const filterNode = (value, data) => {
  if (!value) return true;
  return data.orgName.includes(value);
};

//只展示最后一节节点的复选框
// const checkChange = (data, isChecked) => {
//   if (isChecked) {
//     const checked = [data.id] // id为tree的node-key属性
//     singleTableRef.value.setCheckedKeys(checked)
//   }

// }
//获取复选框中被选中的值
function handleCheck(data, checkedData) {
  let arr = checkedData.checkedNodes;
  for (let i = 0; i < arr.length; i++) {
    const e = arr[i];
    // if (e.hasChildren == true) {
    //   arr.splice(i, 1);
    // }
  }
  currentRow.value = arr;
  currentRow.value.zzlx = props.zzlx;
}

//点击右箭头
function pushRight() {
  rightList.value = currentRow.value;
}
//点击左箭头
function pushLeft() {
  //遍历右边选中的数据
  for (let i = 0; i < leftList.value.length; i++) {
    const e = leftList.value[i];
    if (e.id) {
      singleTableRef.value.setChecked(e.id, false);
    }

    rightList.value.forEach((item, index) => {
      if (e.id == item.id) {
        rightList.value.splice(index, 1);
      }
    });
    currentRow.value.forEach((item, index) => {
      if (e.id == item.id) {
        currentRow.value.splice(index, 1);
      }
    });
  }
}
//点击右边列表的每一项
function clicklist(item, index) {
  item.active = !item.active;
  diIndex.value = index;
  if (item.active) {
    leftList.value.push(item);
  } else {
    for (let i = 0; i < leftList.value.length; i++) {
      const element = leftList.value[i];
      if (element.id == item.id) {
        leftList.value.splice(i, 1);
      }
    }
  }
}
//点击清空列表
function clearlist() {
  leftList.value = [];
  rightList.value = [];
  currentRow.value = [];
  singleTableRef.value.setCheckedKeys([], false);
}
const closeDialog = () => {
  emits("closezzjg", false);
};
const handleSave = () => {
  emits("closezzjg", false);
  emits("listdata", rightList.value);
};
const handleClose = () => {
  emits("closezzjg", false);
};

</script>

<style lang="scss" scoped>
:v-deep .el-tree-node {
  .is-leaf+el-checkbox .el-checkbox__inner {
    display: inline-block;
  }
}

::v-deep .el-overlay-dialog {
  overflow: hidden !important;
}

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

:v-deep .wrap .el-checkbox__input.is-disabled {
  display: none;
}

.el-form {
  margin-top: 10px;
  display: flex;
  //   flex-wrap: wrap;
  justify-content: space-between;
}

.search {
  width: 300px;
  display: flex;
  margin-bottom: 10px;
  justify-content: flex-end;
  margin-left: 65%;
}

.el-form-item {
  text-align: center;
  width: 400px;
}

.flx {
  display: flex;
  width: 100%;

  .el-button {
    margin-left: 10px;
  }
}

.box {
  width: 100%;
  height: 400px;
  overflow-y: hidden;
  border: 1px solid var(--el-color-primary);
  display: flex;
  justify-content: space-between;
}

.treeBox {
  width: 50%;
  overflow: auto;
}

.cskbox {
  width: 40px;
  padding: 107px 0;

  .el-icon {
    width: 4em !important;
    height: 6em !important;
  }

  .el-icon svg {
    width: 3em !important;
    height: 3em !important;
  }
}

.listbox {
  border: 1px solid var(--el-color-primary);
  width: 300px;
  height: 400px;
  overflow-y: scroll;

  ul>li {
    padding: 4px 10px;
    box-sizing: border-box;
  }

  .activ {
    // background-color: var(--el-color-primary) ;
    background: var(--el-color-primary);
    color: #fff
  }
}

.bot_font {
  width: 100%;
  text-align: center;
  font-size: 16px;
  color: #ffff;
  margin-top: 10px;
}

.fenye {
  margin-top: 10px;
}
</style>
