import { createRouter, createWebHistory } from 'vue-router'
import userInfoStore from '@/stores/userInfo'

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/404',
      name: '404',
      component: () => import('../views/common/error.vue'),
      meta: { title: '404未找到', isPass: true }
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/common/login.vue'),
      meta: { title: '登录', isPass: true }
    },
    {
      path: '/loading-page',
      name: 'loading-page',
      component: () => import('../views/common/loading-page.vue'),
      meta: { title: '加载', isPass: true }
    },
    {
      path: '/organization-list',
      name: 'organization-list',
      component: () => import('../views/organization/list.vue'),
      meta: { title: '机构列表', keepAlive: true }
    },
    {
      path: '/organization-detail',
      name: 'organization-detail',
      component: () => import('../views/organization/detail.vue'),
      meta: { title: '机构详情' }
    },
    {
      path: '/comment-list',
      name: 'comment-list',
      component: () => import('../views/comment/list.vue'),
      meta: { title: '点评列表', keepAlive: true }
    },
    {
      path: '/comment-detail',
      name: 'comment-detail',
      component: () => import('../views/comment/detail.vue'),
      meta: { title: '点评详情' }
    },
    {
      path: '/meeting-list',
      name: 'meeting-list',
      component: () => import('../views/meeting/list.vue'),
      meta: { title: '会议列表', keepAlive: true }
    },
    {
      path: '/meeting-detail',
      name: 'meeting-detail',
      component: () => import('../views/meeting/detail.vue'),
      meta: { title: '会议详情' }
    },
    {
      path: '/add-or-update-meeting',
      name: 'add-or-update-meeting',
      component: () => import('../views/meeting/add-or-update-meeting.vue'),
      meta: { title: '会议' }
    },
    {
      path: '/communicate-list',
      name: 'communicate-list',
      component: () => import('../views/communicate/list.vue'),
      meta: { title: '沟通列表', keepAlive: true }
    },
    {
      path: '/communicate-detail',
      name: 'communicate-detail',
      component: () => import('../views/communicate/detail.vue'),
      meta: { title: '沟通详情' }
    },
    {
      path: '/add-cus',
      name: 'add-cus',
      component: () => import('../views/communicate/add-cus.vue'),
      meta: { title: '添加机构' }
    },
    {
      path: '/add-cusUser',
      name: 'add-cusUser',
      component: () => import('../views/communicate/add-cusUser.vue'),
      meta: { title: '添加联系人' }
    },
    {
      path: '/report-list',
      name: 'report-list',
      component: () => import('../views/report/list.vue'),
      meta: { title: '报告列表' }
    },
    {
      path: '/stock-list',
      name: 'stock-list',
      component: () => import('../views/stock/list.vue'),
      meta: { title: '股票动态', keepAlive: true }
    },
    {
      path: '/stock-detail',
      name: 'stock-detail',
      component: () => import('../views/stock/detail.vue'),
      meta: { title: '股票详情', keepAlive: true }
    },
    {
      path: '/target-table',
      name: 'target-table',
      component: () => import('../views/target/table.vue'),
      meta: { title: '重要指标', keepAlive: true, isPass: true }
    },
    {
      path: '/economy-list',
      name: 'economy-list',
      component: () => import('../views/target/economy/economy-list.vue'),
      meta: { title: '经济专题', keepAlive: true }
    },
    {
      path: '/economy-detail/:id?',
      name: 'economy-detail',
      component: () => import('../views/target/economy/economy-detail.vue'),
      meta: { title: '专题详情' }
    },
    {
      path: '/fred-list',
      name: 'fred-list',
      component: () => import('../views/target/fred/fred-list.vue'),
      meta: { title: 'FRED专题', keepAlive: true }
    },
    {
      path: '/fred-detail/:id?',
      name: 'fred-detail',
      component: () => import('../views/target/fred/fred-detail.vue'),
      meta: { title: 'FRED详情' }
    },
    {
      path: '/series-list',
      name: 'series-list',
      component: () => import('../views/target/series/series-list.vue'),
      meta: { title: '指数系列', keepAlive: true }
    },
    {
      path: '/index-list/:id?',
      name: 'index-list',
      component: () => import('../views/target/index/index-list.vue'),
      meta: { title: '指数专题', keepAlive: true }
    },
    {
      path: '/index-detail/:id?',
      name: 'index-detail',
      component: () => import('../views/target/index/index-detail.vue'),
      meta: { title: '指数详情', keepAlive: true }
    },
    {
      path: '/etf-detail/:id?',
      name: 'etf-detail',
      component: () => import('../views/target/etf/etf-detail.vue'),
      meta: { title: 'ETF详情' }
    },
    {
      path: '/fund-list',
      name: 'fund-list',
      component: () => import('../views/target/fund/fund-list.vue'),
      meta: { title: '基金列表', keepAlive: true }
    },
    {
      path: '/fund-detail/:id?',
      name: 'fund-detail',
      component: () => import('../views/target/fund/fund-detail.vue'),
      meta: { title: '基金详情' }
    },
    {
      path: '/group-list',
      name: 'group-list',
      component: () => import('../views/target/group/group-list.vue'),
      meta: { title: '精选组合', keepAlive: true }
    },
    {
      path: '/group-detail/:id?',
      name: 'group-detail',
      component: () => import('../views/target/group/group-detail.vue'),
      meta: { title: '组合详情' }
    },
    {
      path: '/internal-meeting',
      name: 'internal-meeting',
      component: () => import('../views/internal/meeting.vue'),
      meta: { title: '内部会议' }
    },
    {
      path: '/subscription',
      name: 'subscription',
      component: () => import('../views/common/subscription.vue'),
      meta: { title: '订阅' }
    },
    {
      path: '/flow-company',
      name: 'flow-company',
      component: () => import('../views/flow/company.vue'),
      meta: { title: '重点公司', keepAlive: true }
    },
    {
      path: '/flow',
      name: 'flow',
      component: () => import('../views/flow/flow.vue'),
      meta: { title: '重点公司追踪', keepAlive: true }
    },
    {
      path: '/flow-detail',
      name: 'flow-detail',
      component: () => import('../views/flow/flow-detail.vue'),
      meta: { title: '重点公司追踪详情' }
    },
    {
      path: '/user',
      name: 'user',
      component: () => import('../views/user/index.vue'),
      meta: { title: '个人中心', isPass: true }
    },
    {
      path: '/log',
      name: 'log',
      component: () => import('../views/common/log.vue'),
      meta: { title: '日志详情', isPass: true }
    },
    {
      path: '/',
      name: 'home',
      component: () => import('../views/home/index.vue'),
      meta: { title: '首页', isPass: true }
    }
  ]
})

router.beforeEach((to, from, next) => {
  const userInfo = userInfoStore()
  if (!to.meta.isPass && !userInfo.token ) {
    return next({ name: 'login' })
  }
  next()
})

export default router
