<script lang='ts' setup>
import { computed, onMounted, onBeforeMount,nextTick } from 'vue'
import { criteriaForPeopleList } from '@/api/hr/org/type'
import request from '@/utils/request';
import { useAttrs } from 'vue'
const attrs = useAttrs()

const props = withDefaults(
    defineProps<{
        modelValue?: string
        disabled?: boolean
        orgAndManTree?: any;
        data?: any;
    }>(),
    {}
)


const emits = defineEmits<{
    (e: 'update:modelValue', value: string): void
}>()

const value = ref([])
const treeData = ref([]) // 存储懒加载的数据
const isDataLoaded = ref(false) // 标记数据是否已加载
const loading = ref(false) // 是否正在加载
const treeSelectRef = ref() // 树选择器引用

// 上一次选择的值，用于比较
const lastSelectedValue = ref(null)

// 处理值变化
const handleValueChange = (newValue) => {
    if (!multiSelect.value) {
        // 单选模式
        if (newValue.length === 0) {
            // 清空选择
            emits('update:modelValue', '');
            lastSelectedValue.value = null;
        } else if (newValue.length === 1) {
            // 单选模式下只有一个选择
            const currentValue = newValue[0];
            emits('update:modelValue', currentValue);
            lastSelectedValue.value = currentValue;
        } else {
            // 当有多个选择时，找出新增的那个
            const newSelections = newValue.filter(item => item !== lastSelectedValue.value);
            if (newSelections.length > 0) {
                // 只保留最新选择的那个
                const latestSelection = newSelections[newSelections.length - 1];
                value.value = [latestSelection];
                emits('update:modelValue', latestSelection);
                lastSelectedValue.value = latestSelection;
            } else {
                // 如果没有新增的选择，可能是取消选择，保持原样
                value.value = newValue;
            }
        }
    } else {
        // 多选模式下的处理
        if (newValue.length > 0) {
            let str = ""
            let userAry = new Array
            if(Array.isArray(newValue)){
                newValue.forEach(item => {
                    userAry.push(item)
                })
                str = userAry.join(',')
                emits('update:modelValue', str)
            }else{
                let a = new Array
                a.push(newValue)
                a.forEach(item => {
                    userAry.push(item)
                })
                str = userAry.join(',')
                emits('update:modelValue', str)
            }
        } else {
            let str = ""
            emits('update:modelValue', str)
        }
    }
}

watch(value, handleValueChange, { deep: true })

const multiSelect = computed(()=>{
    if(props.data.control.multiSelect && props.data.control.multiSelect == "1"){
        return true
    }else{
        return false
    }
})

function parseStringToArray(str: string) {
    try {
        const result = JSON.parse(str);
        if (Array.isArray(result)) {
            return result;
        } else {
            return [];
        }
    } catch (error) {
        return [];
    }
}

onBeforeMount(() => {
    setTimeout(() => {
        const initialValue = parseStringToArray(props.modelValue)
        value.value = initialValue;
        if (initialValue.length > 0 && !multiSelect.value) {
            lastSelectedValue.value = initialValue[initialValue.length - 1];
        }
    }, 500)
})

// 递归处理树节点，将部门节点设置为禁用
const processTreeData = (data: any[]) => {
    return data.map(node => {
        const newNode = { ...node };
        
        // 如果multiSelect为false且id长度小于5，设置为禁用
        if (!multiSelect.value && node.id && node.id.toString().length < 5) {
            newNode.disabled = true;
        }
        
        // 递归处理子节点
        if (newNode.children && Array.isArray(newNode.children)) {
            newNode.children = processTreeData(newNode.children);
        }
        
        return newNode;
    });
}

// 加载完整数据的函数
const loadFullData = async () => {
    if (isDataLoaded.value) return treeData.value;
    
    loading.value = true;
    
    try {
        // 模拟数据加载过程
        await new Promise(resolve => setTimeout(resolve, 800));
        
        const result = await checkorgAndManTree1();
        
        // 根据multiSelect的值处理数据
        const processedData = !multiSelect.value ? processTreeData(result) : result;
        
        treeData.value = processedData;
        isDataLoaded.value = true;
        return processedData;
    } catch (error) {
        console.error('加载组织数据失败:', error);
        return [];
    } finally {
        loading.value = false;
    }
}

// 点击树选择器时的处理
const handleTreeSelectClick = async () => {
    if (!isDataLoaded.value && !loading.value) {
        await loadFullData();
    }
}

// 下拉框显示状态变化时的处理
const handleVisibleChange = async (visible: boolean) => {
    if (visible && !isDataLoaded.value && !loading.value) {
        await loadFullData();
    }
}

const resData = computed(() => {
    return treeData.value;
})

function checkorgAndManTree1() {
    return new Promise((resolve) => {
        const check = () => {
            let result = []
            let i = 0
            props.orgAndManTree.forEach((element: any) => {
                if (element.hasOwnProperty('tree') && hasNodesInTree(element.tree)) {
                    i++
                }
            });
            if (i == props.orgAndManTree.length) {
                props.orgAndManTree.forEach((item: any) => {
                    if (props.data.name == item.name) {
                        console.log(item.tree)
                        result = item.tree
                    }
                });
                resolve(result)
            } else {
                setTimeout(check, 100)
            }
        }
        check()
    })
}

/**
 * 判断树形结构是否存在节点
 */
function hasNodesInTree(tree) {
    if (!Array.isArray(tree)) {
        return false;
    }

    function checkNode(node) {
        if (node == null) {
            return false;
        }
        return true;
    }

    for (const node of tree) {
        if (checkNode(node)) {
            return true;
        }
        if (Array.isArray(node?.children) && hasNodesInTree(node.children)) {
            return true;
        }
    }

    return false;
}

</script>
<template>
    <div class="tree-select-wrapper" >{{ multiSelect }}
        <el-tree-select 
            ref="treeSelectRef"
            node-key="number" 
            v-model="value" 
            :data="resData" 
            multiple
            :render-after-expand="false"
            show-checkbox
            clearable 
            collapse-tags 
            collapse-tags-tooltip 
            :max-collapse-tags="4" 
            filterable
            @click="handleTreeSelectClick"
            @visible-change="handleVisibleChange"
            :disabled="props.disabled"
            :popper-append-to-body="false"
        >
            <!-- 自定义空状态 -->
            <template #empty>
                <div class="custom-empty">
                    {{ loading ? '正在加载' : '无数据' }}
                </div>
            </template>
        </el-tree-select>
    </div>
</template>
<style lang='scss' scoped>
.tree-select-wrapper {
    position: relative;
    min-height: 40px;
    width: 100%;
}

.wordColor {
    color: #000000;
}

.el-tree-node__expand-icon {
    color: var(--el-tree-expand-icon-color);
    cursor: pointer;
    font-size: 18px;
    transform: rotate(0deg);
    transition: transform var(--el-transition-duration) ease-in-out;
}

.el-tree {
    --el-tree-node-content-height: 30px;
    --el-tree-node-hover-bg-color: var(--el-fill-color-light);
    --el-tree-text-color: var(--el-text-color-regular);
    --el-tree-expand-icon-color: var(--el-text-color-placeholder);
    background: var(--el-fill-color-blank);
    color: var(--el-tree-text-color);
    cursor: default;
    font-size: var(--el-font-size-base);
    position: relative;
}

.el-tree-select__popper .el-tree-node__expand-icon {
    margin-left: 7px;
    margin-right: 7px;
}

/* 自定义空状态样式 */
.custom-empty {
    padding: 10px;
    text-align: center;
    color: #909399;
    font-size: 14px;
}
</style>