import Vue from 'vue';
import Router from 'vue-router';
import { AuthGuard } from '@/utils';
import qs from 'qs';
import { globalRoutes, indexRoutes} from '@/router'
// 基于 @VIEW/应用首页视图/ROUTER.ts.ftl 生成
Vue.use(Router);
<#-- 输出重定向页面路径 -->
<#macro getRedirectPath subView isIncludeIndex>
            <#if subView.getViewType() == "APPREDIRECTVIEW">
            <#-- 应用全局数据重定向视图start -->
            {
                path: '<#if !isIncludeIndex>/</#if>appdataredirectview/:appdataredirectview?',
                meta: {
                    caption: '<#if subView.isPSDEView()>entities.${subView.getPSAppDataEntity().getCodeName()?lower_case}.views.${subView.getPSDEViewCodeName()?lower_case}.caption<#else>app.views.${subView.getCodeName()?lower_case}.caption</#if>',
                    info:'',
                    viewType: 'REDIRECTVIEW',
                    <#if subView.getPSSysImage()??>
                    imgPath: '${subView.getPSSysImage().getImagePath()}',
                    iconCls: '${subView.getPSSysImage().getCssClass()}',
                    </#if>
                    parameters: [
    <#if isIncludeIndex == true>{ pathName: '${view.getCodeName()?lower_case}', parameterName: '${view.getCodeName()?lower_case}' },</#if>
                        { pathName: '${subView.getCodeName()?lower_case}', parameterName: '${subView.getCodeName()?lower_case}' },
                    ],
                    requireAuth: <#if subView.getAccUserMode?? && subView.getAccUserMode()?? && (subView.getAccUserMode() == 1 || subView.getAccUserMode() == 3)>false<#else>true</#if>,
                },
                component: () => import('@pages/${srffilepath2(subView.getPSAppModule().getCodeName())}/${srffilepath2(subView.getCodeName())}/${srffilepath2(subView.getCodeName())}.vue'),
            },
            <#-- 应用全局数据重定向视图end -->
            <#elseif subView.getViewType() == "APPWFREDIRECTVIEW">
            <#-- 应用流程工作重定向视图start -->
            {
                path: '<#if !isIncludeIndex>/</#if>appwfdataredirectview/:appwfdataredirectview?',
                meta: {
                    caption: '<#if subView.isPSDEView()>entities.${subView.getPSAppDataEntity().getCodeName()?lower_case}.views.${subView.getPSDEViewCodeName()?lower_case}.caption<#else>app.views.${subView.getCodeName()?lower_case}.caption</#if>',
                    info:'',
                    viewType: 'REDIRECTVIEW',
                    <#if subView.getPSSysImage()??>
                    imgPath: '${subView.getPSSysImage().getImagePath()}',
                    iconCls: '${subView.getPSSysImage().getCssClass()}',
                    </#if>
                    parameters: [
                        <#if isIncludeIndex == true>{ pathName: '${view.getCodeName()?lower_case}', parameterName: '${view.getCodeName()?lower_case}' },</#if>
                        { pathName: '${subView.getCodeName()?lower_case}', parameterName: '${subView.getCodeName()?lower_case}' },
                    ],
                    requireAuth: <#if subView.getAccUserMode?? && subView.getAccUserMode()?? && (subView.getAccUserMode() == 1 || subView.getAccUserMode() == 3)>false<#else>true</#if>,
                },
                component: () => import('@pages/${srffilepath2(subView.getPSAppModule().getCodeName())}/${srffilepath2(subView.getCodeName())}/${srffilepath2(subView.getCodeName())}.vue'),
            },
            <#-- 应用流程工作重定向视图end -->
            <#elseif subView.getViewType() == "DEWFDATAREDIRECTVIEW">
            <#-- 实体全局重定向视图start -->
            {
                path: '<#if !isIncludeIndex>/</#if>${subView.getViewType()?lower_case}/:${subView.getViewType()?lower_case}?',
                meta: {
                    caption: '<#if subView.isPSDEView()>entities.${subView.getPSAppDataEntity().getCodeName()?lower_case}.views.${subView.getPSDEViewCodeName()?lower_case}.caption<#else>app.views.${subView.getCodeName()?lower_case}.caption</#if>',
                    info:'',
                    viewType: 'REDIRECTVIEW',
                    <#if subView.getPSSysImage()??>
                    imgPath: '${subView.getPSSysImage().getImagePath()}',
                    iconCls: '${subView.getPSSysImage().getCssClass()}',
                    </#if>
                    parameters: [
                        <#if isIncludeIndex == true>{ pathName: '${view.getCodeName()?lower_case}', parameterName: '${view.getCodeName()?lower_case}' },</#if>
                        { pathName: '${subView.getCodeName()?lower_case}', parameterName: '${subView.getCodeName()?lower_case}' },
                    ],
                    requireAuth: <#if subView.getAccUserMode?? && subView.getAccUserMode()?? && (subView.getAccUserMode() == 1 || subView.getAccUserMode() == 3)>false<#else>true</#if>,
                },
                component: () => import('@pages/${srffilepath2(subView.getPSAppModule().getCodeName())}/${srffilepath2(subView.getCodeName())}/${srffilepath2(subView.getCodeName())}.vue'),
            },
            <#-- 实体全局重定向视图end -->
            <#elseif subView.getViewType() == "DEREDIRECTVIEW">
                <#if subView.getRefFlag?? && subView.getRefFlag()>
            <#--  只发布被引用的实体全局重定向视图  -->
            <#-- 实体数据重定向视图start -->
            {
                path: '<#if !isIncludeIndex>/</#if>${srfpluralize(subView.getPSAppDataEntity().codeName)?lower_case}/:${subView.getPSAppDataEntity().getCodeName()?lower_case}?/${subView.getPSDEViewCodeName()?lower_case}/:${subView.getPSDEViewCodeName()?lower_case}?',
                meta: {
                    caption: '<#if subView.isPSDEView()>entities.${subView.getPSAppDataEntity().getCodeName()?lower_case}.views.${subView.getPSDEViewCodeName()?lower_case}.caption<#else>app.views.${subView.getCodeName()?lower_case}.caption</#if>',
                    info:'',
                    viewType: 'REDIRECTVIEW',
                    <#if subView.getPSSysImage()??>
                    imgPath: '${subView.getPSSysImage().getImagePath()}',
                    iconCls: '${subView.getPSSysImage().getCssClass()}',
                    </#if>
                    parameters: [
                        { pathName: '${view.getCodeName()?lower_case}', parameterName: '${view.getCodeName()?lower_case}' },
                        { pathName: '${srfpluralize(subView.getPSAppDataEntity().codeName)?lower_case}', parameterName: '${subView.getPSAppDataEntity().getCodeName()?lower_case}' },
                        { pathName: '${subView.getPSDEViewCodeName()?lower_case}', parameterName: '${subView.getPSDEViewCodeName()?lower_case}' },
                    ],
                    requireAuth: <#if subView.getAccUserMode?? && subView.getAccUserMode()?? && (subView.getAccUserMode() == 1 || subView.getAccUserMode() == 3)>false<#else>true</#if>,
                },
                component: () => import('@pages/${srffilepath2(subView.getPSAppModule().getCodeName())}/${srffilepath2(subView.getCodeName())}/${srffilepath2(subView.getCodeName())}.vue'),
            },
            <#-- 实体数据重定向视图end -->
                </#if>
            <#else>
            <#-- 其他重定向视图start -->
            {
                path: '<#if !isIncludeIndex>/</#if>${subView.getCodeName()?lower_case}/:${subView.getCodeName()?lower_case}?',
                meta: {
                    caption: '<#if subView.isPSDEView()>entities.${subView.getPSAppDataEntity().getCodeName()?lower_case}.views.${subView.getPSDEViewCodeName()?lower_case}.caption<#else>app.views.${subView.getCodeName()?lower_case}.caption</#if>',
                    info:'',
                    viewType: 'REDIRECTVIEW',
                    <#if subView.getPSSysImage()??>
                    imgPath: '${subView.getPSSysImage().getImagePath()}',
                    iconCls: '${subView.getPSSysImage().getCssClass()}',
                    </#if>
                    parameters: [
                        <#if isIncludeIndex == true>{ pathName: '${view.getCodeName()?lower_case}', parameterName: '${view.getCodeName()?lower_case}' },</#if>
                        { pathName: '${subView.getCodeName()?lower_case}', parameterName: '${subView.getCodeName()?lower_case}' },
                    ],
                    requireAuth: <#if subView.getAccUserMode?? && subView.getAccUserMode()?? && (subView.getAccUserMode() == 1 || subView.getAccUserMode() == 3)>false<#else>true</#if>,
                },
                component: () => import('@pages/${srffilepath2(subView.getPSAppModule().getCodeName())}/${srffilepath2(subView.getCodeName())}/${srffilepath2(subView.getCodeName())}.vue'),
            },
            <#-- 其他重定向视图end -->
            </#if>
