<template>
  <div class="tree-node" :data-node-id="node.id">
    <div class="node-content" @click="handleClick" :class="{ 'selected': node.selected }">
      <span v-if="hasChildren" class="toggle-icon">
        {{ node.expanded ? '-' : '+' }}
      </span>
      <span v-else class="empty-space"></span>
      <span class="node-title" :title="node.title">{{ node.title }}</span>
    </div>
    <div v-if="hasChildren && node.expanded" class="node-children">
      <tree-node
        v-for="child in node.children"
        :key="child.id"
        :node="child"
        @toggle="handleToggle"
        :level="level + 1"/>
    </div>
  </div>
</template>

<script>
export default {
  name: 'TreeNode',
  props: {
    node: {
      type: Object,
      required: true
    },
    level: {
      type: Number,
      default: 0
    }
  },
  computed: {
    hasChildren () {
      return this.node.children && this.node.children.length > 0
    }
  },
  methods: {
    handleClick () {
      if (this.hasChildren) {
        this.$emit('toggle', this.node)
      }
    },
    handleToggle (childNode) {
      this.$emit('toggle', childNode)
    }
  }
}
</script>

<style scoped>
.tree-node {
  margin: 1px 0;
  white-space: nowrap;
}

.node-content {
  display: flex;
  align-items: center;
  padding: 3px 2px;
  user-select: none;
  border-radius: 2px;
  overflow: hidden;
}

.toggle-icon {
  width: 12px;
  text-align: center;
  font-size: 14px;
  color:#5D5C5C;
  font-weight: 400;
  margin-right: 3px;
  cursor: pointer;
  flex-shrink: 0;
}

.empty-space {
  width: 12px;
  margin-right: 3px;
  flex-shrink: 0;
}

.node-title {
  flex: 1;
  cursor: default;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
}

.node-content:hover {
  background-color: #f5f5f5;
}

/* 选中状态样式 */
.selected {
  background-color: #e6f7ff;  /* 浅蓝色背景 */
  color: #1890ff;            /* 蓝色文字 */
  font-weight: bold;
}

.node-children {
  padding-left: 15px;
}
</style>
