import Vue from 'vue'
import VueRouter from 'vue-router'
import Cookies from 'js-cookie'
import Permission from '@/utils/permissions'

import {stringifyQuery,parseQuery} from '@/utils/query'

Vue.use(VueRouter)

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
const routes = [
  {
    path: "/route",
    component: () => import('@/utils/model/route.vue'),
  },
  {
    path: "/",
    component: () => import('@/views/index'),
  },

  {
    path: '/login',
    name: 'Login',
    meta: {
      title: '系统登录'
    },
    component: () => import("@/views/login/index.vue")
  },
  {
    path: "/agreeConceal",
    name: 'agreeConceal',
    meta: {
      title: '用户须知'
    },
    component: () => import('@/views/login/compoments/invite/components/index.vue')
  },
  {
    path: "",
    component: () => import('@/views/layout/index.vue'),
    children: [
      {
        path: '/home',
        name: 'Home',
        meta: {
          title: '首页',
          sign: 'home',
          belong: 'home'
        },
        component: () => import('@/views/home/index.vue'),
      },
      //专利数据库
      {
        path: '/project',
        component: { render(c) { return c('router-view') } },
        redirect: '/project',
        children: [
          {
            path: '/',
            name: 'Project',
            meta: {
              title: '专利数据库',
              sign: 'project',
              belong: 'project',
              permission:'xiaoshi/project'
            },
            component: () => import('@/views/project/index.vue'),
          },
          {
            path: '/updateCriteria',
            meta: {
              title: '更新条件',
              sign: 'updateCriteria',
              belong: 'project',
              // permission:'xiaoshi/project'
            },
            component: () => import('@/views/project/components/updateCriteria/index.vue'),
          },
          {
            path: 'patentCollection',
            meta: {
              title: '专利列表',
              sign: 'patentCollection',
              belong: 'project',
              keepAlive:true
            },
            component: () => import('@/views/project/patentCollection/index.vue')
          },
          {
            path: '/patentDetails/:patentNo',
            name: 'patentDetails',
            meta: {
              title: '专利文章',
              sign: 'patentDetails',
              notReturn:true,
              // belong:'project'
            },
            component: () => import('@/views/project/patentDetails/index.vue')
          },
          // 专利数据库导出专利
          {
            path: "/exportPatent",
            name: 'exportPatent',
            meta: {
              title: '专利数据库导出专利',
              sign: 'exportPatent'
            },
            component: () => import('@/views/project/patentCollection/components/export/index.vue'),
          },
          // 专利数据库导出专利
          {
            path: "/patentWorth",
            name: 'patentWorth',
            meta: {
              title: '价值曲线',
              sign: 'patentWorth'
            },
            component: () => import('@/views/project/patentDetails/components/patentMessage/history/echarts/index.vue'),
          },
        ],
      },
      //企业专利数据库
      // {
      //   path: '/enterprisePatentDB',
      //   name:'enterprisePatentDB',
      //   component:{render(c){return c('router-view')}},
      //   redirect:'/enterprisePatentDB',
      //   children:[
      //     {
      //       path:'/',
      //       meta: {
      //         title: '企业专利数据库',
      //       },
      //       component: () => import('@/views/EnterprisePatentDatabase/index.vue'),
      //     },
      //   ]
      // },
      // //专利挖掘
      {
        path: '/patentMining',
        name: 'patentMining',
        component: { render(c) { return c('router-view') } },
        redirect: '/patentMining',
        children: [
          {
            path: '/',
            meta: {
              title: '专利挖掘',
              sign: 'patentMining',
              belong: 'patentMining',
              permission:'xiaoshi/patentMining'
            },
            component: () => import('@/views/patentMining/index.vue'),
          },
          {
            path: '/details',
            meta: {
              title: '专利挖掘详情',
              sign: 'patentMiningDetails',
              belong: 'patentMining'
            },
            component: () => import('@/views/patentMining/components/details/index.vue'),
          },
          {
            path: '/excavateTask',
            meta: {
              title: '专利挖掘任务',
              sign: 'patentMiningExcavateTask',
              belong: 'patentMining'
            },
            component: () => import('@/views/patentMining/components/excavateTask/index.vue'),
          },
          {
            path: '/handleExamine',
            meta: {
              title: '专利挖掘任务详情',
              sign: 'patentMiningHandleExamine',
              belong: 'patentMining'
            },
            component: () => import('@/views/patentMining/components/handleExamine/index.vue'),
          },
        ]
      },
      // // 产品
      {
        path: '/product',
        name: '',
        component: { render(c) { return c('router-view') } },
        redirect: '/product',
        children: [
          {
            path: '/',
            name: 'Product',
            meta: {
              title: '产品/技术',
              sign: 'product',
              belong: 'Product',
              permission:'xiaoshi/product'
            },
            component: () => import('@/views/product/index.vue'),
          },
          {
            path: "/productDetails",
            name: 'productDetails',
            meta: {
              title: '详情',
              sign: 'productDetails',
              belong: 'Product'
            },
            component: () => import('@/views/product/components/details'),
          },
          {
            path: "/mindIndex",
            meta: {
              title: '可视化',
              sign: 'mindIndex',
              belong: 'Product'
            },
            component: () => import('@/views/product/components/jsMind'),
          },
          {
            path: "/chartIndex",
            name: 'chartIndex',
            meta: {
              title: '趋势图',
              sign: 'chartIndex',
              belong: 'Product'
            },
            component: () => import('@/views/product/components/echarts'),
          },
          {
            path: "/frameworkIndex",
            meta: {
              title: '产品架构',
              sign: 'frameworkIndex',
              belong: 'Product'
            },
            component: () => import('@/views/product/components/framework/index.vue'),

          },
          {
            path: "/commodity",
            meta: {
              title: '产品商品化专利',
              sign: 'commodity',
              belong: 'Product'
            },
            component: () => import('@/views/product/components/commodity/index.vue'),

          },
        ],

      },
      //事件
      {
        path: '/Event',
        
        component: { render(c) { return c('router-view') } },
        redirect: '/Event',
        children: [
          {
            path: '/',
            name: 'Event',
            meta: {
              title: '事件',
              sign: 'event',
              belong: 'Event',
              permission:'xiaoshi/event'
            },
            component: () => import('@/views/event/index.vue'),
          },
          //事件可视化
          {
            path: "/eventVisual",
            name: 'eventVisual',
            meta: {
              title: '事件可视化',
              sign: 'eventVisual',
              belong: 'Event'
            },
            component: () => import('@/views/event/components/details/visualRouter.vue'),
          },
        ],
      },
      //报告
      {
        path: '/AllReport',
        component: { render(c) { return c('router-view') } },
        redirect: '/AllReport',
        children: [
          {
            path: '/',
            name:'AllReport',
            meta: {
              title: '报告',
              sign: 'report',
              belong: 'AllReport',
              permission:'xiaoshi/report'
            },
            component: () => import('@/views/report/index.vue'),
          },
          {
            path: '/reportDetails',
            name: 'reportDetails',
            meta: {
              title: '报告详情',
              sign: 'reportDetails',
              belong: 'AllReport',
              keepAlive:true
            },
            component: () => import('@/views/report/components/details'),
          },
          {
            path: '/reportPatentList',
            name: 'reportPatentList',
            meta: {
              title: '报告专利清单',
              sign: 'reportPatentList',
            },
            component: () => import('@/views/report/components/patentList/index.vue'),
          },

          //code码页面
          {
            path: '/identificationCode',
            name: "identificationCode",
            meta: {
              title: '分析系统',
              sign: 'identificationCode',
              belong: 'AllReport'
            },
            component: () => import('@/views/components/identificationCode/index.vue'),
          },
          //FTO风险排查分配任务
          {
            path: '/allocationTask',
            name: "allocationTask",
            meta: {
              title: '分配任务',
              sign: 'allocationTask',
            },
            component: () => import('@/views/report/components/allocationTask/index.vue'),
          },

          //侵权分析
          {
            path: '/tortIndex',
            name: "tortIndex",
            meta: {
              sign: 'tortIndex',
              belong: 'AllReport'
            },
            component: () => import('@/views/report/tort/index.vue'),
          },
          // 产品信息录入
          {
            path: '/informationEntry',
            name: "informationEntry",
            meta: {
              title: '产品信息录入',
              sign: 'informationEntry',
              belong: 'AllReport'
            },
            component: () => import('@/views/report/tort/components/informationEntry/index.vue'),
          },

          //回避设计
          {
            path: '/avoid',
            name: "avoid",
            meta: {
              title: '回避设计',
              sign: 'avoid',
              belong: 'AllReport'
            },
            component: () => import('@/views/report/avoid/index.vue'),
          },

          //无效应对
          {
            path: '/manualImport',
            name: "manualImport",
            meta: {
              title: '批量手动添加证据文献',
              sign: 'manualImport',
              belong: 'AllReport'
            },
            component: () => import('@/views/report/InvalidResponse/components/ManualImport/index.vue'),
          },
          //导入证据文献
          {
            path: '/evidence',
            name: 'evidence',
            meta: {
              title: '证据详情',
              sign: 'evidence',
              belong: 'AllReport'
            },
            component: () => import('@/views/report/InvalidResponse/components/Evidence/index.vue'),
          },
          //报告文档
          {
            path: '/reportFiles',
            name: 'reportFiles',
            meta: {
              title: '报告文档',
              sign: 'reportFiles',
            },
            component: () => import('@/views/report/components/reportFile/index.vue'),
          },
          //检索记录
          {
            path: '/searchRecord',
            name: 'searchRecord',
            meta: {
              title: '检索记录',
              sign: 'searchRecord',
            },
            component: () => import('@/views/report/analysisAndOpinion/components/searchRecords/index.vue'),
          },
        ],
      },
      //查新检索库
      {
        path: '/noveltySearchDB',
        name: 'noveltySearchDB',
        meta: {
          title: '查新检索标注库',
          sign: 'noveltySearchDB',
          belong: 'noveltySearchDB',
          keepAlive:true
        },
        component: () => import('@/views/components/noveltySearch/index.vue'),
      },
      //查新检索
      {
        path: '/noveltySearch',
        name: 'noveltySearch',
        component: { render(c) { return c('router-view') } },
        redirect: '/noveltySearch',
        children: [
          {
            path: '/',
            name:'noveltySearch',
            meta: {
              title: '查新检索',
              sign: 'noveltySearch',
              belong: 'AllReport',
              permission:'xiaoshi/noveltySearch'
            },
            component: () => import('@/views/noveltySearch/index.vue'),
          },
          {
            path: '/noveltySearchDetails',
            name: 'noveltySearchDetails',
            meta: {
              title: '查新检索详情',
              sign: 'noveltySearchDetails',
              belong: 'AllReport',
              keepAlive:true
            },
            component: () => import('@/views/noveltySearch/components/details/index.vue'),
          },
          // 检索记录
          {
            path: '/noveltySearchRecord',
            name: 'noveltySearchRecord',
            meta: {
              title: '查新检索记录',
              sign: 'noveltySearchRecord',
              belong: 'AllReport',
            },
            component: () => import('@/views/noveltySearch/components/details/components/contrastRecords/index.vue'),
          },
          // 对比文件
          {
            path: '/noveltySearchFile',
            name: 'noveltySearchFile',
            meta: {
              title: '查新对比文件',
              sign: 'noveltySearchFile',
              belong: 'AllReport',
            },
            component: () => import('@/views/noveltySearch/components/details/components/contrastFile/index.vue'),
          },
          // 导出报告
          {
            path: '/exportReport',
            name: 'exportReport',
            meta: {
              title: '导出报告',
              sign: 'exportReport',
              belong: 'AllReport',
            },
            component: () => import('@/views/noveltySearch/components/exportReport/index.vue'),
          },
          // 复用结果
          {
            path: '/reuseResults',
            name: 'reuseResults',
            meta: {
              title: '复用结果',
              sign: 'reuseResults',
              belong: 'AllReport',
            },
            component: () => import('@/views/noveltySearch/components/reuseResults/index.vue'),
          },
        ]
      },
      //任务模块
      {
        path: '/myTask',
        name: 'myTask',
        component: { render(c) { return c('router-view') } },
        redirect: '/myTask',
        children: [
          {
            path: '/',
            name:'myTask/',
            meta: {
              title: '我的任务',
              sign: 'myTask',
              belong: 'myTask'
            },
            component: () => import('@/views/task/index.vue'),
          },
        ]
      },
      //场景可视化
      {
        path: '/visual',
        name: 'visual',
        meta: {
          title: '场景可视化',
          sign: 'visual',
          belong: 'visual'
        },
        component: () => import('@/views/visual')
      },
      {
        path: '/visual/:title',
        name: 'flowPath',
        meta: {
          sign: 'flowPath',
          title: '可视化平台',
          belong: 'visual'
        },
        component: () => import('@/views/visual/title/index.vue')
      },
      //图表分析
      {
        path: "customChart",
        name:'customChart',
        component: () => import('@/views/analyse/custom'),
        meta: {
          sign: 'customChart',
          title: '自定义分析列表'
        }
      },
      // {
      //   path: "report",
      //   component: () => import('@/views/analyse/report/create'),
      //   meta: {
      //     title: '一键生成报告'
      //   }
      // },
      //查看文件
      {
        path: '/checkFile',
        name: '/checkFile',
        meta: {
          aside: true,
          sign: 'checkFile',
        },
        component: () => import('@/views/components/view/checkFile.vue'),
      },
      //excel导入
      {
        path: '/import',
        name: "import",
        meta: {
          title: 'excel导入',
          sign: 'import'
        },
        component: () => import('@/views/components/import/excelImport'),
      },
      //专利号导入
      {
        path: '/PatentNoImport',
        name: "PatentNoImport",
        meta: {
          title: '专利号导入',
          sign: 'PatentNoImport'
        },
        component: () => import('@/views/components/import/PatentNoImport'),
      },
      //检索导入
      {
        path: '/conditionImport',
        name: "conditionImport",
        meta: {
          title: '专利检索',
          sign: 'conditionImport',
          belong: 'conditionImport',
          permission:'xiaoshi/patentSearch'
        },
        component: () => import('@/views/components/import/conditionImport'),
      },
      //检索帮助
      {
        path: '/searchHelp',
        name: "searchHelp",
        meta: {
          title: '检索说明',
          sign: 'searchHelp',
          belong: 'searchHelp',
        },
        component: () => import('@/views/components/import/conditionImport/components/searchHelp/index.vue'),
      },
      //检索历史
      {
        path: "/searchHistory",
        name:'searchHistory',
        meta: {
          title: '检索历史',
          sign:'searchHistory',
          belong: 'conditionImport',
        },
        component: () => import('@/views/components/import/conditionImport/components/searchHistory/index.vue'),
      },
      //检索结果
      {
        path: "/searchResult",
        name:'searchResult',
        meta: {
          title: '检索结果',
          // button: [],
        },
        component: () => import('@/views/components/import/conditionImport/searchResultIndex.vue'),
      },
      // 任务清单
      {
        path: "/taskList",
        name: 'taskList',
        meta: {
          title: '任务清单',
          sign: 'taskList'
        },
        component: () => import('@/views/components/import/task/index.vue'),
      },
      //标注库
      {
        path: "/indicia",
        name:'indicia',
        meta: {
          title: '标注库',
          sign: 'indicia',
          permission:'xiaoshi/annotationLibrary'
        },
        component: () => import('@/views/components/indicia/index.vue'),
      },
      //错误网页
      {
        path: '/errorPage',
        name: 'errorPage',
        component: { render(c) { return c('router-view') } },
        redirect: '/errorPage',
        children: [
          {
            path: '/403',
            name:'/403',
            meta: {
              title: '无权限',
              sign: '403',
              belong: 'errorPage',
              notReturn:true
            },
            component: () => import('@/views/components/errorPage/403.vue'),
          },
          {
            path: '/404',
            name:'/404',
            meta: {
              title: '页面不存在',
              sign: '404',
              belong: 'errorPage',
              notReturn:true
            },
            component: () => import('@/views/components/errorPage/404.vue'),
          },
        ],
      },

    ]
  },
]

