// src/stores/counter.ts
import { defineStore } from "pinia";
import { https } from "@/utils/request"; // 绝对路径
import router from "../router";
import { ElMessage } from "element-plus";
export const useCounterStore = defineStore("user", {
  state: () => {
    return {
      name: "兰陵王",
      age: 24,
      isCollapse: false, // 定义初始状态
      sidevarItem: "/home",
      roleList: [], // 权限列表
      btnRole: [], // 功能按钮权限
      collegeRole: [], // 院系权限
      roleId: "", // 角色id
    };
  },
  getters: {
    realAge(state) {
      return state.age * 10;
    },
  },
  actions: {
    setName(name) {
      this.name = name;
    },
    setAge({ name, age }) {
      // 直接通过this修改state
      // this.name = name;
      // this.age = age;
      // or 批量更新state
      this.$patch({
        name,
        age,
      });
    },
    // 控制侧边栏的伸缩
    setInfo(isCollapse) {
      // 直接通过this修改state
      this.isCollapse = isCollapse;
      // or 批量更新state
      // this.$patch({
      //     isCollapse
      // });
    },
    // 保存菜单项
    setItem(sidevarItem) {
      this.sidevarItem = sidevarItem;
      sessionStorage.setItem("sidevarItem", sidevarItem);
    },
    // 动态添加路由
    async ROLEAdd() {
      // 获取菜单权限列表
      this.roleList = [];
      if (sessionStorage.getItem("accountId")) {
        let datas = {
          accountId: sessionStorage.getItem("accountId"),
        };
        const res = await https.get(
          "/welcome/api/welcomeAccount/getAccountAuthority",
          "params",
          datas
        ); // 使用封装的 get 方法
        console.log(res, "获取菜单权限列表");
        if (res.code == 200) {
          this.collegeRole = res.data.welcomeOrgList;
          let roles = res.data.welcomeRole;
          this.roleId = res.data.welcomeRole.id;
          let btn = [];
          // 数据总览
          if (roles.dataManagementSetting) {
            let arr = roles.dataManagementSetting.split(",");
            arr.forEach((i) => {
              btn.push(`dataOverview${i}`);
              if (i == 1) {
                this.roleList.push({
                  name: "数据总览",
                  path: "/dataOverview",
                  icon: "dataOverview",
                });
              }
            });
          }
          // 学生信息设置
          if (roles.studentManagementSetting) {
            let arr = roles.studentManagementSetting.split(",");
            arr.forEach((i) => {
              btn.push(`studentManagementSetting${i}`);
              if (i == 7) {
                this.roleList.push({
                  name: "学生信息管理",
                  path: "/studentInfo",
                  icon: "studentInfo",
                });
              }
            });
          }
          // 楼栋管理设置
          if (roles.buildingManagementSetting) {
            let arr = roles.buildingManagementSetting.split(",");
            arr.forEach((i) => {
              btn.push(`buildingManagementSetting${i}`);
              if (i == 6) {
                this.roleList.push({
                  name: "楼栋信息管理",
                  path: "/building",
                  icon: "building",
                });
              }
            });
          }
          // 宿舍管理设置
          if (roles.dormitoryManagementSetting) {
            let arr = roles.dormitoryManagementSetting.split(",");
            arr.forEach((i) => {
              btn.push(`dormitoryManagementSetting${i}`);
              if (i == 10) {
                this.roleList.push({
                  name: "寝室信息管理",
                  path: "/dormitory",
                  icon: "dormitory",
                });
              }
            });
          }
          // 床位管理设置
          if (roles.bedManagementSetting) {
            let arr = roles.bedManagementSetting.split(",");
            arr.forEach((i) => {
              btn.push(`bedManagementSetting${i}`);
              if (i == 13) {
                this.roleList.push({
                  name: "床位信息管理",
                  path: "/bed",
                  icon: "bed",
                });
              }
            });
          }
          // 学生住宿设置
          if (roles.studentAccommodationSetting) {
            let arr = roles.studentAccommodationSetting.split(",");
            arr.forEach((i) => {
              btn.push(`studentAccommodationSetting${i}`);
              if (i == 2) {
                this.roleList.push({
                  name: "学生住宿信息",
                  path: "/student",
                  icon: "student",
                });
              }
            });
          }
          // 住宿信息统计
          if (roles.accommodationStatisticsSetting) {
            let arr = roles.accommodationStatisticsSetting.split(",");
            arr.forEach((i) => {
              btn.push(`accommodationStatisticsSetting${i}`);
              if (i == 2) {
                this.roleList.push({
                  name: "住宿信息统计",
                  path: "/quarterage",
                  icon: "quarterage",
                });
              }
            });
          }
          // 访客信息管理
          if (roles.visitorManagementSetting) {
            let arr = roles.visitorManagementSetting.split(",");
            arr.forEach((i) => {
              btn.push(`visitorManagementSetting${i}`);
              if (i == 5) {
                this.roleList.push({
                  name: "访客信息管理 ",
                  path: "/caller",
                  icon: "caller",
                });
              }
            });
          }
          // 选宿舍金额设置
          if (roles.dormitoryMoneySetting) {
            let arr = roles.dormitoryMoneySetting.split(",");
            arr.forEach((i) => {
              btn.push(`dormitoryMoneySetting${i}`);
              if (i == 6) {
                this.roleList.push({
                  name: "选宿舍金额设置 ",
                  path: "/moneySetting",
                  icon: "moneySetting",
                });
              }
            });
          }
          // 用户管理
          if (roles.accountManagementSetting) {
            let arr = roles.accountManagementSetting.split(",");
            arr.forEach((i) => {
              btn.push(`accountManagementSetting${i}`);
              if (i == 4) {
                this.roleList.push({
                  name: "用户管理 ",
                  path: "/user",
                  icon: "user",
                });
              }
            });
          }
          // 角色管理
          if (roles.roleManagementSetting) {
            let arr = roles.roleManagementSetting.split(",");
            arr.forEach((i) => {
              btn.push(`roleManagementSetting${i}`);
              if (i == 4) {
                this.roleList.push({
                  name: "角色管理 ",
                  path: "/role",
                  icon: "role",
                });
              }
            });
          }

          // 操作日志
          if (roles.logManagementSetting) {
            console.log("操作日志",roles.logManagementSetting.split(","));
            
            let arr = roles.logManagementSetting.split(",");
            arr.forEach((i) => {
              btn.push(`logManagementSetting${i}`);
              if (i == 1) {
                this.roleList.push({
                  name: "操作日志 ",
                  path: "/log",
                  icon: "log",
                });
              }
            });
          }

          // 系统设置
          if (roles.settingManagementSetting) {
            let arr = roles.settingManagementSetting.split(",");
            arr.forEach((i) => {
              btn.push(`settingManagementSetting${i}`);
              if (i == 2) {
                this.roleList.push({
                  name: "系统设置 ",
                  path: "/system",
                  icon: "system",
                });
              }
            });
          }
          // sessionStorage.setItem("btnRole", btn.toString());
          this.btnRole = btn;
          const modules = import.meta.glob("../views/**/*.vue");
          let roleList = this.roleList;
          if (roleList) {
            roleList.forEach((item) => {
              let arr = {
                path: item.path,
                name: item.path,
                meta: { isAuth: true, title: item.name, showHeader: true },
                component: modules[`../views${item.path}${item.path}.vue`],
              };
              router.addRoute(arr);
            });
            router.addRoute({
              path: "/404",
              name: "NotFound",
              component: () => import("@/views/404/404.vue"),
            });
            router.addRoute({
              path: "/:pathMatch(.*)*",
              redirect: "/404",
            });
            console.log("动态添加路由");
          }

          router.push({
            path: roleList[0].path,
          });
          console.log(roleList);
          
        } else {
          console.log("动态添加路由失败");
          router.addRoute({
            path: "/404",
            name: "NotFound",
            component: () => import("@/views/404/404.vue"),
          });
          router.addRoute({
            path: "/:pathMatch(.*)*",
            redirect: "/404",
          });
        }
      }
    },
    BtnRole(flag) {
      let show = this.btnRole.some((i) => {
        return i == flag;
      });
      return show;
    },
  },
});
