<template>
    <Tree ref="treRef" :data="tree" />
</template>

<script lang="ts" setup>
import { ref, nextTick } from 'vue'
import Tree from '../index.vue'
import { getDeviceTree } from '@/api/cs-device-boot/csLedger'
import { useConfig } from '@/stores/config'
defineOptions({
    name: 'govern/deviceTree'
})
const emit = defineEmits(['init'])
const config = useConfig()
const tree = ref()
const treRef = ref()
getDeviceTree().then(res => {
    let arr: any[] = []
    res.data.forEach((item: any) => {
        item.icon = 'el-icon-HomeFilled'
        item.color = config.getColorVal('elementUiPrimary')
        item.children.forEach((item2: any) => {
            item2.icon = 'el-icon-List'
            item.color = config.getColorVal('elementUiPrimary')
            item2.children.forEach((item3: any) => {
                item3.icon = 'el-icon-Platform'
                item3.color = config.getColorVal('elementUiPrimary')
                if (item3.comFlag === 1) {
                    item3.color = '#e26257 !important'
                }
                arr.push(item3)
            })
        })
    })
    tree.value = res.data
    nextTick(() => {
        treRef.value.treeRef.setCurrentKey(arr[0].id)
        // 注册父组件事件
        emit('init', {
            level: 2,
            ...arr[0]
        })
    })
})
</script>
