import { createRouter, createWebHashHistory } from 'vue-router'
import LoginView from '@/view/system'
import RegisterView from '@/view/system'
import ForgetView from '@/view/system'
import ViewLayout from '@/view/layout'
import Scene from '@/view/scene'
import DispatchView from '@/view/dispatch'
import OrganizationView from '@/view/organization'
import OrganizationInfoView from '@/view/organization/info'
import OrganizationListView from '@/view/organization/list'
import HomeView from '@/view/home'
import VRModelView from '@/view/vrmodel'
// import TeachingView from '@/view/teaching'
import CameraView from '@/view/camera'
import RoleView from '@/view/role'
import UserView from '@/view/user'
import InitiatorView from '@/view/scene/Initiator'
import VisitorView from '@/view/scene/visitor'

export const system = [
  {
    name: 'login',
    path: '/login',
    component: LoginView,
    meta: { title: '登录' }
  },
  {
    name: 'register',
    path: '/register',
    component: RegisterView,
    meta: { title: '注册' }
  },
  {
    name: 'forget',
    path: '/forget',
    component: ForgetView,
    meta: { title: '重置密码' }
  },
]

export const routes = [
  {
    name: 'viewLayout',
    path: '/',
    component: ViewLayout,
    meta: { title: 'VR看房管理' },
    children: [
      ...system,
      {
        name: 'home',
        path: 'home',
        component: HomeView,
        meta: { title: '首页' }
      },
      {
        name: 'vrmodel',
        path: 'vrmodel',
        component: VRModelView,
        meta: { title: '场景管理' }
      },
      {
        name: 'camera',
        path: 'camera',
        component: CameraView,
        meta: { title: '相机管理' }
      },
      {
        name: 'dispatch',
        path: 'dispatch',
        component: DispatchView,
        meta: { title: '火调管理' }
      },
      {
        name: 'organization',
        path: 'organization',
        component: OrganizationView,
        meta: { title: '组织架构' },
        redirect:'/organization/list',
        children:[
          {
            name: 'organizationlist',
            path: 'list',
            component: OrganizationListView,
            meta: { title: '组织架构' },
          },
          {
            name: 'organizationdetail',
            path: 'detail',
            component: OrganizationInfoView,
            meta: { title: '组织架构' },
          },
        ]
      },
      
      {
        name: 'teaching',
        path: 'teaching',
        component: DispatchView,
        meta: { title: '教学平台' }
      },{
        name: 'role',
        path: 'role',
        component: RoleView,
        meta: { title: '角色管理' }
      },
      {
        name: 'user',
        path: 'user',
        component: UserView,
        meta: { title: '用户管理' }
      }
    ]
  },
  {
    name: 'scene',
    path: '/scene/:projectId',
    component: Scene,
    meta: { title: '火调场景' }
  },
  {
    name: 'sceneInitiator',
    path: '/sceneInitiator',
    component: InitiatorView,
    meta: { title: '场景带看' }
  },
  {
    name: 'sceneVisitor',
    path: '/sceneVisitor',
    component: VisitorView,
    meta: { title: '场景带看' }
  }
]

const router = createRouter({ 
  history: createWebHashHistory(),
  routes,

})

router.beforeEach((to, from, next) => {
  if (!to.name || to.name === 'viewLayout') {
    router.replace({name: 'home'})
    return;
  }
  next()
})

const getConfig = (routes) => {
  let config = []

  for (let item of routes) {
    if (!system.includes(item)) {
      config.push({
        ...item,
        children: item.children && getConfig(item.children)
      })
    }
  }

  return config
}

export const routeIsSystem = ($route, proutes = routes) => {
      console.log('proutes',proutes,system,$route.name)
      for (let item of proutes) {
    if (!system.includes(item)) {
      if (item.name === $route.name) {
      console.log('proutes',item,$route.name)
      return false
      } else if (item.children && !routeIsSystem($route, item.children)) {
      console.log('proutes',item,$route.name)
      return false
      }
    }
  }

  return true
}
export const config = getConfig(routes)
export default router
