<template>
    <div class="tree-node">
        <div class="node-content" :class="{ disabled: isDisabled }">
            <span v-if="hasChildren" class="expand-icon" @click="handleExpand">
                <svg v-if="isExpanded" width="16" height="16" viewBox="0 0 16 16">
                    <path d="M8 4L12 8L8 12L11 8L8 4Z" fill="currentColor" transform="rotate(90 8 8)" />
                </svg>
                <svg v-else width="16" height="16" viewBox="0 0 16 16">
                    <path d="M4 8L8 12L12 8L8 4L4 8Z" fill="currentColor" />
                </svg>
            </span>
            <span v-else class="expand-placeholder"></span>

            <label class="checkbox-container">
                <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>

        <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>
    </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import type { TreeData } from './types'
import { Folder, Document, Location, OfficeBuilding } from '@element-plus/icons-vue'

interface Props {
    node: TreeData
    selectedKeys: number[]
    indeterminateKeys: Set<number>
    disabledKeys: Set<number>
    expandedKeys: Set<number>
}

interface Emits {
    (e: 'check', node: TreeData, checked: boolean): void
    (e: 'expand', nodeId: 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 = () => {
    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>
.tree-node {
    width: 100%;
}

.node-content {
    display: flex;
    align-items: center;
    padding: 4px 0;
    cursor: pointer;
}

.node-content.disabled {
    cursor: not-allowed;
    opacity: 0.6;
}

.node-content.disabled input {
    cursor: not-allowed;
}

.expand-icon {
    width: 16px;
    height: 16px;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-right: 4px;
    cursor: pointer;
    color: #666;
}

.expand-icon:hover {
    color: #333;
}

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

.checkbox-container {
    display: flex;
    align-items: center;
    margin-right: 8px;
    position: relative;
}

.checkbox-container input {
    position: absolute;
    opacity: 0;
    cursor: pointer;
    height: 0;
    width: 0;
}

.checkmark {
    height: 16px;
    width: 16px;
    background-color: #fff;
    border: 1px solid #dcdfe6;
    border-radius: 2px;
    transition: all 0.3s;
    position: relative;
}

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

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

.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: 4px;
    height: 8px;
    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;
}

.checkbox-container input:disabled~.checkmark {
    background-color: #f5f7fa;
    border-color: #e4e7ed;
    cursor: not-allowed;
}

.checkbox-container input:disabled:checked~.checkmark {
    background-color: #c0c4cc;
    border-color: #c0c4cc;
}

.node-label {
    font-size: 14px;
    color: #606266;
    flex: 1;
    user-select: none;
}

.node-icon {
    margin-right: 8px;
    color: #606266;
}

.node-content.disabled .node-label {
    color: #c0c4cc;
}

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