</#macro>

<#macro getRouteInfo app>
<#list app.getAllPSAppViews() as subView>
<#if subView.getRefFlag() && subView.getViewType() != "APPINDEXVIEW" && subView.getViewType() != "APPLOGINVIEW">
<#if !subView.isRedirectView()>
<#if subView.isPSDEView()>
    <#assign appDataEntity = subView.getPSAppDataEntity()/>
    <#--  BEGIN:有多重路径  -->
    <#if subView.getPSAppDERSPathCount() gt 0>
    <#list 1..subView.getPSAppDERSPathCount() as count>
    <#assign path = ''/>
    <#--  BEGIN:循环路径下所有关系  -->
    <#list subView.getPSAppDERSPath(count_index) as deRSPath>
    <#assign majorPSAppDataEntity = deRSPath.getMajorPSAppDataEntity()/>
    <#assign path>${path}${srfpluralize(majorPSAppDataEntity.codeName)?lower_case}/:${majorPSAppDataEntity.getCodeName()?lower_case}?/</#assign>
    </#list>
    <#--  END:循环路径下所有关系  -->
    {
        path: '/${path}${srfpluralize(appDataEntity.codeName)?lower_case}/:${appDataEntity.getCodeName()?lower_case}?/${subView.getPSDEViewCodeName()?lower_case}/:${subView.getPSDEViewCodeName()?lower_case}?',
        <#-- 嵌入关系路由未设置name属性 -->
        meta: {
            caption: '<#if subView.isPSDEView()>entities.${subView.getPSAppDataEntity().getCodeName()?lower_case}.views.${subView.getPSDEViewCodeName()?lower_case}.caption<#else>app.views.${subView.getCodeName()?lower_case}.caption</#if>',
            info:'',
            <#if subView.getPSSysImage()??>
            imgPath: '${subView.getPSSysImage().getImagePath()}',
            iconCls: '${subView.getPSSysImage().getCssClass()}',
            </#if>
            parameters: [
                <#list subView.getPSAppDERSPath(count_index) as deRSPath>
                <#assign majorPSAppDataEntity = deRSPath.getMajorPSAppDataEntity()/>
                { pathName: '${srfpluralize(majorPSAppDataEntity.codeName)?lower_case}', parameterName: '${majorPSAppDataEntity.getCodeName()?lower_case}' },
                </#list>
                { pathName: '${srfpluralize(appDataEntity.codeName)?lower_case}', parameterName: '${appDataEntity.getCodeName()?lower_case}' },
                { pathName: '${subView.getPSDEViewCodeName()?lower_case}', parameterName: '${subView.getPSDEViewCodeName()?lower_case}' },
            ],
            requireAuth: <#if subView.getAccUserMode?? && subView.getAccUserMode()?? && (subView.getAccUserMode() == 1 || subView.getAccUserMode() == 3)>false<#else>true</#if>,
        },
        component: () => import('@pages/${srffilepath2(subView.getPSAppModule().getCodeName())}/${srffilepath2(subView.getCodeName())}/${srffilepath2(subView.getCodeName())}.vue'),
    },
    </#list>
    </#if>
    <#--  END:有多重路径  -->
    {
        path: '/${srfpluralize(appDataEntity.codeName)?lower_case}/:${appDataEntity.getCodeName()?lower_case}?/${subView.getPSDEViewCodeName()?lower_case}/:${subView.getPSDEViewCodeName()?lower_case}?',
        meta: {
            caption: '<#if subView.isPSDEView()>entities.${subView.getPSAppDataEntity().getCodeName()?lower_case}.views.${subView.getPSDEViewCodeName()?lower_case}.caption<#else>app.views.${subView.getCodeName()?lower_case}.caption</#if>',
            info:'',
            <#if subView.getPSSysImage()??>
            imgPath: '${subView.getPSSysImage().getImagePath()}',
            iconCls: '${subView.getPSSysImage().getCssClass()}',
            </#if>
            parameters: [
                { pathName: '${srfpluralize(appDataEntity.codeName)?lower_case}', parameterName: '${appDataEntity.getCodeName()?lower_case}' },
                { pathName: '${subView.getPSDEViewCodeName()?lower_case}', parameterName: '${subView.getPSDEViewCodeName()?lower_case}' },
            ],
            requireAuth: <#if subView.getAccUserMode?? && subView.getAccUserMode()?? && (subView.getAccUserMode() == 1 || subView.getAccUserMode() == 3)>false<#else>true</#if>,
        },
        component: () => import('@pages/${srffilepath2(subView.getPSAppModule().getCodeName())}/${srffilepath2(subView.getCodeName())}/${srffilepath2(subView.getCodeName())}.vue'),
    },
