import { getSiteBusiness, getDepartment, getWindow } from "@/api/site";
import { getHomeData } from "@/api/home";
import { findInTree } from "@/utils";
export default {
  namespaced: true,
  state: {
    menusList: [], // 原始菜单列表
    menus: [], // 格式化菜单列表
    routes: [], // 路由列表
    secondaryRoutes: [], // 二级路由
    barList: [], // 登录返回菜单
    homeData: {}, // 首页数据
    token: "",
    siteId: "", // 站点id
    userInfo: {}, // 用户信息
    sysName: "", // 系统名称
    sysLogo: "", // 系统logo
    path: "", // 门户跳转过来的路由
    areaTree: [], // 个人区域树
    businessList: [], // 站点业务列表
    deptList: [], // 站点部门列表
    windowList: [], // 站点窗口列表
    permissions: [], // 按钮权限字符列表
  },
  getters: {
    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_menusList(state, menusList) {
      state.menusList = menusList;
    },
    SET_routes(state, routes) {
      state.routes = routes;
    },
    SET_permissions(state, permissions) {
      state.permissions = permissions;
    },
    SET_secondaryRoutes(state, secondaryRoutes) {
      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;
    },

    // 重置所有仓库状态
    reset: () => {},
  },
  actions: {
    // 获取首页信息
    getHomeData: async (context) => {
      let res = await getHomeData();
      if (res.data.code == 1) {
        let { data } = res.data;
        context.commit("SET_homeData", data);
      }
    },
    // 获取站点业务列表
    getBusiness: async (context, siteId) => {
      let res = await getSiteBusiness({
        page: 1,
        size: -1,
        siteId,
      });
      if (res.data.code == 1) {
        let { data } = res.data.data;
        context.commit("SET_businessList", data);
      }
    },
    // 获取站点部门列表
    getDeptList: async (context, siteId) => {
      let res = await getDepartment({
        page: 1,
        size: -1,
        siteId,
      });
      if (res.data.code == 1) {
        let { data } = res.data.data;
        context.commit("SET_deptList", data);
      }
    },
    // 获取站点窗口列表
    getWindowList: async (context, siteId) => {
      let res = await getWindow({
        page: 1,
        size: -1,
        siteId,
      });
      if (res.data.code == 1) {
        let { data } = res.data.data;
        context.commit("SET_windowList", data);
      }
    },
    // 设置二级路由
    setSecondaryRoutes(context, value) {
      let routes = findInTree(context.state.menus, "path", value);
      let secondaryRoutes = routes.children || [];
      context.commit("SET_secondaryRoutes", secondaryRoutes);
    },
  },
};
