import {createRouter,createWebHashHistory} from "vue-router";
import FormHome from "../views/c-views/formPart/FormHome.vue";
import VisualizationHome from "../views/c-views/visualizationPart/VisualizationHome.vue";
import HouseBuilding from "@/views/c-views/visualizationPart/houseBuilding/HouseBuilding.vue";
import MainPage from "@/views/c-views/visualizationPart/mainPage/MainPage.vue";
import MunicipalFacilities from "@/views/c-views/visualizationPart/municipalFacilities/MunicipalFacilities.vue";
import {ElMessageBox} from "element-plus";
import {useUserStore} from "@/store/userStore.js";
import {getTokenInterior} from "@/service/userInfo.js";



const router = createRouter({
    history:createWebHashHistory(import.meta.env.BASE_URL),
    routes:[
        {
            path:'/login',
            name:'login',
            // component:() => import('../views/login/Login.vue')
        },
        {
            path:'/index:ticket',
        },
        {
            path:'/iotAnalysis',
            name:'IotAnalysis',
            component: () => import('@/views/IOTDataAnalysis/index.vue')
        }
    ]
})
router.beforeEach((to, from, next) => {
    // 外部跳转, 携带票据守卫请求后端服务校验
    if (to.path === '/login') {
        let param = to.query.ticket;
        debugger
        // 调用后端服务进行权限校验
        getTokenInterior(param, import.meta.env.VITE_SYSTEM_ID)
            .then(res => {
            debugger
                // 401状态码，重定向到登录页
                if (res.data.code === '401') {
                    alert(res.data.msg);
                    window.location.href = res.data.data.backUrl;
                }

                // 200状态码，权限校验成功，跳转到 /main/home
                if (res.data.code === '200') {
                    router.replace({ path: '/iotAnalysis' });
                }
            })
            .catch(err => {
                console.error('校验请求失败:', err);
            });
    } else {
        // 继续执行下一步
        next();
    }
});
export default router