import Vue from 'vue';
import Router from 'vue-router';
import { AuthGuard } from '@/utils';
import qs from 'qs';

Vue.use(Router);


const router = new Router({
    routes: [
        {
            path: '/appindexview/:appindexview?',
            beforeEnter: (to: any, from: any, next: any) => {
                const routerParamsName = 'appindexview';
                const params: any = {};
                if (to.params && to.params[routerParamsName]) {
                    Object.assign(params, qs.parse(to.params[routerParamsName], { delimiter: ';' }));
                }
                const url: string = 'v7/appdata';
                const auth: Promise<any> = AuthGuard.getInstance().authGuard(url, params, router);
                auth.then(() => {
                    next();
                }).catch(() => {
                    next();
                });
            },
            meta: {  
                caption: '首页',
                viewType: 'APPINDEX',
                parameters: [
                    { pathName: 'appindexview', parameterName: 'appindexview' },
                ],
                requireAuth: true,
            },
            component: () => import('@pages/ungroup/app-index-view/app-index-view.vue'),
            children: [
                {
                    path: 'orgusers/:orguser?/gridview/:gridview?',
                    meta: {
                        caption: 'orguser.views.gridview.caption',
                        parameters: [
                            { pathName: 'appindexview', parameterName: 'appindexview' },
                            { pathName: 'orgusers', parameterName: 'orguser' },
                            { pathName: 'gridview', parameterName: 'gridview' },
                        ],
                        requireAuth: true,
                    },
                    component: () => import('@pages/external/orgusergrid-view/orgusergrid-view.vue'),
                },
                {
                    path: 'syxbjs/:syxbj?/gridview/:gridview?',
                    meta: {
                        caption: 'syxbj.views.gridview.caption',
                        parameters: [
                            { pathName: 'appindexview', parameterName: 'appindexview' },
                            { pathName: 'syxbjs', parameterName: 'syxbj' },
                            { pathName: 'gridview', parameterName: 'gridview' },
                        ],
                        requireAuth: true,
                    },
                    component: () => import('@pages/external/syxbjgrid-view/syxbjgrid-view.vue'),
                },
                {
                    path: 'orgusers/:orguser?/editview/:editview?',
                    meta: {
                        caption: 'orguser.views.editview.caption',
                        parameters: [
                            { pathName: 'appindexview', parameterName: 'appindexview' },
                            { pathName: 'orgusers', parameterName: 'orguser' },
                            { pathName: 'editview', parameterName: 'editview' },
                        ],
                        requireAuth: true,
                    },
                    component: () => import('@pages/external/orguseredit-view/orguseredit-view.vue'),
                },
                {
                    path: 'jqxbjs/:jqxbj?/gridview/:gridview?',
                    meta: {
                        caption: 'jqxbj.views.gridview.caption',
                        parameters: [
                            { pathName: 'appindexview', parameterName: 'appindexview' },
                            { pathName: 'jqxbjs', parameterName: 'jqxbj' },
                            { pathName: 'gridview', parameterName: 'gridview' },
                        ],
                        requireAuth: true,
                    },
                    component: () => import('@pages/external/jqxbjgrid-view/jqxbjgrid-view.vue'),
                },
                {
                    path: 'orgs/:org?/gridview/:gridview?',
                    meta: {
                        caption: 'org.views.gridview.caption',
                        parameters: [
                            { pathName: 'appindexview', parameterName: 'appindexview' },
                            { pathName: 'orgs', parameterName: 'org' },
                            { pathName: 'gridview', parameterName: 'gridview' },
                        ],
                        requireAuth: true,
                    },
                    component: () => import('@pages/external/orggrid-view/orggrid-view.vue'),
                },
                {
                    path: 'orgs/:org?/pickupgridview/:pickupgridview?',
                    meta: {
                        caption: 'org.views.pickupgridview.caption',
                        parameters: [
                            { pathName: 'appindexview', parameterName: 'appindexview' },
                            { pathName: 'orgs', parameterName: 'org' },
                            { pathName: 'pickupgridview', parameterName: 'pickupgridview' },
                        ],
                        requireAuth: true,
                    },
                    component: () => import('@pages/external/orgpickup-grid-view/orgpickup-grid-view.vue'),
                },
                {
                    path: 'orgs/:org?/editview/:editview?',
                    meta: {
                        caption: 'org.views.editview.caption',
                        parameters: [
                            { pathName: 'appindexview', parameterName: 'appindexview' },
                            { pathName: 'orgs', parameterName: 'org' },
                            { pathName: 'editview', parameterName: 'editview' },
                        ],
                        requireAuth: true,
                    },
                    component: () => import('@pages/external/orgedit-view/orgedit-view.vue'),
                },
                {
                    path: 'jqxbjs/:jqxbj?/editview/:editview?',
                    meta: {
                        caption: 'jqxbj.views.editview.caption',
                        parameters: [
                            { pathName: 'appindexview', parameterName: 'appindexview' },
                            { pathName: 'jqxbjs', parameterName: 'jqxbj' },
                            { pathName: 'editview', parameterName: 'editview' },
                        ],
                        requireAuth: true,
                    },
                    component: () => import('@pages/external/jqxbjedit-view/jqxbjedit-view.vue'),
                },
                {
                    path: 'orgs/:org?/pickupview/:pickupview?',
                    meta: {
                        caption: 'org.views.pickupview.caption',
                        parameters: [
                            { pathName: 'appindexview', parameterName: 'appindexview' },
                            { pathName: 'orgs', parameterName: 'org' },
                            { pathName: 'pickupview', parameterName: 'pickupview' },
                        ],
                        requireAuth: true,
                    },
                    component: () => import('@pages/external/orgpickup-view/orgpickup-view.vue'),
                },
                {
                    path: 'cpxhs/:cpxh?/gridview/:gridview?',
                    meta: {
                        caption: 'cpxh.views.gridview.caption',
                        parameters: [
                            { pathName: 'appindexview', parameterName: 'appindexview' },
                            { pathName: 'cpxhs', parameterName: 'cpxh' },
                            { pathName: 'gridview', parameterName: 'gridview' },
                        ],
                        requireAuth: true,
                    },
                    component: () => import('@pages/external/cpxhgrid-view/cpxhgrid-view.vue'),
                },
                {
                    path: 'cpxhs/:cpxh?/editview/:editview?',
                    meta: {
                        caption: 'cpxh.views.editview.caption',
                        parameters: [
                            { pathName: 'appindexview', parameterName: 'appindexview' },
                            { pathName: 'cpxhs', parameterName: 'cpxh' },
                            { pathName: 'editview', parameterName: 'editview' },
                        ],
                        requireAuth: true,
                    },
                    component: () => import('@pages/external/cpxhedit-view/cpxhedit-view.vue'),
                },
                {
                    path: 'syxbjs/:syxbj?/editview/:editview?',
                    meta: {
                        caption: 'syxbj.views.editview.caption',
                        parameters: [
                            { pathName: 'appindexview', parameterName: 'appindexview' },
                            { pathName: 'syxbjs', parameterName: 'syxbj' },
                            { pathName: 'editview', parameterName: 'editview' },
                        ],
                        requireAuth: true,
                    },
                    component: () => import('@pages/external/syxbjedit-view/syxbjedit-view.vue'),
                },
            ],
        },
    {
        path: '/orgusers/:orguser?/gridview/:gridview?',
        meta: {
            caption: 'orguser.views.gridview.caption',
            parameters: [
                { pathName: 'orgusers', parameterName: 'orguser' },
                { pathName: 'gridview', parameterName: 'gridview' },
            ],
            requireAuth: true,
        },
        component: () => import('@pages/external/orgusergrid-view/orgusergrid-view.vue'),
    },
    {
        path: '/syxbjs/:syxbj?/gridview/:gridview?',
        meta: {
            caption: 'syxbj.views.gridview.caption',
            parameters: [
                { pathName: 'syxbjs', parameterName: 'syxbj' },
                { pathName: 'gridview', parameterName: 'gridview' },
            ],
            requireAuth: true,
        },
        component: () => import('@pages/external/syxbjgrid-view/syxbjgrid-view.vue'),
    },
    {
        path: '/orgusers/:orguser?/editview/:editview?',
        meta: {
            caption: 'orguser.views.editview.caption',
            parameters: [
                { pathName: 'orgusers', parameterName: 'orguser' },
                { pathName: 'editview', parameterName: 'editview' },
            ],
            requireAuth: true,
        },
        component: () => import('@pages/external/orguseredit-view/orguseredit-view.vue'),
    },
    {
        path: '/jqxbjs/:jqxbj?/gridview/:gridview?',
        meta: {
            caption: 'jqxbj.views.gridview.caption',
            parameters: [
                { pathName: 'jqxbjs', parameterName: 'jqxbj' },
                { pathName: 'gridview', parameterName: 'gridview' },
            ],
            requireAuth: true,
        },
        component: () => import('@pages/external/jqxbjgrid-view/jqxbjgrid-view.vue'),
    },
    {
        path: '/orgs/:org?/gridview/:gridview?',
        meta: {
            caption: 'org.views.gridview.caption',
            parameters: [
                { pathName: 'orgs', parameterName: 'org' },
                { pathName: 'gridview', parameterName: 'gridview' },
            ],
            requireAuth: true,
        },
        component: () => import('@pages/external/orggrid-view/orggrid-view.vue'),
    },
    {
        path: '/orgs/:org?/pickupgridview/:pickupgridview?',
        meta: {
            caption: 'org.views.pickupgridview.caption',
            parameters: [
                { pathName: 'orgs', parameterName: 'org' },
                { pathName: 'pickupgridview', parameterName: 'pickupgridview' },
            ],
            requireAuth: true,
        },
        component: () => import('@pages/external/orgpickup-grid-view/orgpickup-grid-view.vue'),
    },
    {
        path: '/orgs/:org?/editview/:editview?',
        meta: {
            caption: 'org.views.editview.caption',
            parameters: [
                { pathName: 'orgs', parameterName: 'org' },
                { pathName: 'editview', parameterName: 'editview' },
            ],
            requireAuth: true,
        },
        component: () => import('@pages/external/orgedit-view/orgedit-view.vue'),
    },
    {
        path: '/jqxbjs/:jqxbj?/editview/:editview?',
        meta: {
            caption: 'jqxbj.views.editview.caption',
            parameters: [
                { pathName: 'jqxbjs', parameterName: 'jqxbj' },
                { pathName: 'editview', parameterName: 'editview' },
            ],
            requireAuth: true,
        },
        component: () => import('@pages/external/jqxbjedit-view/jqxbjedit-view.vue'),
    },
    {
        path: '/orgs/:org?/pickupview/:pickupview?',
        meta: {
            caption: 'org.views.pickupview.caption',
            parameters: [
                { pathName: 'orgs', parameterName: 'org' },
                { pathName: 'pickupview', parameterName: 'pickupview' },
            ],
            requireAuth: true,
        },
        component: () => import('@pages/external/orgpickup-view/orgpickup-view.vue'),
    },
    {
        path: '/cpxhs/:cpxh?/gridview/:gridview?',
        meta: {
            caption: 'cpxh.views.gridview.caption',
            parameters: [
                { pathName: 'cpxhs', parameterName: 'cpxh' },
                { pathName: 'gridview', parameterName: 'gridview' },
            ],
            requireAuth: true,
        },
        component: () => import('@pages/external/cpxhgrid-view/cpxhgrid-view.vue'),
    },
    {
        path: '/cpxhs/:cpxh?/editview/:editview?',
        meta: {
            caption: 'cpxh.views.editview.caption',
            parameters: [
                { pathName: 'cpxhs', parameterName: 'cpxh' },
                { pathName: 'editview', parameterName: 'editview' },
            ],
            requireAuth: true,
        },
        component: () => import('@pages/external/cpxhedit-view/cpxhedit-view.vue'),
    },
    {
        path: '/syxbjs/:syxbj?/editview/:editview?',
        meta: {
            caption: 'syxbj.views.editview.caption',
            parameters: [
                { pathName: 'syxbjs', parameterName: 'syxbj' },
                { pathName: 'editview', parameterName: 'editview' },
            ],
            requireAuth: true,
        },
        component: () => import('@pages/external/syxbjedit-view/syxbjedit-view.vue'),
    },
        {
            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();
            },
            component: () => import('@components/login/login.vue'),
        },
        {
            path: '/404',
            component: () => import('@components/404/404.vue')
        },
        {
            path: '/500',
            component: () => import('@components/500/500.vue')
        },
        {
            path: '*',
            redirect: 'appindexview'
        },
    ],
});

export default router;
