import { constantRoutes } 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) => {
      state.addRoutes = routes
      state.routes = constantRoutes.concat(routes)
    },
    SET_DEFAULT_ROUTES: (state, routes) => {
      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 => {
        const loginType = localStorage.getItem('loginType')
        const isPharmacist = loginType === '2'

        // 根据登录身份动态调整侧边栏菜单
        constantRoutes.forEach(route => {
          // 1) 仅药剂师显示“处方审核”菜单
          if (route.path === '/prescribeAudit') {
            route.hidden = !isPharmacist
          }
          // 2) 药剂师登录时“医生信息”文案改为“药剂师信息”
          if (route.path === '/doctor') {
            if (route.meta) {
              route.meta.title = isPharmacist ? '药剂师信息' : '医生信息'
            }
            if (Array.isArray(route.children)) {
              route.children.forEach(child => {
                if (child.path === 'doctorInfo' && child.meta) {
                  child.meta.title = isPharmacist ? '药剂师信息' : '医生信息'
                }
              })
            }
          }
        })

        // 不追加动态路由，直接使用调整后的 constantRoutes
        commit('SET_ROUTES', [])
        resolve([])
      })
    }
  }
}


export default permission
