<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: 'itemValue', title: 'title' }"
      :defaultExpandAll="true"
      @select="handleSelect"
    />
    <BasicModal @register="register" title="删除" @ok="handleDelete">
      确认要删除菜单吗？
    </BasicModal>
  </div>
</template>
<script lang="ts" setup>
  import { onMounted, ref, h, nextTick, unref } from 'vue';
  import { BasicTree, TreeItem, TreeActionItem } from '@/components/Tree';
  import { functionLoadFormSort, functionDeleteFormSort } from '@/api/demo/formScheme';
  import { PlusOutlined, FormOutlined, DeleteOutlined } from '@ant-design/icons-vue';
  import { BasicModal, useModal } from '@/components/Modal';
  import { router } from '@/router';

  defineOptions({ name: 'FormSchemeTree' });
  const emit = defineEmits(['select']);
  let selectNode = ref('');

  // 删除的弹窗
  const [register, { closeModal, openModal }] = useModal();

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

  // 删除的弹窗的方法
  async function handleDelete() {
    var query = { id: selectNode.value.itemDetailId };
    await functionDeleteFormSort(query);
    closeModal();
  }

  // 树分类获取
  async function fetch() {
    treeData.value = (await functionLoadFormSort()) as unknown as TreeItem[];
    treeData.value = treeData.value.map((item) => ({
      ...item,
      title: item.itemName,
    }));
    // 展开全部
    nextTick(() => {
      unref(asyncExpandTreeRef)?.expandAll(true);
    });
  }

  function handleSelect(keys) {
    emit('select', keys[0]);
  }
  const btnList = router.currentRoute.value.meta.elements;
  const actionList: TreeActionItem[] = [];
  btnList.forEach((element) => {
    if (element.domId == 'btnAdd') {
      actionList.push({
        render: (node) => {
          return h(PlusOutlined, {
            class: 'ml-2',
            onClick: () => {
              emit('add', node);
            },
          });
        },
      });
    } else if (element.domId == 'btnEdit') {
      actionList.push({
        render: (node) => {
          return h(FormOutlined, {
            class: 'ml-2',
            onClick: () => {
              emit('edit', node);
            },
          });
        },
      });
    } else if (element.domId == 'btnDelete') {
      actionList.push({
        render: (node) => {
          return h(DeleteOutlined, {
            class: 'ml-2',
            onClick: () => {
              selectNode.value = node;
              openModal(true, {
                isUpdate: false,
              });
            },
          });
        },
      });
    }
  });

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