import axios from "axios";

import router from "@/router";
import Layout from "@/layout/index.vue";

// 声明变量
const state = {
  collapse: false,
  activeIndex: 1,
  username: "",
  password: "",
  // api: "/carstop/carbook", //
  api: "/smartApi", // 线上
  islogin: 1,
  hasRoutes: false,
  level: 1,
  roles: [
    {
      name: "home",
      meta: { isAuth: true, title: "系统首页" },
    },
    // 房型管理
    {
      name: "user",
      meta: { isAuth: true, title: "用户管理" },
    },
    {
      name: "identity",
      meta: { isAuth: true, title: "身份管理" },
    },
    // 数据交换
    {
      name: "classGrade",
      meta: { isAuth: true, title: "年级管理" },
      children: [
        {
          name: "class",
          meta: { isAuth: true, title: "班级管理" },
        },
        {
          name: "grades",
          meta: { isAuth: true, title: "年级管理" },
        },
        {
          name: "subject",
          meta: { isAuth: true, title: "学科管理" },
        },
        {
          name: "semester",
          meta: { isAuth: true, title: "学期管理" },
        },
        {
          name: "post",
          meta: { isAuth: true, title: "职务管理" },
        },
        {
          name: "exam",
          meta: { isAuth: true, title: "考试类型管理" },
        },
      ],
    },
    // 授权管理
    {
      name: "apply",
      meta: { isAuth: true, title: "应用管理" },
    },
    {
      name: "energy",
      meta: { isAuth: true, title: "能耗管理" },
    },
    // 设备管理
    {
      name: "safety",
      meta: { isAuth: true, title: "校园安全" },
    },
    // 组织架构
    {
      name: "caller",
      meta: { isAuth: true, title: "访客预约" },
    },
    // 轨迹管理
    {
      name: "evaluate",
      meta: { isAuth: true, title: "综合素质评价" },
      children: [
        {
          name: "teacher",
          meta: { isAuth: true, title: "教师" },
        },
        {
          name: "student",
          meta: { isAuth: true, title: "学生" },
        },// 成绩管理
        {
          name: "grade",
          meta: { isAuth: true, title: "成绩管理" },
        },
      ],
    },
    // 综合素质评价
    {
      name: "evaluate",
      meta: { isAuth: true, title: "综合素质评价" },
      children: [
        {
          name: "teacher",
          meta: { isAuth: true, title: "教师" },
        },
        {
          name: "student",
          meta: { isAuth: true, title: "学生" },
        },// 成绩管理
        {
          name: "grade",
          meta: { isAuth: true, title: "成绩管理" },
        },
      ],
    },
    // 数据交换
    {
      name: "dataExchange",
      meta: { isAuth: true, title: "数据交换" },
      children: [
        {
          name: "source",
          meta: { isAuth: true, title: "我的数据源" },
        },
        {
          name: "task",
          meta: { isAuth: true, title: "任务管理" },
        },
        {
          name: "log",
          meta: { isAuth: true, title: "业务日志" },
        },
      ],
    },
    // 系统设置
    {
      name: "system",
      meta: { isAuth: true, title: "系统设置" },
      children: [
        {
          name: "info",
          meta: { isAuth: true, title: "基本信息" },
        },
        {
          name: "screenShot",
          meta: { isAuth: true, title: "截屏风险" },
        },
        {
          name: "roles",
          meta: { isAuth: true, title: "权限管理" },
        },
      ],
    },
  ],
  statRoutes: {
    path: "/wanzai  ",
    name: "wanzai",
    component: Layout,
    meta: { isAuth: true, title: "公寓", icons: "" },
    children: [
      {
        path: "home",
        name: "home",
        meta: { isAuth: true, title: "系统首页" },
        component: () => import("@/views/home/home.vue"),
      },
      {
        path: "user",
        name: "user",
        meta: { isAuth: true, title: "用户管理" },
        component: () => import("@/views/user/user.vue"),
      },
      // {
      //   path: "identity",
      //   name: "identity",
      //   meta: { isAuth: true, title: "身份管理" },
      //   component: () => import("@/views/identity/identity.vue"),
      // },
      {
        path: "class",
        name: "class",
        meta: { isAuth: true, title: "班级管理" },
        component: () => import("@/views/class/class.vue"),
      },
      {
        path: "grades",
        name: "grades",
        meta: { isAuth: true, title: "年级管理" },
        component: () => import("@/views/grades/grades.vue"),
      },
      {
        path: "post",
        name: "post",
        meta: { isAuth: true, title: "职务管理" },
        component: () => import("@/views/post/post.vue"),
      },
      {
        path: "subject",
        name: "subject",
        meta: { isAuth: true, title: "学科管理" },
        component: () => import("@/views/subject/subject.vue"),
      },
      {
        path: "semester",
        name: "semester",
        meta: { isAuth: true, title: "学期管理" },
        component: () => import("@/views/semester/semester.vue"),
      },
      {
        path: "exam",
        name: "exam",
        meta: { isAuth: true, title: "考试类型管理" },
        component: () => import("@/views/exam/exam.vue"),
      },
      {
        path: "apply",
        name: "apply",
        meta: { isAuth: true, title: "应用管理" },
        component: () => import("@/views/apply/apply.vue"),
      },
      {
        path: "energy",
        name: "energy",
        meta: { isAuth: true, title: "能耗管理" },
        component: () => import("@/views/energy/energy.vue"),
      },
      {
        path: "safety",
        name: "safety",
        meta: { isAuth: true, title: "校园安全" },
        component: () => import("@/views/safety/safety.vue"),
      },
      {
        path: "caller",
        name: "caller",
        meta: { isAuth: true, title: "访客预约" },
        component: () => import("@/views/caller/caller.vue"),
      },
      {
        path: "grade",
        name: "grade",
        meta: { isAuth: true, title: "成绩管理" },
        component: () => import("@/views/grade/grade.vue"),
      },
      {
        path: "teacher",
        name: "teacher",
        meta: { isAuth: true, title: "教师" },
        component: () => import("@/views/teacher/teacher.vue"),
      },
      {
        path: "student",
        name: "student",
        meta: { isAuth: true, title: "学生" },
        component: () => import("@/views/student/student.vue"),
      },
      {
        path: "info",
        name: "info",
        meta: { isAuth: true, title: "基本信息" },
        component: () => import("@/views/info/info.vue"),
      },
      {
        path: "screenShot",
        name: "screenShot",
        meta: { isAuth: true, title: "截屏风险" },
        component: () => import("@/views/screenShot/screenShot.vue"),
      },
      {
        path: "roles",
        name: "roles",
        meta: { isAuth: true, title: "权限管理" },
        component: () => import("@/views/roles/roles.vue"),
      },
      {
        path: "source",
        name: "source",
        meta: { isAuth: true, title: "我的数据源" },
        component: () => import("@/views/source/source.vue"),
      },
      {
        path: "task",
        name: "task",
        meta: { isAuth: true, title: "任务管理" },
        component: () => import("@/views/task/task.vue"),
      },
      {
        path: "log",
        name: "log",
        meta: { isAuth: true, title: "业务日志" },
        component: () => import("@/views/log/log.vue"),
      },
    ],
  },
};

