import Vue from "vue";
import Router from "vue-router";

Vue.use(Router);

const originalPush = Router.prototype.push;
Router.prototype.push = function (location) {
    originalPush.call(this, location).catch((err) => err);
};
const originalReplace = Router.prototype.replace;
Router.prototype.replace = function (location) {
    originalReplace.call(this, location).catch((err) => err);
};

/* AppLayout */
import AppLayout from "@/layout/app/index.vue";

/* MainDefaultLayout */
import MainDefaultLayout from "@/layout/main/default/index.vue";

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * allRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const allRoutes = [
    {
        path: "/404",
        component: () => import("@/views/404.vue"),
        hidden: true,
    },

    {
        path: "/",
        redirect: "/bjhd/hdlbgl",
    },
    {
        path: "/bjhd",
        component: AppLayout,
        meta: { title: "班级活动", icon: "el-icon-document" },
        name: 'bjhd',
        redirect: '/bjhd/hdlbgl',
        children: [{
            path: "/bjhd/hdlbgl",
            name: "bjhd_hdlbgl",
            component: () => import("@/views/bjhd/hdlbgl/index.vue"),
            meta: { title: "活动类别管理" },
        }, {
            path: "/bjhd/hdlb",
            component: MainDefaultLayout,
            meta: { title: "活动列表" },
            children: [{
                path: "",
                name: "bjhd_hdlb",
                component: () => import("@/views/bjhd/hdlb/index.vue"),
                meta: { title: "活动列表" },
                props: true,
            }, {
                path: "hdxq/:id",
                hidden: true,
                name: 'bjhd_hdlb_hdxq',
                meta: { title: "活动详情", back: true },
                component: () => import("@/views/bjhd/hdlb/hdxq/index.vue"),
            }, {
                path: "bmlb/:id",
                name: "bjhd_hdlb_hdxq_bmlb",
                hidden: true,
                meta: { title: "报名列表", back: true, },
                component: () => import("@/views/bjhd/hdlb/hdxq/bmlb/index.vue"),
                props: true,
            }]
        }, {
            path: "/bjhd/bmlb",
            name: "bjhd_bmlb",
            component: () => import("@/views/bjhd/bmlb/index.vue"),
            meta: { title: "已报名列表" },
        }, {
            path: "/bjhd/xsdflb",
            name: "bjhd_xsdflb",
            component: () => import("@/views/bjhd/xsdflb/index.vue"),
            meta: { title: "学生得分列表" },
        }]
    },
    {
        path: "/cjxt",
        component: AppLayout,
        meta: { title: "晨检系统", icon: "el-icon-document" },
        name: 'cjxt',
        redirect: '/cjxt/tjdj',
        children: [{
            path: "/cjxt/tjdj",
            name: "cjxt_tjdj",
            component: () => import("@/views/cjxt/tjdj/index.vue"),
            meta: { title: "体检登记" },
        }, {
            path: "/cjxt/ycxs",
            name: "cjxt_ycxs",
            component: () => import("@/views/cjxt/ycxs/index.vue"),
            meta: { title: "异常学生" },
        }, {
            path: "/cjxt/njck/:id",
            name: "cjxt_tjdj_njck",
            hidden: true,
            component: () => import("@/views/cjxt/tjdj/njck/index.vue"),
            meta: { title: "年级查看" , back: true},
        }, {
            path: "/cjxt/bjck/:grade_id/:class_id",
            name: "cjxt_tjdj_bjck",
            hidden: true,
            component: () => import("@/views/cjxt/tjdj/bjck/index.vue"),
            meta: { title: "班级查看" , back: true},
        }]
    },
    {
        path: "/xshjgl",
        component: AppLayout,
        meta: { title: "学生获奖管理", icon: "el-icon-document" },
        name: 'xshjgl',
        redirect: '/xshjgl/jlgl',
        children: [{
            path: "/xshjgl/jlgl",
            name: "xshjgl_jlgl",
            component: () => import("@/views/xshjgl/jlgl/index.vue"),
            meta: { title: "奖励管理" },
        }, {
            path: "/xshjgl/jlgl/rygl/:no/:xm",
            name: "xshjgl_jlgl_rygl",
            hidden: true,
            component: () => import("@/views/xshjgl/jlgl/rygl/index.vue"),
            meta: { title: "荣誉管理" , back: true},
        }, {
            path: "/xshjgl/jlcx",
            name: "xshjgl_jlcx",
            component: () => import("@/views/xshjgl/jlcx/index.vue"),
            meta: { title: "奖励查询" },
        }, {
            path: "/xshjgl/jlsq",
            name: "xshjgl_jlsq",
            component: () => import("@/views/xshjgl/jlsq/index.vue"),
            meta: { title: "奖励申请" },
        }]
    },
    {
        path: "/xsxjk",
        component: AppLayout,
        meta: { title: "学生学籍卡", icon: "el-icon-document" },
        name: 'xsxjk',
        redirect: '/xsxjk/hmc',
        children: [{
            path: "/xsxjk/hmc",
            name: "xsxjk_hmc",
            component: () => import("@/views/xsxjk/hmc/index.vue"),
            meta: { title: "花名册" },
        }, {
            path: "/xsxjk/xjk",
            name: "xsxjk_xjk",
            component: () => import("@/views/xsxjk/xjk/index.vue"),
            meta: { title: "学籍卡" },
        }, {
            path: "/xsxjk/xsydlb",
            name: "xsxjk_xsydlb",
            component: () => import("@/views/xsxjk/xsydlb/index.vue"),
            meta: { title: "学生异动列表" },
        }, {
            path: "/xsxjk/bbgl",
            name: "xsxjk_bbgl",
            component: MainDefaultLayout,
            meta: { title: "报表管理" },
            children: [
                {
                    path: "",
                    name: "xsxjk_bbgl",
                    component: () => import("@/views/xsxjk/bbgl/index.vue"),
                    meta: { title: "报表管理" },
                    props: true,

                },
                {
                    path: "/xsxjk/bbgl/bbxq",
                    name: "xsxjk_bbgl_bbxq",
                    hidden: true,
                    component: MainDefaultLayout,
                    meta: { title: "报表详情", },
                    children: [
                        {
                            path: "bbxq/:id",
                            name: "xsxjk_bbgl_bbxq",
                            component: () => import("@/views/xsxjk/bbgl/bbxq/index.vue"),
                            meta: { title: "报表详情",back: true, },
                            props: true,
                        },
                        {
                            path: "/xsxjk/bbgl/bbxq/xqlb/:id",
                            name: "xsxjk_bbgl_bbxq_xqlb",
                            hidden: true,
                            component: () => import("@/views/xsxjk/bbgl/bbxq/xqlb/index.vue"),
                            meta: { title: "详情列表",back: true, },
                            props: true,

                        },
                    ]

                },
            ]
        }, {
            path: "/xsxjk/xsrstjlb",
            name: "xsxjk_xsrstjlb",
            component: MainDefaultLayout,
            meta: { title: "学生人数统计列表" },
            children: [
                {
                    path: "",
                    name: 'xsxjk_xsrstjlb',
                    component: () => import("@/views/xsxjk/xsrstjlb/index.vue"),
                    meta: { title: "学生人数统计列表" },
                    props: true,

                },
                {
                    path: "xsrstjlbxq/:team_id/:grade_id/:dept_id",
                    hidden: true,
                    meta: { title: "学生人数统计列表详情" ,back: true,},
                    name: "xsxjk_xsrstjlb_xsrstjlbxq",
                    component: () => import("@/views/xsxjk/xsrstjlb/xsrstjlbxq/index.vue"),
                    props: true,
                },]
        }]
    },
    {
        path: "/bjzzpb",
        component: AppLayout,
        meta: { title: "班级值周评比", icon: "el-icon-document" },
        name: 'bjzzpb',
        redirect: '/bjzzpb/sz',
        children: [{
            path: "/bjzzpb/sz",

            component: MainDefaultLayout,
            meta: { title: "设置" },
            children: [
                {
                    path: "bjzzpb/sz/wspb",
                    meta: { title: "卫生评比" },
                    component: MainDefaultLayout,
                    props: true,
                    children: [
                        {
                            path: "",
                            meta: { title: "卫生评比" },
                            name: "bjzzpb_sz_wspb",
                            component: () => import("@/views/bjzzpb/sz/wspb/index.vue"),
                            props: true,
                        }, {
                            path: "wspfx/:id",
                            hidden: true,
                            meta: { title: "评分项", back: true },
                            name: "bjzzpb_sz_wspb_wspfx",
                            component: () => import("@/views/bjzzpb/sz/wspb/wspfx/index.vue"),
                            props: true,
                        },
                    ]
                },
                {
                    path: "bjzzpb/sz/cgpb",
                    meta: { title: "常规评比" },
                    component: MainDefaultLayout,
                    props: true,
                    children: [
                        {
                            path: "",
                            meta: { title: "常规评比" },
                            name: "bjzzpb_sz_cgpb",
                            component: () => import("@/views/bjzzpb/sz/cgpb/index.vue"),
                            props: true,
                        },
                        {
                            path: "cgpfx/:id",
                            hidden: true,
                            meta: { title: "评分项", back: true },
                            name: "bjzzpb_sz_cgpb_cgpfx",
                            component: () => import("@/views/bjzzpb/sz/cgpb/cgpfx/index.vue"),
                            props: true,
                        },
                    ]
                },
                {
                    path: "bjzzpb/sz/zzls",
                    meta: { title: "值周老师" },
                    name: "bjzzpb_sz_zzls",
                    component: () => import("@/views/bjzzpb/sz/zzls/index.vue"),
                    props: true,
                },
                {
                    path: "bjzzpb/sz/zrxs",
                    meta: { title: "值日学生" },
                    name: "bjzzpb_sz_zrxs",
                    component: () => import("@/views/bjzzpb/sz/zrxs/index.vue"),
                    props: true,
                },

            ]
        }, {
            path: "/bjzzpb/wsgl",
            name: "bjzzpb_wsgl",
            component: () => import("@/views/bjzzpb/wsgl/index.vue"),
            meta: { title: "卫生管理" },
        }, {
            path: "/bjzzpb/cggl",
            name: "bjzzpb_cggl",
            component: () => import("@/views/bjzzpb/cggl/index.vue"),
            meta: { title: "常规管理" },
        }, {
            path: "/bjzzpb/bjpb",
            name: "xsxjk_bbgl",
            component: () => import("@/views/bjzzpb/bjpb/index.vue"),
            meta: { title: "班级评比" },
        }]
    },


    {
        path: "/xsczda",
        component: AppLayout,
        meta: { title: "学生成长档案", icon: "el-icon-document" },
        name: 'xsczda_index',
        redirect: '/xslb',
        children: [{
            path: "xslb",
            name: "xsczda_xslb",
            component: MainDefaultLayout,
            meta: { title: "学生列表" },
            children: [
                {
                    path: "",
                    name: 'xsczda_xslb',
                    component: () => import("@/views/xsczda/xslb/index.vue"),
                    meta: { title: "学生列表" },
                    props: true,

                },
                {
                    path: "",
                    hidden: true,
                    meta: { title: "班级列表" },
                    name: "xsczda_xslb_bjlb",
                    component: MainDefaultLayout,
                    children: [
                        {
                            path: "/xsczda/xslb/:id",
                            hidden: true,
                            meta: { title: "班级列表" , back: true},
                            name: "xsczda_xslb_bjlb",
                            component: () => import("@/views/xsczda/xslb/bjlb/index.vue"),
                        }, {
                            path: "/xsczda/xslb/:nj_id/bjlb/:bj_id",
                            hidden: true,
                            name: "xsczda_xslb_bjlb_xslbxq",
                            component: MainDefaultLayout,
                            meta: { title: "学生列表详情",back: true, },
                            props: true,
                            children: [
                                {
                                    path: "",
                                    name: "xsczda_xslb_bjlb_xslbxq",
                                    component: () => import("@/views/xsczda/xslb/bjlb/xslbxq/index.vue"),
                                    meta: { title: "学生列表详情" },
                                    props: true,
                                },

                                {
                                    path: "/xsczda/xslb/:nj_id/bjlb/:bj_id/czda/:id/:xdxx_xsxh",
                                    name: "xsczda_xslb_bjlb_xslbxq_czda",
                                    component: () => import("@/views/xsczda/xslb/bjlb/xslbxq/czda/index.vue"),
                                    meta: { title: "成长档案" },
                                    props: true,
                                },



                            ]
                        }
                    ]
                }

            ]
        },

        {
            path: "/xsczda/hzlxgl",
            name: "xsczda_hzlxgl",
            component: () => import("@/views/xsczda/hzlxgl/index.vue"),
            meta: { title: "徽章类型管理" },
        },
        ]
    },



    {
        path: "/wypj",
        component: AppLayout,
        meta: { title: "五育评价", icon: "el-icon-document" },
        name: 'wypj',
        redirect: '/wypj/wypj',
        children: [{
            path: "/wypj/wypj",
            name: "wypj_wypj",
            component: () => import("@/views/wypj/wypj/index.vue"),
            meta: { title: "五育评价" },
        }, {
            path: "/wypj/wdsz",
            name: "wypj_wdsz",
            component: MainDefaultLayout,
            meta: { title: "维度设置" },
            children: [
                {
                    path: "",
                    name: "wypj_wdsz",
                    component: () => import("@/views/wypj/wdsz/index.vue"),
                    meta: { title: "维度设置" },
                    props: true,
                },
                {
                    path: "wdpfx/:id",
                    hidden: true,
                    meta: { title: "评分项", back: true },
                    name: "wypj_wdsz_wdpfx",
                    component: () => import("@/views/wypj/wdsz/wdpfx/index.vue"),
                    props: true,
                },
            ]
        }, {
            path: "/wypj/xsewm",
            name: "wypj_xsewm",
            component: () => import("@/views/wypj/xsewm/index.vue"),
            meta: { title: "学生二维码" },
            },
            // {
            // path: "/wypj/dyewm",
            // name: "wypj_dyewm",
            // component: () => import("@/views/wypj/dyewm/index.vue"),
            // meta: { title: "德育二维码" },
            // }
        ]
    },
    {
        path: '/scancode',
        component: () => import('@/views/wypj/scanCode.vue'),
        meta: { title: "二维码" },
        hidden: true,
    },
    // {
    //     path: "/fxxt",
    //     component: AppLayout,
    //     meta: { title: "放学系统", icon: "el-icon-document" },
    //     name: 'fxxt',
    //     redirect: '/fxxt/xsfxgl',
    //     children: [
    //         {
    //             path: "xsfxgl",
    //             name: "fxxt_xsfxgl",
    //             component: MainDefaultLayout,
    //             meta: { title: "学生放学管理" },
    //             children: [
    //                 {
    //                     path: "",
    //                     name: 'fxxt_xsfxgl',
    //                     component: () => import("@/views/fxxt/xsfxgl/index.vue"),
    //                     meta: { title: "学生放学管理" },
    //                     props: true,

    //                 },
    //                 {
    //                     path: "bjlb/:id",
    //                     hidden: true,
    //                     meta: { title: "班级列表" },
    //                     name: "fxxt_xsfxgl_bjlb",
    //                     component: () => import("@/views/fxxt/xsfxgl/bjlb/index.vue"),
    //                     props: true,
    //                 },
    //                 {
    //                     path: "xslbxq/:id",
    //                     hidden: true,
    //                     name: "fxxt_xsfxgl_bjlb_xslbxq",
    //                     component: () => import("@/views/fxxt/xsfxgl/bjlb/xslbxq/index.vue"),
    //                     meta: { title: "学生列表详情" },
    //                     props: true,
    //                 }


    //             ]
    //         },

    //         {
    //             path: "/fxxt/zlsgl",
    //             name: "fxxt_zlsgl",
    //             component: () => import("@/views/fxxt/zlsgl/index.vue"),
    //             meta: { title: "滞留生管理" },
    //         }, {
    //             path: "/fxxt/ltxsgl",
    //             name: "fxxt_ltxsgl",
    //             component: () => import("@/views/fxxt/ltxsgl/index.vue"),
    //             meta: { title: "留堂学生管理" },
    //         }]
    // },
    {
        path: "/jcxxgl",
        component: AppLayout,
        meta: { title: "基础信息管理", icon: "el-icon-document" },
        name: 'jcxxgl',
        redirect: '/jcxxgl/xqgl',
        children: [{
            path: "/jcxxgl/xqgl",
            name: "jcxxgl_xqgl",
            component: () => import("@/views/jcxxgl/xqgl/index.vue"),
            meta: { title: "学期管理" },
        }, {
            path: "/jcxxgl/njgl",
            name: "jcxxgl_njgl",
            component: () => import("@/views/jcxxgl/njgl/index.vue"),
            meta: { title: "年级管理" },
        }, {
            path: "/jcxxgl/bjgl",
            name: "jcxxgl_bjgl",
            component: () => import("@/views/jcxxgl/bjgl/index.vue"),
            meta: { title: "班级管理" },
        }]
    },

    {
        path: "/xstzjk",
        component: AppLayout,
        meta: { title: "学生体质健康", icon: "el-icon-document" },
        name: 'xstzjk',
        redirect: '/xstzjk/xslb',
        children: [{
            path: "/xstzjk/xslb",
            name: "xstzjk_xslb",
            component: MainDefaultLayout,
            meta: { title: "学生列表" },
            children: [
                {
                    path: "",
                    name: "xstzjk_xslb",
                    component: () => import("@/views/xstzjk/xslb/index.vue"),
                    meta: { title: "学生列表" },
                    props: true,

                },
                {
                    path: "ckxq/:xtj_xsxh",
                    hidden: true,
                    meta: { title: "查看详情",back: true,},
                    name: "xstzjk_xslb_ckxq",
                    component: () => import("@/views/xstzjk/xslb/ckxq/index.vue"),
                    props: true,
                },

            ]
        }, {
            path: "/xstzjk/tzbb",
            name: "xstzjk_tzbb",
            component: () => import("@/views/xstzjk/tzbb/index.vue"),
            meta: { title: "体质报表" },
        }]
    },

    {
        path: "/zzjgyqx",
        component: AppLayout,
        name: "zzjgyqx",
        meta: { title: "组织机构与权限", icon: "el-icon-document" },
        children: [{
            path: "",
            // name: 'zzjg',
            component: () => import("@/views/zzjgyqx/index.vue"),
            meta: { title: "组织机构与权限" }
        }]
    },
    {
        path: "/xxzx",
        component: AppLayout,
        hidden: true,
        children: [{
            path: "",
            name: "xxzx",

            component: () => import("@/views/xxzx/index.vue"),
            meta: { title: "消息中心" }
        }]
    },
    // 404 page must be placed at the end !!!
    { path: "*", redirect: "/404", hidden: true },
];

