<template>
    <template v-for="item in datas" :key="item?.code">
        <el-sub-menu v-if="item?.children && item?.children.length > 0" :index="item?.code" :popper-offset="0" :popper-class="`aside-menu-popper ${curKey}`" :class="getMenuLevel(item)">
            <template #title>
                <div class="hc-aside-menu-item">
                    <div class="menu---item">
                        <HcIcon v-if="item?.source" :name="item?.source" :fill="curKey === item?.code" class="hc-menu-icon" />
                        <div v-if="isCollapse" class="name truncate">{{ item?.name.substring(0, 2) }}</div>
                        <div v-else class="name truncate">{{ item?.name }}</div>
                        <el-badge v-if="item?.code === 'tasks' && msgCount?.allCount > 0" :value="msgCount.allCount" />
                    </div>
                </div>
                <HcIcon name="arrow-down-s" ui="el-icon el-sub-menu__icon-arrow" />
            </template>
            <MenuItem v-if="!isYunNanProject" :datas="item?.children" :cur="curKey" :msg-count="msgCount" @change="MenuClick" />
            <MenuItem2 v-else :datas="item?.children" :cur="curKey" :msg-count="msgCount" @change="MenuClick" />
        </el-sub-menu>
        <el-menu-item v-else :index="item?.code" :class="getMenuLevel(item)" @click="MenuClick(item)">
            <div class="hc-aside-menu-item">
                <div class="menu---item">
                    <HcIcon v-if="item?.source" :name="item?.source" :fill="curKey === item?.code" class="hc-menu-icon" />
                    <div v-if="isCollapse" class="name truncate">{{ item?.name.substring(0, 2) }}</div>
                    <div v-else class="name truncate">{{ item?.name }}</div>
                    <el-badge v-if="item?.code === 'tasks-data' && msgCount?.taskCount > 0" :value="msgCount.taskCount" />
                    <el-badge v-if="item?.code === 'message-data' && msgCount?.messageCount > 0" :value="msgCount.messageCount" />
                </div>
            </div>
        </el-menu-item>
    </template>
</template>


<script setup>
import { computed, ref, watch } from 'vue'
import MenuItem from '../modules/MenuItem.vue'
import MenuItem2 from '../test-yn/MenuItem-yn.vue'
import { useAppStore } from '~src/store'
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 emit = defineEmits(['change'])
const store = useAppStore()
const projectId = ref(store.getProjectId)
const isYunNanProject = ref(projectId.value === '1904814720589430785')


// 添加watch来更新isYunNanProject
watch(() =>store.getProjectId, (newProjectId) => {
    isYunNanProject.value = newProjectId === '1904814720589430785'
})
//初始变量
const curKey = ref(props.cur)
const isCollapse = ref(props.collapse)

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

const MenuClick = (item) => {
    emit('change', item)
}
// 获取菜单层级样式
const getMenuLevel = (item) => {
  const level = item.code.split('-').length
  return `menu-level-${level}`
}
</script>

<style lang="scss" scoped>
.menu-level-1 {
  padding-left: 0px;
}

.menu-level-2 {
  padding-left: 20px !important;
  
  :deep(.el-sub-menu__title),
  :deep(.el-menu-item) {
    padding-left: 20px !important;
  }
}

.menu-level-3 {
  padding-left: 40px !important;
  
  :deep(.el-sub-menu__title),
  :deep(.el-menu-item) {
    padding-left: 40px !important;
  }
}
</style>