import Vue from 'vue';
import Router from 'vue-router';
import Admin from '@/views/Admin';
import store from './store';
import http from './plugins/http';
import { Message } from 'element-ui';

Vue.use(Router);

const router = new Router({
    mode: 'history',
    base: process.env.BASE_URL,
    routes: [
        {
            path: '/',
            component: Admin,
            children: [
                {
                    path: '/',
                    redirect: 'dashboard'
                },
                {
                    path: '/404',
                    name: '404',
                    component: () => import(/* webpackChunkName: "404" */ '@/views/404.vue')
                },
                {
                    path: '/dashboard',
                    name: 'dashboard',
                    component: () => import(/* webpackChunkName: "404" */ '@/views/Dashboard.vue'),
                    meta: {
                        title: '首页'
                    }
                },
                {
                    path: '/api',
                    name: 'api',
                    component: () => import(/* webpackChunkName: "api" */ '@/views/Api.vue'),
                    meta: {
                        title: '接口调试'
                    }
                },
                {
                    path: '/genCodeEdit',
                    name: 'genCodeEdit',
                    component: () => import(/* webpackChunkName: "genCodeEdit" */ '@/views/GenCodeEdit.vue'),
                    meta: {
                        title: '代码生成编辑'
                    }
                },
                {
                    path: '/genCodeList',
                    name: 'genCodeList',
                    component: () => import(/* webpackChunkName: "genCodeList" */ '@/views/GenCodeList.vue'),
                    meta: {
                        title: '代码生成'
                    }
                },
                {
                    path: '/menus',
                    name: 'menus',
                    component: () => import(/* webpackChunkName: "menus" */ '@/views/Menus.vue'),
                    meta: {
                        title: '菜单配置'
                    }
                },
                {
                    path: '/menuAuthority',
                    name: 'menuAuthority',
                    component: () => import(/* webpackChunkName: "menuAuthority" */ '@/views/MenuAuthority.vue'),
                    meta: {
                        title: '菜单权限'
                    }
                },
                {
                    path: '/userEdit',
                    name: 'userEdit',
                    component: () => import(/* webpackChunkName: "userEdit" */ '@/views/user/UserEdit.vue'),
                    meta: {
                        title: '用户编辑'
                    }
                },
                {
                    path: '/userList',
                    name: 'userList',
                    component: () => import(/* webpackChunkName: "userList" */ '@/views/user/UserList.vue'),
                    meta: {
                        title: '用户管理'
                    }
                },
                {
                    path: '/echarts',
                    name: 'echarts',
                    component: () => import('@/views/Echarts.vue'),
                    meta: {
                        title: '地图图表'
                    }
                },
                {
                    path: '/video',
                    name: 'video',
                    component: () => import(/* webpackChunkName: "userList" */ '@/views/Video.vue'),
                    meta: {
                        title: '监控平台'
                    }
                },
                {
                    path: '/assignExpert',
                    name: 'assignExpert',
                    component: () => import(/* webpackChunkName: "userList" */ '@/views/rate/AssignExpert.vue'),
                    meta: {
                        title: '分配专家组'
                    }
                },
                {
                    path: '/arrangeAssignExpert',
                    name: 'arrangeAssignExpert',
                    component: () =>
                        import(
                            /* webpackChunkName: "arrangeAssignExpert" */ '@/views/performance/ArrangeAssignExpert.vue'
                        ),
                    meta: {
                        title: '分配评委'
                    }
                },
                {
                    path: '/gradeList',
                    name: 'gradeList',
                    component: () => import(/* webpackChunkName: "userList" */ '@/views/rate/GradeList.vue'),
                    meta: {
                        title: '评分'
                    }
                },
                {
                    path: '/organization',
                    name: 'organization',
                    component: () => import(/* webpackChunkName: "userList" */ '@/views/organization/Organization.vue'),
                    meta: {
                        title: '等级评定'
                    }
                },
                {
                    path: '/organization1',
                    name: 'organization1',
                    component: () =>
                        import(/* webpackChunkName: "userList" */ '@/views/organization/Organization1.vue'),
                    meta: {
                        title: '等级评定'
                    }
                },
                {
                    path: '/expertList',
                    name: 'expertList',
                    component: () => import(/* webpackChunkName: "expertList" */ '@/views/user/ExpertList.vue'),
                    meta: {
                        title: '专家管理'
                    }
                },
                {
                    path: '/expertEdit',
                    name: 'expertEdit',
                    component: () => import(/* webpackChunkName: "expertEdit" */ '@/views/user/ExpertEdit.vue'),
                    meta: {
                        title: '专家编辑'
                    }
                },
                {
                    path: '/sysConfigList',
                    name: 'sysConfigList',
                    component: () => import(/* webpackChunkName: "sysConfigList" */ '@/views/SysConfigList.vue'),
                    meta: {
                        title: '参数配置'
                    }
                },
                {
                    path: '/upload',
                    name: 'upload',
                    component: () => import(/* webpackChunkName: "upload" */ '@/views/Upload.vue'),
                    meta: {
                        title: '上传'
                    }
                },
                {
                    path: '/recordEdit',
                    name: 'RecordEdit',
                    component: () => import(/* webpackChunkName: "recordEdit" */ '@/views/record/RecordEdit.vue'),
                    meta: {
                        title: '备案管理编辑'
                    }
                },
                {
                    path: '/recordOrganizationList',
                    name: 'RecordOrganizationList',
                    component: () =>
                        import(/* webpackChunkName: "recordList" */ '@/views/record/RecordOrganizationList.vue'),
                    meta: {
                        title: '备案管理'
                    }
                },
                {
                    path: '/recordCheck',
                    name: 'RecordCheck',
                    component: () => import(/* webpackChunkName: "recordCheck" */ '@/views/record/RecordCheck.vue'),
                    meta: {
                        title: '检查记录'
                    }
                },
                {
                    path: '/recordRegulationList',
                    name: 'RecordRegulationList',
                    component: () =>
                        import(/* webpackChunkName: "recordList" */ '@/views/record/RecordRegulationList.vue'),
                    meta: {
                        title: '监管管理'
                    }
                },
                {
                    path: '/recordRegulationHistory',
                    name: 'RecordRegulationHistory',
                    component: () =>
                        import(/* webpackChunkName: "recordList" */ '@/views/record/RecordRegulationHistory.vue'),
                    meta: {
                        title: '监管管理'
                    }
                },
                {
                    path: '/videoList',
                    name: 'VideoList',
                    component: () => import(/* webpackChunkName: "videoList" */ '@/views/record/VideoList.vue'),
                    meta: {
                        title: '视频监控'
                    }
                },
                {
                    path: '/videoOrganizationList',
                    name: 'VideoOrganizationList',
                    component: () =>
                        import(
                            /* webpackChunkName: "videoOrganizationList" */ '@/views/record/VideoOrganizationList.vue'
                        ),
                    meta: {
                        title: '视频监控'
                    }
                },
                {
                    path: '/recordDistrictList2',
                    name: 'RecordDistrictList2',
                    component: () =>
                        import(/* webpackChunkName: "recordList" */ '@/views/record/RecordDistrictList2.vue'),
                    meta: {
                        title: '监管管理'
                    }
                },
                {
                    path: '/recordDistrictList',
                    name: 'RecordList',
                    component: () =>
                        import(/* webpackChunkName: "recordList" */ '@/views/record/RecordDistrictList.vue'),
                    meta: {
                        title: '备案管理'
                    }
                },
                {
                    path: '/recordList',
                    name: 'RecordList',
                    component: () => import(/* webpackChunkName: "recordList" */ '@/views/record/RecordList.vue'),
                    meta: {
                        title: '备案管理'
                    }
                },
                {
                    path: '/performanceEdit',
                    name: 'PerformanceEdit',
                    component: () =>
                        import(/* webpackChunkName: "performanceEdit" */ '@/views/performance/PerformanceEdit.vue'),
                    meta: {
                        title: '展演管理编辑'
                    }
                },
                {
                    path: '/performanceList',
                    name: 'PerformanceList',
                    component: () =>
                        import(/* webpackChunkName: "performanceList" */ '@/views/performance/PerformanceList.vue'),
                    meta: {
                        title: '展演管理'
                    }
                },
                {
                    path: '/performanceListShow',
                    name: 'PerformanceListShow',
                    component: () =>
                        import(/* webpackChunkName: "performanceList" */ '@/views/performance/PerformanceListShow.vue'),
                    meta: {
                        title: '展演列表'
                    }
                },
                {
                    path: '/performanceShow',
                    name: 'PerformanceShow',
                    component: () =>
                        import(/* webpackChunkName: "performanceShow" */ '@/views/performance/PerformanceShow.vue'),
                    meta: {
                        title: '展演详情'
                    }
                },
                {
                    path: '/performanceApplyEdit',
                    name: 'PerformanceApplyEdit',
                    component: () =>
                        import(/* webpackChunkName: "performanceApplyEdit" */ '@/views/PerformanceApplyEdit.vue'),
                    meta: {
                        title: '展演报名编辑'
                    }
                },
                {
                    path: '/performanceApplyList',
                    name: 'PerformanceApplyList',
                    component: () =>
                        import(/* webpackChunkName: "performanceApplyList" */ '@/views/PerformanceApplyList.vue'),
                    meta: {
                        title: '展演报名'
                    }
                },
                {
                    path: '/performanceApplyList2',
                    name: 'PerformanceApplyList2',
                    component: () =>
                        import(/* webpackChunkName: "performanceApplyList" */ '@/views/PerformanceApplyList2.vue'),
                    meta: {
                        title: '展演报名'
                    }
                },
                {
                    path: '/personEdit',
                    name: 'PersonEdit',
                    component: () => import(/* webpackChunkName: "personEdit" */ '@/views/PersonEdit.vue'),
                    meta: {
                        title: '参演人员编辑'
                    }
                },
                {
                    path: '/personList',
                    name: 'PersonList',
                    component: () => import(/* webpackChunkName: "personList" */ '@/views/PersonList.vue'),
                    meta: {
                        title: '参演人员'
                    }
                },
                {
                    path: '/rateEdit',
                    name: 'RateEdit',
                    component: () => import(/* webpackChunkName: "rateEdit" */ '@/views/rate/RateEdit.vue'),
                    meta: {
                        title: '等级评定申请'
                    }
                },
                {
                    path: '/rateOrganizerList',
                    name: 'RateOrganizerList',
                    component: () => import(/* webpackChunkName: "rateList" */ '@/views/rate/RateOrganizerList.vue'),
                    meta: {
                        title: '等级评定'
                    }
                },
                {
                    path: '/rateExpertList',
                    name: 'RateExpertList',
                    component: () => import(/* webpackChunkName: "rateList" */ '@/views/rate/RateExpertList.vue'),
                    meta: {
                        title: '等级评定'
                    }
                },
                {
                    path: '/rateAudit',
                    name: 'RateAudit',
                    component: () => import(/* webpackChunkName: "rateAudit" */ '@/views/rate/RateAudit.vue'),
                    meta: {
                        title: '等级评定申请'
                    }
                },
                {
                    path: '/rateGrade',
                    name: 'RateGrade',
                    component: () => import(/* webpackChunkName: "rateGrade" */ '@/views/rate/RateGrade.vue'),
                    meta: {
                        title: '打分'
                    }
                },
                {
                    path: '/rateDistrictList',
                    name: 'RateDistrictList',
                    component: () => import(/* webpackChunkName: "rateList" */ '@/views/rate/RateDistrictList.vue'),
                    meta: {
                        title: '等级评定'
                    }
                },
                {
                    path: '/rateDistrictListPending',
                    name: 'RateDistrictListPending',
                    component: () =>
                        import(/* webpackChunkName: "rateList" */ '@/views/rate/RateDistrictListPending.vue'),
                    meta: {
                        title: '申请待办'
                    }
                },
                {
                    path: '/rateDistrictListDone',
                    name: 'RateDistrictListDone',
                    component: () => import(/* webpackChunkName: "rateList" */ '@/views/rate/RateDistrictListDone.vue'),
                    meta: {
                        title: '申请已办'
                    }
                },
                {
                    path: '/rateList',
                    name: 'RateList',
                    component: () => import(/* webpackChunkName: "rateList" */ '@/views/rate/RateList.vue'),
                    meta: {
                        title: '等级评定'
                    }
                },
                {
                    path: '/rateListPending',
                    name: 'RateListPending',
                    component: () => import(/* webpackChunkName: "rateList" */ '@/views/rate/RateListPending.vue'),
                    meta: {
                        title: '申请待办'
                    }
                },
                {
                    path: '/rateListDone',
                    name: 'RateListDone',
                    component: () => import(/* webpackChunkName: "rateList" */ '@/views/rate/RateListDone.vue'),
                    meta: {
                        title: '申请完成'
                    }
                },
                {
                    path: '/rateListRejected',
                    name: 'RateListRejected',
                    component: () => import(/* webpackChunkName: "rateList" */ '@/views/rate/RateListRejected.vue'),
                    meta: {
                        title: '驳回记录'
                    }
                },
                {
                    path: '/organizationInfo',
                    name: 'OrganizationInfo',
                    component: () =>
                        import(/* webpackChunkName: "organizationInfo" */ '@/views/organization/OrganizationInfo.vue'),
                    meta: {
                        title: '基本信息'
                    }
                },
                {
                    path: '/organizationEdit',
                    name: 'OrganizationEdit',
                    component: () =>
                        import(/* webpackChunkName: "organizationEdit" */ '@/views/organization/OrganizationEdit.vue'),
                    meta: {
                        title: '承办单位编辑'
                    }
                },
                {
                    path: '/organizationList',
                    name: 'OrganizationList',
                    component: () =>
                        import(/* webpackChunkName: "organizationList" */ '@/views/organization/OrganizationList.vue'),
                    meta: {
                        title: '承办单位'
                    }
                },
                {
                    path: '/artTypes',
                    name: 'ArtTypes',
                    component: () => import(/* webpackChunkName: "artTypes" */ '@/views/ArtTypes.vue'),
                    meta: {
                        title: '艺术分类'
                    }
                },
                {
                    path: '/artTypeEdit',
                    name: 'ArtTypeEdit',
                    component: () => import(/* webpackChunkName: "artTypeEdit" */ '@/views/ArtTypeEdit.vue'),
                    meta: {
                        title: '艺术分类编辑'
                    }
                },
                {
                    path: '/artTypeList',
                    name: 'ArtTypeList',
                    component: () => import(/* webpackChunkName: "artTypeList" */ '@/views/ArtTypeList.vue'),
                    meta: {
                        title: '艺术分类'
                    }
                },
                {
                    path: '/gradingOrganizationEdit',
                    name: 'GradingOrganizationEdit',
                    component: () =>
                        import(/* webpackChunkName: "gradingOrganizationEdit" */ '@/views/GradingOrganizationEdit.vue'),
                    meta: {
                        title: '考级机构编辑'
                    }
                },
                {
                    path: '/gradingOrganizationList',
                    name: 'GradingOrganizationList',
                    component: () =>
                        import(/* webpackChunkName: "gradingOrganizationList" */ '@/views/GradingOrganizationList.vue'),
                    meta: {
                        title: '考级机构'
                    }
                },
                {
                    path: '/organizationExpert',
                    name: 'OrganizationExpert',
                    component: () =>
                        import(
                            /* webpackChunkName: "gradingOrganizationList" */ '@/views/organization/OrganizationExpert.vue'
                        ),
                    meta: {
                        title: '分配专家组'
                    }
                },
                {
                    path: '/settingEdit',
                    name: 'SettingEdit',
                    component: () => import(/* webpackChunkName: "settingEdit" */ '@/views/SettingEdit.vue'),
                    meta: {
                        title: '设置编辑'
                    }
                },
                {
                    path: '/settings',
                    name: 'Settings',
                    component: () => import(/* webpackChunkName: "settingList" */ '@/views/Settings.vue'),
                    meta: {
                        title: '设置'
                    }
                },
                {
                    path: '/operationLogList',
                    name: 'OperationLogList',
                    component: () => import(/* webpackChunkName: "operationLogList" */ '@/views/OperationLogList.vue'),
                    meta: {
                        title: '操作日志'
                    }
                },
                {
                    path: '/exceptionLogList',
                    name: 'ExceptionLogList',
                    component: () => import(/* webpackChunkName: "exceptionLogList" */ '@/views/ExceptionLogList.vue'),
                    meta: {
                        title: '异常日志'
                    }
                },
                {
                    path: '/examRoomEdit',
                    name: 'ExamRoomEdit',
                    component: () => import(/* webpackChunkName: "examRoomEdit" */ '@/views/ExamRoomEdit.vue'),
                    meta: {
                        title: '考场管理编辑'
                    }
                },
                {
                    path: '/examRoomList',
                    name: 'ExamRoomList',
                    component: () => import(/* webpackChunkName: "examRoomList" */ '@/views/ExamRoomList.vue'),
                    meta: {
                        title: '考场管理'
                    }
                },
                {
                    path: '/historyRoomList',
                    name: 'HistoryRoomList',
                    component: () =>
                        import(/* webpackChunkName: "historyRoomList" */ '@/views/video/HistoryRoomList.vue'),
                    meta: {
                        title: '视频管理'
                    }
                },
                {
                    path: '/showRoomList',
                    name: 'ShowRoomList',
                    component: () => import(/* webpackChunkName: "showRoomList" */ '@/views/video/ShowRoomList.vue'),
                    meta: {
                        title: '监控列表'
                    }
                },
                {
                    path: '/recordInfo',
                    name: 'RecordInfo',
                    component: () => import(/* webpackChunkName: "recordInfo" */ '@/views/record/RecordInfo.vue'),
                    meta: {
                        title: '备案管理'
                    }
                },
                {
                    path: '/complainEdit',
                    name: 'ComplainEdit',
                    component: () => import(/* webpackChunkName: "complainEdit" */ '@/views/ComplainEdit.vue'),
                    meta: {
                        title: '咨询投诉编辑'
                    }
                },
                {
                    path: '/complainShow',
                    name: 'ComplainShow',
                    component: () => import(/* webpackChunkName: "complainShow" */ '@/views/ComplainShow.vue'),
                    meta: {
                        title: '咨询投诉详情'
                    }
                },
                {
                    path: '/complainList',
                    name: 'ComplainList',
                    component: () => import(/* webpackChunkName: "complainList" */ '@/views/ComplainList.vue'),
                    meta: {
                        title: '咨询投诉'
                    }
                },
                {
                    path: '/recordExpertEdit',
                    name: 'RecordExpertEdit',
                    component: () => import(/* webpackChunkName: "recordExpertEdit" */ '@/views/RecordExpertEdit.vue'),
                    meta: {
                        title: '考级专业考官编辑'
                    }
                },
                {
                    path: '/programmeShow',
                    name: 'ProgrammeShow',
                    component: () =>
                        import(/* webpackChunkName: "programmeShow" */ '@/views/performance/ProgrammeShow.vue'),
                    meta: {
                        title: '节目详情'
                    }
                },
                {
                    path: '/programmeEdit',
                    name: 'ProgrammeEdit',
                    component: () =>
                        import(/* webpackChunkName: "programmeEdit" */ '@/views/performance/ProgrammeEdit.vue'),
                    meta: {
                        title: '节目管理编辑'
                    }
                },
                {
                    path: '/programmeList',
                    name: 'ProgrammeList',
                    component: () =>
                        import(/* webpackChunkName: "programmeList" */ '@/views/performance/ProgrammeList.vue'),
                    meta: {
                        title: '节目列表'
                    }
                },
                {
                    path: '/programmeOrgList',
                    name: 'ProgrammeOrgList',
                    component: () =>
                        import(/* webpackChunkName: "programmeOrgList" */ '@/views/performance/ProgrammeOrgList.vue'),
                    meta: {
                        title: '节目管理'
                    }
                },
                {
                    path: '/programmeGroupList',
                    name: 'ProgrammeGroupList',
                    component: () =>
                        import(
                            /* webpackChunkName: "programmeGroupList" */ '@/views/performance/ProgrammeGroupList.vue'
                        ),
                    meta: {
                        title: '节目分组'
                    }
                },
                {
                    path: '/programmeSignList',
                    name: 'ProgrammeSignList',
                    component: () =>
                        import(/* webpackChunkName: "programmeSignList" */ '@/views/performance/ProgrammeSignList.vue'),
                    meta: {
                        title: '节目签到'
                    }
                },
                {
                    path: '/programmeScoreList',
                    name: 'ProgrammeScoreList',
                    component: () =>
                        import(
                            /* webpackChunkName: "programmeScoreList" */ '@/views/performance/ProgrammeScoreList.vue'
                        ),
                    meta: {
                        title: '节目评审'
                    }
                },
                {
                    path: '/participantEdit',
                    name: 'ParticipantEdit',
                    component: () =>
                        import(/* webpackChunkName: "participantEdit" */ '@/views/performance/ParticipantEdit.vue'),
                    meta: {
                        title: '参演人员编辑'
                    }
                },
                {
                    path: '/participantList',
                    name: 'ParticipantList',
                    component: () =>
                        import(/* webpackChunkName: "participantList" */ '@/views/performance/ParticipantList.vue'),
                    meta: {
                        title: '参演人员'
                    }
                },
                {
                    path: '/arrangeEdit',
                    name: 'ArrangeEdit',
                    component: () =>
                        import(/* webpackChunkName: "arrangeEdit" */ '@/views/performance/ArrangeEdit.vue'),
                    meta: {
                        title: '节目编排编辑'
                    }
                },
                {
                    path: '/arrangeList',
                    name: 'ArrangeList',
                    component: () =>
                        import(/* webpackChunkName: "arrangeList" */ '@/views/performance/ArrangeList.vue'),
                    meta: {
                        title: '节目编排'
                    }
                },
                {
                    path: '/arrangeJudgeList',
                    name: 'ArrangeJudgeList',
                    component: () =>
                        import(/* webpackChunkName: "arrangeJudgeList" */ '@/views/performance/ArrangeJudgeList.vue'),
                    meta: {
                        title: '分配评委'
                    }
                },
                {
                    path: '/awardEdit',
                    name: 'AwardEdit',
                    component: () => import(/* webpackChunkName: "awardEdit" */ '@/views/AwardEdit.vue'),
                    meta: {
                        title: '奖项设置编辑'
                    }
                },
                {
                    path: '/awardList',
                    name: 'AwardList',
                    component: () => import(/* webpackChunkName: "awardList" */ '@/views/AwardList.vue'),
                    meta: {
                        title: '奖项设置'
                    }
                }
                /**INSERT_LOCATION**/
            ]
        },
        {
            path: '/login',
            name: 'login',
            component: () => import('@/views/Login'),
            meta: {
                title: '登录'
            }
        }
    ]
});
router.beforeEach((to, from, next) => {
    //console.log(to);
    if (/^\/http/.test(to.path)) {
        console.log(Vue.$baseUrl);
        let url = to.path.replace('/', '');
        let params = [];
        if (to.query) {
            for (let key in to.query) {
                if (to.query.hasOwnProperty(key)) {
                    params.push(`${key}=${to.query[key]}`);
                }
            }
        }
        if (params.length > 0) {
            url += `?${params.join('&')}`;
        }
        window.open(url);
        return;
    }
    // console.log(store.state.isORGANIZER);
    if (!store.state.userInfo && to.path !== '/login') {
        store
            .dispatch('getUserInfo')
            .then(() => {
                store
                    .dispatch('checkOrganization')
                    .then(() => {
                        if (to.path === '/dashboard' && store.state.isORGANIZER) {
                            next('/organizationInfo');
                        } else {
                            next();
                        }
                    })
                    .catch(() => {
                        next('/organizationInfo');
                    });
            })
            .catch(() => {
                sessionStorage.removeItem('token');
                next('/login');
            });
    } else if (!to.matched.length) {
        next('/404');
    } else if (to.path !== '/organizationInfo' && to.path !== '/login') {
        store
            .dispatch('checkOrganization')
            .then(() => {
                if (to.path === '/dashboard' && store.state.isORGANIZER) {
                    next();
                }
                next();
            })
            .catch(() => {
                if (from.path === '/organizationInfo') {
                    Message({
                        message: '请补全信息',
                        type: 'warning'
                    });

                    next(false);
                }
                next('/organizationInfo');
            });
    } else if (to.path === '/dashboard' && store.state.isORGANIZER) {
        next('/organizationInfo');
    } else {
        next();
    }
});

export default router;
