<template>
  <div class="tree-node">
    <div class="node-content" :class="{ disabled: isDisabled, 'is-expanded': isExpanded }" @click="handleExpand">
      <!-- 展开/收起图标 -->
      <span v-if="hasChildren" class="expand-icon" :class="{ 'is-expanded': isExpanded }">
        <el-icon><ArrowRight /></el-icon>
      </span>
      <span v-else class="expand-placeholder"></span>

      <!-- 自定义复选框 -->
      <label class="checkbox-container" @click.stop>
        <input 
          type="checkbox" 
          :checked="isChecked" 
          :indeterminate="isIndeterminate" 
          :disabled="isDisabled" 
          @change="handleCheck" 
        />
        <span class="checkmark"></span>
      </label>

      <!-- 节点文案 -->
      <span class="node-label">
        <slot :node="node" :data="node">
          <span>{{ node.name }}</span>
        </slot>
      </span>
    </div>

    <!-- 子节点区域 - 使用动画过场 -->
    <el-collapse-transition>
      <div v-if="isExpanded && hasChildren" class="children">
        <tree-node
          v-for="child in node.children"
          :key="child.id"
          :node="child"
          :selected-keys="selectedKeys"
          :indeterminate-keys="indeterminateKeys"
          :disabled-keys="disabledKeys"
          :expanded-keys="expandedKeys"
          @check="handleChildCheck"
          @expand="handleChildExpand"
        >
          <template #default="{ node: childNode, data }">
            <slot :node="childNode" :data="data"></slot>
          </template>
        </tree-node>
      </div>
    </el-collapse-transition>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue';
import type { TreeData } from './types.ts';
import { ArrowRight } from '@element-plus/icons-vue';

interface Props {
  node: TreeData;
  selectedKeys: (string | number)[];
  indeterminateKeys: Set<string | number>;
  disabledKeys: Set<string | number>;
  expandedKeys: Set<string | number>;
}

interface Emits {
  (e: 'check', node: TreeData, checked: boolean): void;
  (e: 'expand', nodeId: string | number): void;
}

const props = defineProps<Props>();
const emit = defineEmits<Emits>();

// 检查节点是否被选中
const isChecked = computed(() => {
  return props.selectedKeys.includes(props.node.id);
});

// 检查节点是否为半选状态
const isIndeterminate = computed(() => {
  return props.indeterminateKeys.has(props.node.id);
});

// 检查节点是否被禁用
const isDisabled = computed(() => {
  return props.disabledKeys.has(props.node.id);
});

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

// 检查是否展开
const isExpanded = computed(() => {
  return props.expandedKeys.has(props.node.id);
});

// 处理选中状态变化
const handleCheck = (event: Event) => {
  const target = event.target as HTMLInputElement;
  emit('check', props.node, target.checked);
};

// 处理展开/折叠
const handleExpand = () => {
  if (hasChildren.value) {
    emit('expand', props.node.id);
  }
};

// 处理子节点事件转发
const handleChildCheck = (node: TreeData, checked: boolean) => emit('check', node, checked);
const handleChildExpand = (nodeId: number) => emit('expand', nodeId);
</script>

<style scoped lang="scss">
.tree-node {
  width: 100%;
}

.node-content {
  display: flex;
  align-items: center;
  padding: 4px 8px;
  cursor: pointer;
  border-radius: 4px;
  transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
  margin-bottom: 1px;
  
  &:hover {
    background-color: #f5f7fa;
  }
}

.expand-icon {
  width: 18px;
  height: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 4px;
  color: #909399;
  transition: transform 0.25s ease;
  
  &.is-expanded {
    transform: rotate(90deg);
  }
  
  .el-icon { font-size: 12px; }
}

.expand-placeholder {
  width: 18px;
  height: 18px;
  margin-right: 4px;
}

.checkbox-container {
  display: flex;
  align-items: center;
  margin-right: 8px;
  position: relative;
  
  input {
    position: absolute;
    opacity: 0;
    cursor: pointer;
    height: 0;
    width: 0;
  }
}

.checkmark {
  height: 16px;
  width: 16px;
  background-color: #fff;
  border: 1.5px solid #dcdfe6;
  border-radius: 3px;
  transition: all 0.2s ease;
  position: relative;
  box-sizing: border-box;
}

.checkbox-container:hover input ~ .checkmark {
  border-color: #409eff;
}

.checkbox-container input:checked ~ .checkmark,
.checkbox-container input:indeterminate ~ .checkmark {
  background-color: #409eff;
  border-color: #409eff;
}

.checkbox-container input:checked ~ .checkmark::after {
  content: '';
  position: absolute;
  left: 5px;
  top: 1px;
  width: 3px;
  height: 7px;
  border: solid white;
  border-width: 0 2px 2px 0;
  transform: rotate(45deg);
}

.checkbox-container input:indeterminate ~ .checkmark::after {
  content: '';
  position: absolute;
  left: 3px;
  top: 6px;
  width: 8px;
  height: 2px;
  background-color: white;
}

.node-label {
  font-size: 13px;
  color: #303133;
  flex: 1;
  user-select: none;
  font-weight: 400;
}

.children {
  margin-left: 20px;
  border-left: 1px dashed #ebeef5;
  padding-left: 4px;
}
</style>
