<template>
  <li>
    <!-- 节点标题 + 展开按钮 -->
    <div @click="toggleExpand(node)">
      <span v-if="hasChildren">{{ node.expanded ? "▼" : "▶" }}</span>
      {{ node.Name }}
    </div>

    <!-- 递归渲染子节点 -->
    <ul v-if="hasChildren">
      <TreeItem v-for="child in node.children" :key="child.Id" :node="child" />
    </ul>
  </li>
</template>

<script setup>
import { computed } from "vue";

const props = defineProps({
  node: Object, // 节点数据
});

// 检查是否有子节点
const hasChildren = computed(() => props.node.children?.length > 0);

// 切换展开状态
const toggleExpand = (node) => {
  if (hasChildren.value) {
    node.expanded = !node.expanded;
  }
};
</script>
