import Vue from 'vue';
import Router from 'vue-router';
import {getStore} from "@/utils/storage";

Vue.use(Router);

const routes = [
    {
        path: '/',
        redirect: '/login'
    },
    {
        path: '/login',
        name: 'login',
        component: () => import('./view/login'),
        meta: {
            // title: '体温打卡，中国加油！',
            title: '企业登录',
            no_login: true
        }
    },
    {
        path: '/join',
        name: 'join',
        component: () => import('./view/business/join'),
        meta: {
            title: '企业入驻'
        }
    },
    {
        path: '/stafflogin',
        name: 'stafflogin',
        component: () => import('./view/stafflogin'),
        meta: {
            title: '员工登录'
        }
    },
    {
        path: '/invitation',
        name: 'invitation',
        component: () => import('./view/invitation'),
        meta: {
            title: '体温打卡，中国加油！'
        },
    },
    {
        path: '/next',
        name: 'next',
        component: () => import('./view/invitation/next'),
        meta: {
            title: '体温打卡，中国加油！'
        },
    },
    {
        path: '/index',
        name: 'index',
        component: () => import('./view/index/index'),
        meta: {
            title: '体温打卡，中国加油！'
        }
    },
    {
        path: '/business/temperature',
        name: 'temperature',
        component: () => import('./view/business/temperature'),
        meta: {
            title: '员工体温信息'
        }
    },
    {
        path: '/business/return_journey',
        name: 'return_journey',
        component: () => import('./view/business/return_journey'),
        meta: {
            title: '员工返程信息'
        }
    },
    {
        path: '/business/journey_detail',
        name: 'journey_detail_business',
        component: () => import('./view/business/journey_detail'),
        meta: {
            title: '返程信息详情'
        }
    },
    {
        path: '/journey_detail/journey_detail',
        name: 'journey_detail',
        component: () => import('./view/journey_detail/journey_detail'),
        meta: {
            title: '返程信息详情'
        }
    },
    {
        path: '/staff_temperature',
        name: 'staff_temperature',
        component: () => import('./view/staff/temperature'),
        meta: {
            title: '每日体温打卡'
        }
    },
    {
        path: '/clock',
        name: 'clock',
        component: () => import('./view/staff/clock'),
        meta: {
            title: '体温打卡记录'
        }
    },
    {
        path: '/donations',
        name: 'donations',
        component: () => import('./view/donations/index'),
        meta: {
            title: '捐款记录'
        }
    },
    {
        path: '/interview/xiapu',
        name: 'xiapu',
        component: () => import('./view/interview/xiapu'),
        meta: {
            title: '夏普面试登记'
        }
    },
    {
        path: '/talent_exchange/index',
        name: 'talentExchange',
        component: () => import('./view/talent_exchange/Index'),
        meta: {
            title: '人才交流会签到'
        }
    },
    {
        path: '/captcha/captcha',
        name: 'captcha',
        component: () => import('./view/captcha/captcha'),
        meta: {
            title: '获取验证码'
        }
    },
    {
        path: '*',
        name: 'login',
        component: () => import('./view/login/index'),
        meta: {
            title: '企业登录'
        }
    },
];

// add route path
routes.forEach(route => {
    route.path = route.path || '/' + (route.name || '');
});

const router = new Router(
    {
        mode: 'history',
        routes
    });
const whiteList = [
    'login',
    'stafflogin',
    'invitation',
    'next',
    'join',
    'xiapu',
    'talentExchange',
    'captcha'
]
router.beforeEach((to, from, next) => {
    const title = to.meta && to.meta.title;
    if (title) {
        document.title = title;
    }

    if ( whiteList.includes(to.name)  ) {
        if(to.name === 'talentExchange'){
            const body = document.getElementsByTagName('body')[0];
            body.style.background = '#f5f5f5'
            body.style.height = '100%'
        }
        next()
    } else {
        if (getStore('role')) {
            next()
        } else {
            next({name: 'login'})
        }
    }

});

export {
    router
};