const router = new VueRouter({
  mode: 'history',
  base: '/',
  stringifyQuery: stringifyQuery, // 序列化query参数
  parseQuery: parseQuery, // 反序列化query参数
  routes
})



function savePreviousRoute(to){
  if(to.meta.notReturn || to.path == '/login' || to.path == '/'){
    return
  }
  var redirectUrl = {
    userName:localStorage.getItem('username'),
    path:to.path,
    query:to.query,
    params:to.params
  }
  localStorage.setItem('redirectUrl',JSON.stringify(redirectUrl))
}

let lastRoute = null

function setLastRoute(to,from){
  if(to.name == 'patentDetails'){
    lastRoute = {
      path:from.path,
      query:from.query,
      params:from.params
    }
  }
}

export function getLastRoute(){
  return lastRoute
}


router.beforeEach(async (to, from, next) => {
  setLastRoute(to,from)
  savePreviousRoute(to)
  // if(to.path == '/login'){
  //   Cookies.remove("token")
  //   localStorage.removeItem('vuex')
  // }
  // if(from.path.indexOf('/patentDetails/')!=-1 && to.path.indexOf('/patentDetails/')==-1){
  //   sessionStorage.setItem('search', JSON.stringify({}))
  // }
  const tokenStr = Cookies.get('token')
  if (to.meta.title) {
    document.title = to.meta.title;
  }
  if(to.path === '/login' || to.path === '/' || to.path == '/agreeConceal' || to.meta.allowPath){
    if(to.path === '/login'){
      if(to.query.url){
        if(tokenStr){
          var url = to.query.url
            if(url){
              next({
                path: url
              })
              return
            }
        }
      }else{
        Cookies.remove("token")
        localStorage.removeItem('vuex')
      }
    }
    next()
  }else{
    if(tokenStr === undefined){
      if (to.meta.title) {
        document.title = to.meta.title;
      }
      next({ name: 'Login' })
      return
    }
    if(to.meta.permission && !Permission.FunPermissions(to.meta.permission)){
      next({ path: '/403' })
      return
    }

    if((to.path=='/403' || to.path=='/404') && from.path=='/login'){
      next({ path:'/home'})
      return
    }

    //单个页面设置权限
    if(to.path=='/project/patentCollection'){
      var projectId = to.query.id
      if(!projectId){
        next({ path: '/404' })
        return
      }
      var sign = await Permission.projectPermission(projectId)
      if(sign === 0){
        next({ path: '/403' })
        return
      }
      next()
      return
    }
    next()
  }


  // if (to.path === '/login' || to.path === '/' || to.path == '/agreeConceal') {
  //   if(to.path === '/login'){
  //     if(to.query.url){
  //       if(tokenStr){
  //         var url = to.query.url
  //           if(url){
  //             next({
  //               path: url
  //             })
  //             return
  //           }
  //       }
  //     }else{
  //       Cookies.remove("token")
  //       localStorage.removeItem('vuex')
  //     }
  //   }
  //   if (to.meta.title) {
  //     document.title = to.meta.title;
  //   }
  //   next()
  // }
  // else if(to.meta.permission && !Permission.FunPermissions(to.meta.permission)){
  //   next({ path: '/403' })
  // }
  // else if (to.meta.allowPath) {
  //   if (to.meta.title) {
  //     document.title = to.meta.title;
  //   }
  //   next()
  // }
  // else {
  //   switch (tokenStr) {
  //     case undefined:
  //       if (to.meta.title) {
  //         document.title = to.meta.title;
  //       }
  //       next({ name: 'Login' })
  //       break
  //     default:
  //       if (to.meta.title) {
  //         document.title = to.meta.title;
  //       }
  //       next()
  //       break;
  //   }
  // }
})

router.afterEach((to, from) => {

});

export default router