function hasPermission(route, role) {
    if (Array.isArray(route?.meta?.roles)) {
        return route.meta.roles.includes(role);
    } else {
        return true;
    }
}

function handleFilterRoutes(routes, role) {
    const res = [];
    routes.forEach((route) => {
        const tmp = { ...route };
        if (hasPermission(tmp, role)) {
            if (tmp.children) {
                tmp.children = handleFilterRoutes(tmp.children, role);
            }
            res.push(tmp);
        }
    });
    return res;
}

import { user } from "@/stores/user";

// var filterRoutes;
// const ifPower = user.power;
// var announceMark = ifPower.announce;
// if (announceMark == 1) {
//     //设定了权限;
//     var aa = filterNewRoutes(allRoutes, ifPower);
//
//     filterRoutes = handleFilterRoutes(aa, user.roleName)
//     console.log(filterRoutes, "9999")
// } else {
//     //没有设定权限
//     filterRoutes = handleFilterRoutes(allRoutes, user.roleName);
// }

// var filterRoutes = handleFilterRoutes(allRoutes, user.roleName);

function filterNewRoutes(allRoutes, ifPower) {
    var temp_routesArr = [];
    allRoutes.forEach((item) => {
        const tmp = { ...item };
        if (item.name == "mhgl") {
            delete item.meta.roles;
            if (item.children) {
                item.children.forEach((itemSec) => {
                    //将活动公告，通知公告的roles去除
                    if (itemSec.path == "hdgg" || itemSec.path == "tzgg") {
                        delete itemSec.meta.roles;
                    }
                });
            }
        }
        temp_routesArr.push(tmp);
    });
    return temp_routesArr;
}

const createRouter = () =>
    new Router({
        // mode: 'history', // require service support
        scrollBehavior: () => ({ y: 0 }),
        routes: allRoutes,
    });

const router = createRouter();

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
    const newRouter = createRouter();
    router.matcher = newRouter.matcher; // reset router
}

export default router;

/* 
=====================================================================================
=====================================================================================
*/

import getPageTitle from "@/utils/get-page-title";

router.beforeEach(async (to, from, next) => {
    document.title = getPageTitle(to.meta.title);
    console.groupCollapsed(
        `router ${from.meta.title || "新进入"} => ${to.meta.title}`
    );
    console.log("from :>> ", from);
    console.log("to :>> ", to);
    console.groupEnd();
    next();
});
