import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import { setSeoTitle } from '@/utils/util.js'
import store from '@/store'

const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView,
    meta: {
      title: "首页",
      path:'/',
      topBarShow:true
    }
  },
  {
    path: '/about',
    name: 'about',
    component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  }, {
    path: '/choosing-identity',
    name: 'choosing-identity',
    component: () => import(/* webpackChunkName: "choosing-identity" */ '@/views/choosing-identity.vue'),
    meta: {
      title: "选择发布者身份"
    }
  },
  {
    path: '/edit',
    name: 'edit',
    component: () => import(/* webpackChunkName: "edit" */ '@/views/edit.vue'),
    meta: {
      path:'/edit'
    }

  },
  {
    path: '/user',
    name: 'user',
    component: () => import(/* webpackChunkName: "user" */ '@/views/user.vue'),
    meta: {
      title: "我的",
      path:'/user',
      topBarShow:true
    }
  },
  {
    path: '/detail',
    name: 'detail',
    component: () => import(/* webpackChunkName: "detail" */ '@/views/detail.vue'),
    meta: {
      title: "详情",
      path:'/detail',
    }
  },
  {//个人房源
    path: '/personHousing',
    component: () => import(/* webpackChunkName: "housing" */ '@/views/housingView/person.vue'),
    meta: {
      title: '个人房源',
      path:'/personHousing',
      topBarShow:true
    }
  },
  {//中介房源
    path: '/intermediaryHousing',
    component: () => import(/* webpackChunkName: "housing" */ '@/views/housingView/intermediary.vue'),
    meta: {
      title: '中介房源',
      path:'/intermediaryHousing',
      topBarShow:true
    }
  },
  {//求房源
    path: '/needHousing',
    component: () => import(/* webpackChunkName: "housing" */ '@/views/housingView/needHousing.vue'),
    meta: {
      title: '求房源',
      path:'/needHousing',
      topBarShow:true
    }
  },
  {//搜索
    path: '/seachPage/:type?/:data?/:areaItem?/:types?',
    component: () => import(/* webpackChunkName: "seachPage" */ '@/views/seachIndex.vue'),
    meta: {
      title: '搜索',
      path:'/seachPage'
    }
  },
  {
    path: "/apartment",
    component: () => import(/* webpackChunkName: "housing" */ '@/views/housingView/apartment.vue'),
    meta: {
      title: "品牌公寓",
      path:'/apartment',
      topBarShow:true
    }
  },
  {
    path: "/apartmentDetail",
    component: () => import(/* webpackChunkName: "housing" */ '@/views/apartmentDetail.vue'),
    meta: {
      title: "品牌公寓详情",
      path:'/apartmentDetail'
    }
  }
]


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


router.beforeEach((to, from, next) => {
  next()
  let meta = to['meta'] || {}
  let title = meta['title']
  if (title) setSeoTitle(title)
})

export default router
