import Vue from 'vue'
import Router from 'vue-router'
import store from '../vuex'
import axios from 'axios'

Vue.use(Router);

const router = new Router({
    routes: [{
            path: '/',
            component: () =>
                import ('../pages/Admin'),
            children: [{
                    path: '/',
                    redirect: 'dashboard'
                },
                {
                    path: '/404',
                    name: '404',
                    component: () =>
                        import ('../pages/404')
                },
                {
                    path: '/dashboard',
                    name: 'dashboard',
                    component: () =>
                        import ('../pages/Dashboard')
                },
                {
                    path: '/users',
                    name: 'users',
                    component: () =>
                        import ('../pages/Users')
                },
                {
                    path: '/user',
                    name: 'user',
                    component: () =>
                        import ('../pages/User')
                },
                {
                    path: '/menus',
                    name: 'menus',
                    component: () =>
                        import ('../pages/Menus')
                },
                {
                    path: '/roles',
                    name: 'roles',
                    component: () =>
                        import ('../pages/Roles')
                },
                {
                    path: '/role',
                    name: 'role',
                    component: () =>
                        import ('../pages/Role')
                },
                {
                    path: '/api',
                    name: 'api',
                    component: () =>
                        import ('../pages/Api')
                },
                {
                    path: '/genCodes',
                    name: 'genCodes',
                    component: () =>
                        import ('../pages/GenCodes')
                },
                {
                    path: '/genCode',
                    name: 'genCode',
                    component: () =>
                        import ('../pages/GenCode')
                },
                {
                    path: '/permissions',
                    name: 'permissions',
                    component: () =>
                        import ('../pages/Permissions')
                },
                {
                    path: '/permission',
                    name: 'permission',
                    component: () =>
                        import ('../pages/Permission')
                },
                {
                    path: '/departs',
                    name: 'departs',
                    component: () =>
                        import ('../pages/Departs')
                },
                {
                    path: '/lineChart',
                    name: 'lineChart',
                    component: () =>
                        import ('../pages/LineChart')
                },
                {
                    path: '/barChart',
                    name: 'barChart',
                    component: () =>
                        import ('../pages/BarChart')
                },
                {
                    path: '/polarAreaChart',
                    name: 'polarAreaChart',
                    component: () =>
                        import ('../pages/PolarAreaChart')
                },
                {
                    path: '/pieChart',
                    name: 'pieChart',
                    component: () =>
                        import ('../pages/PieChart')
                },
                {
                    path: '/radarChart',
                    name: 'radarChart',
                    component: () =>
                        import ('../pages/RadarChart')
                },
                {
                    path: '/bubbleChart',
                    name: 'bubbleChart',
                    component: () =>
                        import ('../pages/BubbleChart')
                },
                {
                    path: '/processModels',
                    name: 'processModels',
                    component: () =>
                        import ('../pages/ProcessModels')
                },
                {
                    path: '/processDefinitions',
                    name: 'processDefinitions',
                    component: () =>
                        import ('../pages/ProcessDefinitions')
                },
                {
                    path: '/myProcesses',
                    name: 'myProcesses',
                    component: () =>
                        import ('../pages/MyProcesses')
                },
                {
                    path: '/myTasks',
                    name: 'myTasks',
                    component: () =>
                        import ('../pages/MyTasks')
                },
                {
                    path: '/completeTaskDynamic',
                    name: 'CompleteTaskDynamic',
                    component: () =>
                        import ('../pages/CompleteTaskDynamic')
                },
                {
                    path: '/tableShouyiYaopinxinxis',
                    name: 'tableShouyiYaopinxinxis',
                    component: () =>
                        import ('../pages/TableShouyiYaopinxinxis')
                },
                {
                    path: '/tableShouyiYaopinxinxi',
                    name: 'tableShouyiYaopinxinxi',
                    component: () =>
                        import ('../pages/TableShouyiYaopinxinxi')
                },
                {
                    path: '/testAaas',
                    name: 'testAaas',
                    component: () =>
                        import ('../pages/TestAaas')
                },
                {
                    path: '/testAaa',
                    name: 'testAaa',
                    component: () =>
                        import ('../pages/TestAaa')
                },
                {
                    path: '/testAaaLineChart',
                    name: 'testAaaLineChart',
                    component: () =>
                        import ('../pages/TestAaaLineChart')
                },
                {
                    path: '/commonQuerys',
                    name: 'commonQuerys',
                    component: () =>
                        import ('../pages/CommonQuerys')
                },
                {
                    path: '/commonQuery',
                    name: 'commonQuery',
                    component: () =>
                        import ('../pages/CommonQuery')
                },
                {
                    path: '/commonIframe',
                    name: 'commonIframe',
                    component: () =>
                        import ('../pages/CommonIframe')
                },
                {
                    path: '/queryConfigs',
                    name: 'queryConfigs',
                    component: () =>
                        import ('../pages/QueryConfigs')
                },
                {
                    path: '/queryConfig',
                    name: 'queryConfig',
                    component: () =>
                        import ('../pages/QueryConfig')
                },
                {
                    path: '/testBbbs',
                    name: 'testBbbs',
                    component: () =>
                        import ('../pages/TestBbbs')
                },
                {
                    path: '/testBbb',
                    name: 'testBbb',
                    component: () =>
                        import ('../pages/TestBbb')
                },
                {
                    path: '/testBbbCopys',
                    name: 'testBbbCopys',
                    component: () =>
                        import ('../pages/TestBbbCopys')
                },
                {
                    path: '/testBbbCopy',
                    name: 'testBbbCopy',
                    component: () =>
                        import ('../pages/TestBbbCopy')
                },
                {
                    path: '/changePassword',
                    name: 'changePassword',
                    component: () =>
                        import ('../pages/changePassword')
                },
                {
                    path: '/dataSourceInfos',
                    name: 'dataSourceInfos',
                    component: () =>
                        import ('../pages/DataSourceInfos')
                },
                {
                    path: '/dataSourceInfo',
                    name: 'dataSourceInfo',
                    component: () =>
                        import ('../pages/DataSourceInfo')
                },
                {
                    path: '/testSqlserverAaas',
                    name: 'testSqlserverAaas',
                    component: () =>
                        import ('../pages/TestSqlserverAaas')
                },
                {
                    path: '/TestSqlserverAaa',
                    name: 'TestSqlserverAaa',
                    component: () =>
                        import ('../pages/TestSqlserverAaa')
                },
                {
                    path: '/deploySystem',
                    name: 'DeploySystem',
                    component: () =>
                        import ('../pages/DeploySystem')
                },
                {
                    path: '/testDeploy',
                    name: 'TestDeploy',
                    component: () =>
                        import ('../pages/TestDeploy')
                },
                {
                    path: '/testDeploys',
                    name: 'TestDeploys',
                    component: () =>
                        import ('../pages/TestDeploys')
                },
                {
                    path: '/testaaaone',
                    name: 'Testaaaone',
                    component: () =>
                        import ('../pages/Testaaaone')
                },
                {
                    path: '/testaaaones',
                    name: 'Testaaaones',
                    component: () =>
                        import ('../pages/Testaaaones')
                },
                {
                    path: '/testgit',
                    name: 'Testgit',
                    component: () =>
                        import ('../pages/Testgit')
                },
                {
                    path: '/testgits',
                    name: 'Testgits',
                    component: () =>
                        import ('../pages/Testgits')
                },
                {
                    path: '/testCaidan',
                    name: 'TestCaidan',
                    component: () =>
                        import ('../pages/TestCaidan')
                },
                {
                    path: '/testCaidans',
                    name: 'TestCaidans',
                    component: () =>
                        import ('../pages/TestCaidans')
                },
                {
                    path: '/tableNiuseRongliang',
                    name: 'TableNiuseRongliang',
                    component: () =>
                        import ('../pages/TableNiuseRongliang')
                },
                {
                    path: '/tableNiuseRongliangs',
                    name: 'TableNiuseRongliangs',
                    component: () =>
                        import ('../pages/TableNiuseRongliangs')
                },
                /**INSERT_LOCATION**/

                {
                    path: '/handleLeaveAdd',
                    name: 'handleLeaveAdd',
                    component: () =>
                        import ('../pages/HandleLeaveAdd'),
                    meta: {
                        type: '手持机录入',
                        title: '离场登记'
                    }
                },

                {
                    path: '/handleEstrusEvent',
                    name: 'handleEstrusEvent',
                    component: () =>
                        import ('../pages/HandleEstrusEvent'),
                    meta: {
                        type: '手持机录入',
                        title: '发情事件'
                    }
                },

                {
                    path: '/handleMatch',
                    name: 'handleMatch',
                    component: () =>
                        import ('../pages/HandleMatch'),
                    meta: {
                        type: '手持机录入',
                        title: '配种任务'
                    }
                },
                {
                    path: '/handleSimultaneousMatch',
                    name: 'handleSimultaneousMatch',
                    component: () =>
                        import ('../pages/HandleSimultaneousMatch'),
                    meta: {
                        type: '手持机录入',
                        title: '同期配种'
                    }
                },
                {
                    path: '/handleInitial',
                    name: 'handleInitial',
                    component: () =>
                        import ('../pages/HandleInitial'),
                    meta: {
                        type: '手持机录入',
                        title: '初检录入'
                    }
                },
                {
                    path: '/handleRecheck',
                    name: 'handleRecheck',
                    component: () =>
                        import ('../pages/HandleRecheck'),
                    meta: {
                        type: '手持机录入',
                        title: '复检录入'
                    }
                },
                {
                    path: '/handleAbortion',
                    name: 'handleAbortion',
                    component: () =>
                        import ('../pages/HandleAbortion'),
                    meta: {
                        type: '手持机录入',
                        title: '流产登记'
                    }
                },
                {
                    path: '/handleDryMilk',
                    name: 'handleDryMilk',
                    component: () =>
                        import ('../pages/HandleDryMilk'),
                    meta: {
                        type: '手持机录入',
                        title: '干奶登记'
                    }
                },
                {
                    path: '/handlePerinatalCare',
                    name: 'handlePerinatalCare',
                    component: () =>
                        import ('../pages/HandlePerinatalCare'),
                    meta: {
                        type: '手持机录入',
                        title: '围产期护理'
                    }
                },
                {
                    path: '/handleFeeding',
                    name: 'handleFeeding',
                    component: () =>
                        import ('../pages/HandleFeeding'),
                    meta: {
                        type: '手持机录入',
                        title: '投喂博威钙'
                    }
                },
                {
                    path: '/handleHoofCare',
                    name: 'handleHoofCare',
                    component: () =>
                        import ('../pages/HandleHoofCare'),
                    meta: {
                        type: '手持机录入',
                        title: '修蹄保健'
                    }
                },
                {
                    path: '/handleTransferHouse',
                    name: 'handleTransferHouse',
                    component: () =>
                        import ('../pages/HandleTransferHouse'),
                    meta: {
                        type: '手持机录入',
                        title: '奶牛转舍'
                    }
                },
                {
                    path: '/handleCalving',
                    name: 'handleCalving',
                    component: () =>
                        import ('../pages/HandleCalving'),
                    meta: {
                        type: '手持机录入',
                        title: '产犊登记'
                    }
                },
                {
                    path: '/handleCalf',
                    name: 'handleCalf',
                    component: () =>
                        import ('../pages/HandleCalf'),
                    meta: {
                        type: '手持机录入',
                        title: '犊牛登记'
                    }
                },
                {
                    path: '/handlePerinatal',
                    name: 'handlePerinatal',
                    component: () =>
                        import ('../pages/HandlePerinatal'),
                    meta: {
                        type: '手持机录入',
                        title: '产前围产'
                    }
                },
                {
                    path: '/handleDiagnosis',
                    name: 'handleDiagnosis',
                    component: () =>
                        import ('../pages/HandleDiagnosis'),
                    meta: {
                        type: '手持机录入',
                        title: '疾病确诊'
                    }
                },
                {
                    path: '/handleTreatment',
                    name: 'handleTreatment',
                    component: () =>
                        import ('../pages/HandleTreatment'),
                    meta: {
                        type: '手持机录入',
                        title: '日常治疗'
                    }
                },
                {
                    path: '/handleSampling',
                    name: 'handleSampling',
                    component: () =>
                        import ('../pages/HandleSampling'),
                    meta: {
                        type: '手持机录入',
                        title: '过抗采样'
                    }
                },
                {
                    path: '/handleOutcome',
                    name: 'handleOutcome',
                    component: () =>
                        import ('../pages/HandleOutcome'),
                    meta: {
                        type: '手持机录入',
                        title: '转归处理'
                    }
                },
                {
                    path: '/handleBlindMilk',
                    name: 'handleBlindMilk',
                    component: () =>
                        import ('../pages/HandleBlindMilk'),
                    meta: {
                        type: '手持机录入',
                        title: '盲乳登记'
                    }
                },
                {
                    path: '/handleGrowth',
                    name: 'handleGrowth',
                    component: () =>
                        import ('../pages/HandleGrowth'),
                    meta: {
                        type: '手持机录入',
                        title: '生长指标测定'
                    }
                },
                {
                    path: '/handleProhibition',
                    name: 'handleProhibition',
                    component: () =>
                        import ('../pages/HandleProhibition'),
                    meta: {
                        type: '手持机录入',
                        title: '禁配解禁'
                    }
                },
                {
                    path: '/handleFeedingMagnet',
                    name: 'handleFeedingMagnet',
                    component: () =>
                        import ('../pages/HandleFeedingMagnet'),
                    meta: {
                        type: '手持机录入 ',
                        title: '投喂磁石'
                    }
                },
                {
                    path: '/handleImmunopotency',
                    name: 'handleImmunopotency',
                    component: () =>
                        import ('../pages/HandleImmunopotency'),
                    meta: {
                        type: '手持机录入 ',
                        title: '免疫效价'
                    }
                },
                {
                    path: '/handlePhoto',
                    name: 'handlePhoto',
                    component: () =>
                        import ('../pages/HandlePhoto'),
                    meta: {
                        type: '手持机录入 ',
                        title: '犊牛拍照'
                    }
                },
                {
                    path: '/handleBeestings',
                    name: 'handleBeestings',
                    component: () =>
                        import ('../pages/HandleBeestings'),
                    meta: {
                        type: '手持机录入 ',
                        title: '初乳饲喂'
                    }
                },
                {
                    path: '/handleWeaning',
                    name: 'handleWeaning',
                    component: () =>
                        import ('../pages/HandleWeaning'),
                    meta: {
                        type: '手持机录入 ',
                        title: '断奶登记'
                    }
                },
                {
                    path: '/handleApproach',
                    name: 'handleApproach',
                    component: () =>
                        import ('../pages/HandleApproach'),
                    meta: {
                        type: '手持机录入 ',
                        title: '进场登记'
                    }
                },
                {
                    path: '/handleTransition',
                    name: 'handleTransition',
                    component: () =>
                        import ('../pages/HandleTransition'),
                    meta: {
                        type: '手持机录入 ',
                        title: '转场登记'
                    }
                },
                {
                    path: '/handleTuberculosis',
                    name: 'handleTuberculosis',
                    component: () =>
                        import ('../pages/HandleTuberculosis'),
                    meta: {
                        type: '手持机录入 ',
                        title: '结核检验'
                    }
                },
                {
                    path: '/handleQuarantine',
                    name: 'handleQuarantine',
                    component: () =>
                        import ('../pages/HandleQuarantine'),
                    meta: {
                        type: '手持机录入 ',
                        title: '布疫检疫'
                    }
                },
                {
                    path: '/handleDisinfection',
                    name: 'handleDisinfection',
                    component: () =>
                        import ('../pages/HandleDisinfection'),
                    meta: {
                        type: '手持机录入 ',
                        title: '消毒登记'
                    }
                },
                {
                    path: '/handleChamfer',
                    name: 'handleChamfer',
                    component: () =>
                        import ('../pages/HandleChamfer'),
                    meta: {
                        type: '手持机录入 ',
                        title: '去角登记'
                    }
                },
                {
                    path: '/handleMamma',
                    name: 'handleMamma',
                    component: () =>
                        import ('../pages/HandleMamma'),
                    meta: {
                        type: '手持机录入 ',
                        title: '除副乳登记'
                    }
                },
                {
                    path: '/handleBodyConditionScore',
                    name: 'handleBodyConditionScore',
                    component: () =>
                        import ('../pages/HandleBodyConditionScore'),
                    meta: {
                        type: '手持机录入 ',
                        title: '体况评分'
                    }
                },
                {
                    path: '/handleElectronicTag',
                    name: 'handleElectronicTag',
                    component: () =>
                        import ('../pages/HandleElectronicTag'),
                    meta: {
                        type: '手持机录入 ',
                        title: '电子标签'
                    }
                }
            ]
        },
        {
            path: '/login',
            name: 'login',
            component: () =>
                import ('../pages/Login')
        }
    ]
});
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;
    }
    if (!store.state.userInfo && to.path !== '/login') {
        axios.get('/userInfo/getUserInfo').then(res => {
            if (res.status === 200) {
                if (res.data.success && res.data.data) {
                    store.commit('updateUserInfo', res.data.data);
                    axios.get('/departInfo/userDeparts?userId=' + res.data.data.id).then(res => {
                        if (res.status === 200) {
                            if (res.data.success && res.data.data) {
                                if (res.data.data.length > 0) {
                                    store.commit('uploadfarmInfo', res.data.data[res.data.data.length - 1]);
                                }

                            }
                        }
                    })

                    axios.get('/departInfo/childDepartsByUserId?userId=' + res.data.data.id).then(res => {
                        if (res.status === 200) {
                            if (res.data.success && res.data.data) {
                                if (res.data.data.length > 0) {
                                    var childDepartInfo = {
                                        misCode: '',
                                        misName: '',
                                        misCodeAndName: '',

                                    }

                                    var misCodeList = [];
                                    var misNameList = [];

                                    res.data.data.forEach(item => {

                                        if (item.misOrgCode) {

                                            misCodeList.push("'" + item.misOrgCode + "'");
                                        }
                                        if (item.misOrgName) {

                                            misNameList.push("'" + item.misOrgName + "'");
                                        }
                                    })

                                    childDepartInfo.misCode = misCodeList.join()
                                    childDepartInfo.misName = misNameList.join()
                                    if (childDepartInfo.misCode && childDepartInfo.misName) {
                                        childDepartInfo.misCodeAndName = childDepartInfo.misCode + ';' + childDepartInfo.misName
                                    }


                                    store.commit('uploadChildDepartInfo', childDepartInfo);
                                }

                            }
                        }
                    })

                    next();
                    return;
                }
            }
            next('/login');
        }).catch(() => {
            next('/login');
        })
    } else if (!to.matched.length) {
        next('/404');
    } else {
        next();
    }
});
router.afterEach((to, from) => {
    window.onresize();
});

export default router;