import { asyncRoutes, constantRoutes } from '@/router'
import { getSilder } from '@/api/silder'
// import { stat } from 'fs'
// import { removeToken } from '@/utils/auth'
// import Layout from '@/layout'

/**
 * 遍历接口菜单添加页面
 * @param asyncRoutes
 * @param getMenu
 */
function generateAsyncRouter (asyncRoutes, data) {
  if (!data) {
    return []
  }

  data.forEach((item) => {
    item.component = asyncRoutes[item.component]
    if (item.children && item.children.length > 0) {
      generateAsyncRouter(asyncRoutes, item.children)
    }
  })
  return data
}

/**
 * 判断平台端添加首页
 * @param type
 */
// const type = getters.type

const state = {
  routes: [],
  addRoutes: [],
  type: '', // 登录的平台类型
  permission: [] // 权限
}

const mutations = {
  SET_ROUTES: (state, routes) => {
    state.addRoutes = routes
    state.routes = constantRoutes.concat(routes)
  },
  SET_PERMISSION: (state, permission) => {

    state.permission = permission
  }
}
// 递归遍历数组
function recursionRouter (arr) {
  if (arr.length > 0) {
    let newArr = [];
    for (let i = 0; i < arr.length; i++) {
      if (arr[i].type == 1) {
        continue
      }
      let obj = {};
      obj.component = arr[i].component;
      // if (item.type != '1' && item.component) {
      //   if (!item.path.startsWith('/') && item.component != 'Layout') {
      //     obj.names = item.name
      //   }
      // }
      arr[i].hid == 0 ? obj.hidden = false : obj.hidden = true
      // console.log('高亮标签'+arr[i].parentPermission,'普通路径'+arr[i].path)
      
      obj.path = arr[i].path;
      obj.meta = { 'title': arr[i].name, 'icon': arr[i].icon, 'noCache': arr[i].keepAlive,'activeMenu': arr[i].parentPermission}
      if (arr[i].sysMenus && arr[i].sysMenus.length > 0) {
        obj.children = recursionRouter(arr[i].sysMenus);
      }
      newArr.push(obj)
    }
    // newArr = arr.map(item => {
    //   let obj = {};
    //   obj.component = item.component;
    //   // if (item.type != '1' && item.component) {
    //   //   if (!item.path.startsWith('/') && item.component != 'Layout') {
    //   //     obj.names = item.name
    //   //   }
    //   // }
    //   item.hid == 0 ? obj.hidden = false : obj.hidden = true
    //   obj.path = item.path;
    //   obj.meta = { 'title': item.name, 'icon': item.icon }
    //   if (item.sysMenus && item.sysMenus.length > 0) {
    //     obj.children = recursionRouter(item.sysMenus);
    //   }
    //   return obj
    // })
    return newArr
  }
}

// 递归遍历数组
let tempArr = []
function recursionPerission (arr) {
  arr.map(item => {
    tempArr.push(item.permission)
    if (item.sysMenus && item.sysMenus.length > 0) {
      recursionPerission(item.sysMenus)
    }
  })
}

const actions = {
  generateRoutes ({ commit }) {
    return new Promise(resolve => {
      // 获取接口返回的权限菜单
      getSilder().then(res => {
        if (res.code == 200) {
          let newData = recursionRouter(res.data);
          recursionPerission(res.data)
          let accessedRoutes
          // 生成异步路由表
          accessedRoutes = generateAsyncRouter(asyncRoutes, newData)
          // var result = accessedRoutes.concat({ path: '*', redirect: '/404', hidden: true })
          commit('SET_ROUTES', accessedRoutes)
          // commit('SET_PERMISSION', recursionPerission(res.data).flat(Infinity))
          window.localStorage.removeItem('permission')
          window.localStorage.setItem('permission', tempArr)
          resolve(accessedRoutes)
        }
      })
    })
  },
  removePermission({ commit }){
    window.localStorage.removeItem('permission')
    commit('SET_PERMISSION', [])
    
  }
}

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