import {
  getBreadCrumbList,
  setTagNavListInLocalstorage,
  getMenuByRouter,
  getTagNavListFromLocalstorage,
  getHomeRoute,
  getNextRoute,
  routeHasExist,
  routeEqual,
  getRouteTitleHandled,
  sessionSave,
  sessionRead,
} from '@/libs/base/util'
import router from '@/router'
import config from '@/config'
import { loadMenu } from '@/libs/base/router-util'
import routers from '@/router/routers'
const { homeName } = config

const closePage = (state, route) => {
  //获取到下一个路由
  const nextRoute = getNextRoute(state.tagNavList, route)
  state.tagNavList = state.tagNavList.filter(item => {
    return !routeEqual(item, route)
  })
  router.push(nextRoute)
}

export default {
  state: {
    breadCrumbList: [],
    tagNavList: [],
    homeRoute: {},
    local: sessionRead('local') || 'zh-CN',
    local_lan: sessionRead('local_lan') || 'zh_CN',
    errorList: [],
    hasReadErrorPage: false,
    menuList:[],
    //菜单类型 默认抽屉
    menuType: 'drawer'
  },
  getters: {
    // menuList: (state, getters, rootState) => getMenuByRouter(getMoreMenu(), rootState.user.access),
    //todo 临时拼接基础数据的路由routers
    // 原版  ↓
    // menuList: (state, getters, rootState) => getMenuByRouter(state.menuList.length > 0 ? state.menuList : loadMenu(), rootState.user.access),
    // menuList: (state, getters, rootState) => getMenuByRouter([...routers, ...state.menuList.length > 0 ? state.menuList : loadMenu()], rootState.user.access),
    // menuList: (state, getters, rootState) => getMenuByRouter(routers, rootState.user.access),
    menuList: (state, getters, rootState) => getMenuByRouter(state.menuList.length > 0 ? state.menuList : loadMenu(), rootState.user.access),
    errorCount: state => state.errorList.length
  },
  mutations: {
    setMenuList(state, routes) {
      state.menuList = routes
    },
    // // 接受前台数组，刷新菜单
    // updateMenuList(state, routes) {
    //   router.addRoutes(routes)
    //   state.menuList = routes
    // },
    setBreadCrumb (state, route) {
      state.breadCrumbList = getBreadCrumbList(route, state.homeRoute)
    },
    setHomeRoute (state, routes) {
      state.homeRoute = getHomeRoute(routes, homeName)
    },
    /**
     * @desc   : 设置工具栏菜单
     * @author : 周兴
     * @date   : 2022/5/19 18:45
     */
    setTagNavList (state, list) {
      let tagList = []
      if (list) {
        tagList = [...list]
      } else {
        tagList = getTagNavListFromLocalstorage() || []
      }
      state.tagNavList = tagList
      setTagNavListInLocalstorage([...tagList])
    },
    /**
     * @desc   : 关闭标签
     * @author : 周兴
     * @date   : 2022/5/19 20:21
     */
    closeTag (state, route) {
      // if(!this.$route.meta){
      //   this.$route.meta = {}
      // }
      // this.$route.meta.notCache = false;
      let tag = state.tagNavList.filter(item => routeEqual(item, route))
      route = tag[0] ? tag[0] : null
      if (!route) return
      closePage(state, route)
    },
    /**
     * @desc   : 添加标签
     * @author : 周兴
     * @date   : 2022/5/19 20:21
     */
    addTag (state, { route, type = 'unshift', lastName }) {
      let router = getRouteTitleHandled(route)
      if (!routeHasExist(state.tagNavList, router)) {
        let names = []
        for (let it of state.tagNavList){
          names.push(it.name)
        }
        //插入到末尾
        if (type === 'push' ) state.tagNavList.push(router)
        // //插入到当前页之后
        // else if (type === 'push' && !route.meta.menuFlag) state.tagNavList.splice(names.indexOf(lastName) + 1,0,router)
        else {
          // if (router.name === homeName) state.tagNavList.unshift(router)
          // else
          if(router.name !== homeName){
            state.tagNavList.splice(1, 0, router)
          }
        }
        setTagNavListInLocalstorage([...state.tagNavList])
      }
    },
    setLocal (state, lang) {
      sessionSave('local', lang)
      state.local = lang
    },
    addError (state, error) {
      state.errorList.push(error)
    },
    setHasReadErrorLoggerStatus (state, status = true) {
      state.hasReadErrorPage = status
    },
    /**
     * @desc   : 保存菜单类型
     * @author : 洪旭东
     * @date   : 2023-08-14 11:22
     */
    setMenuType (state, menuType) {
      state.menuType = menuType
    }
  },
  actions: {
  }
}
