<template>
  <div>
    <el-dialog title="组织机构部门选择" width="50%" v-model="props.showDdld" @close="closeDialog" :close-on-click-modal="false">
      <div class="search">
        <el-input></el-input>
        <el-button type="primary">
          <el-icon>
            <Search />
          </el-icon>
          <span>搜索</span>
        </el-button>
      </div>
      <!-- 组织机构信息 -->
      <div class="box">
        <div class="treeBox">
          <el-tree ref="singleTableRef" :data="treeData" :props="endProps" node-key="id" show-checkbox
            @check-change="checkChange" :load="loadNode" @check="handleCheck" lazy :default-expanded-keys="treemrData"
            :default-checked-keys="[id]" />
        </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,
  watch,
  onMounted,
  ref,
  defineEmits,
} from "vue";
import {
  getDept,

} from "@/api/user-manage";
const singleTableRef = ref();
const endProps = {
  children: "childDeptList",
  value: "id",
  label: "orgName",
  isLeaf: "leaf"
};
const treemrData = ref([]);
const node_had = ref([]);
const resolve_had = ref([]);
const treeData = ref([]); // 树的数据
const listQuery = ref({
  deptcode: "",
  parentid: "",
  searchType: "03",
  pageNum: 1,
  pageSize: 20
});
const props = defineProps({
  showDdld: {
    type: Boolean,
    default: true
  },
  zzlx: String,
  formInfo: {
    type: Object,
    default: {}
  }
});
const currentRow = ref(-1); //选中的数据
const emits = defineEmits(["closezzjg", "listdata"]);
onMounted(() => {
  getTreeData();
});
// 获取部门树的数据
const getTreeData = () => {
  getDept(listQuery.value).then((res) => {
    if (res.length == 1) {
      res.forEach(element => {
        treemrData.value.push(element.id)
        emits("getdeptData", element);
      });
    }
    for (let i = 0; i < res.length; i++) {
      res[i].leaf = !res[i].hasChildren;
    }
    if (res.length > 0) treeData.value = res;
  });
};
//懒加载方法
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);
    });
  }
}

const checkChange = (data, isChecked) => {
  if (isChecked) {
    const checked = [data.id]; // id为tree的node-key属性
    singleTableRef.value.setCheckedKeys(checked);
  }
};
//获取复选框中被选中的值
function handleCheck(data, checkedData) {
  // 这是选中的节点数组
  currentRow.value = checkedData.checkedNodes[0];
  currentRow.value.zzlx = props.zzlx;
}

//清空
function clearlist() {
  currentRow.value = [];
  singleTableRef.value.setCheckedKeys([], false);
}

const handleNodeClick = (value) => {
  listQuery.value.deptId = value[value.length - 1];
};
const id = ref("");
watch(
  () => [props.formInfo, props.zzlx],
  (val) => {
    if (props.zzlx == "01") {
      id.value = props.formInfo.leaderid;
    } else {
      id.value = props.formInfo.zzid;
    }
  }
);

const closeDialog = () => {
  emits("closezzjg", false);
};
const handleSave = () => {
  emits("closezzjg", false);
  emits("listdata", currentRow.value);
};
const handleClose = () => {
  emits("closezzjg", false);
};
// // 表格多选
// const handleSelectionChange = (val) => {
//   currentRow.value = val;
// };
// //选中
// const handleCurrentChange = (val) => {
//   currentRow.value = val;
//   currentRow.value.zzlx = props.zzlx;
// };
</script>

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

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

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

.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: scroll;
  border: 1px solid var(--el-color-primary);
}

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

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

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