import { constantRoutes , storeRouter} from '@/router'
import Layout from '@/layout/index'
import ParentView from '@/components/ParentView';
import InnerLink from '@/layout/components/InnerLink'

const permission = {
  state: {
    routes: [],
    addRoutes: [],
    defaultRoutes: [],
    topbarRouters: [],
    sidebarRouters: []
  },
  mutations: {
    SET_ROUTES: (state, routes) => {
      const cachedStoreRouter = localStorage.getItem('store_router')
      const defaultArray = storeRouter || [];
      let constantRoutes = defaultArray;
      // console.log("打印数据-----------》", cachedStoreRouter);
      if (cachedStoreRouter) {
        try {
          const temp = JSON.parse(cachedStoreRouter);
          constantRoutes = Array.isArray(temp) ? temp : defaultArray;
          // console.log("打印数据-----------》", constantRoutes);
        } catch (error) {
          console.error('localStorage 数据解析失败，使用默认数组:', error);
          localStorage.removeItem('store_router');
        }
      }
      state.addRoutes = routes
      state.routes = constantRoutes.concat(routes)
    },
    SET_DEFAULT_ROUTES: (state, routes) => {
      const cachedStoreRouter = localStorage.getItem('store_router')
      const defaultArray = storeRouter || [];
      let constantRoutes = defaultArray;
      if (cachedStoreRouter) {
        try {
          const temp = JSON.parse(cachedStoreRouter);
          constantRoutes = Array.isArray(temp) ? temp : defaultArray;
        } catch (error) {
          console.error('localStorage 数据解析失败，使用默认数组:', error);
          localStorage.removeItem('store_router');
        }
      }
      state.defaultRoutes = constantRoutes.concat(routes)
    },
    SET_TOPBAR_ROUTES: (state, routes) => {
      // 顶部导航菜单默认添加统计报表栏指向首页
      // const index = [{
      //   path: 'index',
      //   meta: { title: '统计报表', icon: 'dashboard'}
      // }]
      // state.topbarRouters = routes.concat(index);

      state.topbarRouters = routes;
    },
    SET_SIDEBAR_ROUTERS: (state, routes) => {
      state.sidebarRouters = routes
    },
  },
  actions: {
    // 生成路由
    getRoutes({ commit }) {
      return new Promise(resolve => {
  var rewriteRoutes=  [ {"hidden": true,}]
          commit('SET_ROUTES', rewriteRoutes)
          resolve(rewriteRoutes)
      })
    }
  }
}


export default permission
