import history from '@/utils/history'
import React from 'react'
import { Route, Router, Switch } from 'react-router-dom'
import SpinLoding from './SpinLoding'
import NotFound from '@/components/NotFound'
import AuthRoute from './AuthRoute'

const A1home = React.lazy(() => import('@/pages/A1home'))
const B1exhibit = React.lazy(() => import('@/pages/B1exhibit'))
const Login = React.lazy(() => import('@/pages/Z1login'))
const C1map = React.lazy(() => import('@/pages/C1map'))

// 2个不同的首页路由设置
let homeFlag: '课堂' | '展馆' = '课堂'
if (window.location.href.includes('exhi')) {
  homeFlag = '展馆'
}

// 关于课堂
const routerArr = [
  {
    id: 2,
    name: '预约须知',
    path: '/notice',
    exact: true,
    Com: React.lazy(() => import('@/pages/A2notice'))
  },
  {
    id: 3,
    name: '选择日期',
    path: '/selectDay',
    exact: true,
    Com: React.lazy(() => import('@/pages/A3selectDay'))
  },
  {
    id: 4,
    name: '选择课程',
    path: '/selectCourse/:bookDate/:bookTime',
    exact: true,
    Com: React.lazy(() => import('@/pages/A4selectCourse'))
  },
  {
    id: 5,
    name: '填写信息',
    path: '/order/:id/:bookDate/:bookTime',
    exact: true,
    Com: React.lazy(() => import('@/pages/A5order'))
  },
  {
    id: 6,
    name: '我的申请',
    path: '/my',
    exact: true,
    Com: React.lazy(() => import('@/pages/A6my'))
  },
  {
    id: 7,
    name: '团体认证选择',
    path: '/team',
    exact: true,
    Com: React.lazy(() => import('@/pages/A7team'))
  },
  {
    id: 12,
    name: '邀请码认证',
    path: '/codeAuth',
    exact: true,
    Com: React.lazy(() => import('@/pages/A8_1codeAuth'))
  },
  {
    id: 8,
    name: '团体认证表单',
    path: '/proof/:val',
    exact: true,
    Com: React.lazy(() => import('@/pages/A8proof'))
  },

  // 关于展馆 exhiXXX
  {
    id: 9,
    name: '展馆-我的预约',
    path: '/exhiMy',
    exact: true,
    Com: React.lazy(() => import('@/pages/B2myz'))
  },
  {
    id: 10,
    name: '展馆-发起预约',
    path: '/exhiStart/:key',
    exact: true,
    Com: React.lazy(() => import('@/pages/B3start'))
  },
  {
    id: 11,
    name: '展馆-填写信息',
    path: '/exhiForm/:bookDate/:index/:key',
    exact: true,
    Com: React.lazy(() => import('@/pages/B4form'))
  },
  {
    id: 90,
    name: '找不到页面',
    path: '*',
    exact: false,
    Com: NotFound
  }
]

function RouterOrder() {
  return (
    <Router history={history}>
      <React.Suspense fallback={<SpinLoding />}>
        <Switch>
          <Route path='/' component={homeFlag === '课堂' ? A1home : B1exhibit} exact={true} />
          <Route path='/exhi' component={B1exhibit} exact={true} />
          <Route path='/login/:key' component={Login} exact={true} />
          <Route path='/map' component={C1map} exact={true} />
          {routerArr.map(v => (
            <AuthRoute key={v.id} path={v.path} exact={v.exact} component={v.Com} />
          ))}
        </Switch>
      </React.Suspense>
    </Router>
  )
}

const MemoRouterOrder = React.memo(RouterOrder)

export default MemoRouterOrder
