<template>
    <template v-for="item in datas" :key="item?.code">
        <el-sub-menu v-if="item?.children && item?.children.length > 0" :index="item?.code">
            <template #title>
                <div class="hc-aside-menu-item">
                    <div class="menu---item">
                        <hc-icon v-if="item?.source" :name="item?.source" :fill="curKey === item?.code" class="hc-menu-icon" />
                        <div class="name truncate">{{ item?.name }}</div>
                    </div>
                </div>
                <hc-icon name="arrow-down-s" ui="el-icon el-sub-menu__icon-arrow" />
            </template>
            <MenuItem :datas="item?.children" :cur="curKey" @change="MenuClick" />
        </el-sub-menu>
        <el-menu-item v-else :index="item?.code" @click="MenuClick(item)">
            <div class="hc-aside-menu-item">
                <div class="menu---item">
                    <hc-icon v-if="item?.source" :name="item?.source" :fill="curKey === item?.code" class="hc-menu-icon" />
                    <div class="name truncate">{{ item?.name }}</div>
                </div>
            </div>
        </el-menu-item>
    </template>
</template>

<script setup>
import { ref, watch } from 'vue'
import MenuItem from './MenuItem.vue'

const props = defineProps({
    datas: {
        type: Array,
        default: () => ([]),
    },
    cur: {
        type: String,
        default: '',
    },
})
//事件
const emit = defineEmits(['change'])

//监听
const curKey = ref(props.cur)
watch(() => props.cur, (cur) => {
    curKey.value = cur
})

const MenuClick = (item) => {
    emit('change', item)
}
</script>
