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

// 声明变量
const state = {
  collapse: false,
  activeIndex: "", // 记录当前路由
  username: "",
  password: "",
  api: "/jgcloudApi/jgcloud/api", // 线上
  // api: "/jgcloudApi/jgcloud/api", // 测试
  roleList: [
    {
      path: "home",
      name: "home",
      component: () => import("@/views/home/home.vue"),
      meta: { title: "首页", icon: "home" },
    },
    {
      path: "project",
      name: "project",
      component: () => import("@/views/project/project.vue"),
      meta: { title: "项目管理", icon: "project" },
    },
    {
      path: "role",
      name: "role",
      component: () => import("@/views/role/role.vue"),
      meta: { title: "角色管理", icon: "role" },
    },
    {
      path: "user",
      name: "user",
      component: () => import("@/views/user/user.vue"),
      meta: { title: "用户管理", icon: "user" },
    },
    {
      path: "system",
      name: "system",
      component: () => import("@/views/system/system.vue"),
      meta: { title: "系统管理", icon: "system" },
    },
  ],
  roles: [
    
  ],
  hasRoutes: false,
};

// 修改变量（state不能直接赋值修改，只能通过mutations）
const mutations = {
  menuClose: (state, value) => {
    state.collapse = value;
  },
  indexUp: (state, value) => {
    sessionStorage.setItem("sidevarItem", value);
    state.activeIndex = value;
    // if (!value) {
    //   let roleId = sessionStorage.getItem("roleId");
    //   state.user.roles.forEach((item) => {
    //     if (item.roles == roleId) {
    //       state.activeIndex = `/jianguanCloud/${item.list[0]}`;
    //       sessionStorage.setItem(
    //         "sidevarItem",
    //         `/jianguanCloud/${item.list[0]}`
    //       );
    //     }
    //   });
    // } else {
    //   sessionStorage.setItem("sidevarItem", value);
    // }
  },
  SET_ROUTES_STATE: (state, value) => {
    state.hasRoutes = value;
  },
  userName: (state, value) => {
    state.username = value;
  },
  passWord: (state, value) => {
    state.password = value;
  },
  ROLELIST: (state, value) => {
    const roleId = sessionStorage.getItem("roleId");
    console.log(roleId);

    if (roleId) {
      state.roles.forEach((item) => {
        if (item.roles == roleId) {
          let modules = import.meta.glob("../views/**/*.vue");
          router.addRoute({
            path: "/jianguanCloud",
            name: "jianguanCloud",
            // component: modules[`../views/${item.name}/${item.name}.vue`],
            component: Layout,
            children: [],
          });
          item.list.forEach((item) => {
            let arr = {
              path: item.name,
              name: item.name,
              // component: modules[`../views/${item.name}/${item.name}.vue`],
              component: () =>
                import(`../../views/${item.name}/${item.name}.vue`),
            };
            // state[0].roleList.children.push(arr);
            router.addRoute("jianguanCloud", arr);
          });
        }
      });
    }
  },
};
// 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: "post",
        url: state.api + "/login/Login",
        data: data,
      });

      console.log(res, "登录账号");
      if (res.data.code == 200) {
        sessionStorage.setItem("userName", res.data.data.userName);
        sessionStorage.setItem("token", res.data.data.token);
        sessionStorage.setItem("roleId", res.data.data.roleId);
        sessionStorage.setItem("userhead", res.data.data.userhead);
        sessionStorage.setItem("provinceId", res.data.data.provinceId);
        sessionStorage.setItem("cityId", res.data.data.cityId);
        sessionStorage.setItem("districtId", res.data.data.districtId);
        sessionStorage.setItem("address", res.data.data.address);
        sessionStorage.setItem("addressId", res.data.data.addressId);

        commit("userName", res.data.data.userName);
        commit("ROLELIST", "");
        if (res.data.data.roleId == 1) {
          router.push({ path: "/jianguanCloud/project" });
          commit("indexUp", "/jianguanCloud/project");
        } else {
          router.push({ path: "/jianguanCloud/home" });
          commit("indexUp", "/jianguanCloud/home");
        }
        resolve("success");
      } else {
        resolve({ name: "reject", data: res });
      }
    });
  },
};

export default {
  state,
  mutations,
  actions,
};
