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: '/dealer',
                    name: 'dealer',
                    component: () => import('../views/DealerView.vue'),
                    meta: {
                        title: '经销商列表'
                    }
                },
                {
                    path: 'sysConfig',
                    name: 'sysConfig',
                    component: () => import('../views/SysConfigView.vue'),
                    meta: {
                        title: '系统设置'
                    }
                },
                {
                    path: '/phoneList',
                    name: 'phoneList',
                    component: () => import('../views/PhoneListView.vue'),
                    meta: {
                        title: '发送列表'
                    }
                },
                {
                    path: '/task',
                    name: 'task',
                    component: () => import('../views/TaskView.vue'),
                    meta: {
                        title: '任务列表'
                    }
                },
                {
                    path: '/device',
                    name: 'device',
                    component: () => import('../views/DeviceView.vue'),
                    meta: {
                        title: '设备列表'
                    }
                },
                {
                    path: '/channel',
                    name: 'channel',
                    component: () => import('../views/ChannelView.vue'),
                    meta: {
                        title: '渠道列表'
                    }
                },
                {
                    path: '/rcsNumber',
                    name: 'rcsNumber',
                    component: () => import('../views/RcsNumberView.vue'),
                    meta: {
                        title: 'RCS号码'
                    }
                },
                {
                    path: '/balance',
                    name: 'balance',
                    component: () => import('../views/BalanceView.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('/admin/users/get')
            setUser(res)
            next()
        } catch (err) {
            http.setToken(null)
            next({ name: 'login' })
        }
    } else if (!to.matched.length) {
        next('/404')
    } else {
        next()
    }
})

export default router
