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: '/',
            name: 'admin',
            component: () => import('../pages/App'),
            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: '/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: '/districtInfos',
                    name: 'districtInfos',
                    component: () => import('../pages/DistrictInfos')
                },
                {
                    path: '/districtInfo',
                    name: 'districtInfo',
                    component: () => import('../pages/DistrictInfo')
                },
                //地铁线
                {
                    path: '/metroInfos',
                    name: 'metroInfos',
                    component: () => import('../pages/MetroInfos')
                },
                {
                    path: '/metroInfo',
                    name: 'metroInfo',
                    component: () => import('../pages/MetroInfo')
                },

                //地铁站
                {
                    path: '/metroStations',
                    name: 'metroStations',
                    component: () => import('../pages/MetroStations')
                },
                {
                    path: '/metroStation',
                    name: 'metroStation',
                    component: () => import('../pages/MetroStation')
                },

                //商圈
                {
                    path: '/businessCircles',
                    name: 'businessCircles',
                    component: () => import('../pages/BusinessCircles')
                },
                {
                    path: '/businessCircle',
                    name: 'businessCircle',
                    component: () => import('../pages/BusinessCircle')
                },

                //房源特色
                {
                    path: '/houseFeatures',
                    name: 'houseFeatures',
                    component: () => import('../pages/HouseFeatures')
                },
                {
                    path: '/houseFeature',
                    name: 'houseFeature',
                    component: () => import('../pages/HouseFeature')
                },

                //房源配置
                {
                    path: '/houseParts',
                    name: 'houseParts',
                    component: () => import('../pages/HouseParts')
                },
                {
                    path: '/housePart',
                    name: 'housePart',
                    component: () => import('../pages/HousePart')
                },

                //用户收藏
                {
                    path: '/userCollections',
                    name: 'userCollections',
                    component: () => import('../pages/UserCollections')
                },
                {
                    path: '/userCollection',
                    name: 'userCollection',
                    component: () => import('../pages/UserCollection')
                },

                //看房预约
                {
                    path: '/seeOrders',
                    name: 'seeOrders',
                    component: () => import('../pages/SeeOrders')
                },
                {
                    path: '/seeOrder',
                    name: 'seeOrder',
                    component: () => import('../pages/SeeOrder')
                },

                //房源信息
                {
                    path: '/houseInfos',
                    name: 'houseInfos',
                    component: () => import('../pages/HouseInfos')
                },
                {
                    path: '/houseInfo',
                    name: 'houseInfo',
                    component: () => import('../pages/HouseInfo')
                },

                //房源图片
                {
                    path: '/houseImages',
                    name: 'houseImages',
                    component: () => import('../pages/HouseImages')
                },
                {
                    path: '/houseImage',
                    name: 'houseImage',
                    component: () => import('../pages/HouseImage')
                },

                //咨询攻略
                {
                    path: '/newsInfos',
                    name: 'newsInfos',
                    component: () => import('../pages/NewsInfos')
                },
                {
                    path: '/newsInfo',
                    name: 'newsInfo',
                    component: () => import('../pages/NewsInfo')
                },

                //联系记录
                {
                    path: '/contactRecords',
                    name: 'contactRecords',
                    component: () => import('../pages/ContactRecords')
                },
                {
                    path: '/contactRecord',
                    name: 'contactRecord',
                    component: () => import('../pages/ContactRecord')
                },

                //意见反馈
                {
                    path: '/feedbacks',
                    name: 'feedbacks',
                    component: () => import('../pages/Feedbacks')
                },
                {
                    path: '/feedback',
                    name: 'feedback',
                    component: () => import('../pages/Feedback')
                },


                //小区
                {
                    path: '/housingDistricts',
                    name: 'housingDistricts',
                    component: () => import('../pages/HousingDistricts')
                },
                {
                    path: '/housingDistrict',
                    name: 'housingDistrict',
                    component: () => import('../pages/HousingDistrict')
                },

                //管家
                {
                    path: '/houseKeepers',
                    name: 'houseKeepers',
                    component: () => import('../pages/HouseKeepers')
                },
                {
                    path: '/houseKeeper',
                    name: 'houseKeeper',
                    component: () => import('../pages/HouseKeeper')
                },

                //热门搜索
                {
                    path: '/hotSearchs',
                    name: 'hotSearchs',
                    component: () => import('../pages/HotSearchs')
                },
                {
                    path: '/hotSearch',
                    name: 'hotSearch',
                    component: () => import('../pages/HotSearch')
                },

                //首页Banner
                {
                    path: '/bannerInfos',
                    name: 'bannerInfos',
                    component: () => import('../pages/BannerInfos')
                },
                {
                    path: '/bannerInfo',
                    name: 'bannerInfo',
                    component: () => import('../pages/BannerInfo')
                },

                //朝向
                {
                    path: '/facedInfos',
                    name: 'facedInfos',
                    component: () => import('../pages/FacedInfos')
                },
                {
                    path: '/facedInfo',
                    name: 'facedInfo',
                    component: () => import('../pages/FacedInfo')
                },

                //公司介绍
                {
                    path: '/companyInfos',
                    name: 'companyInfos',
                    component: () => import('../pages/CompanyInfos')
                },
                {
                    path: '/companyInfo',
                    name: 'companyInfo',
                    component: () => import('../pages/CompanyInfo')
                },
                //房屋托管
                {
                    path: '/housingTrusts',
                    name: 'housingTrusts',
                    component: () => import('../pages/HousingTrusts')
                },
                {
                    path: '/housingTrust',
                    name: 'housingTrust',
                    component: () => import('../pages/HousingTrust')
                },

            ]
        },
        {
            path: '/login',
            name: 'login',
            component: () => import('../pages/Login')
        },
        {
            path: '*',
            component: () => import('../pages/404')
        },
        {
            path: '/appLineChart',
            name: 'appLineChart',
            component: () => import('../pages/LineChart')
        },
        {
            path: '/appBarChart',
            name: 'appBarChart',
            component: () => import('../pages/BarChart')
        },
        {
            path: '/appPieChart',
            name: 'appPieChart',
            component: () =>
                import('../pages/PieChart')
        },
        {
            path: '/appPolarAreaChart',
            name: 'appPolarAreaChart',
            component: () =>
                import('../pages/PolarAreaChart')
        },
        {
            path: '/appRadarChart',
            name: 'appRadarChart',
            component: () =>
                import('../pages/RadarChart')
        },
        {
            path: '/appBubbleChart',
            name: 'appBubbleChart',
            component: () =>
                import('../pages/BubbleChart')
        }
    ]
});
router.beforeEach((to, from, next) => {
    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) {
                    store.commit('updateUserInfo', res.data.data);
                    next();
                    return;
                }
            }
            next('/login');
        }).catch(() => {
            next('/login');
        })
    } else {
        next();
    }
});
router.afterEach((to, from) => {
    window.onresize();
});

export default router;
