import Layout from '@/components/Layout'
import asyncRoutes from '@/router/asyncRoutes'

const permissionBtns = []

function hasPermission(name, menus) {
  const data = menus.filter(item => {
    return item.resourceCode === name
  })
  return data[0] || {}
}

function generatePageRouteTable(routes, ret = []) {
  routes.forEach(item => {
    if (item.children && item.children.length) {
      const route = { ...item }
      delete route.children
      ret.push(route)
      generatePageRouteTable(item.children, ret)
    } else {
      ret.push(item)
    }
  })
}

/**
 * Filter asynchronous menu tables by recursion
 * @param menus asyncBtns
 */
export function filterAsyncBtns(menus) {
  menus.forEach(menu => {
    const tmp = { ...menu }
    if (tmp.resourceType === 1) {
      permissionBtns.push(tmp.resourceCode)
    }
  })
  return permissionBtns
}

/**
 * Filter asynchronous routing tables by recursion
 * @param routes asyncRoutes
 * @param roles
 */
export function filterAsyncRoutes(routes, menus) {
  const res = []
  // 权限接口为空则不过滤接口
  if (menus.length === 0) {
    return routes
  }
  routes.forEach(route => {
    const tmp = { ...route }
    const data = hasPermission(tmp.name, menus)
    if (data.resourceCode) {
      if (tmp.children) {
        tmp.children = filterAsyncRoutes(tmp.children, menus)
      }
      tmp.meta.title = data.resourceName
      if (data.icon) {
        tmp.meta.icon = data.icon
      }
      res.push(tmp)
    }
  })
  return res
}

const state = {
  routes: [],
  addRoutes: [],
  currentChildRoute: [],
  currentRouteTitle: '',
  permissionBtns: []
}

const mutations = {
  SET_ROUTES: (state, routes) => {
    state.addRoutes = routes
  },
  SET_CHILD_ROUTES: (state, data) => {
    state.currentChildRoute = data.route
    state.currentRouteTitle = data.title
  },
  SET_PERMISSION_BTNS: (state, permissionBtns) => {
    state.permissionBtns = permissionBtns
  }
}

const actions = {
  generateRoutes({ commit }, menus) {
    return new Promise(resolve => {
      filterAsyncBtns(menus)
      // 获取当前有权限的路由
      const accessedRoutes = filterAsyncRoutes(asyncRoutes, menus)
      commit('SET_ROUTES', accessedRoutes)
      // 将页面路由转换为二级
      const pageRouteTable = [
        {
          path: '/',
          component: Layout,
          redirect: '/home',
          name: 'Home',
          children: []
        },
        { path: '*', redirect: '/404' }
      ]
      generatePageRouteTable(accessedRoutes, pageRouteTable[0].children)
      resolve(pageRouteTable)
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
