<template>
    <div class="point-tree">
        <div style="flex: 1; overflow: hidden">
            <Tree ref="treeRef" :data="tree" style="width: 100%; height: 100%" :canExpand="false" v-bind="$attrs" />
        </div>
    </div>
</template>

<script lang="ts" setup>
import { nextTick, onMounted, ref, useAttrs } from 'vue'
import Tree from '../index.vue'
import { useAdminInfo } from '@/stores/adminInfo'
import { useDictData } from '@/stores/dictData'
import { useConfig } from '@/stores/config'
import { defineProps } from 'vue'
import { getTree } from '@/api/advance-boot/assess'

defineOptions({
    name: 'pms/pointTree'
})
interface Props {
    showSelect?: boolean
}
const props = withDefaults(defineProps<Props>(), {
    showSelect: true
})
const emit = defineEmits(['init'])
const attrs = useAttrs()
const adminInfo = useAdminInfo()
const dictData = useDictData()
const config = useConfig()
const classificationData = dictData.getBasicData('Statistical_Type', ['Report_Type'])
const tree = ref()
const treeRef = ref()
const formData = ref({
    deptIndex: adminInfo.$state.deptIndex,
    monitorFlag: 2,
    powerFlag: 2,
    loadType: null,
    manufacturer: null,
    serverName: 'event-boot',
    statisticalType: classificationData[0].id,
    scale: null
})

// 添加数据转换函数
const transformTreeData = (data: any[]) => {
    if (!data || !Array.isArray(data)) return []
    return data.map(item => {
        // 创建新对象，确保不修改原始数据
        const newItem: any = {
            name: item.treeName,
            id: item.treeId,
            children: []
        }

        // 递归处理子节点
        if (item.children && Array.isArray(item.children)) {
            newItem.children = transformTreeData(item.children)
        }
        return newItem
    })
}

const loadData = () => {
    let obj = classificationData.find(function (i) {
        return i.id === formData.value.statisticalType
    }) || { code: '' }
    let form = JSON.parse(JSON.stringify(formData.value))
    form.statisticalType = classificationData.find((item: any) => item.id == form.statisticalType)
    let nodeKey = ''
    getTree({}).then(res => {
        // 转换数据结构为指定格式
        const transformedData = transformTreeData(res.data)

        // 确保有数据再进行处理
        if (
            transformedData &&
            transformedData.length > 0 &&
            transformedData[0].children &&
            transformedData[0].children.length > 0
        ) {
            nodeKey = transformedData[0].children[0].id
            emit('init', transformedData[0].children[0])

            tree.value = transformedData

            if (nodeKey) {
                nextTick(() => {
                    treeRef.value.treeRef.setCurrentKey(nodeKey)
                })
            }
        }
    })
}
const scrollToNode = (id: string) => {
    // 树滚动
    treeRef.value.scrollToNode(id)
}
loadData()
defineExpose({
    tree,
    scrollToNode
})
</script>
<style lang="scss">
.point-tree {
    height: 100%;
    width: 100%;
    display: flex;
    flex-direction: column;
    background: #fff;
    border: 1px solid var(--el-border-color);
}
</style>
