import Vue from 'vue'
import Router from 'vue-router'
import {autoSaveAuth} from "@/api/AdminAuth"
Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/* Router Modules */
// import componentsRouter from './modules/components'
// import chartsRouter from './modules/charts'
// import tableRouter from './modules/table'
// import nestedRouter from './modules/nested'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    noCache: true                if set true, the page will no be cached(default is false)
    affix: true                  if set true, the tag will affix in the tags-view
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/auth-redirect',
    component: () => import('@/views/login/auth-redirect'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        name: 'Dashboard',

        meta: { title: '用户首页', icon: 'dashboard', affix: true }
      }
    ]
  },
  {
    path: '/profile',
    component: Layout,
    redirect: '/profile/index',
    hidden: true,
    children: [
      {
        path: 'index',
        component: () => import('@/views/profile/index'),
        name: 'Profile',
        meta: { title: 'Profile', icon: 'user', noCache: true }
      }
    ]
  },
  {
    path: '/document/create',
    component: () => import('@/views/document/create'),
    name: 'documentCreate',
    hidden:true,
    meta: { title: '创建文档',  noCache: true }
  },
  {
    path: '/error',
    component: Layout,
    redirect: 'noRedirect',
    name: 'ErrorPages',
    hidden:true,
    meta: {
      title: 'Error Pages',
      icon: '404'
    },
    children: [
      {
        path: '401',
        component: () => import('@/views/error-page/401'),
        name: 'Page401',
        meta: { title: '401', noCache: true }
      },
      {
        path: '404',
        component: () => import('@/views/error-page/404'),
        name: 'Page404',
        meta: { title: '404', noCache: true }
      }
    ]
  }
]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [
  {
    path: '/project',
    component: Layout,
    type:1,
    meta: {
      title: '项目管理',
      icon: 'paizhao-01-20',
      roles: ['admin']
    },
    children: [
      {
        path: 'index',
        component: () => import('@/views/project/search'),
        name: 'project',
        meta: { title: '项目管理', noCache: true,roles: ['admin'] }
      },
      {
        path: 'category',
        component: () => import('@/views/project/category/search'),
        name: 'projectCategory',
        meta: { title: '项目分类',  noCache: true ,roles: ['admin']}
      }
    ]
  },
  {
    path: '/customer',
    component: Layout,
    type:2,
    meta: {
      title: '客户管理',
      icon: 'kehu',
      roles: ['admin']
    },
    children: [
      {
        path: 'index',
        component: () => import('@/views/customer/search'),
        name: 'customerIndex',
        meta: { title: '客户管理',  noCache: true,roles: ['admin'] }
      }
    ]
  },
  {
    path: '/product',
    component: Layout,
    type:3,
    meta: {
      title: '产品管理',
      icon: 'chanpin',
      roles: ['admin']
    },
    children: [
      {
        path: 'index',
        component: () => import('@/views/product/search'),
        name: 'product',
        meta: { title: '产品管理',  noCache: true ,roles: ['admin']}
      },
      {
        path: 'category',
        component: () => import('@/views/product/category/search'),
        name: 'productCategory',
        meta: { title: '产品分类',  noCache: true ,roles: ['admin']}
      },
      {
        path: '/supplier/search',
        component: () => import('@/views/product/supplier/search'),
        name: 'supplierSearch',
        meta: { title: '供应商管理',  noCache: true ,roles: ['admin']}
      },
      {
        path: '/brand/search',
        component: () => import('@/views/product/brand/search'),
        name: 'brandSearch',
        meta: { title: '品牌设置',  noCache: true ,roles: ['admin']}
      }
    ]
  },

  {
    path: '/document',
    component: Layout,
    type:4,
    meta: {
      title: '文档管理',
      icon: 'wendang'
    },
    children: [
      {
        path: 'index',
        component: () => import('@/views/document/search'),
        name: 'documentSearch',
        meta: { title: '文档管理',  noCache: true }
      },
      {
        path: 'new',
        redirect: '/document/create',
        target:"_blank",
        name:"createNewDocument",
        meta: { title: '创建文档',  noCache: true }
      },
      {
        path: 'category/search',
        component: () => import('@/views/document/category/search'),
        name: 'documentCategory',
        meta: { title: '文档分类',  noCache: true ,roles: ['admin']}
      },
      {
        path: '/template/category/search',
        component: () => import('@/views/template/category/search'),
        name: 'templateCategory',
        meta: { title: '模块分类',  noCache: true ,roles: ['admin']}
      },
      {
        path: '/template/search',
        component: () => import('@/views/template/search'),
        name: 'template',
        meta: { title: '模块列表',  noCache: true ,roles: ['admin']}
      },



      {
        path: 'dataCenter/index',
        component: () => import('@/views/dataCenter/search'),
        name: 'dataCenter',
        meta: { title: '原始数据', noCache: true,roles: ['admin'] }
      },



      {
        path: 'dataCenter/create',
        component: () => import('@/views/dataCenter/create'),
        name: 'dataCreate',
        hidden:true,
        meta: { title: '创建原始数据', noCache: true,roles: ['admin'] }
      },
      {
        path: 'dataCenter/info',
        component: () => import('@/views/dataCenter/dataView'),
        name: 'dataView',
        hidden:true,
        meta: { title: '预览数据', noCache: true,roles: ['admin'] }
      },
      {
        path: 'dataCenter/category/index',
        component: () => import('@/views/dataCenter/category/search'),
        name: 'dataCenterCategorySearch',
        meta: { title: '原始数据分类', noCache: true,roles: ['admin'] }
      },
    ]
  },
  {
    path: '/system',
    component: Layout,
    type:5,
    meta: {
      title: '系统设置',
      icon: 'xitong',
      roles: ['admin']
    },
    children: [
      {
        path: 'user/index',
        component: () => import('@/views/user/search'),
        name: 'user',
        meta: { title: '用户管理', noCache: true,roles: ['admin']}
      },
      {
        path: 'role/index',
        component: () => import('@/views/role/search'),
        name: 'role',
        meta: { title: '角色权限',  noCache: true,roles: ['admin'] }
      },
      {
        path: 'role/create',
        component: () => import('@/views/role/create'),
        name: 'createRole',
        hidden:true,
        meta: { title: '创建角色',  noCache: true,roles: ['admin'] }
      },
      {
        path: 'role/edit',
        component: () => import('@/views/role/edit'),
        name: 'editRole',
        hidden:true,
        meta: { title: '修改角色',  noCache: true,roles: ['admin'] }
      },
      {
        path: 'auth/index',
        component: () => import('@/views/auth/search'),
        name: 'auth',
        meta: { title: '权限配置',  noCache: true,roles: ['admin'] }
      },
      // {
      //   path: 'flow/index',
      //   component: () => import('@/views/flow/search'),
      //   name: 'flow',
      //   meta: { title: '流程设计', noCache: true,roles: ['admin'] }
      // },
    ]
  }
,
  { path: '*', redirect: '/404', hidden: true }
]



const saveRoute=(route)=>{
  let items=route.filter(o=>o.children!=undefined);
  let routes=[];
  items=items.map(res=>{
  res.children.map((subRes)=>{
      routes.push({
        code:subRes.name,
        name:subRes.meta.title,
        type:res.type==undefined?0:res.type
      });
    });
  })
/*   autoSaveAuth({data:JSON.stringify(routes)}) */
}
/* saveRoute([...asyncRoutes]); */
const createRouter = () => new Router({
  mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}


/* const createRouter = () => new Router({
  mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
} */

export default router