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

<script lang="ts" setup>
import { ref, nextTick } from 'vue'
import Tree from './index.vue'

import { getTerminalTree } from '@/api/device-boot/Business.ts'
import { useConfig } from '@/stores/config'
defineOptions({
    name: 'govern/deviceTree'
})
const emit = defineEmits(['init'])
const config = useConfig()
const expanded: any = ref([])
const tree = ref()
const treRef = ref()
const info = (id: any) => {
  
    expanded.value = [id]
    getTerminalTree().then(res => {
        // let arr: any[] = []
        res.data.forEach((item: any) => {
            item.icon = 'el-icon-Menu'
            item.level = 0
            item.children.forEach((item2: any) => {
                item2.icon = 'el-icon-HomeFilled'
                item2.level = 100
                expanded.value.push(item2.id)
                item2.children.forEach((item3: any) => {
                    item3.icon = 'el-icon-CollectionTag'
                    item3.level = 200
                    item3.children.forEach((item4: any) => {
                        item4.icon = 'el-icon-Flag'
                        item4.level = 300
                        // arr.push(item4)
                        item4.children.forEach((item5: any) => {
                            item5.icon = 'el-icon-OfficeBuilding'
                            item5.level = 400
                            item5.children.forEach((item6: any) => {
                                item6.icon = 'el-icon-Film'
                                item6.level = 500
                                item6.children.forEach((item7: any) => {
                                    item7.icon = 'el-icon-Share'
                                    item7.level = 600
                                    item7.children.forEach((item8: any) => {
                                        item8.icon = 'el-icon-Location'
                                        item8.level = 700
                                    })
                                })
                            })
                        })
                    })
                })
            })
        })
        tree.value = res.data

        nextTick(() => {
            treRef.value.setCurrentKey(id)
            // if (arr.length) {
            //     treRef.value.treeRef.setCurrentKey(arr[0].id)
            //     // 注册父组件事件
            //     emit('init', {
            //         level: 2,
            //         ...arr[0]
            //     })
            // } else {
            //     emit('init')
            // }
        })
    })
}
info('')

defineExpose({ info })
</script>
<style lang="scss" scoped>
.el-tree {
    background: #efeff0;
}
</style>
