import { createRouter, createWebHistory } from 'vue-router'
export const Layout = () => import('@/layout/index.vue')
import { UserStore } from '@/store/user'
import axios from 'axios'
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/index'
    },
    {
      path: '/index',
      name: 'index',
      meta: { title: '产学研用平台' },
      component: () => import('@/views/index/index.vue')
    },
    {
      path: '/',
      name: 'Layout',
      component: Layout,
      children: [
        {
          path: '/home',
          name: 'home',
          meta: { title: '产学研用平台-首页' },
          component: () => import('@/views/home/index.vue')
        },
        {
          path: '/news',
          name: 'news',
          meta: { title: '产学研用平台-政策新闻' },
          component: () => import('@/views/news/index.vue')
        },
        {
          path: '/demand',
          name: 'demand',
          meta: { title: '产学研用平台-供需商城' },
          component: () => import('@/views/demand/index.vue')
        },
        {
          path: '/brain',
          name: 'brain',
          meta: { title: '产学研用平台-新闻资讯' },
          component: () => import('@/views/brain/index.vue')
        },
        {
          path: '/innovation',
          name: 'innovation',
          meta: { title: '产学研用平台-创新大赛' },
          component: () => import('@/views/innovation/index.vue')
        },
        {
          path: '/achievement',
          name: 'achievement',
          meta: { title: '产学研用平台-成果展示' },
          component: () => import('@/views/achievement/index.vue')
        },
        {
          path: '/company',
          name: 'company',
          meta: { title: '产学研用平台-企业' },
          component: () => import('@/views/company/index.vue')
        },
        {
          path: '/expert',
          name: 'expert',
          meta: { title: '产学研用平台-专家' },
          component: () => import('@/views/expert/index.vue')
        },
        {
          path: '/project',
          name: 'project',
          meta: { title: '产学研用平台-项目' },
          component: () => import('@/views/project/index.vue')
        },
        {
          path: '/achievement/detail',
          meta: { title: '吉林省重点领域技术转移平台-详情' },
          component: () => import('@/views/achievement/detail.vue')
        },
        {
          path: '/demand/detail',
          meta: { title: '吉林省重点领域技术转移平台-详情' },
          component: () => import('@/views/demand/detail.vue')
        },
        {
          path: '/news/detail',
          meta: { title: '吉林省重点领域技术转移平台-详情' },
          component: () => import('@/views/news/detail.vue')
        },
        {
          path: '/innovation/detail',
          meta: { title: '吉林省重点领域技术转移平台-详情' },
          component: () => import('@/views/innovation/detail.vue')
        },
        {
          path: '/project/detail',
          meta: { title: '产学研用平台-详情' },
          component: () => import('@/views/project/detail.vue')
        },
        {
          path: '/expert/detail',
          meta: { title: '产学研用平台-专家' },
          component: () => import('@/views/expert/detail.vue')
        },
        {
          path: '/company/detail',
          meta: { title: '产学研用平台-企业' },
          component: () => import('@/views/company/detail.vue')
        },
        {
          path: '/release',
          meta: { title: '产学研用平台-发布' },
          component: () => import('@/views/release/index.vue')
        },
        {
          path: '/search',
          meta: { title: '产学研用平台-搜索' },
          component: () => import('@/views/search/index.vue')
        },
        {
          path: '/chat',
          meta: { title: '产学研用平台-聊天记录' },
          component: () => import('@/views/chat/index.vue')
        },
        {
          path: '/login',
          meta: { title: '产学研用平台-用户登录' },
          component: () => import('@/views/login/index.vue')
        },
        {
          path: '/register',
          meta: { title: '产学研用平台-用户注册' },
          component: () => import('@/views/register/index.vue')
        },
        {
          path: '/center',
          meta: { title: '产学研用平台-个人中心' },
          component: () => import('@/views/center/index.vue')
        },
        {
          path: '/sign',
          meta: { title: '产学研用平台-我的报名' },
          component: () => import('@/views/sign/index.vue')
        },
        {
          path: '/help',
          meta: { title: '产学研用平台-帮助中心' },
          component: () => import('@/views/help/index.vue')
        }
      ]
    }
  ]
})
router.beforeEach(async (to, from, next) => {
  const userStore = UserStore()
  document.title = `${to.meta.title} `
  const token = localStorage.getItem('token')
  if (token) {
    const res = await axios.request({
      method: 'get',
      url: `${import.meta.env.VITE_APP_BASE_API}/token/tokenView`,
      responseType: 'json',
      headers: {
        token: token
      }
    })
    if (res.data.errcode == '0') userStore.setUser(res.data.data)
    next()
  } else next()
})
router.afterEach(() => {
  window.scrollTo(0, 0)
})
export default router
