import Vue from 'vue'
import VueRouter from 'vue-router'
import storeInfo from '../store/index'


// 进度条
import NProgress from 'nprogress'

let activeUrl = window.sessionStorage.getItem('activeItem') || '/Home/inStock'
NProgress.configure({showSpinner: false});

Vue.use(VueRouter)

const routes = [
    {path: '/', redirect: activeUrl},
    {path: '/Login', component: () => import('@/views/Login.vue')},
    {path: '/unitLogin', component: () => import('@/views/Login.vue')},
    {
        path: '/Home',
        component: () => import('@/views/Home_dev.vue'),
        children: [
            // 叶酸发放
            {path: 'folviteIndex', name: '首页', component: () => import('@/views/Index.vue')},
            //在库管理
            {path: 'inStock', name: '在库管理', component: () => import('@/views/indexComponent/inStockManage/inStockManage.vue')},
            //叶酸发放登记
            {path: 'folviteDistribution', name: '叶酸发放登记列表', component: () => import('@/views/indexComponent/folviteDistribution/folviteDistribution.vue')},
            //分配入库
            {path: 'distributionWarehousing', name: '分配入库列表', component: () => import('@/views/indexComponent/distributionWarehousing/distributionWarehousing.vue')},
            //发货方分配记录
            {path: 'sendRecord', name: '分配入库列表', component: () => import('@/views/indexComponent/distributionWarehousing/sendRecord.vue')},
            //出入库记录
            {path: 'stockTransfer', name: '出入库记录', component: () => import('@/views/indexComponent/stockTransfer/stockTransfer.vue')},
            //叶酸申请管理
            {path: 'folviteApply', name: '叶酸申请管理', component: () => import('@/views/indexComponent/folviteApply/folviteApply.vue')},
            //知情同意书管理
            {path: 'InformedConsent', name: '知情同意书', component: () => import('@/views/indexComponent/InformedConsent/InformedConsent.vue')},
            //供应商管理
            {path: 'supplyManage', name: '供应商管理', component: () => import('@/views/indexComponent/supplyManage/supplyManage.vue')},
        ],
    },
    //在库管理
    {path: '/inStock/add', name: '库存录入', secondFlag: true,component: () => import('@/views/indexComponent/inStockManage/components/addStock.vue')},
    {path: '/inStock/addMaterialDistribution', name: '调拨分配', component: () => import('@/views/indexComponent/inStockManage/components/addMaterialDistribution.vue')},
    {path: '/inStock/inStockManageDetail', name: '在库详情', component: () => import('@/views/indexComponent/inStockManage/components/inStockManageDetail.vue')},
    //叶酸发放登记
    {path: '/folviteDistribution/add', name: '叶酸发放登记', component: () => import('@/views/indexComponent/folviteDistribution/components/addFolviteDistribution.vue')},
    {path: '/folviteDistribution/detail', name: '叶酸发放详情', component: () => import('@/views/indexComponent/folviteDistribution/components/folviteDistributionDetail.vue')},
    //分配入库
    {path: '/distributionWarehousing/add', name: '分配入库', component: () => import('@/views/indexComponent/distributionWarehousing/components/addDistributionWarehousing.vue')},
    {path: '/distributionWarehousing/detail', name: '分配入库详情', component: () => import('@/views/indexComponent/distributionWarehousing/components/distributionWarehousingDetail.vue')},
    //叶酸申请管理
    {path: '/folviteApply/grant', name: '叶酸申请发放', component: () => import('@/views/indexComponent/folviteApply/components/folviteApplyGrant.vue')},
    //供应商管理
    {path: '/supplyManage/add', name: '新增供应商', component: () => import('@/views/indexComponent/supplyManage/components/addSupply.vue')},
]

const router = new VueRouter({
    mode: 'hash',
    base: process.env.BASE_URL,
    routes
})


// 路由守卫
router.beforeEach((to, form, next) => {
    NProgress.start();
    if (to.path === '/Home/folviteIndex') {
        return next();
    };
    //未匹配到配置路由,默认跳转登录页
    if (to.matched.length === 0){
        next("/Login");
        return ;
    }
    if (to.path==='/Login'){
        next();
        return;
    }
    const token = window.sessionStorage.getItem('token');
    if (token) {
        next();
    } else {
        next("/Login");
        //window.top.postMessage({messageType:"LOGOUT"}, '*')
    }
});
router.afterEach((to, form, next) => {
    NProgress.done();
});
// 解决重复点击二级菜单报错的问题
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err);
    // return originalPush.call(this, location);

}

export default router
