
import { $t } from '@/locales'
// 静态路由配置
// 书写格式与动态路由格式一致，全部经由框架统一转换
// 比较动态路由在meta中多加入了role角色权限，为数组类型。一个菜单是否有权限显示，取决于它以及后代菜单是否有权限。
// routes 显示在左侧菜单中的路由(显示顺序在动态路由之前)
// 示例如下
const routes = [
    {
		name: "userManage",
		path: "/userManage",
		meta: {
			icon: "sc-icon-user",
			title: $t('menu.userManage'),
			role: ["YHGL"]
		},
		children: [{
			name: "userList",
			path: "/userManage/userList",
			component: "userManage/userList",
			meta: {
				icon: "sc-icon-user-list",
				title: $t('menu.userList'),
				role: ["YHLB"]
			}
		},
        {
			name: "roleList",
			path: "/userManage/roleList",
			component: "userManage/roleList",
			meta: {
				icon: "sc-icon-user-role",
				title: $t('menu.roleList'),
				role: ["PTJS"]
			}
		},
        {
			name: "authority",
			path: "/userManage/authority",
			component: "userManage/authority",
			meta: {
				icon: "sc-icon-user-authority",
				title: $t('menu.authority'),
				role: ["QXLB"]
			}
		}
        ]
	},
	{
		name: "adminManage",
		path: "/adminManage",
		meta: {
			icon: "sc-icon-admin",
			title: $t('menu.adminManage'),
			role: ["HTXTGL"]
		},
		children: [{
			name: "adminList",
			path: "/adminManage/adminList",
			component: "adminManage/adminList",
			meta: {
				icon: "sc-icon-admin-list",
				title: $t('menu.adminList'),
				role: ["HTGLYLB"]
			}
		},
        {
			name: "adminRole",
			path: "/adminManage/adminRole",
			component: "adminManage/adminRole",
			meta: {
				icon: "sc-icon-admin-role",
				title: $t('menu.adminRole'),
				role: ["HTGLYJS"]
			}
		},
        {
			name: "adminAuthority",
			path: "/adminManage/adminAuthority",
			component: "adminManage/adminAuthority",
			meta: {
				icon: "sc-icon-admin-authority",
				title: $t('menu.adminAuthority'),
				role: ["HTQXLB"]
			}
		}
        ]
	},
    {
		name: "deviceManage",
		path: "/deviceManage",
		meta: {
			icon: "sc-icon-device",
			title: $t('menu.deviceManage'),
			role: ["SBGL"]
		},
		children: [{
			name: "deviceList",
			path: "/deviceManage/deviceList",
			component: "deviceManage/deviceList",
			meta: {
				icon: "sc-icon-device-list",
				title: $t('menu.deviceList'),
				role: ["SBLB"]
			}
		},
        {
			name: "deviceTypeList",
			path: "/deviceManage/deviceTypeList",
			component: "deviceManage/deviceTypeList",
			meta: {
				icon: "sc-icon-device-type-list",
				title: $t('menu.deviceTypeList'),
				role: ["SBLXLB"]
			}
		}
        ]
	},
    {
		name: "organizationManage",
		path: "/organizationManage",
		meta: {
			icon: "sc-icon-hospital",
			title: $t('menu.organizationManage'),
			role: ["JGGL"]
		},
		children: [{
			name: "organizationList",
			path: "/organizationManage/organizationList",
			component: "organizationManage/organizationList",
			meta: {
				icon: "sc-icon-hospital-list",
				title: $t('menu.organizationList'),//"机构列表",
				role: ["JGLB"]
			}
		}
        ]
	}
]

export default {
    getRouteList: function (selectedRoutes,roles) {
		if (!selectedRoutes)
		{
			selectedRoutes = routes;
		}
		if (roles)
        {
            if (roles.length === 1 && roles[0] === 'admin')
            {
                return selectedRoutes;
            }
            else
            {
                var menu = [];
                //过滤
                selectedRoutes.forEach(route => {
                    const tmp = { ...route }
                    if (this.hasPermission(roles, tmp)) {
                        if (tmp.children) {
                            tmp.children = this.getRouteList(tmp.children, roles)
                        }
                        menu.push(tmp)
                    }
                })     
                return menu;
                
            }
        }
        else
        {
            return [];
        }
	},
    hasPermission: function (roles, route) {
        if (roles === null || roles === undefined || roles.length === 0) {
            return false
          }
          if (roles[0] === '') {
            return false
          }
          if (route.meta && route.meta.roles) {
            return roles.some(role => route.meta.roles.includes(role))
          } else {
            return true
        }
    }
};