<#else>
    {
        path: '/${subView.getCodeName()?lower_case}/:${subView.getCodeName()?lower_case}?',
        meta: {
            caption: '<#if subView.isPSDEView()>entities.${subView.getPSAppDataEntity().getCodeName()?lower_case}.views.${subView.getPSDEViewCodeName()?lower_case}.caption<#else>app.views.${subView.getCodeName()?lower_case}.caption</#if>',
            info:'',
            <#if subView.getPSSysImage()??>
            imgPath: '${subView.getPSSysImage().getImagePath()}',
            iconCls: '${subView.getPSSysImage().getCssClass()}',
            </#if>
            parameters: [
                { pathName: '${subView.getCodeName()?lower_case}', parameterName: '${subView.getCodeName()?lower_case}' },
            ],
            requireAuth: <#if subView.getAccUserMode?? && subView.getAccUserMode()?? && (subView.getAccUserMode() == 1 || subView.getAccUserMode() == 3)>false<#else>true</#if>,
        },
        component: () => import('@pages/${srffilepath2(subView.getPSAppModule().getCodeName())}/${srffilepath2(subView.getCodeName())}/${srffilepath2(subView.getCodeName())}.vue'),
    },
</#if>
<#else>
<@getRedirectPath subView=subView  isIncludeIndex=false />
</#if>
</#if>
</#list>
</#macro>

