import { h } from 'vue'
import {
    HomeOutlined,
    ShoppingCartOutlined,
    GatewayOutlined,
    ExceptionOutlined
} from '@ant-design/icons-vue'
import { getCache } from '@/utils/authority';
const pages = [
    {
        icon: () => h(HomeOutlined),
        path: '',
        name: '首页',
        isMenu: true
    },
    {
        icon: () => h(ExceptionOutlined),
        path: '/BackManage',
        name: '反馈管理',
        children: [{
            path: '/BackSite',
            name: '停车点申请',
            isMenu: true
        }
        ]
    },
    {
        icon: () => h(ShoppingCartOutlined),
        path: '/Produce',
        name: '生产管理',
        children: [{
            path: '/CompProduce',
            name: '组件生产',
            isMenu: true
        },
        {
            path: '/BikeQRCode',
            name: '车辆二维码',
            isMenu: true
        },
        {
            path: '/BatteryQRCode',
            name: '电池二维码',
            isMenu: true
        },
        {
            path: '/HelmetQRCode',
            name: '头盔二维码',
            isMenu: true
        }
        ]
    },
    {
        icon: () => h(GatewayOutlined),
        path: '/Urban',
        name: '城市管理',
        children: [
            {
                path: '/OperateRegion',
                name: '运营区域',
                isMenu: true
            },
            {
                path: '/SiteManage',
                name: '站点列表',
                isMenu: true
            },
            {
                path: '/OperateRegionAdd',
                name: '运营区新增',
                isMenu: false
            }
        ]
    },
    {
        icon: () => h(GatewayOutlined),
        path: '/SysManage',
        name: '系统管理',
        children: [
            {
                path: '/OrgManage',
                name: '品牌商管理',
                isMenu: true
            },
            {
                path: '/PermManage',
                name: '菜单管理',
                isMenu: true
            },
            {
                path: '/RoleManage',
                name: '角色管理',
                isMenu: true
            },
            {
                path: '/AccountManage',
                name: '账号管理',
                isMenu: true
            }
        ]
    }
]

const modules = import.meta.glob('./views/**/*.vue');
const getRouters = () => {
    const routes = [];
    pages.forEach((item) => {
        let ipath = item.path;
        if (ipath === '') {
            ipath = '/Home';
        }
        const component = modules['./views' + ipath + '/index.vue'];
        const route = {
            path: item.path,
            name: item.name,
            component: component, // 动态导入组件
        };
        if (item.children) {
            item.children.forEach((child) => {
                let cpath = ipath + child.path;
                route.children = route.children || [];
                const component = modules['./views' + cpath + '/index.vue'];
                route.children.push({
                    path: cpath,
                    name: child.name,
                    component: component, // 动态导入组件 
                })
            })
        };
        routes.push(route);
    });
    return routes;
}

const getMenus = () => {
    const permcode = 'operate.web';
    const permcodes = getCache('ebike-permcodes');
    const menus = [];
    if (permcodes.length === 0) {
        return menus;
    }
    pages.forEach((item) => {
        let ipath = item.path;
        if (ipath === '') {
            ipath = '/';
        }
        let ikey = item.key;
        if (ikey === undefined) {
            ikey = ipath.replace(/\//g, '-').substring(1);
            if (ikey === '') {
                ikey = 'Home'
            }
        }
        const menu = {
            key: ikey,
            icon: item.icon,
            label: item.name,
            title: item.name,
            path: ipath
        }
        let ipermcode = permcode + '.' + ikey.toLowerCase().replace(/-/g, '.');
        if (permcodes.some(item => item.includes(ipermcode)) || ikey.toLowerCase() === 'home') {
            menus.push(menu)
            if (item.children) {
                item.children.forEach((child) => {
                    let cpath = ipath + child.path;
                    let ckey = child.key;
                    if (ckey === undefined) {
                        ckey = cpath.replace(/\//g, '-').substring(1);
                        if (ckey === '') {
                            ckey = 'Home'
                        }
                    }
                    menu.children = menu.children || [];
                    let cpermcode = permcode + '.' + ckey.toLowerCase().replace(/-/g, '.');
                    if (child['isMenu'] && permcodes.some(item => item.includes(cpermcode))) {
                        menu.children.push({
                            key: ckey,
                            label: child.name,
                            title: child.name,
                            path: cpath
                        })
                    }
                })
            }
        }
    });
    return menus;
}
export { getRouters, getMenus };