
/**头部路由一级菜单点击逻辑 */
function headrRouter(initialState: any, history: any) {
    let ok = true
    let u = navigator?.userAgent
    let isPhone = !!u.match(/AppleWebKit.*Mobile.*/) || u?.indexOf('iPad') > -1
    //登录时指向/辅助处理看当前用户的一级菜单有哪些，找到第一个符合的跳转
    initialState?.menu?.data.forEach((item: { roles: string[], path: string, routes: { path: string, routes: any[] }[] }) => {
        if (ok) {
            ok = false
            let path = item?.routes?.length > 0 ? item?.routes[0]?.routes?.length > 0 ? item?.routes[0]?.routes[0]?.path ? item?.routes[0]?.routes[0]?.path : item?.routes[0].path : item?.routes[0].path : item?.routes[0].path
            if (history?.location?.pathname === '/') {
                history.push(path)
            }
        }
    })
    if (history?.location?.pathname === '/launchSystemV3') { //当切换一级菜单的辅助跳转
        history?.goBack()
        initialState?.menu?.data.forEach((item: { roles: string[], path: string, routes: { path: string, routes: any[] }[] }) => {
            if (item?.routes?.some((i: { path: string }) => i.path?.includes('/launchSystemV3'))) {
                let path = item?.routes?.length > 0 ? (item?.routes[0]?.routes?.length > 0 && item?.routes[0]?.routes[0]?.path) ? item?.routes[0]?.routes[0]?.path : item?.routes[0].path : item?.routes[0].path
                isPhone ? history.push(path) : window.open(location.origin + '/#' + path)
            }
        })
    }
    if (history?.location?.pathname === '/launchSystemNew') { //当切换一级菜单的辅助跳转
        history?.goBack()
        initialState?.menu?.data.forEach((item: { roles: string[], path: string, routes: { path: string, routes: any[] }[] }) => {
            if (item?.routes?.some((i: { path: string }) => i.path?.includes('/launchSystemNew'))) {
                let path = item?.routes?.length > 0 ? (item?.routes[0]?.routes?.length > 0 && item?.routes[0]?.routes[0]?.path) ? item?.routes[0]?.routes[0]?.path : item?.routes[0].path : item?.routes[0].path
                isPhone ? history.push(path) : window.open(location.origin + '/#' + path)
            }
        })
    }
    if (history?.location?.pathname === '/toutiao') { //当切换一级菜单的辅助跳转
        history?.goBack()
        initialState?.menu?.data.forEach((item: { roles: string[], path: string, routes: { path: string, routes: any[] }[] }) => {
            if (item?.routes?.some((i: { path: string }) => i.path?.includes('/toutiao'))) {
                let path = item?.routes?.length > 0 ? (item?.routes[0]?.routes?.length > 0 && item?.routes[0]?.routes[0]?.path) ? item?.routes[0]?.routes[0]?.path : item?.routes[0].path : item?.routes[0].path
                isPhone ? history.push(path) : window.open(location.origin + '/#' + path)
            }
        })
    }
}
//返回按钮路由
function btnFun(items: any) {
    return items?.childrenBtn?.map((btn: any,) => {
        return { name: btn.meta.title, key: btn?.menuId }
    })
}
//返回子路由
function routeFun(items: any) {
    return items.children.map((route: any) => {//循环重组
        // console.log(route)
        if (route?.children?.length > 0) {
            let arr = routeFun(route)
            return { path: route.path, name: route.meta.title, component: route.component, routes: arr, icon: route.meta.icon, key: route?.menuId }
        }
        if (route?.childrenBtn?.length > 0) {
            let arr = btnFun(route)
            return { path: route.path, name: route.meta.title, component: route.component, routes: arr, icon: route.meta.icon, key: route?.menuId }
        }
        return { path: route.path, name: route.meta.title, component: route.component, icon: route.icon, key: route?.menuId }
    })
}
/**获取线上菜单并重组路由格式和权限*/
function getMyMenu(code: any, data: any,) {
    let newData: any = []
    // let roles = userInfo?.roles.map((role: any) => role.roleKey)//权限从个人信息中取，以防路由列表中有错误
    if (code === 200) {
        let new_data: any[] = []
        // 循环判断不为空的主菜单加入
        Object.keys(data)?.reverse()?.map((key) => {
            if (Array.isArray(data[key]) && data[key]?.length > 0) {
                new_data.push(data[key][0])
            }
        })
        new_data?.forEach((items: any) => {
            if (items?.children?.length === 0) {//假如路由的子路由只有一个代表当前组件没有子路由，将children中的component组件路径给外层component
                newData.push({ path: items.children[0].path, name: items.meta.title, icon: items.meta.icon, component: items.children[0].component, key: items.menuId })  // roles: roles
            }
            if (items?.children?.length >= 0) {//假如路由的子路由个数大于1代表有子路由
                let arr: any[] = routeFun(items)
                newData.push({ path: items.path, name: items.meta.title, icon: items.meta.icon, routes: arr, key: items.menuId })//最后添加进routes , roles: roles
            }
        })
    }
    return newData
}
const launchSystemV3 = {
    path: '/launchSystemV3',
    routes: [
        {
            path: '/launchSystemV3/adMonitorListV3',
            name: '广告监控',
            component: './launchSystemV3/adMonitorListV3',
            access: 'adMonitorListV3',
        },
        {
            path: '/launchSystemV3/monitorEWList',
            name: '企微客服组监控',
            component: './launchSystemV3/monitorEWList',
            access: 'monitorEWList',
        },
        {
            path: '/launchSystemV3/adqv3',
            name: '腾讯广告',
            component: './launchSystemV3/adqv3',
            access: 'adqv3',
        },
        {
            path: '/launchSystemV3/account',
            name: '广告账户管理',
            access: 'account',
            component: './launchSystemV3/account',
        },
        {
            path: '/launchSystemV3/tencentAdPutIn',
            name: '广告投放',
            access: 'tencentAdPutIn',
            routes: [
                {
                    name: '广告创建',
                    path: '/launchSystemV3/tencentAdPutIn/create',
                    access: 'create',
                    component: './launchSystemV3/tencentAdPutIn/create',
                },
                {
                    name: '任务列表',
                    path: '/launchSystemV3/tencentAdPutIn/taskList',
                    access: 'taskList',
                    component: './launchSystemV3/tencentAdPutIn/taskList',
                },
            ],
        },
        {
            path: '/launchSystemV3/tencenTasset',
            name: '资产',
            access: 'tencenTasset',
            routes: [
                {
                    name: '定向模板',
                    path: '/launchSystemV3/tencenTasset/targeting',
                    access: 'targeting',
                    component: './launchSystemV3/tencenTasset/targeting',
                },
                {
                    name: '品牌形象',
                    path: '/launchSystemV3/tencenTasset/brand',
                    access: 'brand',
                    component: './launchSystemV3/tencenTasset/brand',
                },
                {
                    name: '头像昵称跳转页',
                    path: '/launchSystemV3/tencenTasset/profiles',
                    access: 'profiles',
                    component: './launchSystemV3/tencenTasset/profiles',
                },
                {
                    name: '原生推广页',
                    path: '/launchSystemV3/tencenTasset/wechatCanvasPage',
                    access: 'wechatCanvasPage',
                    component: './launchSystemV3/tencenTasset/wechatCanvasPage',
                },
                {
                    name: '微信小程序',
                    path: '/launchSystemV3/tencenTasset/miniProgramWechat',
                    access: 'miniProgramWechat',
                    component: './launchSystemV3/tencenTasset/miniProgramWechat',
                },
                {
                    name: '企业微信',
                    path: '/launchSystemV3/tencenTasset/corpWechat',
                    access: 'corpWechat',
                    routes: [
                        {
                            name: '企业微信管理',
                            path: '/launchSystemV3/tencenTasset/corpWechat/manage',
                            access: 'manage',
                            component: './launchSystemV3/tencenTasset/corpWechat/manage',
                        },
                        {
                            name: '客服号轮换策略',
                            path: '/launchSystemV3/tencenTasset/corpWechat/userRotatePolicy',
                            access: 'userRotatePolicy',
                            component: './launchSystemV3/tencenTasset/corpWechat/userRotatePolicy',
                        },
                        {
                            name: '客服组',
                            path: '/launchSystemV3/tencenTasset/corpWechat/csgroup',
                            access: 'csgroup',
                            component: './launchSystemV3/tencenTasset/corpWechat/csgroup',
                        }
                    ]
                },
                {
                    name: '游戏库',
                    path: '/launchSystemV3/tencenTasset/game',
                    access: 'game',
                    component: './launchSystemV3/tencenTasset/game',
                },
                {
                    name: '账户资产共享',
                    path: '/launchSystemV3/tencenTasset/accountAssetSharing',
                    access: 'accountAssetSharing',
                    component: './launchSystemV3/tencenTasset/accountAssetSharing',
                },
                {
                    name: '文案库',
                    path: '/launchSystemV3/tencenTasset/copyWriting',
                    access: 'copyWriting',
                    component: './launchSystemV3/tencenTasset/copyWriting',
                },
                {
                    name: '推广应用',
                    path: '/launchSystemV3/tencenTasset/application',
                    access: 'application',
                    component: './launchSystemV3/tencenTasset/application',
                },
                {
                    name: '创意组件',
                    path: '/launchSystemV3/tencenTasset/manageComponent',
                    access: 'manageComponent',
                    component: './launchSystemV3/tencenTasset/manageComponent',
                }
            ],
        },
        {
            path: '/launchSystemV3/material',
            name: '素材库',
            access: 'material',
            icon: 'DatabaseOutlined',
            routes: [
                {
                    name: '本地素材',
                    path: '/launchSystemV3/material/cloud',
                    access: 'cloud',
                    component: './launchSystemNew/material/cloud',
                },
                {
                    name: '云端素材',
                    path: '/launchSystemV3/material/tencent',
                    access: 'tencent',
                    component: './launchSystemV3/material/tencent',
                },
                {
                    name: '素材库',
                    path: '/launchSystemV3/material/cloudNew',
                    access: 'cloud',
                    component: './launchSystemV3/material/cloudNew',
                },
            ],
        },
    ]
}
/** 投放系统 */
const launchSystem = {
    path: '/launchSystemNew',
    routes: [
        {
            path: '/launchSystemNew/adMonitorList',
            name: '广告监控',
            component: './adMonitor/adMonitorList',
            access: 'adMonitorList',
        },
        {
            path: '/launchSystemNew/account',
            name: '广告账户管理',
            access: 'account',
            routes: [
                {
                    name: '小说',
                    path: '/launchSystemNew/account/novel',
                    access: 'novel',
                    component: './launchSystemNew/account/novel',
                },
                {
                    name: '游戏',
                    path: '/launchSystemNew/account/game',
                    access: 'game',
                    component: './launchSystemNew/account/game',
                },
            ],
        },
        {
            path: '/launchSystemNew/adq',
            name: '腾讯广告',
            component: './launchSystemNew/adq',
            access: 'adq',
        },
        {
            path: '/launchSystemNew/sysWarningRule',
            name: '监控告警',
            component: './launchSystemNew/sysWarningRule',
            access: 'sysWarningRule',
        },
        {
            path: '/launchSystemNew/launchManage',
            name: '广告投放',
            access: 'launchManage',
            routes: [
                {
                    path: '/launchSystemNew/launchManage/localAd',
                    name: '广告模板',
                    access: 'localAd',
                    component: './launchSystemNew/launchManage/localAd',
                },
                {
                    path: '/launchSystemNew/launchManage/createAd',
                    name: '创建广告',
                    access: 'createAd',
                    component: './launchSystemNew/launchManage/createAd',
                },
                {
                    path: '/launchSystemNew/launchManage/taskList',
                    name: '任务列表',
                    access: 'taskList',
                    component: './launchSystemNew/launchManage/taskList',
                }
            ]
        },
        {
            path: '/launchSystemNew/material',
            name: '素材管理',
            access: 'material',
            icon: 'DatabaseOutlined',
            routes: [
                {
                    name: '本地素材',
                    path: '/launchSystemNew/material/cloud',
                    access: 'cloud',
                    component: './launchSystemNew/material/cloud',
                },
            ],
        },
        {
            path: '/launchSystemNew/adminSpecialUseConsume',
            name: '管理员拉消耗专用',
            component: './launchSystemNew/adminSpecialUseConsume',
            access: 'adminSpecialUseConsume',
        },
    ],

}

/** 头条 */
const adMonitor = {
    path: '/toutiao',
    routes: [
        {
            path: '/toutiao/ttAccountManage',
            name: '广告监控',
            component: './toutiao/ttAccountManage',
            access: 'ttAccountManage',
        },
    ]
}

export {
    headrRouter,
    getMyMenu,
    launchSystemV3,
    launchSystem,
    adMonitor
}