import { createRouter, createWebHistory } from "vue-router";
import store from "@/store";
/**
 * 说明
 * meta中 
 * keepAlive表示是否要缓存  
 * isRoot代表是否为侧边栏组件用于处理面包屑数据 
 * hasBack 控制是否有返回按钮
 */

const routes=[
  {
    path: "/",
    name: "Layout",
    redirect:'/report/index',
    component: ()=>import("@/layout/Index.vue"),
  },
  //活动模块
  {
    path: "/activity",
    name: "Activity",
    redirect:'/activity/list',
    component: () => import("@/layout/Index.vue"),
    meta: {
      title:"活动"
    },
    children: [
      {
        path: "list",
        name: "ActivityList",
        component: () => import("@/views/activity/List.vue"),
        meta: {
          title: "活动",
          keepAlive:true,
          isRoot:true
        },
      },
      {
        path:'detail',//query: id(活动id)
        name:"ActivityDetail",
        component: () => import("@/views/activity/Detail.vue"),
        meta: {
          title: "活动详情",
          hasBack:true
        },
      },
      {
        path: "reportdetail",
        name: "ActivityReportDetail",
        component: () => import("@/views/activity/ReportDetail.vue"),
        meta: {
          title: "报告详情",
          hasBack:true
        }
      },
      {
        path: "chapterdetail",
        name: "ActivityChapterReportDetail",
        component: () => import("@/views/activity/ChapterDetail.vue"),
        meta: {
          title: "报告详情",
          hasBack:true
        }
      }
    ]
  },
  // 报告模块
  {
    path:'/report',
    name:'Report',
    redirect: '/report/classify',
    component: () => import("@/layout/Index.vue"),
    meta: {
      title:"研报"
    },
    children:[
      // 研报与首页区分开了 首页为原来的研报页面，现在的研报页面是分类 -- 2022/10/17
      {
        path: "index",
        name: "ReportIndex",
        component: () => import("@/views/report/Index.vue"),
        meta: {
          title: "首页",
          keepAlive:true,
          isRoot:true
        },
      },
      {
        path: "classify",
        name: "ReportClassify",
        component: () => import("@/views/report/Classify.vue"),
        meta: {
          title: "研报",
          keepAlive:false,
          isRoot:true
        },
      },
      {
        path: "search",
        name: "ReportSearch",
        component: () => import("@/views/report/Search.vue"),
        meta: {
          title: "研报搜索",
          keepAlive:true,
          isRoot:false,
          hasBack:true
        },
      },
      {
        path: "detail",
        name: "ReportDetail",
        component: () => import("@/views/report/Detail.vue"),
        meta: {
          title: "报告详情",
          keepAlive:false,
          isRoot:false,
          hasBack:true
        },
      },
      {
        path: "chapterdetail",
        name: "ReportChapterDetail",
        component: () => import("@/views/report/ChapterDetail.vue"),
        meta: {
          title: "报告详情",
          keepAlive:false,
          isRoot:false,
          hasBack:true
        },
      },
      {
        path: "list",
        name: "ReportList",
        component: () => import("@/views/report/List.vue"),
        meta: {
          title: "报告列表",
          keepAlive:true,
          isRoot:false,
          hasBack:true
        },
      },
      {
        path: "specialcolumnlist",
        name: "ReportSpecialColumnList",
        component: () => import("@/views/report/specialColumn/List.vue"),
        meta: {
          title: "专栏列表",
          keepAlive:true,
          isRoot:false,
          hasBack:true
        },
      },
      {
        path: "specialcolumndetail",
        name: "ReportSpecialColumnDetail",
        component: () => import("@/views/report/specialColumn/Detail.vue"),
        meta: {
          title: "专栏详情",
          keepAlive:true,
          isRoot:false,
          hasBack:true
        },
      },
      {
        path: "ficcserveintro",
        name: "FiccServeIntro",
        component: () => import("@/views/report/FiccIntroduce.vue"),
        meta: {
          title: "FICC服务介绍",
          keepAlive:false,
          isRoot:false,
          hasBack:true
        },
      },
      {
        path: "varietyreportlist",
        name: "VarietyReportList",
        component: () => import("@/views/report/reportForVariety/List.vue"),
        meta: {
          title: "报告列表",
          keepAlive:true,
          isRoot:false,
          hasBack:true
        },
      },
      {
        path: "previewPDF",
        name: "PreviewPDF",
        component: () => import("@/views/report/PreviewPDF.vue"),
        meta: {
          title: "预览报告",
          keepAlive:true,
          isRoot:false,
          hasBack:true
        },
      },
      {
        path: "disseminatePage",
        name: "disseminatePage",
        component: () => import("@/views/report/disseminatePage.vue"),
        meta: {
          title: "活动详情",
          keepAlive:true,
          isRoot:false,
          hasBack:true
        },
      },
      {
        path: "surveyHistory",
        name: "surveyHistory",
        component: () => import("@/views/report/surveyHistory.vue"),
        meta: {
          title: "调研列表",
          keepAlive:true,
          isRoot:true
        },
      },
    ]
  },
  {
    path: "/apply",
    name: "Apply",
    component: () => import("@/layout/Index.vue"),
    meta: {
      title:"申请试用"
    },
    children: [
      {
        path: "permission",// source来源(如：2) fromPage=来源页面(如:活动列表)
        name: "ApplyPermission",
        component: () => import("@/views/applyPermission/Apply.vue"),
        meta: {
          title: "申请试用",
          hasBack:true
        }
      },
      {
        path: "result",
        name: "ApplyPermissionResult",
        component: () => import("@/views/applyPermission/Result.vue"),
        meta: {
          title: "申请结果",
          hasBack:true
        }
      }
    ]
  },

  // 图库模块
  {
    path:'/chart',
    name:"Chart",
    component: () => import("@/layout/Index.vue"),
    meta:{
      title:"ETA图库"
    },
    children:[
      {
        path:"list",
        name:"ChartList",
        component:()=>import('@/views/chart/List.vue'),
        meta: {
          title: "ETA图库",
          keepAlive:true,
          isRoot:true
        }
      }
    ]
  },

  // 沙盘推演模块
  {
    path:'/sandBox',
    name:"sandBox",
    component: () => import("@/layout/Index.vue"),
    redirect:'/sandBox/list',
    meta:{
      title:"沙盘推演"
    },
    children:[
      {
        path:"list",
        name:"sandBoxList",
        component:()=>import('@/views/sandBox/List.vue'),
        meta: {
          title: "沙盘推演",
          keepAlive:true,
          isRoot:true
        }
      }
    ]
  },
    //问答模块
    {
      path: "/question",
      name: "Question",
      redirect:'/question/list',
      component: () => import("@/layout/Index.vue"),
      meta: {
        title:"问答"
      },
      children: [
        {
          path: "list",
          name: "QuestionList",
          component: () => import("@/views/question/List.vue"),
          meta: {
            title: "问答社区",
            keepAlive:true,
            isRoot:true
          }
        },{
          path: "mylist",
          name: "QuestionMyList",
          component: () => import("@/views/question/MyList.vue"),
          meta: {
            title: "我的问答",
            keepAlive:true,
            // isRoot:false
            isRoot:true
          }
        }
      ]
    },
  // 用户模块
  {
    path:'/user',
    name:"User",
    component: () => import("@/layout/Index.vue"),
    meta:{
      title:"用户"
    },
    children:[
      {
        path:"setinfo",
        name:"UserSetInfo",
        component:()=>import('@/views/user/SetUserInfo.vue'),
        meta: {
          title: "我的设置",
          keepAlive:false,
          isRoot:false,
          hasBack:true
        }
      }
    ]
  },

  //价格驱动
  {
    path: '/pricedriven',
    name: "priceDriven",
    component: () => import("@/layout/Index.vue"),
    meta: {
      title:"价格驱动"
    },
    children: [
      {
        path: "/pricedriven",
        name: "priceDrivenDeteail",
        component: () => import("@/views/priceDriven/detail.vue"),
        meta: {
          title: "价格驱动",
          keepAlive:false,
          isRoot:true
        },
      },
    ]  
  },
  //视频社区模块
  {
    path:'/video',
    name:"Video",
    component: () => import("@/layout/Index.vue"),
    meta:{
      title:"视频社区"
    },
    children:[
      {
        path:"list",
        name:"VideoList",
        component:()=>import('@/views/video/List.vue'),
        meta: {
          title: "视频社区",
          keepAlive:true,
          isRoot:true
        }
      }
    ]
  },
  //语音播报模块
  {
    path:'/voice',
    name:"Voice",
    component: () => import("@/layout/Index.vue"),
    meta:{
      title:"语音播报"
    },
    children:[
      {
        path:"list",
        name:"VoiceList",
        component:()=>import('@/views/voice/List.vue'),
        meta: {
          title: "语音播报",
          keepAlive:true,
          isRoot:true
        }
      },
      {
        path:"mine",
        name:"VoiceMine",
        component:()=>import('@/views/voice/Mine.vue'),
        meta: {
          title: "我的语音",
          keepAlive:true,
          isRoot:true
        }
      },
      {
        path:"detail",
        name:"VoiceDetail",
        component:()=>import('@/views/voice/Detail.vue'),
        meta: {
          title: "语音详情",
          keepAlive:false,
          isRoot:false,
          hasBack:true
        }
      }
    ]
  },
  //路演视频模块
  {
    path:'/roadshow',
    name:"RoadShow",
    component: () => import("@/layout/Index.vue"),
    meta:{
      title:"线上路演"
    },
    children:[
      {
        path:"video/list",
        name:"RoadShowVideoList",
        component:()=>import('@/views/roadShow/video/List.vue'),
        meta: {
          title: "线上路演",
          keepAlive:true,
          isRoot:true
        }
      }
    ]
  },
  // 我的图库模块
  {
    path:"/mychart",
    name: "MyChart",
    component: () => import("@/layout/Index.vue"),
    meta: {
      title:"我的图库"
    },
    children:[
      {
        path:"list",
        name:"MyChartList",
        component:()=>import('@/views/myChart/List.vue'),
        meta: {
          title: "我的图库",
          keepAlive:true,
          isRoot:true
        }
      },
      {
        path:"search",
        name:"MyChartSearch",
        component:()=>import('@/views/myChart/Search.vue'),
        meta: {
          title: "搜索",
          keepAlive:true,
          isRoot:false,
          hasBack:true
        }
      },
      {
        path:"detail",
        name:"MyChartDetail",
        component:()=>import('@/views/myChart/Detail.vue'),
        meta: {
          title: "图表详情",
          keepAlive:false,
          isRoot:false,
          hasBack:true
        }
      }
    ]
  },
  //持仓分析模块
  {
    path:"/positionanalysis",
    name:"PositionAnalysis",
    component: () => import("@/layout/Index.vue"),
    meta: {
      title:"持仓分析"
    },
    children:[
      {
        path:"index",
        name:"PositionAnalysisIndex",
        component:()=>import('@/views/positionAnalysis/Index.vue'),
        meta: {
          title: "持仓列表",
          keepAlive:true,
          isRoot:true
        }
      },
      {
        path:"detail",
        name:"PositionAnalysisDetail",
        component:()=>import('@/views/positionAnalysis/Detail.vue'),
        meta: {
          title: "持仓详情",
          keepAlive:false,
          isRoot:false,
          hasBack:true
        }
      },
    ]
  },

  {
    path: '/:pathMatch(.*)',
    name: 'error',
    component: () => import("@/views/404.vue"),
    meta: { title: '404' },
  }

]

const router=createRouter({
  history:createWebHistory(import.meta.env.VITE_APP_BASE_URL),
  routes,
  scrollBehavior(to, from, savedPosition){
    if (savedPosition&&to.meta.keepAlive) {
      return savedPosition
    } else {
      return new Promise((resolve)=>{
        setTimeout(() => {
          resolve({left:0,top:0})
        }, 0);
      })
    }
  }
})

router.beforeEach((to, from, next) => {
	if (to.query.token) {
		store.commit('getToken', to.query.token)
    store.dispatch('getUserInfo')
	}
  if (to.query.platform_source) { // 来源是否是小程序
		sessionStorage.setItem('platformSource', to.query.platform_source)
	}
  if(to.meta.isRoot){
    store.commit('setBreadCrumb', to)
  }else{
    if(store.state.breadCrumbList.length==1){
      store.commit('modifyBreadCrumb',to.meta.title)
    }
  }
  
  document.title=to.meta.title
	next();
})


export default router