<template>
    <div class="point-tree">
        <div style="flex: 1; overflow: hidden">
            <Tree ref="treeRef" :data="tree" :canExpand="false" style="width: 100%; height: 100%" v-bind="$attrs"
                default-expand-all @onAddTree="onAddTree" />
        </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 { getTerminalTreeForFive } from '@/api/device-boot/terminalTree'
import { useConfig } from '@/stores/config'
import { queryAll } from '@/api/supervision-boot/database/index'
defineOptions({
    name: 'pms/pointTree'
})
const emit = defineEmits(['init', 'onAddTree'])
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 loadData = (id?: any) => {
   
    let nodeKey = ''
    queryAll().then(res => {
        res.data.forEach((item: any) => {
            item.icon = 'el-icon-FolderOpened'
            item.color = config.getColorVal('elementUiPrimary')
            item.children.forEach((item2: any) => {
                item2.icon = 'el-icon-Document'
                item2.color = config.getColorVal('elementUiPrimary')
                item2.childrens = item2.children
                item2.children = []
                if (item2.id == id) {
                    emit('init', item2)
                }
            })
        })

        nodeKey = res.data.length == 0 ? "" : res.data[0].children[0]?.id ? res.data[0].children[0].id : res.data[0].id

        tree.value = res.data
        if (id) {
            setTimeout(() => {
                treeRef.value.treeRef.setCurrentKey(id)
            }, 10)
        } else {
            setTimeout(() => {
                treeRef.value.treeRef.setCurrentKey(nodeKey)
                emit('init', res.data.length == 0 ? "" : (res.data[0].children[0] || res.data[0]))
            }, 10)
        }
    })
}

const onAddTree = () => {
    emit('onAddTree')
}
loadData()
defineExpose({ loadData })
</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>
