// 动态路由
const routers = [
  // 服务大厅
  {
    path: '/service',
    name: 'service',
    meta: {
      icon: 'ios-cart',
      title: 'service_hall'
    },
    component: () => import('@/components/main'),
    children: [
      {
        path: 'my-order',
        name: 'myOrder',
        meta: {
          icon: 'ios-reorder',
          title: 'my_order'
        },
        component: () => import('@/view/service/my-order')
      },
      {
        path: 'my-set-meal',
        name: 'mySetMeal',
        meta: {
          icon: 'ios-options-outline',
          title: 'my_set_meal'
        },
        component: () => import('@/view/service/my-set-meal')
      }
    ]
  },
  // 下单
  {
    path: '/place-an-order',
    name: 'place',
    // redirect: 'place-an-order/index',
    meta: {
      title: 'router_1',
      hideInMenu: true
    },
    component: () => import('@/components/main'),
    children: [
      {
        path: '/place-an-order/index',
        name: 'placeAnOrder',
        meta: {
          icon: 'md-flower',
          title: 'router_2'
          // hideInMenu: true
        },
        component: () => import('@/view/place-an-order/index')
      },
      {
        path: '/place-an-order/result',
        name: 'resultOrder',
        meta: {
          icon: 'md-flower',
          title: 'router_2'
          // hideInMenu: true
        },
        component: () => import('@/view/place-an-order/result')
      }
    ]
  },
  // 应用管理
  {
    path: '/application',
    name: 'application',
    meta: {
      icon: 'md-apps',
      title: 'app_management'
    },
    component: () => import('@/components/main'),
    children: [
      {
        path: 'list',
        name: 'applicationList',
        meta: {
          icon: 'ios-albums-outline',
          title: 'my_applications'
        },
        component: () => import('@/view/application/list')
      }
    ]
  },
  // 工单
  {
    path: '/api-usb-workorder',
    name: 'apiUsbWorkOrder',
    // redirect: 'api-usb-demo/demo',
    meta: {
      icon: 'md-reorder',
      title: 'workorderlist18'
    },
    component: () => import('@/components/main'),
    children: [
      {
        path: '/api-usb-workorder/list',
        name: 'apiUsbWorkOrderList',
        meta: {
          icon: 'ios-paper-outline',
          title: 'workorderlist1'
        },
        component: () => import('@/view/api-usb-workorder/list.vue')
      },
      {
        path: '/api-usb-workorder/add',
        name: 'apiUsbWorkOrderAdd',
        meta: {
          icon: 'ios-add-circle-outline',
          title: 'workorderlist4'
        },
        component: () => import('@/view/api-usb-workorder/add.vue')
      },
      {
        path: '/api-usb-workorder/info',
        name: 'apiUsbWorkOrderInfo',
        meta: {
          icon: 'md-flower',
          title: 'workorderinfo4',
          hideInMenu: true
        },
        component: () => import('@/view/api-usb-workorder/info.vue')

      }
    ]
  },
  // 接口权限
  {
    path: '/permission',
    name: 'apiPermission',
    redirect: 'permission/settings',
    meta: {
      icon: 'md-flower',
      title: 'api_permissions'
    },
    component: () => import('@/components/main'),
    children: [
      {
        path: 'settings',
        name: 'setPermission',
        meta: {
          icon: 'md-flower',
          title: 'api_permissions',
          hideInMenu: true,
          hideInBread: true
        },
        component: () => import('@/view/permission')
      }
    ]
  },
  // 个人账号信息管理
  {
    path: '/account',
    name: 'account',
    component: () => import('@/components/main'),
    meta: {
      icon: 'md-person',
      title: 'developer_center'
    },
    children: [
      {
        path: 'accountInfo',
        name: 'accountInfo',
        meta: {
          icon: 'md-list',
          title: 'developer_info',
          meta: { keepAlive: false }
        },
        component: () => import('@/view/account/accountInfo.vue')
      },
      {
        path: 'resetPassword',
        name: 'resetPassword',
        meta: {
          icon: 'ios-build',
          title: 'pwd_alter',
          meta: { keepAlive: false }
        },
        component: () => import('@/view/account/resetPwd.vue')
      },
      {
        path: 'accountSettings',
        name: 'accountSettings',
        meta: {
          icon: 'md-flower',
          title: 'developer_info_alter',
          hideInMenu: true,
          meta: { keepAlive: false }
        },
        component: () => import('@/view/account/accountSettings.vue')
      }
    ]
  }
]

// 转换为绝对路由
const resolvePath = (...paths) => {
  let i = paths.length
  // 反向查找 / 开头的路径
  while (--i) {
    if (/^\//.test(paths[i])) break
  }
  return paths.slice(i)
    .join('/') // 加入 '/'
    .replace('//', '/') // 替换可能存在的 '//'
}
const restructure = (routes = [], base = '/') => {
  return routes.map(route => {
    let { path, children = [] } = route
    path = resolvePath(base, path)
    if (children.length > 0) {
      route.children = restructure(children, path)
    }
    return {
      ...route,
      path
    }
  })
}

// 转换为绝对路由
export const asynRouters = restructure(routers, '/') // 用于匹配权限路由

const [...routers_copy] = routers
export const asynRouters_copy = restructure(routers_copy, '/') // 用于匹配401页面
