import { asyncRoutes, constantRoutes } from '@/router'
import { console } from 'vuedraggable/src/util/helper'
import { getRouter } from '@/api/user'
import Layout from '@/layout'
/**
 * Use meta.role to determine if the current user has permission
 * @param roles
 * @param route
 */
function hasPermission(roles, route) {
  if (route.meta && route.meta.roles) {
    return roles.some(role => route.meta.roles.includes(role))
  } else {
    return true
  }
}

/**
 * Filter asynchronous routing tables by recursion
 * @param routes asyncRoutes
 * @param roles
 */
export function filterAsyncRoutes(routes, roles) {
  const res = []

  routes.forEach(route => {
    const tmp = { ...route }
    if (hasPermission(roles, tmp)) {
      if (tmp.children) {
        tmp.children = filterAsyncRoutes(tmp.children, roles)
      }
      res.push(tmp)
    }
  })

  return res
}

const state = {
  routes: [],
  addRoutes: []
}

const mutations = {
  SET_ROUTES: (state, routes) => {
    state.addRoutes = routes;
    console.log(routes, 23232)
    state.routes = constantRoutes.concat(routes)
  }
}

const actions = {
  generateRoutes({ commit }, roles) {
    return new Promise((resolve, reject) => {
      getRouter().then(response => {

        console.log('获取的路由信息：', response);
        response.data.shift();
        let accessRoutes = response.data;
        let routers = [];
        accessRoutes.forEach((item, index) => {
          if (item.component === 'Layout') {
            var data = {
              children: [],
              component: Layout,
              meta: {
                title: item.meta.title
              },
              name: item.title,
              path: item.path,
              // redirect: item.path
            }
            routers.push(data)
          }
          if (item.children.length !== 0 && item.children !== null) {
            item.children.forEach((itemChildren, indexChildren) => {
              var dataChildren = {
                component: (resolve) => require([`@/views/${itemChildren.component}`], resolve),
                meta: itemChildren.meta,
                name: itemChildren.meta.title + "C",
                path: itemChildren.path,
              }
              if (itemChildren.state) {
                dataChildren.hidden = true;
              }
              routers[index].children.push(dataChildren)
            })
          }

        })

        commit('SET_ROUTES', routers)
        resolve(routers)
      }).catch(error => {
        // reject(error)
      })
      // if (roles.includes('admin')) {
      //   accessedRoutes = asyncRoutes || []
      // } else {
      //   accessedRoutes = filterAsyncRoutes(asyncRoutes, roles)
      // }
    })
  }
  // GenerateRoutes({ commit }, data) {
  //   return new Promise(resolve => {
  //     const { perms } = data
  //     let accessedRouters
  //     if (perms.includes('*')) {
  //       layout[0].children = asyncRouterMap
  //       accessedRouters = layout
  //     } else {
  //       layout[0].children = filterAsyncRouter(asyncRouterMap, perms)
  //       accessedRouters = layout
  //     }
  //     commit('SET_ROUTES', accessedRouters)
  //     resolve()
  //   })
  // }
}

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