import { constantRouterMap } from '@/router'

let buttonDatas = {}// 按钮数据
/**
 * 递归过滤异步路由表，返回符合用户角色权限的路由表
 * @param menudatas 后台获取的菜单数据
 */
function filterAsyncRouter(menudatas) {
  const accessedRouters = []
  for (var i = 0, len = menudatas.length; i < len; i++) {
    let cpath = ''// 用于存放 获取的菜单路径
    let fpath = ''// 菜单的路径 用于 path
    let furl = ''// 菜单的路径 用于 component内的路径
    let mflag = ''// 菜单代码值
    let mbtn = []// 菜单按钮
    let redirect//
    let mIcon = ''// 菜单的icon
    if (Number(menudatas[i].F_ParentId) === 0) {
      accessedRouters.push({
        path: '',
        redirect: 'noredirect',
        name: menudatas[i].F_MenuCode,
        component: () => import('@/views/layout/Layout'),
        id: menudatas[i].F_Id,
        // target: menudatas[i].F_Target,//菜单类型
        // stateFlag: menudatas[i].F_State,//是否启用
        // remark: menudatas[i].F_Remark,//介绍
        meta: {
          title: menudatas[i].F_MenuName,
          icon: menudatas[i].F_ImgUrl,
          permission: []
        },
        children: []
      })
    } else {
      // 只有二级目录只有一个子菜单的时候给icon的值
      if (len === 1 && patch(menudatas[i].F_OptUrl) === 1) {
        mIcon = menudatas[i].F_ImgUrl
      }
      if (menudatas[i].childnodes.length >= 0) {
        cpath = menudatas[i].F_OptUrl
        mflag = menudatas[i].F_MenuCode
        if (cpath.indexOf(mflag) > -1) {
          furl = cpath.substring(0, cpath.indexOf(mflag)) + mflag
          if (menudatas[i].childnodes.length > 0) {
            fpath = ''
            redirect = 'noredirect'
          } else {
            redirect = undefined
            fpath = cpath.substring(0, cpath.indexOf(mflag)) + mflag
            mbtn = getButtons(menudatas[i].F_Id)// 获取最后一级 菜单对应的按钮
          }
        } else {
          // console.log(`请您检查菜单: ${ menudatas[i].F_MenuName }的菜单代码值和菜单链接!`)
        }
        // console.log(furl)
        // console.log(fpath)
      }
      accessedRouters.push({
        path: `${fpath}`,
        redirect: redirect,
        component: () => import(`@/views/${furl}/index`),
        name: menudatas[i].F_MenuCode,
        meta: {
          icon: mIcon,
          title: menudatas[i].F_MenuName,
          permission: mbtn
        },
        id: menudatas[i].F_Id
        // target: menudatas[i].F_Target,
        // stateFlag: menudatas[i].F_State,
        // remark: menudatas[i].F_Remark,
      })
    }
    if (menudatas[i].childnodes && menudatas[i].childnodes.length > 0) {
      accessedRouters[i].children = filterAsyncRouter(menudatas[i].childnodes)
    }
  }
  return accessedRouters
}

/**
 * 根据id获取button
 * mid 菜单id
 */
function getButtons(mid) {
  const btnArr = []
  let midStr = mid + ''
  if (buttonDatas) {
    for (const i in buttonDatas) {
      if (i === midStr) {
        if (buttonDatas[i] && buttonDatas[i].length > 0) {
          for (const j in buttonDatas[i]) {
            btnArr.push(buttonDatas[i][j].F_ButtonCode)
          }
        }
      }
    }
  }
  return btnArr
}

const permission = {
  state: {
    routers: constantRouterMap,
    addRouters: []
  },
  mutations: {
    SET_ROUTERS: (state, routers) => {
      state.addRouters = routers
      state.routers = constantRouterMap.concat(routers)
    }
  },
  actions: {
    GenerateRoutes({ commit }, data) {
      return new Promise(resolve => {
        const menuDatas = data.menuDatas
        buttonDatas = data.buttonDatas
        const accessedRouters = filterAsyncRouter(menuDatas)
        accessedRouters.push({ path: '*', redirect: '/404', hidden: true })
        // console.log(accessedRouters)
        commit('SET_ROUTERS', accessedRouters)
        resolve()
      })
    }
  }
}

// 判断 / 字符在整个字符串中出现的次数
function patch(s) {
  const re = s.match(/\//g)
  return s.match(re) ? s.match(re).length : 0
}

export default permission