// 修改变量（state不能直接赋值修改，只能通过mutations）
const mutations = {
  menuClose: (state, value) => {
    state.collapse = value;
  },
  indexUp: (state, value) => {
    state.activeIndex = value;
    console.log(value);
    sessionStorage.setItem("sidevarItem", value);
  },
  userName: (state, value) => {
    state.username = value;
  },
  passWord: (state, value) => {
    state.password = value;
  },
  SET_ROUTES_STATE: (state, value) => {
    state.hasRoutes = value;
  },
  LEVEL: (state, value) => {
    state.level = value;
    console.log("设置level", value);
  },
  ROLELIST: async (state, value) => {
    let roleList = JSON.parse(sessionStorage.getItem("roleList"));
    console.log(roleList);
    if (roleList) {
      router.addRoute({
        path: "/wanzai",
        name: "wanzai",
        // component: modules[`../views/${item.name}/${item.name}.vue`],
        component: Layout,
        children: [],
      });
      roleList.forEach((item) => {
        let list = item.route.split("/");
        // console.log(list[2]);
        let arr = {
          path: list[2],
          name: list[2],
          meta: { isAuth: true, title: list[2] },
          component: () => import(`../../views/${list[2]}/${list[2]}.vue`),
        };
        router.addRoute("wanzai", arr);
      });
      router.addRoute("wanzai", {
        path: "/:pathMatch(.*)*",
        name: "notFound",
        component: () => import("@/views/notFound/notFound.vue"),
      });
    }
  },
};
// mutations的值由actions传入(异步)
const actions = {
  newsAsync: async ({ commit, state }) => {
    let data = new FormData();
    let res = await axios({
      method: "post",
      url: state.api + "/carBook/usermesUnRead.action",
      headers: {
        token: sessionStorage.getItem("token"),
      },
      data: data,
    });
    console.log(res, "系统消息");

    commit("newsUp", res.data.u_count);
    sessionStorage.setItem("newsNum", res.data.u_count);
    console.log("vuex", res.data.u_count);
  },
  // 系统扫码时间
  login: async ({ commit, state }, data) => {
    return new Promise(async (resolve, reject) => {
      let res = await axios({
        method: "get",
        url: state.api + "/mhotel/managerAdminloginAdmin.action",
        params: data,
      });

      console.log(res, "登录账号");
      if (res.data.code == 200) {
        sessionStorage.setItem("name", data.password);

        sessionStorage.setItem("token", res.data.data.managerId);
        sessionStorage.setItem("roomId", res.data.data.id);
        // 1 是  0 否
        if (res.data.isLogin == 1) {
          sessionStorage.setItem("isLogin", 1);
        } else {
          sessionStorage.setItem("isLogin", 0);
        }
        sessionStorage.setItem("username", res.data.data.adminName);
        commit("LEVEL", res.data.data.level);

        commit("userName", data.user);

        // commit("roles", asyncRouter);
        // sessionStorage.setItem("roles", JSON.stringify(asyncRouter));

        resolve("success");
      } else {
        resolve({ name: "reject", data: res });
      }
    });
  },
};

export default {
  state,
  mutations,
  actions,
};
