<template>
  <div>
    <el-dialog title="组织机构部门选择" width="900px" v-model="props.showDdld" @close="closeDialog" :close-on-click-modal="false"
      :show-close="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-click="handleNodeClicks" node-key="id"
            show-checkbox @check-change="checkChange" :check-strictly="props.Isstrictly" @check="handleCheck"
            :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,
  reactive,
  watch,
  onMounted,
  ref,
  defineEmits,
  onBeforeUnmount,
  watchEffect
} from "vue";
import { getApi, deleteApi, putApi, postApi } from "@/api/tobAcco_api.js";
import { getDept } from "@/api/user-manage";
const singleTableRef = ref();
const endProps = {
  children: "children",
  value: "id",
  label: "orgname",
  isLeaf: "leaf",
  disabled: function (data, node) {//带子级的节点不能选中
    if (!props.Isstrictly && data.children) {
      return true
    } else {
      return false
    }
  }

};
const treemrData = ref([]);
const node_had = ref([]);
const resolve_had = ref([]);
const treeData = ref([]); // 树的数据
const props = defineProps({
  showDdld: {
    type: Boolean,
    default: false
  },
  //是否可选择本级true：可以  false:不可以
  Isstrictly: {
    type: Boolean,
    default: false
  },
  zzlx: String,
  formInfo: {
    type: Object,
    default: {}
  },
  // 查询层级 01.查询所有层级(默认) 02.查询所有省、市公司 03.查询所有省、市、县公司
  searchType: {
    type: String,
    default: "02"
  },
  // 查询当前部门下的人员
  deptId: {
    type: String,
    default: null
  },
  //查询单位下的安全部门
  isAqbm: {
    type: Boolean,
    default: false
  }
});
const listQuery = ref({

  type: props.searchType
});
const treemrs = ref(null);
const currentRow = ref(-1); //选中的数据
const emits = defineEmits(["closezzjg", "listdata"]);
onMounted(() => {
  // getTreeData()
  if (props.showDdld) {
    listQuery.value.type = props.searchType
    getTreeData()
  }
});
onBeforeUnmount(() => {
  // clearTimeout(treemrs.value)
  treemrs.value = null
})
// watch(
//   () => props.searchType,
//   (val) => {
//     listQuery.value.type = val
//     getTreeData()
//   }
// )

watch(
  () => props.showDdld,
  (val) => {
    if (val == true) {
      listQuery.value.type = props.searchType
      getTreeData()
    }
  }
);

// 获取部门树的数据
const getTreeData = () => {

  treeData.value = []
  treemrData.value = []
  getApi(listQuery.value, "/mosty-base/deptFeign/getDeptAndJgbm").then((res) => {
    if (res.length == 1) {
      res.forEach((element) => {
        //  treemrs.value = setTimeout(() => { singleTableRef.value.setCurrentKey(element.id) }, 200)
        // treemrData.value.push(element.id);
        treemrData.value = [element.id]
      });
    }
    for (let i = 0; i < res.length; i++) {
      res[i].leaf = res[i].children.length > 0 ? false : true;
    }
    if (res.length > 0) treeData.value = res;
  });
  const filterNode = (value, data) => {
    if (!value) return true;
    return data.orgname.includes(value);
  };
};

//懒加载方法
async function loadNode(node, resolve) {
  // listQuery.value.parentid = node.data.id ? node.data.id : props.deptId;
  if (node.level === 0) {
    node_had.value = node;
    resolve_had.value = resolve;
    getTreeData();
  }
  if (node.level >= 1) {
    getApi(listQuery.value, "/mosty-base/deptFeign/getDeptAndJgbm").then((res) => {
      for (let i = 0; i < res.length; i++) {
        res[i].leaf = res[i].children ? false : true;
      }
      resolve(res);
    });
  }
}

const checkChange = (data, isChecked) => {
  if (isChecked) {
    const checked = [data.id]; // id为tree的node-key属性
    singleTableRef.value.setCheckedKeys(checked);
  }
};
function handleNodeClicks(node, checkedData) {
  if (!props.Isstrictly && node.hasChildren) {
    return
  } else {
    const checked = [node.id]; // id为tree的node-key属性
    singleTableRef.value.setCheckedKeys(checked);
    currentRow.value = node;
  }

}
//获取复选框中被选中的值
function handleCheck(data, checkedData) {
  // 这是选中的节点数组
  currentRow.value = checkedData.checkedNodes[0];
  // currentRow.value.zzlx = props.zzlx?props.zzlx:null;
}

//清空
function clearlist() {
  id.value = null
  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 ? props.formInfo.zzid : null;
    }
  }
);

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

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

::v-deep .el-tree-node__content .el-tree-node__expand-icon {
  display: black;
}

// ::v-deep .el-checkbox .el-checkbox__input .el-checkbox__inner {
//   display: none !important;
// }

// :v-deep .el-tree-node {
//   .el-checkbox .el-checkbox__inner {
//     display: none !important;
//     background: red !important;
//   }
// }

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

// ::v-deep .el-tree-node__expand-icon {
//   display: block !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>
