import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) return savedPosition
    if (to.hash) return { el: to.hash, top: 84, behavior: 'smooth' }
    return { top: 0 }
  },
  routes: [
    {
      path: '/',
      name: 'home',
      component: HomeView,
      meta: { title: '首页' },
    },
    {
      path: '/products',
      name: 'products',
      component: () => import('../views/ProductsView.vue'),
      meta: { title: '产品' },
    },
    {
      path: '/solutions',
      name: 'solutions',
      component: () => import('../views/SolutionsView.vue'),
      meta: { title: '解决方案' },
    },
    {
      path: '/cases',
      name: 'cases',
      component: () => import('../views/CasesView.vue'),
      meta: { title: '客户案例' },
    },
    {
      path: '/news',
      name: 'news',
      component: () => import('../views/NewsView.vue'),
      meta: { title: '新闻动态' },
    },
      {
      path: '/news/1',
      component: () => import('../views/news/new1.vue'),
      meta: { title: '新闻动态' },
    },
     {
      path: '/news/2',
      component: () => import('../views/news/new2.vue'),
      meta: { title: '新闻动态' },
    },
      {
      path: '/news/4',
      component: () => import('../views/news/new4.vue'),
      meta: { title: '新闻动态' },
    },

    {
      path: '/cases/1',
      component: () => import('../views/cases/case1.vue'),
      meta: { title: '客户案例' },
    },
      {
      path: '/cases/2',
      component: () => import('../views/cases/case2.vue'),
      meta: { title: '客户案例' },
    },
     {
      path: '/cases/3',
      component: () => import('../views/cases/case3.vue'),
      meta: { title: '客户案例' },
    },
    {
      path: '/about',
      name: 'about',
      component: () => import('../views/AboutView.vue'),
      meta: { title: '关于我们' },
    },
    {
      path: '/contact',
      name: 'contact',
      component: () => import('../views/ContactView.vue'),
      meta: { title: '联系咨询' },
    },
    {
      path: '/:pathMatch(.*)*',
      name: 'not-found',
      component: () => import('../views/NotFoundView.vue'),
      meta: { title: '页面不存在' },
    },
  ],
})

router.afterEach((to) => {
  const pageTitle = to.meta?.title ? `${to.meta.title} · 工业互联网门户` : '工业互联网门户'
  document.title = pageTitle
})

export default router
