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: '/assetDetail',
                name: 'assetDetail',
                component: () => import('../views/AssetDetail.vue'),
                meta: {
                    title: 'NFT详情'
                }
            },
            {
                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: '资料账号',
                            checkLogin: true
                        }
                    },
                    {
                        path: '/address',
                        name: 'address',
                        component: () => import('../views/user/Address.vue'),
                        meta: {
                            title: '地址管理',
                            checkLogin: true
                        }
                    },
                    {
                        path: '/give',
                        name: 'give',
                        component: () => import('../views/user/Give.vue'),
                        meta: {
                            title: '我赞过的',
                            checkLogin: true
                        }
                    },
                    {
                        path: '/collectionorder',
                        name: 'collectionorder',
                        component: () => import('../views/user/CollectionOrder.vue'),
                        meta: {
                            title: '藏品订单',
                            checkLogin: true
                        }
                    },
                    {
                        path: '/payrecord',
                        name: 'payrecord',
                        component: () => import('../views/user/PayRecord.vue'),
                        meta: {
                            title: '支付记录',
                            checkLogin: true
                        }
                    },
                    {
                        path: '/transactionordes',
                        name: 'transactionordes',
                        component: () => import('../views/user/TransactionOrdes.vue'),
                        meta: {
                            title: '交易记录',
                            checkLogin: true
                        }
                    },
                    {
                        path: '/ordervalue',
                        name: 'ordervalue',
                        component: () => import('../views/user/OrderValue.vue'),
                        meta: {
                            title: '价值证明',
                            checkLogin: true
                        }
                    }
                ]
            },
            {
                path: '/security',
                name: 'security',
                component: () => import('../views/user/Security.vue'),
                meta: {
                    title: '修改登录账号',
                    checkLogin: true
                }
            },
            {
                path: '/securitysuccess',
                name: 'securitysuccess',
                component: () => import('../views/user/SecuritySuccess.vue'),
                meta: {
                    title: '修改登录账号',
                    checkLogin: true
                }
            },
            {
                path: '/transaction',
                name: 'transaction',
                component: () => import('../views/user/Transaction.vue'),
                meta: {
                    title: '修改交易密码',
                    checkLogin: true
                }
            },
            {
                path: '/transactionsuccess',
                name: 'transactionsuccess',
                component: () => import('../views/user/TransactionSuccess.vue'),
                meta: {
                    title: '修改交易密码',
                    checkLogin: true
                }
            },
            {
                path: '/modifypad',
                name: 'modifypad',
                component: () => import('../views/user/Modifypad.vue'),
                meta: {
                    title: '修改密码',
                    checkLogin: true
                }
            },
            {
                path: '/addipc',
                name: 'addipc',
                component: () => import('../views/user/Addipc.vue'),
                meta: {
                    title: '添加银行卡',
                    checkLogin: true
                }
            },
            {
                path: '/authentication',
                name: 'authentication',
                component: () => import('../views/user/Authentication.vue'),
                meta: {
                    title: '认证',
                    checkLogin: true
                }
            },
            {
                path: '/userauthentication',
                name: 'userauthentication',
                component: () => import('../views/user/UserAuthentication.vue'),
                meta: {
                    title: '个人认证',
                    checkLogin: true
                }
            },
            {
                path: '/enterpriseauthentication',
                name: 'enterpriseauthentication',
                component: () => import('../views/user/EnterpriseAuthentication.vue'),
                meta: {
                    title: '企业认证',
                    checkLogin: true
                }
            }
        ]
    }
];

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(backNext(to));
            })
            .catch(() => {
                if (to.meta.checkLogin) {
                    MessageBox.confirm('用户未登录,是否立即登录', '提示', {
                        confirmButtonText: '立即登录'
                    }).then(() => {
                        eventBus.$emit('login');
                    });
                    next(false);
                } else {
                    next(backNext(to));
                }
            });
    } else {
        next(backNext(to));
    }
});

function backNext(to) {
    if (to.path === '/productDetail') {
        to.path = '/collectionDetail';
        return to;
    } else if (to.path === '/creatorDetail') {
        to.path = '/castingDetail';
        return to;
    } else {
        return;
    }
}
export default router;
