import Vue from "vue";
import Router from "vue-router";
import store from '../store'
Vue.use(Router);

const originalPush = Router.prototype.push;
Router.prototype.push = function push(location) {
    return originalPush.call(this, location).catch((err) => err);
};
// 其他路由
let constantRoutes = [
    {
        path: "/",
        component: () => import("@/views/login/login.vue"),
        meta: {
            title: "login",
        },
    },
    {
        path: "/login",
        component: () => import("@/views/login/login_ruoyan.vue"),
        meta: {
            title: "login",
        },
    },
    {
        path: "/logins",
        component: () => import("@/views/login/logins.vue"),
        meta: {
            title: "logins",
        },//工作台登录
    },
    //靶向提升
    {
        path:'/targetImprove',
        redirect: "/targetImprove/pracice",
        component: () => import("@/views/targetImprove/index"),
        meta: {
            title: "靶向提升",
        },
        children: [
            {
                path: "pracice",
                component: () => import("@/views/targetImprove/practice/index.vue"),
                meta: {
                    title: "自主练习",
                },
            },
            {
                path: "praciceResultDetail",
                component: () => import("@/views/targetImprove/practice/practiceResults/practiceDetail.vue"),
                meta: {
                    title: "自主练习结果详情",
                },
            },
            {
                path: "consolidate",
                component: () => import("@/views/targetImprove/consolidate/index.vue"),
                meta: {
                    title: "错题巩固",
                },
            },
        ],
    },
    // 个人中心
    {
        path: "/userInfo",
        redirect: "/userInfo/personInfo",
        component: () => import("@/views/userInfo/index"),
        meta: {
            title: "个人中心",
        },
        children: [
            {
                path: "personInfo",
                component: () => import("@/views/userInfo/personInfo"),
                meta: {
                    title: "个人信息",
                },
            },
        ],
    },
];
// 学生端分析报告
let studentAnalysisReport = {
    path: "/studentAnalysisReport",
    redirect: "/studentAnalysisReport/list",
    component: () => import("@/views/analysisReport/index"),
    children: [
        {
            path: "/jointStudentAnalysisReport/list", // 报告列表
            meta: {
                title: "分析报告",
            },
            component: () => import("@/views/analysisReport/studentPage/list/mainPage"),
        },
        // {
        //     path: "/studentAnalysisReport/list", // 报告列表
        //     meta: {
        //         title: "校考分析报告",
        //     },
        //     component: () => import("@/views/analysisReport/studentPage/list/mainPage"),
        // },
        {
            name: "reportDetails",
            path: "/studentAnalysisReport/reportDetails",
            component: () => import("@/views/analysisReport/studentPage/mainPage"),
            children: [
                {
                    path: "scrolReport",
                    component: () => import("@/views/analysisReport/studentPage/scrolReport/transcript"),
                    meta: {
                        title: "学生分析",
                    },
                },
                {
                    path: "studentReport",
                    meta: {
                        title: "学生报告册",
                    },
                    component: () => import("@/views/analysisReport/studentPage/downloadPdf/studentReport"),
                },
                {
                    path: "personalWrongQuestions",
                    component: () => import("@/views/analysisReport/wrongQuestion/index"),
                    meta: {
                        title: "举一反三",
                    },
                },
                {
                    path: "personalProfile",
                    component: () => import("@/views/analysisReport/personalProfile/index"),
                    meta: {
                        title: "个人画像",
                    },
                },
                {
                    path: "studentCase",
                    component: () => import("@/views/analysisReport/studentPage/studentCase/mainPage"),
                    meta: {
                        title: "一生一案",
                    },
                },
                {
                    path: "studentReport",
                    component: () => import("@/views/analysisReport/studentPage/downloadPdf/studentReport"),
                    meta: {
                        title: "一生一案",
                    },
                }
            ],
        },
    ],
};
const createRouter = () =>
    new Router({
        mode: "hash",
        routes: [
            ...constantRoutes,
            studentAnalysisReport
        ],
        scrollBehavior(to, from, savedPosition) {
            if (savedPosition) {
                return savedPosition;
            } else {
                return {
                    x: 0,
                    y: 0,
                };//路由切换时，滚动条回到顶部
            }
        },
    });
// 添加统计分析脚本的函数
function addAnalyticsScript() {

    // 移除已存在的统计脚本
    let existingScript = document.getElementById('_bxtj');
    while (existingScript) {
        existingScript.remove();
        existingScript = document.getElementById('_bxtj');
    }
    // 移除已存在的统计脚本
    let existingScriptFrame = document.getElementById('_bxtjiframe');
    while (existingScriptFrame) {
        existingScriptFrame.remove();
        existingScriptFrame = document.getElementById('_bxtjiframe');
    }
    // 等待DOM加载完成
    setTimeout(() => {
        var _s = document.createElement('script');
        _s.setAttribute('type', 'text/javascript');
        _s.setAttribute('id', '_bxtj');
        _s.setAttribute('async', true);

        // 获取当前用户信息
        const userInfo = store.state.user.userInfo || {};
        const currentUser = userInfo.loginName || ''; // 当前用户名
        const subSiteName = userInfo.schoolName || '';
        const mySiteId = localStorage.getItem("user_schoolWebSiteId") || ''; // 你的站点ID
        if (mySiteId != '') {
            // 构建URL（按照原始代码格式）
            let src = `https://bjedures.bjedu.cn/bjjw_logdb/bxlog.js?user=${currentUser}&id=${mySiteId}`;
            if (subSiteName) {
                src += `&subSiteName=${encodeURIComponent(subSiteName)}`;
            }
            _s.setAttribute('src', src);
            var body = document.getElementsByTagName('body');
            body[0].appendChild(_s);
        }
    }, 1000);//1秒后添加
}
let router = createRouter();
router.beforeEach((to, from, next) => {
    if (to.meta.title) {
        document.title = "大数据精准教学诊断平台-" + to.meta.title;
    }
    next();
});
// 路由后置守卫：添加统计脚本
router.afterEach(() => {
    // 添加统计分析脚本
    addAnalyticsScript()
});

export default router;
