<template>
  <div class="m-4 mr-0 overflow-hidden bg-white">
    <BasicTree
      ref="asyncExpandTreeRef"
      title="功能目录"
      toolbar
      search
      treeWrapperClassName="h-[calc(100%-35px)] overflow-auto"
      loadData
      :actionList="actionList"
      :renderIcon="createIcon"
      :clickRowToExpand="false"
      :treeData="treeData"
      :fieldNames="{ key: 'id', title: 'name' }"
      :defaultExpandAll="true"
      @select="handleSelect"
    />
  </div>
</template>
<script lang="ts" setup>
  import { onMounted, ref, nextTick, unref } from 'vue';
  import { BasicTree, TreeItem, TreeActionItem } from '@/components/Tree';

  import { getMenuList } from '@/api/demo/system';

  defineOptions({ name: 'DeptTree' });

  const emit = defineEmits(['select', 'edit']);

  const treeData = ref<TreeItem[]>([]);
  const asyncExpandTreeRef = ref<Nullable<TreeActionType>>(null);

  async function fetch() {
    treeData.value = (await getMenuList()) as unknown as TreeItem[];
    // 展开全部
    nextTick(() => {
      unref(asyncExpandTreeRef)?.expandAll(true);
    });
  }

  function handleSelect(keys) {
    emit('select', keys[0]);
  }
  const actionList: TreeActionItem[] = [];

  onMounted(() => {
    fetch();
  });
  defineExpose({
    fetch,
  });
</script>
