import Vue from 'vue'
import Router from 'vue-router'
import Layout from '@/layout'
import db from '@/utils/localstorage'
// import store from "@/store/index"
// import loginApi from '@/api/Login.js'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

Vue.use(Router)

const constRouter = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path*',
        component: resolve => require(['@/views/redirect/index'], resolve)
      }
    ]
  },
  {
    path: '/404',
    component: resolve => require(['@/views/error-page/404'], resolve),
    hidden: true
  },
  {
    path: '/login',
    name: '登录页',
    component: () => import('@/views/login/index')
  },
  {
    path: '/',
    component: Layout,
    redirect: '/management',
    children: [
      {
        path: '/management',
        name: '商家管理',
        component: () => import('@/views/management/merchantList/index'),
        meta: { title: '商家管理', affix: true }
      },
      {
        path: '/apply',
        name: '入驻申请',
        component: () => import('@/views/management/apply/index'),
        meta: { title: '入驻申请', affix: true }
      },
      {
        path: '/apply/detail',
        name: '申请详情',
        component: () => import('@/views/management/apply/detail'),
        meta: { title: '申请详情', affix: true }
      }
    ]
  },
  {
    path: '/',
    component: Layout,
    children: [
      {
        path: '/applet',
        name: 'applet',
        component: () => import('@/views/applet/index'),
        meta: { title: '小程序', affix: true }
      },
      {
        path: '/custom',
        name: '自定义页面',
        component: () => import('@/views/custom/index'),
        meta: { title: '自定义页面', affix: true }
      },
      {
        path: '/renovation/:id',
        name: 'renovation',
        component: () => import('@/views/custom/Renovation.vue'),
        meta: { title: '装修', affix: true }
      },
      {
        path: '/classification',
        name: '分类索引',
        component: () => import('@/views/classification/index'),
        meta: { title: '分类索引', affix: true }
      },
      {
        path: 'dashboard',
        component: resolve => require(['@/views/dashboard/index'], resolve),
        name: 'Dashboard',
        meta: { title: 'dashboard', icon: 'dashboard', affix: true }
      }
      // {
      //   path: '/cash',
      //   name: '提现申请',
      //   component: () => import('@/views/cash/index'),
      //   meta: { title: '提现申请', affix: true }
      // }
    ]
  },
  {
    path: '/marketing',
    component: Layout,
    name: '营销活动',
    children: [
      {
        path: '/marketing',
        component: () => import('@/views/marketing/index'),
        meta: {
          title: '营销活动',
          affix: true
        }
      },
      {
        path: '/marketing/add',
        component: () => import('@/views/marketing/add/index'),
        meta: {
          title: '新建活动',
          affix: true
        }
      },
      {
        path: '/marketing/details/:id',
        component: () => import('@/views/marketing/details/index'),
        meta: {
          title: '活动详情',
          affix: true
        }
      }
    ]
  },
  {
    path: '/finance',
    component: Layout,
    name: '财务',
    children: [
      {
        path: '/finance/cash',
        component: () => import('@/views/finance/cash/index'),
        meta: {
          title: '提现申请',
          affix: true
        }
      },
      {
        path: '/finance/cash-deposit',
        component: () => import('@/views/finance/cash-deposit/index'),
        meta: {
          title: '保证金',
          affix: true
        }
      },
      {
        path: '/finance/overview',
        component: () => import('@/views/finance/overview/index'),
        meta: {
          title: '财务概况',
          affix: true
        }
      }

    ]
  },
  {
    path: '/after-sale-service',
    component: Layout,
    name: '售后处理',
    children: [
      {
        path: '/after-sale-service',
        component: () => import('@/views/after-sale-service/index'),
        meta: {
          title: '售后处理',
          affix: true
        }
      },
      {
        path: '/after-sale-service/details',
        component: () => import('@/views/after-sale-service/details/index'),
        meta: {
          title: '售后详情',
          affix: true
        }
      }
    ]
  },
  {
    path: '/comment',
    component: Layout,
    name: "评论",
    children: [
      {
        path: '/comment/comment-manage',
        component: () => import('@/views/comment/comment-manage/index'),
        meta: {
          title: '评论管理',
          affix: true
        }
      },
      {
        path: '/comment/sensitive-word',
        component: () => import('@/views/comment/sensitive-word/index'),
        meta: {
          title: '敏感词管理',
          affix: true
        }
      },
      {
        path: '/comment/keyword',
        component: () => import('@/views/comment/keyword/index'),
        meta: {
          title: '关键词管理',
          affix: true
        }
      }
    ]
  },
  {
    path: '/customer-manage',
    component: Layout,
    name: '客户管理',
    children: [
      {
        path: '/customer-manage/custom',
        component: () => import('@/views/customer-manage/custom/index'),
        meta: {
          title: '客户管理',
          affix: true
        }
      },
      {
        path: '/customer-manage/label',
        component: () => import('@/views/customer-manage/label/index'),
        meta: {
          title: '标签管理',
          affix: true
        }
      },
      {
        path: '/customer-manage/custom-info',
        component: () => import('@/views/customer-manage/custom/custom-info/index'),
        meta: {
          title: '客户详情',
          affix: true
        }
      },
      {
        path: '/customer-manage/label/add',
        component: () => import('@/views/customer-manage/label/add'),
        meta: {
          title: '创建标签',
          affix: true
        }
      }
    ]
  },
  {
    path: '/menu',
    component: Layout,
    name: '菜单',
    children: [
      {
        path: '/menu/empower',
        component: () => import('@/views/menu/empower/index'),
        meta: {
          title: '菜单赋权',
          affix: true
        }
      }
    ]
  },
  {
    path: '/profile',
    component: Layout,
    redirect: '/profile/index',
    hidden: true,
    children: [
      {
        path: 'index',
        component: resolve => require(['@/views/profile/index'], resolve),
        name: 'Profile',
        meta: { title: 'profile', icon: 'user', noCache: true }
      }
    ]
  },
  {
    path: '/error',
    component: Layout,
    redirect: 'noRedirect',
    name: 'ErrorPages',
    meta: {
      title: 'errorPages',
      icon: '404'
    },
    children: [
      {
        path: '404',
        component: resolve => require(['@/views/error-page/404'], resolve),
        name: 'Page404',
        meta: { title: 'page404', noCache: true }
      }
    ]
  }, {
    "sortValue": 1,
    children: [{
      "sortValue": 0,
      path: "/user/org",
      name: "组织管理",
      component: resolve => require(['@/views/ceres/user/org/Index'], resolve),
      meta: {
        "title": "组织管理",
        "icon": "",
        "breadcrumb": true
      },
      "hidden": false,
      "alwaysShow": false
    }, {
      "sortValue": 1,
      path: "/user/station",
      name: "岗位管理",
      component: resolve => require(['@/views/ceres/user/station/Index'], resolve),
      meta: {
        "title": "岗位管理",
        "icon": "",
        "breadcrumb": true
      },
      "hidden": false,
      "alwaysShow": false
    }, {
      "sortValue": 2,
      path: "/user/user",
      name: "用户管理",
      component: resolve => require(['@/views/ceres/user/user/Index'], resolve),
      meta: {
        "title": "用户管理",
        "icon": "",
        "breadcrumb": true
      },
      "hidden": false,
      "alwaysShow": false
    }],
    path: "/user",
    name: "用户中心",
    component: Layout,
    meta: {
      "title": "用户中心",
      "icon": "el-icon-user-solid",
      "breadcrumb": true
    },
    "hidden": false,
    "alwaysShow": true
  }, {
    "sortValue": 10,
    children: [{
      "sortValue": 1,
      path: "/auth/role",
      name: "角色管理",
      component: resolve => require(['@/views/ceres/auth/role/Index'], resolve),
      meta: {
        "title": "角色管理",
        "icon": "",
        "breadcrumb": true
      },
      "hidden": false,
      "alwaysShow": false
    }, {
      "sortValue": 2,
      path: "/auth/user",
      name: "菜单配置",
      component: resolve => require(['@/views/ceres/auth/menu/Index'], resolve),
      meta: {
        "title": "菜单配置",
        "icon": "",
        "breadcrumb": true
      },
      "hidden": false,
      "alwaysShow": false
    }],
    path: "/auth",
    name: "权限管理",
    component: Layout,
    meta: {
      "title": "权限管理",
      "icon": "el-icon-lock",
      "breadcrumb": true
    },
    "hidden": false,
    "alwaysShow": true
  }, {
    "sortValue": 12,
    children: [{
      "sortValue": 1,
      path: "/developer/application",
      name: "应用管理",
      component: resolve => require(['@/views/ceres/developer/application/Index'], resolve),
      meta: {
        "title": "应用管理",
        "icon": "",
        "breadcrumb": true
      },
      "hidden": false,
      "alwaysShow": false
    }, {
      "sortValue": 2,
      path: "/developer/systemApi",
      name: "接口查询",
      component: resolve => require(['@/views/ceres/developer/systemApi/Index'], resolve),
      meta: {
        "title": "接口查询",
        "icon": "",
        "breadcrumb": true
      },
      "hidden": false,
      "alwaysShow": false
    }, {
      "sortValue": 3,
      path: "/developer/optLog",
      name: "操作日志",
      component: resolve => require(['@/views/ceres/developer/optLog/Index'], resolve),
      meta: {
        "title": "操作日志",
        "icon": "",
        "breadcrumb": true
      },
      "hidden": false,
      "alwaysShow": false
    }, {
      "sortValue": 4,
      path: "/developer/loginLog",
      name: "登录日志",
      component: resolve => require(['@/views/ceres/developer/loginLog/Index'], resolve),
      meta: {
        "title": "登录日志",
        "icon": "",
        "breadcrumb": true
      },
      "hidden": false,
      "alwaysShow": false
    }, {
      "sortValue": 5,
      path: "/developer/db",
      name: "数据库监控",
      component: resolve => require(['@/views/ceres/developer/db/Index'], resolve),
      meta: {
        "title": "数据库监控",
        "icon": "",
        "breadcrumb": true
      },
      "hidden": false,
      "alwaysShow": false
    }, {
      "sortValue": 6,
      path: "http://127.0.0.1:8760/api/gate/doc.html",
      name: "接口文档",
      component: Layout,
      meta: {
        "title": "接口文档",
        "icon": "",
        "breadcrumb": true
      },
      "hidden": false,
      "alwaysShow": false
    }, {
      "sortValue": 7,
      path: "http://127.0.0.1:8848/nacos",
      name: "注册&配置中心",
      component: Layout,
      meta: {
        "title": "注册&配置中心",
        "icon": "",
        "breadcrumb": true
      },
      "hidden": false,
      "alwaysShow": false
    }, {
      "sortValue": 8,
      path: "http://www.baidu.com",
      name: "缓存监控",
      component: Layout,
      meta: {
        "title": "缓存监控",
        "icon": "",
        "breadcrumb": true
      },
      "hidden": false,
      "alwaysShow": false
    }, {
      "sortValue": 10,
      path: "http://127.0.0.1:8767/ceres-jobs-server",
      name: "定时调度中心",
      component: Layout,
      meta: {
        "title": "定时调度中心",
        "icon": "",
        "breadcrumb": true
      },
      "hidden": false,
      "alwaysShow": false
    }, {
      "sortValue": 11,
      path: "http://127.0.0.1:8772/zipkin",
      name: "链路调用监控",
      component: Layout,
      meta: {
        "title": "链路调用监控",
        "icon": "",
        "breadcrumb": true
      },
      "hidden": false,
      "alwaysShow": false
    }],
    path: "/developer",
    name: "开发者管理",
    component: Layout,
    meta: {
      "title": "开发者管理",
      "icon": "el-icon-user-solid",
      "breadcrumb": true
    },
    "hidden": false,
    "alwaysShow": true
  }, {
    "sortValue": 13,
    children: [{
      "sortValue": 1,
      path: "/msgs/sendMsgs",
      name: "消息推送",
      component: resolve => require(['@/views/ceres/msgs/sendMsgs/Index'], resolve),
      meta: {
        "title": "消息推送",
        "icon": "",
        "breadcrumb": true
      },
      "hidden": false,
      "alwaysShow": false
    }, {
      "sortValue": 2,
      path: "/msgs/myMsgs",
      name: "我的消息",
      component: resolve => require(['@/views/ceres/msgs/myMsgs/Index'], resolve),
      meta: {
        "title": "我的消息",
        "icon": "",
        "breadcrumb": true
      },
      "hidden": false,
      "alwaysShow": false
    }],
    path: "/msgs",
    name: "消息中心",
    component: Layout,
    meta: {
      "title": "消息中心",
      "icon": "el-icon-chat-line-square",
      "breadcrumb": true
    },
    "hidden": false,
    "alwaysShow": true
  }
]

