import * as VueRouter from 'vue-router'
import { useUserStore } from '@/store'
import { isElectron } from '../utils/electron'
const DEFAULT_LAYOUT = () => import('@/layout/default-layout.vue')
const HTML_VIEW = () => import('@/layout/html-view.vue')
// import { Message } from '@arco-design/web-vue'

const routes = [
    {
        path: "/",
        name: "main",
        component: () => import('../pages/Main/Main.vue'),
        meta: {
            hideInMenu: true,
            onlyWeb: true
        }
    },
    {
        path: "/faceReco",
        name: "faceReco",
        component: () => import('../pages/face/index.vue'),
        meta: {
            hideInMenu: true,
            onlyWeb: true,
            viewport: 'width=device-width, initial-scale=1'
        }
    },
    {
        path: "/login",
        name: "login",
        component: () => import('../pages/login/login.vue'),
        meta: {
            title: '用户登录',
            hideInMenu: true
        }
    },
    {
        path: "/proxy",
        name: "proxy",
        component: DEFAULT_LAYOUT,
        meta: {
            title: '乐跑登录',
            icon: 'icon-command',
            onlyElectron: true
        },
        children: [
            {
                path: 'openProxy',
                name: 'proxy.openProxy',
                component: () => import('../pages/openProxy/index.vue'),
                meta: {
                    title: '启动登录器'
                }
            }
        ]
    },
    {
        path: "/htmlView",
        name: "htmlView",
        component: HTML_VIEW,
        meta: {
            title: '第三方页面',
            hideInMenu: true
        },
        children: [
            {
                path: 'view',
                name: 'htmlView.view',
                component: () => import('../pages/htmlView/index.vue'),
                meta: {
                    title: '第三方页面'
                }
            }
        ]
    },
    {
        path: "/store",
        name: 'store',
        redirect: '/store/goodsList',
        component: DEFAULT_LAYOUT,
        meta: {
            title: '云商城',
            icon: 'icon-gift'
        },
        children: [
            {
                path: 'goodsList',
                name: 'store.goodsList',
                component: () => import('../pages/store/goodsList/index.vue'),
                meta: {
                    title: '商品列表'
                }
            },
            {
                path: 'goodsDetail/:id',
                name: 'store.goodsDetail',
                component: () => import('../pages/store/goodsDetail/index.vue'),
                meta: {
                    title: '商品详情',
                    hideInMenu: true
                }
            },
            {
                path: 'orderDetail/:id',
                name: 'store.orderDetail',
                component: () => import('../pages/store/orders/orderDetail/index.vue'),
                meta: {
                    title: '订单详情',
                    hideInMenu: true
                }
            },
            {
                path: 'myOrder',
                name: 'store.myOrder',
                component: () => import('../pages/store/orders/orderList/index.vue'),
                meta: {
                    title: '我的订单'
                }
            },
        ]
    },
    {
        path: "/lepao",
        name: 'lepao',
        redirect: '/lepao/accountList',
        component: DEFAULT_LAYOUT,
        meta: {
            title: '校园乐跑',
            icon: 'icon-dashboard'
        },
        children: [
            {
                path: 'accountList',
                name: 'lepao.accountList',
                component: () => import('../pages/lepao/accountList/index.vue'),
                meta: {
                    title: '乐跑账号'
                }
            },
            {
                path: 'lepaoRecords',
                name: 'lepao.lepaoRecords',
                component: () => import('../pages/lepao/lepaoRecords/index.vue'),
                meta: {
                    title: '乐跑记录'
                }
            },
            {
                path: 'recordDetail/:id',
                name: 'lepao.recordDetail',
                component: () => import('../pages/lepao/lepaoRecords/recordDetail.vue'),
                meta: {
                    title: '路线详情',
                    hideInMenu: true
                }
            }
        ]
    },
    {
        path: "/qxs",
        name: "qxs",
        component: DEFAULT_LAYOUT,
        meta: {
            title: '趣选书',
            icon: 'icon-book'
        },
        children: [
            {
                path: 'getBookList',
                name: 'qxs.getBookList',
                component: () => import('../pages/qxs/getBookList.vue'),
                meta: {
                    title: '书单查询'
                }
            }
        ]
    },
    {
        path: "/power",
        name: "power",
        component: DEFAULT_LAYOUT,
        meta: {
            title: '宿舍电费',
            icon: 'icon-thunderbolt'
        },
        children: [
            {
                path: 'accountList',
                name: 'power.accountList',
                component: () => import('../pages/power/accountList.vue'),
                meta: {
                    title: '定制电费提醒'
                }
            }
        ]
    },
    {
        path: "/service",
        name: 'service',
        redirect: '/service/orderList',
        component: DEFAULT_LAYOUT,
        meta: {
            title: '售后服务',
            icon: 'icon-customer-service'
        },
        children: [
            {
                path: 'createOrder',
                name: 'service.createOrder',
                component: () => import('../pages/service/createOrder.vue'),
                meta: {
                    title: '提交工单'
                }
            },
            {
                path: 'orderList',
                name: 'service.orderList',
                component: () => import('../pages/service/orderList.vue'),
                meta: {
                    title: '我的工单'
                }
            },
            {
                path: 'orderDetail/:id',
                name: 'service.orderDetail',
                component: () => import('../pages/service/orderDetail.vue'),
                meta: {
                    title: '工单详情',
                    hideInMenu: true
                }
            }
        ]
    },
    // {
    //     path: "/download",
    //     name: "download",
    //     component: DEFAULT_LAYOUT,
    //     meta: {
    //         title: '下载专区',
    //         onlyWeb: true,
    //         icon: 'icon-download'
    //     },
    //     children: [
    //         {
    //             path: 'down',
    //             name: 'download.down',
    //             component: () => import('../pages/download/index.vue'),
    //             meta: {
    //                 title: '客户端下载'
    //             }
    //         }
    //     ]
    // },
    {
        path: "/admin",
        name: 'admin',
        redirect: '/admin/service/orderList',
        component: DEFAULT_LAYOUT,
        meta: {
            title: '网站管理',
            icon: 'icon-computer',
            permission: ['admin', 'service', 'product']
        },
        children: [
            {
                path: 'userList',
                name: 'admin.userList',
                component: () => import('../pages/admin/user/userList.vue'),
                meta: {
                    title: '用户管理',
                    permission: ['admin', 'userList']
                }
            },
            {
                path: 'lepaoAccount',
                name: 'admin.lepaoAccount',
                component: () => import('../pages/admin/lepaoAccount/accountList.vue'),
                meta: {
                    title: '乐跑账号管理',
                    permission: ['admin', 'lepaoAccount']
                }
            },
            {
                path: 'lepaoRecords',
                name: 'admin.lepaoRecords',
                component: () => import('../pages/admin/lepaoRecords/lepaoRecords.vue'),
                meta: {
                    title: '乐跑记录',
                    permission: ['admin', 'service']
                }
            },
            {
                path: 'lepaoRecords/:id',
                name: 'admin.lepaoRecords.detail',
                component: () => import('../pages/admin/lepaoRecords/recordDetail.vue'),
                meta: {
                    title: '乐跑记录详情',
                    hideInMenu: true,
                    permission: ['admin', 'service']
                }
            },
            {
                path: 'service/orderList',
                name: 'admin.service.orderList',
                component: () => import('../pages/admin/workOrder/orderList.vue'),
                meta: {
                    title: '工单管理',
                    permission: ['admin', 'service']
                }
            },
            {
                path: 'service/orderDetail/:id',
                name: 'admin.service.orderDetail',
                component: () => import('../pages/admin/workOrder/orderDetail.vue'),
                meta: {
                    title: '工单详情',
                    hideInMenu: true,
                    permission: ['admin', 'service']
                }
            },
            {
                path: 'reqLog',
                name: 'admin.log.reqLog',
                component: () => import('../pages/admin/reqLog/index.vue'),
                meta: {
                    title: '请求日志',
                    permission: ['admin']
                }
            }
        ]
    },
    {
        path: '/goodsManage',
        name: 'goodsManage',
        component: DEFAULT_LAYOUT,
        meta: {
            title: '商品管理',
            icon: 'icon-apps',
            permission: ['admin', 'product']
        },
        children: [
            {
                path: 'goods/addGoods',
                name: 'admin.goods.addGoods',
                component: () => import('../pages/admin/goods/addGoods.vue'),
                meta: {
                    title: '添加商品',
                    permission: ['admin', 'product']
                }
            },
            {
                path: 'goods/addGoods/:id',
                name: 'admin.goods.editGoods',
                component: () => import('../pages/admin/goods/addGoods.vue'),
                meta: {
                    title: '编辑商品',
                    hideInMenu: true,
                    permission: ['admin', 'product']
                }
            },
            {
                path: 'goods/goodsList',
                name: 'admin.goods.goodsList',
                component: () => import('../pages/admin/goods/goodsList.vue'),
                meta: {
                    title: '商品管理',
                    permission: ['admin', 'product']
                }
            }
        ]
    },
    {
        path: "/path",
        name: "path",
        redirect: '/path/list',
        component: DEFAULT_LAYOUT,
        meta: {
            title: '路径数据',
            icon: 'icon-location',
            permission: ['admin', 'path']
        },
        children: [
            {
                path: 'list',
                name: 'path.list',
                component: () => import('../pages/path/pathList.vue'),
                meta: {
                    title: '路径列表'
                }
            },
            {
                path: 'detail/:id',
                name: 'path.detail',
                component: () => import('../pages/path/pathDetail.vue'),
                meta: {
                    title: '路径详情',
                    hideInMenu: true
                }
            }
        ]
    },
    {
        path: "/user",
        name: "user",
        redirect: '/user/setting',
        component: DEFAULT_LAYOUT,
        meta: {
            title: '个人中心',
            icon: 'icon-user'
        },
        children: [
            {
                path: 'setting',
                name: 'user.setings',
                component: () => import('../pages/user/setting/index.vue'),
                meta: {
                    title: '用户设置'
                },
            }
        ]
    }
]

