<template>
    <div>
        <h2> 子组件, 树类型{{ treeType }}, 机构汇总关系:{{ relationNo }},机构:{{ txBrNo }},账套:{{ sobNo }},选中:{{ checkedKeys }},展开:{{
            expandedKeys }}</h2>
        <a-tree :tree-data="treeData" @check="onCheck" v-model:checkedKeys="localCheckedKeys"
            v-model:expandedKeys="localExpandedKeys" v-bind="$attrs">
        </a-tree>
    </div>
</template>

<script setup>
import { ref, defineProps, defineExpose, onMounted, watch, nextTick, provide, defineEmits } from 'vue';
// 定义组件接收的 props
const props = defineProps({
    treeType: {
        type: String,
        required: true
    },
    relationNo: {
        type: String,
        default: ''
    },
    txBrNo: {
        type: String,
        default: ''
    },
    sobNo: {
        type: String,
        default: ''
    },
    checkedKeys: {
        type: Array,
        default: () => []
    },
    expandedKeys: {
        type: Array,
        default: () => []
    },
    // 这里假设传递树数据，实际根据你的业务调整
    treeData: {
        type: Array,
        default: () => []
    }
});

// 定义自定义事件，用于通知父组件 checkedKeys 变化
const emits = defineEmits(['onCheck', 'update:txBrNo', 'update:sobNo']);


// 本地的 checkedKeys 变量，初始化为父组件传递的值
const localCheckedKeys = ref([...props.checkedKeys]);
const localExpandedKeys = ref([...props.expandedKeys]);

// 监听父组件传递的 checkedKeys 变化
watch(() => props.checkedKeys, (newValue) => {
    localCheckedKeys.value = [...newValue];
});

// 监听父组件传递的 checkedKeys 变化
watch(() => props.expandedKeys, (newValue) => {
    localExpandedKeys.value = [...newValue];
});

// 根据不同类型的树监听相应参数的变化
if (props.treeType === 'deptTree') {
    watch(() => props.relationNo, async () => {
        await fetchTreeData();
    });
} else if (props.treeType === 'sobTree') {
    // 监听账套树
    watch(() => props.txBrNo, async () => {
        await fetchTreeData();
    });
} else if (props.treeType === 'sobItemTree') {
    watch(() => props.sobNo, async () => {
        await fetchTreeData();
    });
}
// 树数据
const treeData = ref([]);


const fetchTreeData = async () => {
    let params = {};
    let apiUrl = '';
    switch (props.treeType) {
        case 'deptTree':
            // params = { relationNo: props.relationNo };
            // apiUrl = '/api/deptTree'; // 替换为实际的机构树接口地址
            if (props.relationNo === 'AAAAAA') {
                treeData.value = [
                    {
                        title: '[811000]马鞍山市汇总',
                        key: '1',
                        children: [
                            {
                                title: '[811001]马鞍山市花山区',
                                key: '1-1'
                            },
                            {
                                title: '[811001]马鞍山市雨山区',
                                key: '1-2'
                            }
                        ]
                    }
                ];
                // localCheckedKeys.value = ['1-1'];
                // localExpandedKeys.value = ['1'];
            } else {
                treeData.value = [
                    {
                        title: '[812000]当涂市汇总',
                        key: '2',
                        children: [
                            {
                                title: '[812001]当涂市博望区汇总',
                                key: '2-1'
                            },
                            {
                                title: '[812001]当涂市和县区汇总',
                                key: '2-2'
                            }
                        ]
                    }
                ];
                // localCheckedKeys.value = ['2-1'];
                // localExpandedKeys.value = ['2'];
            }
            //console.log('wyq-1--------checkedKeys', localExpandedKeys.value);
            emits('update:txBrNo', localCheckedKeys.value[0]);
            break;
        case 'sobTree':
            params = { txBrNo: props.txBrNo };
            apiUrl = '/api/sobTree'; // 替换为实际的账套树接口地址

            if (props.txBrNo === '1' || props.txBrNo === '1-1' || props.txBrNo === '1-2') {
                treeData.value = [
                    {
                        title: '新会计准则',
                        key: '1',
                        children: [
                            {
                                title: '默认账套1',
                                key: '1-1'
                            },
                            {
                                title: '非默认账套2',
                                key: '1-2'
                            }
                        ]
                    }
                ];
                localCheckedKeys.value = ['1-1'];
                localExpandedKeys.value = ['1'];
            } else {
                treeData.value = [
                    {
                        title: '旧会计准则',
                        key: '2',
                        children: [
                            {
                                title: '默认账套1',
                                key: '2-1'
                            },
                            {
                                title: '非默认账套2',
                                key: '2-2'
                            }
                        ]
                    }
                ];
                localCheckedKeys.value = ['2-1'];
                localExpandedKeys.value = ['2'];
            }
            emits('update:sobNo', localCheckedKeys.value[0]);
            break;
        case 'sobItemTree':
            params = { sobNo: props.sobNo };
            apiUrl = '/api/sobItemTree'; // 替换为实际的科目树接口地址
            break;
        default:
            break;
    }

    // try {
    //     const response = await axios.get(apiUrl, { params });
    //     treeData.value = response.data;
    // } catch (error) {
    //     console.error('获取树数据失败:', error);
    // }
};
// 初始化时获取树数据
fetchTreeData();

// 处理树节点勾选变化的事件
const onCheck = (checkedKeys, info) => {
    // 触发自定义事件通知父组件 checkedKeys 已更新
    if (props.treeType === 'deptTree') {
        emits('update:txBrNo', checkedKeys[0]);
    } else if (props.treeType === 'sobTree') {
        emits('update:sobNo', checkedKeys[0]);
    }
    emits('onCheck', checkedKeys, info);
};
</script>
