/**头部路由一级菜单点击逻辑 */
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 === '/operatePage') { //当切换一级菜单的辅助跳转
        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('/operatePage'))) {
                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
                // history.push(path)
                isPhone ? history.push(path) : window.open(location.origin+'/#'+path) 
                // window.open(location.origin+'/#'+path)
            }
        })

        // localStorage.setItem('bg', 'light')
    }
    if (history?.location?.pathname === '/dataStatistics') { //当切换一级菜单的辅助跳转
        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('/dataStatistics'))) {
                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
                // history.push(path)
                // window.open(location.origin+'/#'+path)
                isPhone ? history.push(path) : window.open(location.origin+'/#'+path) 
            }
        })
        // localStorage.setItem('bg', 'dark')
    }
    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
                // history.push(path)
                // window.open(location.origin+'/#'+path)
                isPhone ? history.push(path) : window.open(location.origin+'/#'+path) 
            }
        })
        // localStorage.setItem('bg', 'dark')
    }
    if (history?.location?.pathname === '/enterpriseWeChat') { //当切换一级菜单的辅助跳转
        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('/enterpriseWeChat'))) {
                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
                // history.push(path)
                // window.open(location.origin+'/#'+path)
                isPhone ? history.push(path) : window.open(location.origin+'/#'+path) 
            }
        })
        // localStorage.setItem('bg', 'dark')
    }
}
//返回按钮路由
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) => {//循环重组
        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: items.meta.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 = new_data.sort((a, b) => {
            let v1 = a.orderNum
            let v2 = b.orderNum
            return v1 - v2
        })
        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
}
/**本地运营路由配置 */
let operatePage = {
    path: '/operatePage',
    access: 'operatePage',
    name: '运营',
    routes: [
        {
            path: '/operatePage/account/wxcode',
            layout: false,
            routes: [
                {
                    name: 'wxcode',
                    path: '/operatePage/account/wxcode',
                    component: './operatePage/account/wxcode',
                },
            ],
        },
        {
            path: '/operatePage/operate',
            name: '运营中心',
            access: 'operate',
            icon: 'DesktopOutlined',
            component: './operatePage/operate',
        },
        {
            path: '/operatePage/interact',
            name: '智能互动',
            access: 'interact',
            icon: 'MessageOutlined',
            routes: [
                {
                    name: '智能推送',
                    path: '/operatePage/interact/push',
                    access: 'push',
                    component: './operatePage/interact/push',
                },
                {
                    name: '关注回复',
                    path: '/operatePage/interact/reply',
                    access: 'reply',
                    component: './operatePage/interact/reply',
                },
                {
                    name: '关键字回复',
                    path: '/operatePage/interact/keyword',
                    access: 'keyword',
                    component: './operatePage/interact/keyword',
                },
                {
                    name: '自定义菜单',
                    path: '/operatePage/interact/custommenu',
                    access: 'custommenu',
                    component: './operatePage/interact/custommenu',
                }
            ]
        },
        {
            path: '/operatePage/news',
            name: '群发推送',
            access: 'news',
            icon: 'SendOutlined',
            routes: [
                {
                    name: '客服消息',
                    path: '/operatePage/news/aservice',
                    access: 'aservice',
                    component: './operatePage/news/aservice',
                },
                {
                    name: '模板消息',
                    path: '/operatePage/news/atemplate',
                    access: 'atemplate',
                    component: './operatePage/news/atemplate',
                },
                {
                    name: '高级群发',
                    path: '/operatePage/news/advanced',
                    access: 'advanced',
                    component: './operatePage/news/advanced',
                }
            ]
        },
        {
            path: '/operatePage/fans',
            name: '粉丝管理',
            access: 'fans',
            icon: 'TeamOutlined',
            routes: [
                {
                    name: '粉丝消息',
                    path: '/operatePage/fans/message',
                    access: 'message',
                    component: './operatePage/fans/message',
                },
                {
                    name: '粉丝列表',
                    path: '/operatePage/fans/list',
                    access: 'list',
                    component: './operatePage/fans/list',
                },
                {
                    name: '粉丝标签',
                    path: '/operatePage/fans/tag',
                    access: 'tag',
                    component: './operatePage/fans/tag',
                }
            ]
        },
        {
            path: '/operatePage/material',
            name: '素材管理',
            access: 'material',
            icon: 'DatabaseOutlined',
            routes: [
                {
                    name: '本地素材',
                    path: '/operatePage/material/cloud',
                    access: 'cloud',
                    component: './operatePage/material/cloud',
                },
                {
                    name: '微信素材',
                    path: '/operatePage/material/weChat',
                    access: 'weChat',
                    component: './operatePage/material/weChat',
                }
            ],
        },
        {
            path: '/operatePage/account',
            name: '公众号管理',
            access: 'account',
            icon: 'QrcodeOutlined',
            component: './operatePage/account',
        },
        {
            path: '/operatePage/weMenu',
            name: '公众号菜单管理',
            access: 'weMenu',
            icon: 'MobileOutlined',
            component: './operatePage/weMenu',
        },
        {
            path: '/operatePage/books',
            name: '小说平台管理',
            access: 'books',
            icon: 'ReadOutlined',
            routes: [
                {
                    name: '小说平台管理',
                    path: '/operatePage/books/bookcity',
                    access: 'bookcity',
                    component: './operatePage/books/bookcity',
                },
                {
                    name: '小说分类管理',
                    path: '/operatePage/books/bookify',
                    access: 'bookify',
                    component: './operatePage/books/bookify',
                },
                {
                    name: '书籍管理',
                    path: '/operatePage/books/book',
                    access: 'book',
                    component: './operatePage/books/book',
                },
            ],
        },
        {
            path: '/operatePage/automation',
            name: '自动化管理',
            access: 'automation',
            icon: 'ReadOutlined',
            routes: [
                {
                    name: '自动化创建',
                    path: '/operatePage/automation/create',
                    access: 'automationCreate',
                    component: './operatePage/automation/create',
                },
                {
                    name: '书城链接',
                    path: '/operatePage/automation/links',
                    access: 'automationLinks',
                    component: './operatePage/automation/links',
                },
            ],
        },
        {
            path: '/operatePage/advertisements',
            name: '广告',
            access: 'advertisements',
            icon: 'DesktopOutlined',
            routes: [
                {
                    name: '广告变现说明',
                    path: '/operatePage/advertisements/course',
                    access: 'course',
                    component: './operatePage/advertisements/course'
                },
                {
                    name: '广告数据统计',
                    path: '/operatePage/advertisements/dataCensus',
                    access: 'course',
                    component: './operatePage/advertisements/dataCensus'
                },
            ],
        },
        {
            path: '/operatePage/examine',
            name: '消息审查',
            access: 'examine',
            icon: 'DesktopOutlined',
            routes: [
                {
                    name: '高级群发',
                    path: '/operatePage/examine/examineAdvanced',
                    access: 'examineAdvanced',
                    component: './operatePage/examine/examineAdvanced'
                },
                {
                    name: '客服消息',
                    path: '/operatePage/examine/examineAservice',
                    access: 'examineAservice',
                    component: './operatePage/examine/examineAservice'
                },
                {
                    name: '模板消息',
                    path: '/operatePage/examine/examineAtemplate',
                    access: 'examineAtemplate',
                    component: './operatePage/examine/examineAtemplate'
                },
                {
                    name: '立即/延迟消息',
                    path: '/operatePage/examine/examineInteract',
                    access: 'examineInteract',
                    component: './operatePage/examine/examineInteract'
                },
            ],
        },
        {
            path: '/operatePage/accountyyb',
            name: '账号管理',
            access: 'accountyyb',
            icon: 'ReadOutlined',
            component: './operatePage/accountyyb',
        },
        {
            path: '/operatePage/kyyBoxManage',
            name: '快应用包管理',
            access: 'kyyBoxManage',
            icon: 'ReadOutlined',
            component: './operatePage/kyyBoxManage',
        },
        {
            path: '/operatePage/apitest',
            name: 'api测试',
            access: 'apitest',
            icon: 'ReadOutlined',
            component: './operatePage/apitest',
        },
    ]
}
/**本地数据中心路由配置 */
const dataStatistics = {
    path: '/dataStatistics',
    routes: [
        {
            path: '/dataStatistics/weChat',
            name: '公众号数据',
            access: 'weChat',
            routes: [
                {
                    path: '/dataStatistics/weChat/weChatList',
                    name: '公众号总数据',
                    access: 'weChatList',
                    component: './dataStatistics/weChat/weChatList',
                },
                {
                    path: '/dataStatistics/weChat/advertising',
                    name: '广告投放数据',
                    access: 'advertising',
                    component: './dataStatistics/weChat/advertising',
                },
                {
                    path: '/dataStatistics/weChat/ChannelAnalysis',
                    name: '公众号用户充值查询',
                    access: 'ChannelAnalysis',
                    component: './dataStatistics/weChat/ChannelAnalysis',
                },
                {
                    path: '/dataStatistics/weChat/todayRanking',
                    name: '今日消耗排行',
                    access: 'todayRanking',
                    component: './dataStatistics/weChat/todayRanking',
                },
                {
                    path: '/dataStatistics/weChat/sevenDay',
                    name: '公众号7日复充趋势',
                    access: 'sevenDay',
                    component: './dataStatistics/weChat/sevenDay',
                },
                {
                    path: '/dataStatistics/weChat/payFans',
                    name: '公众号付费粉丝激活数据',
                    access: 'payFans',
                    component: './dataStatistics/weChat/payFans',
                },
                {
                    path: '/dataStatistics/weChat/payTrend',
                    name: '公众号付费趋势表',
                    access: 'payTrend',
                    component: './dataStatistics/weChat/payTrend',
                },
                {
                    path: '/dataStatistics/weChat/payTrendCom',
                    name: '公众号付费趋势(含赔付金)',
                    access: 'payTrend',
                    component: './dataStatistics/weChat/payTrendCom',
                },
                {
                    path: '/dataStatistics/weChat/osoRoi',
                    name: '公众号回收查询',
                    access: 'osoRoi',
                    component: './dataStatistics/weChat/osoRoi',
                },
            ]
        },
        {
            path: '/dataStatistics/pitcher',
            name: '投手数据',
            access: 'pitcher',
            routes: [
                {
                    path: '/dataStatistics/pitcher/pitcherPay',
                    name: '投手付费趋势表',
                    access: 'pitcherPay',
                    component: './dataStatistics/pitcher/pitcherPay',
                },
                {
                    path: '/dataStatistics/pitcher/dataCenter',
                    name: '投手概况',
                    access: 'dataCenter',
                    component: './dataStatistics/pitcher/dataCenter',
                },
                {
                    path: '/dataStatistics/pitcher/overview',
                    name: '投手总数据',
                    access: 'overview',
                    component: './dataStatistics/pitcher/overview',
                },
                {
                    path: '/dataStatistics/pitcher/pitcherChannel',
                    name: '投手投放号',
                    access: 'pitcherChannel',
                    component: './dataStatistics/pitcher/pitcherChannel',
                },
                {
                    path: '/dataStatistics/pitcher/recoveryQuery',
                    name: '投手回收查询',
                    access: 'recoveryQuery',
                    component: './dataStatistics/pitcher/recoveryQuery',
                },
            ]
        },
        {
            path: '/dataStatistics/book',
            name: '小说数据',
            access: 'novel',
            routes: [
                {
                    path: '/dataStatistics/book/payTrend',
                    name: '小说付费趋势',
                    access: 'payTrend',
                    component: './dataStatistics/book/payTrend',
                },
                {
                    path: '/dataStatistics/book/summarizing',
                    name: '小说总数据',
                    access: 'summarizing',
                    component: './dataStatistics/book/summarizing',
                },
            ]
        },
        {
            path: '/dataStatistics/imageRank',
            name: '素材数据',
            access: 'imageRank',
            routes: [
                {
                    path: '/dataStatistics/imageRank/imageLibrary',
                    name: '素材库',
                    access: 'imageLibrary',
                    component: './dataStatistics/imageRank/imageLibrary',
                },
                {
                    path: '/dataStatistics/imageRank/adCost',
                    name: '素材消耗排行榜',
                    access: 'adCost',
                    component: './dataStatistics/imageRank/adCost',
                },
            ]
        },
        {
            path: '/dataStatistics/rechargeMonitor',
            name: '充值监控',
            access: 'rechargeMonitor',
            routes: [
                {
                    path: '/dataStatistics/rechargeMonitor/rechargeHis',
                    name: '充值历史',
                    access: 'rechargeHis',
                    component: './dataStatistics/rechargeMonitor/rechargeHis',
                },
                {
                    path: '/dataStatistics/rechargeMonitor/rechargeHisYear',
                    name: '充值历史(年)',
                    access: 'rechargeHisYear',
                    component: './dataStatistics/rechargeMonitor/rechargeHisYear',
                },
            ]
        },
        {
            path: '/dataStatistics/allSurvey',
            name: '整体概况',
            access: 'allSurvey',
            component: './dataStatistics/allSurvey',
        },
    ]
}
/** 投放系统 */
const launchSystem = {
    path: '/launchSystemNew',
    routes: [
        {
            path: '/launchSystemNew/launchManage',
            name: '广告投放',
            access: 'launchManage',
            routes: [
                {
                    path: '/launchSystemNew/launchManage/adAuthorize',
                    name: '授权',
                    access: 'adAuthorize',
                    component: './launchSystemNew/launchManage/adAuthorize',
                },
                // {
                //     path: '/launchSystemNew/launchManage/weChat',
                //     name: '微信',
                //     access: 'weChat',
                //     component: './launchSystemNew/launchManage/weChat',
                // },
                // {
                //     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',
                },
            ],
        },
    ]
}
/** 企业微信 */
const enterpriseWeChat = {
    path: '/enterpriseWeChat',
    routes: [
        {
            path: '/enterpriseWeChat/userManage',
            name: '账号管理',
            access: 'enterpriseWeChatUserManage',
            component: './enterpriseWeChat/userManage'
        },
        {
            path: '/enterpriseWeChat/complaint',
            name: '投诉信息',
            access: 'enterpriseWeChatComplaint',
            component: './enterpriseWeChat/complaint'
        },
    ]
}

export {
    headrRouter,
    getMyMenu,
    operatePage,
    dataStatistics,
    launchSystem,
    enterpriseWeChat
}