import Vue from 'vue';
import VueRouter from 'vue-router';
import store from '../store';
import { MessageBox } from 'element-ui';
// 解决ElementUI导航栏中的vue-router在3.0版本以上重复点菜单报错问题
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err);
};
Vue.use(VueRouter);
import eventBus from '../eventBus';

const routes = [
    {
        path: '/',
        redirect: 'home'
    },
    {
        path: '/',
        component: () => import('../views/Index.vue'),
        children: [
            {
                path: '/home',
                name: 'home',
                component: () => import('../views/Home.vue'),
                meta: {
                    title: '首页'
                }
            },
            {
                path: '/casting',
                name: 'casting',
                component: () => import('../views/Casting.vue'),
                meta: {
                    title: '铸造'
                }
            },
            {
                path: '/castingDetail',
                name: 'castingDetail',
                component: () => import('../views/CastingDetail.vue'),
                meta: {
                    title: '铸造'
                }
            },
            {
                path: '/collection',
                name: 'collection',
                component: () => import('../views/Collection.vue'),
                meta: {
                    title: '收藏探索'
                }
            },
            {
                path: '/blindbox',
                name: 'blindbox',
                component: () => import('../views/Blindsbox.vue'),
                meta: {
                    title: '数字盲盒'
                }
            },
            {
                path: '/collectionDetail',
                name: 'collectionDetail',
                component: () => import('../views/CollectionDetail.vue'),
                meta: {
                    title: '数字盲盒详情'
                }
            },
            {
                path: '/my',
                name: 'my',
                component: () => import('../views/My.vue'),
                meta: {
                    title: '我的NFT',
                    checkLogin: true
                }
            },
            {
                path: '/',
                component: () => import('../views/user/Personal'),
                children: [
                    {
                        path: '/accountdata',
                        name: 'accountdata',
                        component: () => import('../views/user/AccountData.vue'),
                        meta: {
                            title: '资料账号'
                        }
                    },
                    {
                        path: '/fans',
                        name: 'fans',
                        component: () => import('../views/user/Fans.vue'),
                        meta: {
                            title: '粉丝'
                        }
                    }
                ]
            },
            {
                path: '/security',
                name: 'security',
                component: () => import('../views/user/Security.vue'),
                meta: {
                    title: '修改登录账号'
                }
            },
            {
                path: '/securitysuccess',
                name: 'securitysuccess',
                component: () => import('../views/user/SecuritySuccess.vue'),
                meta: {
                    title: '修改登录账号'
                }
            },
            {
                path: '/transaction',
                name: 'transaction',
                component: () => import('../views/user/Transaction.vue'),
                meta: {
                    title: '修改交易密码'
                }
            },
            {
                path: '/transactionsuccess',
                name: 'transactionsuccess',
                component: () => import('../views/user/TransactionSuccess.vue'),
                meta: {
                    title: '修改交易密码'
                }
            },
            {
                path: '/modifypad',
                name: 'modifypad',
                component: () => import('../views/user/Modifypad.vue'),
                meta: {
                    title: '修改密码'
                }
            },
            {
                path: '/authentication',
                name: 'authentication',
                component: () => import('../views/user/Authentication.vue'),
                meta: {
                    title: '认证'
                }
            },
            {
                path: '/userauthentication',
                name: 'userauthentication',
                component: () => import('../views/user/UserAuthentication.vue'),
                meta: {
                    title: '个人认证'
                }
            },
            {
                path: '/enterpriseauthentication',
                name: 'enterpriseauthentication',
                component: () => import('../views/user/EnterpriseAuthentication.vue'),
                meta: {
                    title: '企业认证'
                }
            }
        ]
    }
];

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes,
    scrollBehavior(to, from, savedPosition) {
        console.log(savedPosition);
        if (to.path !== from.path) {
            if (savedPosition) {
                return savedPosition;
            }

            return {
                x: 0,
                y: 0
            };
        }
    }
});

router.beforeEach((to, from, next) => {
    if (!store.state.userInfo) {
        store
            .dispatch('getUserInfo')
            .then(() => {
                next();
            })
            .catch(() => {
                if (to.meta.checkLogin) {
                    MessageBox('用户未登录,是否立即登录', '提示', {
                        confirmButtonText: '立即登录'
                    }).then(() => {
                        eventBus.$emit('login');
                    });
                    next('/');
                } else {
                    next();
                }
            });
    } else {
        next();
    }
});

export default router;