const router = new Router({
    routes: [
        <#--  路由内容  -->
        {
            path: '/${view.getCodeName()?lower_case}/:${view.getCodeName()?lower_case}?',
            beforeEnter: (to: any, from: any, next: any) => {
                const routerParamsName = '${view.getCodeName()?lower_case}';
                const params: any = {};
                if (to.params && to.params[routerParamsName]) {
                    Object.assign(params, qs.parse(to.params[routerParamsName], { delimiter: ';' }));
                }
                const url: string = '/appdata';
                const auth: Promise<any> = AuthGuard.getInstance().authGuard(url, params, router);
                auth.then(() => {
                    next();
                }).catch(() => {
                    next();
                });
            },
            meta: {  
                caption: '<#if view.isPSDEView()>entities.${view.getPSAppDataEntity().getCodeName()?lower_case}.views.${view.getPSDEViewCodeName()?lower_case}.caption<#else>app.views.${view.getCodeName()?lower_case}.caption</#if>',
                info:'',
                viewType: 'APPINDEX',
                <#if view.getPSSysImage()??>
                imgPath: '${view.getPSSysImage().getImagePath()}',
                iconCls: '${view.getPSSysImage().getCssClass()}',
                </#if>
                parameters: [
                    { pathName: '${view.getCodeName()?lower_case}', parameterName: '${view.getCodeName()?lower_case}' },
                ],
                requireAuth: <#if view.getAccUserMode?? && view.getAccUserMode()?? && (view.getAccUserMode() == 1 || view.getAccUserMode() == 3)>false<#else>true</#if>,
            },
            component: () => import('@pages/${srffilepath2(view.getPSAppModule().getCodeName())}/${srffilepath2(view.getCodeName())}/${srffilepath2(view.getCodeName())}.vue'),
            children: [
            <#list view.getAllRelatedPSAppViewsEx() as subView>
            <#if !subView.isRedirectView()>
            <#if subView.isPSDEView()>
                <#assign appDataEntity = subView.getPSAppDataEntity()/>
                <#--  BEGIN:有多重路径  -->
                <#if subView.getPSAppDERSPathCount() gt 0>
                <#list 1..subView.getPSAppDERSPathCount() as count>
                <#assign path = ''/>
                <#--  BEGIN:循环路径下所有关系  -->
                <#list subView.getPSAppDERSPath(count_index) as deRSPath>
                <#assign majorPSAppDataEntity = deRSPath.getMajorPSAppDataEntity()/>
                <#assign path>${path}${srfpluralize(majorPSAppDataEntity.codeName)?lower_case}/:${majorPSAppDataEntity.getCodeName()?lower_case}?/</#assign>
                </#list>
                <#--  END:循环路径下所有关系  -->
                {
                    path: '${path}${srfpluralize(appDataEntity.codeName)?lower_case}/:${appDataEntity.getCodeName()?lower_case}?/${subView.getPSDEViewCodeName()?lower_case}/:${subView.getPSDEViewCodeName()?lower_case}?',
                    meta: {
                        caption: '<#if subView.isPSDEView()>entities.${subView.getPSAppDataEntity().getCodeName()?lower_case}.views.${subView.getPSDEViewCodeName()?lower_case}.caption<#else>app.views.${subView.getCodeName()?lower_case}.caption</#if>',
                        info:'',
                        <#if subView.getPSSysImage()??>
                        imgPath: '${subView.getPSSysImage().getImagePath()}',
                        iconCls: '${subView.getPSSysImage().getCssClass()}',
                        </#if>
                        parameters: [
                            { pathName: '${view.getCodeName()?lower_case}', parameterName: '${view.getCodeName()?lower_case}' },
                            <#list subView.getPSAppDERSPath(count_index) as deRSPath>
                            <#assign majorPSAppDataEntity = deRSPath.getMajorPSAppDataEntity()/>
                            { pathName: '${srfpluralize(majorPSAppDataEntity.codeName)?lower_case}', parameterName: '${majorPSAppDataEntity.getCodeName()?lower_case}' },
                            </#list>
                            { pathName: '${srfpluralize(appDataEntity.codeName)?lower_case}', parameterName: '${appDataEntity.getCodeName()?lower_case}' },
                            { pathName: '${subView.getPSDEViewCodeName()?lower_case}', parameterName: '${subView.getPSDEViewCodeName()?lower_case}' },
                        ],
                        requireAuth: <#if subView.getAccUserMode?? && subView.getAccUserMode()?? && (subView.getAccUserMode() == 1 || subView.getAccUserMode() == 3)>false<#else>true</#if>,
                    },
                    component: () => import('@pages/${srffilepath2(subView.getPSAppModule().getCodeName())}/${srffilepath2(subView.getCodeName())}/${srffilepath2(subView.getCodeName())}.vue'),
                },
                </#list>
                </#if>
                <#--  END:有多重路径  -->
                {
                    path: '${srfpluralize(appDataEntity.codeName)?lower_case}/:${appDataEntity.getCodeName()?lower_case}?/${subView.getPSDEViewCodeName()?lower_case}/:${subView.getPSDEViewCodeName()?lower_case}?',
                    meta: {
                        caption: '<#if subView.isPSDEView()>entities.${subView.getPSAppDataEntity().getCodeName()?lower_case}.views.${subView.getPSDEViewCodeName()?lower_case}.caption<#else>app.views.${subView.getCodeName()?lower_case}.caption</#if>',
                        info:'',
                        <#if subView.getPSSysImage()??>
                        imgPath: '${subView.getPSSysImage().getImagePath()}',
                        iconCls: '${subView.getPSSysImage().getCssClass()}',
                        </#if>
                        parameters: [
                            { pathName: '${view.getCodeName()?lower_case}', parameterName: '${view.getCodeName()?lower_case}' },
                            { pathName: '${srfpluralize(appDataEntity.codeName)?lower_case}', parameterName: '${appDataEntity.getCodeName()?lower_case}' },
                            { pathName: '${subView.getPSDEViewCodeName()?lower_case}', parameterName: '${subView.getPSDEViewCodeName()?lower_case}' },
                        ],
                        requireAuth: <#if subView.getAccUserMode?? && subView.getAccUserMode()?? && (subView.getAccUserMode() == 1 || subView.getAccUserMode() == 3)>false<#else>true</#if>,
                    },
                    component: () => import('@pages/${srffilepath2(subView.getPSAppModule().getCodeName())}/${srffilepath2(subView.getCodeName())}/${srffilepath2(subView.getCodeName())}.vue'),
                },
            <#else>
                {
                    path: '${subView.getCodeName()?lower_case}/:${subView.getCodeName()?lower_case}?',
                    meta: {
                        caption: '<#if subView.isPSDEView()>entities.${subView.getPSAppDataEntity().getCodeName()?lower_case}.views.${subView.getPSDEViewCodeName()?lower_case}.caption<#else>app.views.${subView.getCodeName()?lower_case}.caption</#if>',
                        info:'',
                        <#if subView.getPSSysImage()??>
                        imgPath: '${subView.getPSSysImage().getImagePath()}',
                        iconCls: '${subView.getPSSysImage().getCssClass()}',
                        </#if>
                        parameters: [
                            { pathName: '${view.getCodeName()?lower_case}', parameterName: '${view.getCodeName()?lower_case}' },
                            { pathName: '${subView.getCodeName()?lower_case}', parameterName: '${subView.getCodeName()?lower_case}' },
                        ],
                        requireAuth: <#if subView.getAccUserMode?? && subView.getAccUserMode()?? && (subView.getAccUserMode() == 1 || subView.getAccUserMode() == 3)>false<#else>true</#if>,
                    },
                    component: () => import('@pages/${srffilepath2(subView.getPSAppModule().getCodeName())}/${srffilepath2(subView.getCodeName())}/${srffilepath2(subView.getCodeName())}.vue'),
                },
            </#if>
            </#if>
            </#list>
            <#if app.getAllPSAppViews()??>
            <#list app.getAllPSAppViews() as subView>
            <#if subView.isRedirectView() && subView.getRefFlag()>
                <@getRedirectPath subView=subView  isIncludeIndex=true />
            </#if>
            </#list>
            </#if>
            ...indexRoutes,
            ],
        },
        <@getRouteInfo app/>
        ...globalRoutes,
        {
            path: '/login/:login?',
            name: 'login',
            meta: {  
                caption: '登录',
                viewType: 'login',
                requireAuth: false,
                ignoreAddPage: true,
            },
            beforeEnter: (to: any, from: any, next: any) => {
                router.app.$store.commit('resetRootStateData');
                next();
            },
            <#list app.getAllPSAppViews() as subView>
            <#if subView.getRefFlag() && subView.getViewType() == "APPLOGINVIEW">
            <#assign loginView = subView>
            </#if>
            </#list>
            <#if loginView??>
            component: () => import('@pages/${srffilepath2(loginView.getPSAppModule().getCodeName())}/${srffilepath2(loginView.getCodeName())}/${srffilepath2(loginView.getCodeName())}.vue'),
            <#else>
            component: () => import('@components/login/login.vue'),
            </#if>
        },
        {
            path: '/lock',
            name: 'lock',
            meta: {  
                caption: '锁屏',
                viewType: 'lock',
                requireAuth: false,
                ignoreAddPage: true,
            },
            beforeEnter: (to: any, from: any, next: any) => {
                next();
            },
            component: () => import('@components/app-lock/app-lock.vue'),
        },
        {
            path: '/404',
            component: () => import('@components/404/404.vue')
        },
        {
            path: '/500',
            component: () => import('@components/500/500.vue')
        },
        {
            path: '*',
            redirect: '${view.getCodeName()?lower_case}'
        },
    ],
});
// 解决路由跳转路由重复时报错
const originalPush = Router.prototype.push
Router.prototype.push = function push(location: any) {
    let result: any = originalPush.call(this, location);
    return result.catch((err: any) => err);
}
export default router;