import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    name: 'Home',
    meta: { layout: 1 },
    component: () => import('@/views/Home.vue')
  },
  {
    path: '/helpCenter',
    name: 'HelpCenter',
    meta: { layout: 2 },
    component: () => import('@/views/HelpCenter.vue'),
  },
  {
    path: '/digitalAssets',
    name: 'DigitalAssets',

    component: () => import('@/views/DigitalAssets.vue'),
  },
  {
    path: '/personalProvenance',
    name: 'PersonalProvenance',
    component: () => import('@/views/PersonalProvenance.vue'),
  },
  {
    path: '/companyProvenance',
    name: 'CompanyProvenance',

    component: () => import('@/views/CompanyProvenance.vue'),
  },
  {
    path: '/autoDeposits',
    name: 'AutoDeposits',

    component: () => import('@/views/Auto.vue'),
  },
  {
    path: '/mail',
    name: 'Mail',

    component: () => import('@/views/Mail.vue'),
  },
  {
    path: '/solutions',
    name: 'Solutions',
    meta: { layout: 3 },
    component: () => import('@/views/Solutions/index.vue'),
    redirect: { name: "FoodSafety" },
    children: [
      {
        path: 'foodSafety',
        name: 'FoodSafety',
        component: () => import('@/views/Solutions/foodSafety.vue'),
      },
      {
        path: 'healthProvenance',
        name: 'HealthProvenance',
        component: () => import('@/views/Solutions/health.vue'),
      },
      {
        path: 'publicWelfare',
        name: 'PublicWelfare',
        component: () => import('@/views/Solutions/publicWelfare.vue'),
      },
      {
        path: 'team',
        name: 'Team',
        component: () => import('@/views/Solutions/team.vue'),
      },
      {
        path: 'digitalDocument',
        name: 'DigitalDocument',
        component: () => import('@/views/Solutions/document.vue'),
      },
      {
        path: 'digitalContract',
        name: 'DigitalContract',
        component: () => import('@/views/Solutions/contract.vue'),
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes,
  scrollBehavior(to, from, savedPosition) {
    // always scroll to top
    return { top: 0 }
  },
})


router.beforeEach((to, from, next) => {
  if (to.matched.length === 0) {  //如果未匹配到路由
    next('/');
  } else {
    next();    //如果匹配到正确跳转
  }
})

export default router
