import Vue from "vue";
import Vuex from "vuex";
import createPersistedState from "vuex-persistedstate";
// import SecureLS from "secure-ls";
// var ls = new SecureLS({ isCompression: false });
import { SessionCrypto } from "@/utils";
Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    layout: "head", //导航布局，可选 side 和 head，分别为侧边导航和顶部导航
    token: "",
    siteId: "", // 站点id
    deptList: [], // 部门列表
    sysName: "", // 系统名称
    sysLogo: "", // 系统logo
    path: "", // 门户跳转过来的路由
    menusList: [], // 原始菜单列表
    menus: [], // 格式化菜单列表
    routes: [], // 路由列表
    secondaryRoutes: [], // 二级路由
    barList: [], // 登录返回菜单
    homeData: {}, // 首页数据
    userInfo: {}, // 用户信息
    permissions: [], // 按钮权限字符列表
  },
  getters: {
    layout(state) {
      return state.layout;
    },
    siteId(state) {
      return state.siteId;
    },
    deptList(state) {
      return state.deptList;
    },
    userInfo(state) {
      return state.userInfo;
    },
    token(state) {
      return state.token;
    },
    path(state) {
      return state.path;
    },
    barList(state) {
      let { barList } = state.homeData;
      return barList || [];
    },
    routes(state) {
      return state.routes;
    },
    menus(state) {
      return state.menus;
    },
    permissions(state) {
      return state.permissions;
    },
    menusList(state) {
      return state.menusList;
    },
    secondaryRoutes(state) {
      return state.secondaryRoutes;
    },
  },
  mutations: {
    SET_layout(state, layout) {
      state.layout = layout;
    },
    SET_menusList(state, menusList) {
      state.menusList = menusList;
    },
    SET_routes(state, routes) {
      state.routes = routes;
    },
    SET_permissions(state, permissions) {
      state.permissions = permissions;
    },
    SET_secondaryRoutes(state, value) {
      let routes = state.menus.find((v) => v.path == value);
      let secondaryRoutes = [];
      if (routes) {
        secondaryRoutes = routes.children;
      }
      state.secondaryRoutes = secondaryRoutes;
    },
    SET_path(state, path) {
      state.path = path;
    },
    SET_menus(state, menus) {
      state.menus = menus;
    },
    SET_siteId(state, siteId) {
      state.siteId = siteId;
    },
    SET_userInfo(state, userInfo) {
      state.userInfo = userInfo;
    },
    SET_sysName(state, sysName) {
      state.sysName = sysName;
    },
    SET_sysLogo(state, sysLogo) {
      state.sysLogo = sysLogo;
    },
    SET_token(state, token) {
      state.token = token;
    },
    SET_areaTree(state, areaTree) {
      state.areaTree = areaTree;
    },
    SET_businessList(state, businessList) {
      state.businessList = businessList;
    },
    SET_deptList(state, deptList) {
      state.deptList = deptList;
    },
    SET_windowList(state, windowList) {
      state.windowList = windowList;
    },
    SET_barList(state, barList) {
      state.barList = barList;
    },
    SET_homeData(state, homeData) {
      state.homeData = homeData;
    },
  },
  actions: {},
  modules: {},
  // 持久化插件
  plugins: [
    // createPersistedState({
    //   storage: window.sessionStorage,
    // }),
    createPersistedState({
      key: "info",
      storage: {
        getItem: (key) => SessionCrypto.getItem(key),
        setItem: (key, value) => SessionCrypto.setItem(key, value),
        removeItem: (key) => SessionCrypto.remove(key),
      },
    }),
  ],
});
