<template>
  <SideBarItem :menuList="menuList" />
</template>
<script>
import SideBarItem from "@/components/SideBarItem.vue";
import { mapGetters } from "vuex";
export default {
  components: {
    SideBarItem,
  },
  computed: {
    ...mapGetters("user", ["userInfoLogin"]),
  },
  data() {
    return {
      activeMenuName: "/userInfo/personInfo",
      menuList: [
        {
          icon: "el-icon-user-solid",
          externalLink: "/userInfo/personInfo",
          menuName: "个人信息",
        },
      ],
      allMenuList:[],//总菜单
    };
  },
  watch: {
    userInfo: {
      handler(newValue, oldValue) {
        console.log(newValue, oldValue);
        if (
          this.userInfoLogin.roles &&
          this.userInfoLogin.roles.includes("admin")
        ) {
          this.menuList = [
            {
              icon: "el-icon-user-solid",
              url: "/userInfo/personInfo",
              name: "个人信息",
            },
            {
              icon: "el-icon-school",
              url: "/userInfo/schoolGradeSubject",
              name: "年级科目",
            },
            {
              icon: "el-icon-set-up",
              url: "/userInfo/schoolGradeTerm",
              name: "学年学期",
            },
            {
              icon: "el-icon-s-check",
              url: "/userInfo/userManagement",
              name: "用户管理",
            },
            {
              icon: "el-icon-reading",
              url: "/userInfo/schoolClassStudent",
              name: "班级名单",
            },
            {
              icon: "el-icon-s-opportunity",
              url: "/userInfo/schoolTeachersList",
              name: "任课教师",
            },
            {
              icon: "el-icon-s-tools",
              url: "/userInfo/schoolManaPerson",
              name: "权限管理",
            },
          ];
        } else {
          this.menuList = [
            {
              icon: "el-icon-user-solid",
              url: "/userInfo/personInfo",
              name: "个人信息",
            },
          ];
        }
      },
      deep: true, // 监听对象的深层变化
    },
  },
  created() {
    
  },
  mounted() {
    this.allMenuList=this.$store.state.user.menuList || [];
    console.log("打印个人中心的菜单this.menuList",this.$store.state.user.menuList);
    
    if(this.allMenuList.length>0)
    {
      
      this.allMenuList.forEach(item => {
        if (item.menuName == '个人中心') 
        {
          this.menuList = item.childMenus;
        }
      });
    }
    else
    {
      if (
        this.userInfoLogin.roles &&
        this.userInfoLogin.roles.includes("admin")
      ) {
        this.menuList = [
          {
            icon: "el-icon-user-solid",
            externalLink: "/userInfo/personInfo",
            menuName: "个人信息",
          },
          {
            icon: "el-icon-school",
            externalLink: "/userInfo/schoolGradeSubject",
            menuName: "年级科目",
          },
          {
            icon: "el-icon-set-up",
            externalLink: "/userInfo/schoolGradeTerm",
            menuName: "学年学期",
          },
          {
            icon: "el-icon-s-check",
            externalLink: "/userInfo/userManagement",
            menuName: "用户管理",
          },
          {
            icon: "el-icon-reading",
            externalLink: "/userInfo/schoolClassStudent",
            menuName: "班级名单",
          },
          {
            icon: "el-icon-s-opportunity",
            externalLink: "/userInfo/schoolTeachersList",
            menuName: "任课教师",
          },
          {
            icon: "el-icon-s-tools",
            externalLink: "/userInfo/schoolManaPerson",
            menuName: "权限管理",
          },
        ];
      } else {
        this.menuList = [
          {
            icon: "el-icon-user-solid",
            externalLink: "/userInfo/personInfo",
            menuName: "个人信息",
          },
        ];
      }
    }
    
    


    this.activeMenu();
  },
  methods: {
    activeMenu() {
      const route = this.$route;
      const { path } = route;
      if (path == "/userInfo") {
        this.activeMenuName = "/userInfo/personInfo";
      }
    },
  },
};
</script>
<style lang="scss" scoped>
</style>
