<template>
    <template v-for="item in datas">
        <el-sub-menu :index="item?.code" :popper-offset="0" :popper-class="'aside-menu-popper ' + curKey" v-if="item?.children && item?.children.length > 0">
            <template #title>
                <div class="hc-aside-menu-item">
                    <div class="menu---item">
                        <HcIcon :name="item?.source" :fill="curKey === item?.code" class="hc-menu-icon" v-if="item?.source"/>
                        <div class="name truncate" v-if="isCollapse">{{ item?.name.substring(0,2) }}</div>
                        <div class="name truncate" v-else>{{ item?.name }}</div>
                        <el-badge :value="msgCount.allCount" v-if="item?.code === 'tasks' && msgCount?.allCount > 0"/>
                    </div>
                </div>
                <HcIcon name="arrow-down-s" ui="el-icon el-sub-menu__icon-arrow"/>
            </template>
            <MenuItem :datas="item?.children" :cur="curKey" :msgCount="msgCount" @change="MenuClick"/>
        </el-sub-menu>
        <el-menu-item :index="item?.code" v-else @click="MenuClick(item)">
            <div class="hc-aside-menu-item">
                <div class="menu---item">
                    <HcIcon :name="item?.source" :fill="curKey === item?.code" class="hc-menu-icon" v-if="item?.source"/>
                    <div class="name truncate" v-if="isCollapse">{{ item?.name.substring(0,2) }}</div>
                    <div class="name truncate" v-else>{{ item?.name }}</div>
                    <el-badge :value="msgCount.taskCount" v-if="item?.code === 'tasks-data' && msgCount?.taskCount > 0"/>
                    <el-badge :value="msgCount.messageCount" v-if="item?.code === 'message-data' && msgCount?.messageCount > 0"/>
                </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: ''
    },
    collapse: {
        type: Boolean,
        default: false
    },
    msgCount: {
        type: Object,
        default: () => ({
            allCount: 0,
            taskCount: 0,
            messageCount: 0,
            messageCount_1:0,
            messageCount_2:0,
            messageCount_3:0,
            messageCount_4:0,
            messageCount_5:0,
        })
    },
})
//初始变量
const curKey = ref(props.cur);
const isCollapse = ref(props.collapse);

//监听
watch(() => [
    props.cur,
    props.collapse
], ([cur,collapse]) => {
    curKey.value = cur
    isCollapse.value = collapse
})

//事件
const emit = defineEmits(['change'])
const MenuClick = (item) => {
    emit('change', item)
}
</script>