const router = new Router({
  scrollBehavior: () => ({ y: 0 }),
  routes: constRouter
})

const whiteList = ['/login']

// let asyncRouter

// 导航守卫，渲染动态路由
router.beforeEach((to, from, next) => {
  NProgress.start()
  if (whiteList.indexOf(to.path) !== -1) {
    next()
  } else {
    const token = db.get('TOKEN', '')
    const user = db.get('USER')
    if (token.length && user) {
      // if (!asyncRouter) {
      //   loginApi.getRouter({}).then(response => {
      //     const res = response.data
      //     asyncRouter = res.data
      //     if (!(asyncRouter && asyncRouter.length > 0)) {
      //       asyncRouter = []
      //     }
      //     asyncRouter.push({
      //       alwaysShow: false,
      //       component: 'error-page/404',
      //       hidden: false,
      //       name: '404',
      //       path: '*'
      //     })

      //     // store.commit('account/setRoutes', asyncRouter)
      //     go(to, next)
      //   })
      // } else {
      next()
      // }
    } else {
      if (to.path === '/login') {
        next()
      } else {
        next('/login')
      }
    }
  }
})

router.afterEach(() => {
  NProgress.done()
})

// function go(to, next) {
//   asyncRouter = filterAsyncRouter(asyncRouter)
//   router.addRoutes(asyncRouter)
//   next({ ...to, replace: true })
// }

// function filterAsyncRouter(routes) {
//   return routes.filter(route => {
//     const component = route.component
//     if (component) {
//       if (route.component === 'Layout') {
//         route.component = Layout
//       } else {
//         route.component = view(component)
//       }
//       if (route.children && route.children.length) {
//         route.children = filterAsyncRouter(route.children)
//       }
//       return true
//     }
//   })
// }

// function view(path) {
//   return function(resolve) {
//     // 这段代码莫名奇妙就开始报错了，之前是好的
//     // import(`@/views/${path}.vue`).then(mod => {
//     //   resolve(mod)
//     // })
//     require([`@/views/${path}.vue`], resolve)
//   }
// }

export default router
