<template>
    <el-scrollbar>
        <el-menu :collapse="isCollapse" :default-active="curKey" class="hc-aside-menu" unique-opened text-color="#fff">
            <MenuItem :collapse="isCollapse" :cur="curKey" :datas="datas" :msg-count="taskCounts" @change="MenuClick" />
        </el-menu>
    </el-scrollbar>
</template>

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

const props = defineProps({
    datas: {
        type: Array,
        default: () => ([]),
    },
    cur: {
        type: String,
        default: 'home-index',
    },
    collapse: {
        type: Boolean,
        default: false,
    },
    msgCount: {
        type: [Number, String],
        default: 0,
    },
})

//事件
const emit = defineEmits(['change'])

//初始变量
const curKey = ref(props.cur)
const isCollapse = ref(props.collapse)

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

//监听菜单数据
const taskCounts = ref(props.msgCount)
watch(() => props.msgCount, (val) => {
    taskCounts.value = val ?? 0
}, { immediate: true, deep: true })

//处理菜单数据
const setMenuItem = async (item) => {
    curKey.value = item?.code || ''
    emit('change', item)
}

//菜单被点击
const MenuClick = (item) => {
    setMenuItem(item)
}
</script>

<style lang="scss">
.el-menu.hc-aside-menu .el-badge .el-badge__content {
    top: 10px;
    right: calc(-12px + var(--el-badge-size) / 2);
}
</style>
