import { constantRoutes } from '@/router'
import { getRouters } from '@/api/menu'
import Layout from '@/layout/index'

const permission = {
    state: {
        routes: [],
        addRoutes: []
    },
    mutations: {
        SET_ROUTES: (state, routes) => {
            state.addRoutes = routes
            state.routes = constantRoutes.concat(routes)
        }
    },
    actions: {
        // 生成路由
        GenerateRoutes({ commit }) {
            return new Promise(resolve => {
                // 向后端请求路由数据
                getRouters().then(res => {
                    let arr = [
                        // {
                        //     path: 'business/Middleware/Subsystem',
                        //     component: () =>
                        //         import ('@/views/business/Middleware/Subsystem'),
                        //     hidden: true,
                        // },
                        {
                            component: "Layout",
                            hidden: false,
                            path: "/",
                            children: [{
                                component: "business/Middleware/subsystem",
                                hidden: true,
                                meta: { title: "子系统管理", icon: "cascader", noCache: true, link: null },
                                name: "Middleware/subsystem",
                                path: "Middleware/subsystem",
                            }]
                        },
                        {
                            component: "Layout",
                            hidden: false,
                            path: "/",
                            children: [{
                                component: "business/Middleware/functionalInterface",
                                hidden: true,
                                meta: { title: "功能接口管理", icon: "guide", noCache: true, link: null },
                                name: "Middleware/functionalInterface",
                                path: "Middleware/functionalInterface",
                            }]
                        },
                        {
                            component: "Layout",
                            hidden: false,
                            path: "/",
                            children: [{
                                component: "business/Middleware/hostEquipment",
                                hidden: true,
                                meta: { title: "主机设备管理", icon: "international", noCache: true, link: null },
                                name: "Middleware/hostEquipment",
                                path: "Middleware/hostEquipment",
                            }]
                        },
                        {
                            component: "Layout",
                            hidden: false,
                            path: "/",
                            children: [{
                                component: "business/Middleware/bpmn",
                                hidden: false,
                                meta: { title: "流程图", icon: "工作流程图", noCache: true, link: null },
                                name: "Middleware/bpmn",
                                path: "Middleware/bpmn",
                            }]
                        },
                        {
                            component: "Layout",
                            hidden: false,
                            path: "/",
                            children: [{
                                component: "business/Middleware/bpmn/customModeler",
                                hidden: true,
                                meta: { title: "流程图绘制", icon: "international", noCache: true, link: null },
                                name: "Middleware/bpmn/customModeler",
                                path: "Middleware/bpmn/customModeler",
                            }]
                        },
                        {
                            component: "Layout",
                            hidden: false,
                            path: "/",
                            children: [{
                                component: "business/Middleware/bpmn/executionRecord",
                                hidden: false,
                                meta: { title: "历史流程图执行记录", icon: "log", noCache: true, link: null },
                                name: "Middleware/bpmn/executionRecord",
                                path: "Middleware/bpmn/executionRecord",
                            }]
                        },
                        {
                            component: "Layout",
                            hidden: false,
                            path: "/",
                            children: [{
                                component: "business/Middleware/bpmn/executionLog",
                                hidden: true,
                                meta: { title: "历史流程图执行日志", icon: "log", noCache: true, link: null },
                                name: "Middleware/bpmn/executionLog",
                                path: "Middleware/bpmn/executionLog",
                            }]
                        },




                        // {
                        //   path: '/Middleware/',
                        //   component: Layout,
                        //   meta: {
                        //     icon: "system",
                        //     link: null,
                        //     noCache: false,
                        //     title: "中间件管理",
                        //     name: "Middleware",
                        //     path: "/business/Middleware",
                        //     redirect: "noRedirect"
                        //   },
                        //   children: [
                        //     {
                        //       path: 'Subsystem',
                        //       name: 'Subsystem',
                        //       component: () =>
                        //       import ('@/views/business/Middleware/Subsystem/index'),
                        //       meta: { title: '子系统管理', icon: 'home' }
                        //     },
                        //     {
                        //       path: 'functionalInterface',
                        //       name: 'functionalInterface',
                        //       component: () =>
                        //       import ('@/views/business/Middleware/functionalInterface/index'),
                        //       meta: { title: '功能接口管理', icon: 'home' }
                        //     }
                        //   ]
                        // }
                    ]
                    const accessedRoutes = filterAsyncRouter(arr)
                        //const accessedRoutes = filterAsyncRouter(res.data)

                    accessedRoutes.push({ path: '*', redirect: '/404', hidden: true })
                    commit('SET_ROUTES', accessedRoutes)
                    resolve(accessedRoutes)
                })
            })
        }
    }
}

// 遍历后台传来的路由字符串，转换为组件对象
function filterAsyncRouter(asyncRouterMap) {
    return asyncRouterMap.filter(route => {
        if (route.component) {
            // Layout组件特殊处理
            if (route.component === 'Layout') {
                route.component = Layout
            } else {
                route.component = loadView(route.component)
            }
        }
        if (route.children != null && route.children && route.children.length) {
            route.children = filterAsyncRouter(route.children)
        }
        return true
    })
}

export const loadView = (view) => { // 路由懒加载
    return (resolve) => require([`@/views/${view}`], resolve)
}

export default permission