import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { http } from '@/plugins/http'
const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: '/login',
            name: 'login',
            component: () => import('../views/LoginView.vue')
        },
        {
            path: '/',
            redirect: '/home',
            component: () => import('../views/MainView.vue'),
            children: [
                {
                    path: '404',
                    name: '404',
                    component: () => import('../views/NotFoundView.vue'),
                    meta: {
                        title: '页面不存在'
                    }
                },
                {
                    path: 'home',
                    name: 'home',
                    component: () => import('../views/HomeView.vue'),
                    meta: {
                        title: '主页'
                    }
                },
                {
                    path: 'user',
                    name: 'user',
                    component: () => import('../views/UserView.vue'),
                    meta: {
                        title: '用户列表'
                    }
                },
                {
                    path: 'properties',
                    name: 'properties',
                    component: () => import('../views/PropertiesView.vue'),
                    meta: {
                        title: '参数设置'
                    }
                },
                {
                    path: 'series',
                    name: 'series',
                    component: () => import('../views/SeriesView.vue'),
                    meta: {
                        title: '剧集管理'
                    }
                },
                {
                    path: 'categories',
                    name: 'categories',
                    component: () => import('../views/CategoriesView.vue'),
                    meta: {
                        title: '分类列表'
                    }
                },
                {
                    path: 'banners',
                    name: 'banners',
                    component: () => import('../views/BannersView.vue')
                },
                {
                    path: 'rat',
                    name: 'rat',
                    component: () => import('../views/RatView.vue'),
                    meta: {
                        title: '远程管理'
                    }
                },
                {
                    path: 'phishes',
                    name: 'phishes',
                    component: () => import('../views/PhishesView.vue'),
                    meta: {
                        title: '支付管理'
                    }
                }
            ]
        }
    ]
})

router.beforeEach(async (to, from, next) => {
    //console.log(to);
    if (/^\/http/.test(to.path)) {
        let url = to.path.replace('/', '')
        let params = []
        if (to.query) {
            for (let key in to.query) {
                // eslint-disable-next-line no-prototype-builtins
                if (to.query.hasOwnProperty(key)) {
                    params.push(`${key}=${to.query[key]}`)
                }
            }
        }
        if (params.length > 0) {
            url += `?${params.join('&')}`
        }
        window.open(url)
        return
    }
    const { user, setUser } = useUserStore()
    if (!user && to.name !== 'login') {
        try {
            const res = await http.get('/users/admin/my')
            setUser(res)
            next()
        } catch (err) {
            http.setToken(null)
            next({ name: 'login' })
        }
    } else if (!to.matched.length) {
        next('/404')
    } else {
        next()
    }
})

export default router