const router = VueRouter.createRouter({
    history: VueRouter.createWebHashHistory(),
    routes: routes
})

const allow = ['/', '/login', '/qxs/getBookList', '/htmlView/view', '/download/down', '/faceReco']

router.beforeEach(async (to, from, next) => {
    if (!allow.includes(to.path)) {
        const userStore = useUserStore()
        let user = await userStore.getInfo()

        if (!user || !user.uuid || !user.session) {
            // Message.error('请先登录')
            return router.push(`/login?from=${to.path}`)
        }

        if (to.meta && to.meta.permission && to.meta.permission.length > 0) {
            if (!to.meta.permission.some((perm) => user.roles.includes(perm)))
                return next('/')
        }
    }

    if (to.meta.viewport) {
        const meta = document.querySelector('meta[name="viewport"]')
        meta.setAttribute('content', to.meta.viewport)
    }

    if (to.meta && (to.meta.onlyWeb || to.meta.onlyElectron)) {
        const electronEnv = isElectron()
        if (to.meta.onlyWeb && electronEnv) return next('/proxy/openProxy')
        if (to.meta.onlyElectron && !electronEnv) return next('/lepao/accountList')
    }

    if (!to.meta.title) document.title = 'RunForge - 智能校园乐跑平台'
    else document.title = to.meta.title + ' - RunForge'

    next()
})

export { routes, router }
