import { createRouter, createWebHistory, createWebHashHistory } from 'vue-router';
import store from '../store';
import { Page } from './Page';
import { Dialog } from 'vant';
import http from '../plugins/http';

jsapiSign();
function jsapiSign() {
    return;
    // eslint-disable-next-line no-unreachable
    setTimeout(() => {
        if (/micromessenger/i.test(navigator.userAgent)) {
            if (/localhost|(192\.168)/i.test(location.host)) {
                return;
            }
            let isIOS = /iphone|ipad/i.test(navigator.userAgent);
            http.http
                .get('/wx/jsapiSign', { url: isIOS ? store.state.firstUrl : location.origin + location.href })
                .then(res => {
                    wx.config({
                        debug: false,
                        appId: res.appId,
                        timestamp: res.timestamp,
                        nonceStr: res.nonceStr,
                        signature: res.signature,
                        jsApiList: [
                            'chooseWXPay',
                            'updateAppMessageShareData',
                            'updateTimelineShareData',
                            'hideAllNonBaseMenuItem',
                            'scanQRCode'
                        ],
                        openTagList: ['wx-open-launch-weapp']
                    });
                    wx.error(function (res) {
                        console.log(res);
                    });
                    wx.ready(function () {
                        wx.updateAppMessageShareData({
                            desc: '全球首个基于区块链的游戏资产集换中心',
                            link: location.origin + '/saas',
                            imgUrl: 'https://9space-2021.oss-cn-shenzhen.aliyuncs.com/nft/2021-11-05-15-58-30YwqLzMjy.jpg'
                        });
                        wx.updateTimelineShareData({
                            title: '宇宙-全球首个基于区块链的游戏资产集换中心',
                            link: location.origin + '/saas',
                            imgUrl: 'https://9space-2021.oss-cn-shenzhen.aliyuncs.com/nft/2021-11-05-15-58-30YwqLzMjy.jpg'
                        });
                    });
                })
                .catch(e => {});
        }
    }, 200);
}
jsapiSign();
const routes = [
    {
        path: '/:companyId',
        component: () => import('../views/CompanyIndex.vue'),
        children: [
            {
                path: '/',
                redirect: 'home'
            },
            {
                path: '/:companyId',
                name: 'index',
                component: () => import('../views/Index.vue'),
                children: [
                    {
                        path: 'home',
                        name: 'home',
                        component: () =>
                            store.state.hopeMarket
                                ? import('../views/product/HopeMarket.vue')
                                : import('../views/Home.vue'),
                        meta: {
                            pageType: Page.Every,
                            menuPage: true
                        }
                    },
                    {
                        path: 'discover',
                        name: 'discover',
                        component: () => import('../views/product/HopeMarket.vue'),
                        meta: {
                            pageType: Page.Every,
                            menuPage: true
                        }
                    },
                    {
                        path: 'creator',
                        name: 'creator',
                        component: () => import('../views/Creator.vue'),
                        meta: {
                            pageType: Page.Every,
                            menuPage: true
                        }
                    },
                    {
                        path: 'store',
                        name: 'store',
                        component: () => import('../views/Store.vue'),
                        meta: {
                            pageType: Page.Every,
                            menuPage: true
                        }
                    },
                    {
                        path: 'mine',
                        name: 'mine',
                        component: () => import('../views/Mine.vue'),
                        meta: {
                            pageType: Page.Every,
                            menuPage: true
                        }
                    }
                ]
            },
            {
                path: 'hall',
                name: 'hall',
                component: () => import('../views/hall/Detail.vue'),
                meta: {
                    pageType: Page.Every,
                    menuPage: true,
                    isHall: true
                }
            },
            {
                path: 'hallEdit',
                name: 'hallEdit',
                component: () => import('../views/hall/Edit.vue'),
                meta: {
                    pageType: Page.Every,
                    isHall: true,
                    tabColor: '#181818'
                }
            },
            {
                path: 'productAdd',
                name: 'productAdd',
                component: () => import('../views/hall/ProductAdd.vue'),
                meta: {
                    pageType: Page.Every,
                    isHall: true,
                    tabColor: '#181818'
                }
            },
            {
                path: 'productChoose',
                name: 'productChoose',
                component: () => import('../views/hall/ProductChoose.vue'),
                meta: {
                    pageType: Page.Every,
                    isHall: true,
                    tabColor: '#181818'
                }
            },
            {
                path: 'login',
                name: 'userLogin',
                component: () => import('../views/account/Login.vue'),
                meta: {
                    pageType: Page.Login,
                    title: '登录',
                    tabColor: '#181818'
                }
            },
            {
                path: 'agreement',
                name: 'agreement',
                component: () => import('../views/account/Agreement.vue'),
                meta: {
                    pageType: Page.Login,
                    title: '协议'
                }
            },
            {
                path: 'register',
                name: 'userRegister',
                component: () => import('../views/account/Register.vue'),
                meta: {
                    pageType: Page.Login,
                    title: '注册',
                    tabColor: '#181818'
                }
            },
            {
                path: 'forget',
                name: 'userForget',
                component: () => import('../views/account/Forget.vue'),
                meta: {
                    pageType: Page.Login,
                    // menuPage: true,
                    title: '忘记密码'
                }
            },
            {
                path: 'changePhone',
                name: 'changePhone',
                component: () => import('../views/account/ChangePhone.vue'),
                meta: {
                    title: '修改密码'
                }
            },
            {
                path: 'verified',
                name: 'userVerified',
                component: () => import('../views/account/Verified.vue')
            },
            {
                path: 'verifiedSuc',
                name: 'verifiedSuc',
                component: () => import('../views/account/VerifiedSuc.vue')
            },
            {
                path: 'verifiedSucs',
                name: 'verifiedSucs',
                component: () => import('../views/account/VerifiedSucs.vue')
            },
            {
                path: 'Authentication',
                name: 'Authentication',
                component: () => import('../views/account/Authentication.vue')
            },
            {
                path: 'setting',
                name: 'userSetting',
                component: () => import('../views/account/Setting.vue')
            },
            {
                path: 'changeText',
                name: 'userChangeText',
                component: () => import('../views/account/ChangeText.vue')
            },
            {
                path: 'waiting',
                name: 'userWaiting',
                component: () => import('../views/account/Waiting.vue')
            },
            {
                path: 'security',
                name: 'security',
                component: () => import('../views/account/Security.vue')
            },
            {
                path: 'about',
                name: 'about',
                component: () => import('../views/account/About.vue'),
                meta: {
                    pageType: Page.Every
                }
            },
            {
                path: 'message',
                name: 'message',
                component: () => import('../views/account/Message.vue'),
                meta: {
                    pageType: Page.Every,
                    title: '客服留言'
                }
            },
            {
                path: 'messageAdd',
                name: 'messageAdd',
                component: () => import('../views/account/MessageAdd.vue'),
                meta: {
                    pageType: Page.Every,
                    title: '反馈问题'
                }
            },
            {
                path: 'messageDetail',
                name: 'messageDetail',
                component: () => import('../views/account/MessageDetail.vue'),
                meta: {
                    pageType: Page.Every,
                    title: '反馈记录'
                }
            },
            {
                path: 'introduction',
                name: 'introduction',
                component: () => import('../views/account/Introduction.vue'),
                meta: {
                    pageType: Page.Every
                }
            },
            {
                path: 'question',
                name: 'question',
                component: () => import('../views/account/Question.vue'),
                meta: {
                    pageType: Page.Every
                }
            },
            {
                path: 'connect',
                name: 'connect',
                component: () => import('../views/account/Connect.vue')
            },
            {
                path: 'tradingPassword',
                name: 'tradingPassword',
                component: () => import('../views/account/TradingPassword.vue')
            },
            {
                path: 'productDetail/:id',
                name: 'productDetail',
                component: () => import('../views/product/Detail.vue'),
                meta: {
                    pageType: Page.Every,
                    tabColor: '#181818'
                }
            },
            {
                path: 'newsDetail',
                name: 'newsDetail',
                component: () => import('../views/product/NewsDetail.vue'),
                meta: {
                    pageType: Page.Every
                }
            },
            {
                path: 'newsList',
                name: 'newsList',
                component: () => import('../views/product/NewsList.vue'),
                meta: {
                    pageType: Page.Every,
                    title: '新闻公告',
                    tabColor: '#222426'
                }
            },
            {
                path: 'productTasks',
                name: 'productTasks',
                component: () => import('../views/product/Tasks.vue'),
                meta: {
                    pageType: Page.Every,
                    menuPage: true,

                    tabColor: '#181818'
                }
            },
            {
                path: 'productDetail2',
                name: 'productDetail2',
                component: () => import('../views/product/DetailMode.vue'),
                meta: {
                    pageType: Page.Every
                }
            },
            {
                path: 'productList',
                name: 'productList',
                component: () => import('../views/product/List.vue'),
                meta: {
                    pageType: Page.Every
                }
            },
            {
                path: 'hallList',
                name: 'hallList',
                component: () => import('../views/hall/List.vue'),
                meta: {
                    pageType: Page.Every,

                    tabColor: '#222426'
                }
            },
            {
                path: 'hopeMarket',
                name: 'hopeMarket',
                component: () => import('../views/product/HopeMarket.vue'),
                meta: {
                    pageType: Page.Every,
                    tabColor: '#222426'
                }
            },
            {
                path: 'auction',
                name: 'auction',
                component: () => import('../views/auction/Home.vue'),
                meta: {
                    pageType: Page.Every,
                    title: '拍卖'
                }
            },
            {
                path: 'auctionDetail',
                name: 'auctionDetail',
                component: () => import('../views/auction/Detail.vue'),
                meta: {
                    pageType: Page.Every,
                    title: '拍卖'
                }
            },
            {
                path: 'auctionList',
                name: 'auctionList',
                component: () => import('../views/auction/List.vue'),
                meta: {
                    pageType: Page.Every,
                    title: '拍卖'
                }
            },
            {
                path: 'auctionSearch',
                name: 'auctionSearch',
                component: () => import('../views/auction/Search.vue'),
                meta: {
                    pageType: Page.Every,
                    title: '拍卖'
                }
            },
            {
                path: 'auctionOffer',
                name: 'auctionOffer',
                component: () => import('../views/auction/Offer.vue'),
                meta: {
                    pageType: Page.Every,
                    title: '拍卖'
                }
            },
            {
                path: 'auctionSubmit',
                name: 'auctionSubmit',
                component: () => import('../views/auction/Submit.vue'),
                meta: {
                    pageType: Page.Every,
                    title: '拍卖'
                }
            },
            {
                path: 'productSearch',
                name: 'productSearch',
                component: () => import('../views/product/Search.vue'),
                meta: {
                    pageType: Page.Every
                }
            },
            {
                path: 'productSearchCorpse',
                name: 'productSearchCorpse',
                component: () => import('../views/product/SearchCorpse.vue'),
                meta: {
                    pageType: Page.Every,
                    tabColor: '#222426'
                }
            },
            {
                path: 'creatorDetail',
                name: 'creatorDetail',
                component: () => import('../views/creator/Detail.vue'),
                meta: {
                    pageType: Page.Every,
                    tabColor: '#222426'
                }
            },
            {
                path: 'creatorHome',
                name: 'creatorHome',
                component: () => import('../views/creator/Home.vue'),
                meta: {
                    pageType: Page.Every,
                    tabColor: '#222426'
                }
            },
            {
                path: 'creatorList',
                name: 'creatorList',
                component: () => import('../views/creator/List.vue'),
                meta: {
                    pageType: Page.Every
                }
            },
            {
                path: 'creatorSearch',
                name: 'creatorSearch',
                component: () => import('../views/creator/Search.vue'),
                meta: {
                    pageType: Page.Every
                }
            },
            {
                path: 'assetDetail',
                name: 'assetDetail',
                component: () => import('../views/asset/Detail.vue'),
                meta: {
                    pageType: Page.Every,
                    tabColor: '#181818'
                }
            },
            {
                path: 'Consignment',
                name: 'Consignment',
                component: () => import('../views/asset/Consignment.vue'),
                meta: {
                    pageType: Page.Every
                }
            },
            {
                path: 'auctionPublish',
                name: 'auctionPublish',
                component: () => import('../views/asset/AuctionPublish.vue'),
                meta: {
                    pageType: Page.Every,
                    title: '宇宙'
                }
            },
            {
                path: 'consignmentAgreement',
                name: 'consignmentAgreement',
                component: () => import('../views/asset/Agreement.vue'),
                meta: {
                    pageType: Page.Every
                }
            },
            {
                path: 'assetSearch',
                name: 'assetSearch',
                component: () => import('../views/asset/Search.vue'),
                meta: {}
            },
            {
                path: 'giveSearch',
                name: 'giveSearch',
                component: () => import('../views/asset/GiveSearch.vue'),
                meta: {}
            },
            {
                path: 'submit',
                name: 'submit',
                component: () => import('../views/Submit.vue'),
                meta: {}
            },
            {
                path: 'couponList',
                name: 'couponList',
                component: () => import('../views/order/CouponList.vue'),
                meta: {}
            },
            {
                path: 'givesubmit',
                name: 'givesubmit',
                component: () => import('../views/Givesubmit.vue'),
                meta: {}
            },
            {
                path: 'orders',
                name: 'orders',
                component: () => import('../views/order/Orders.vue'),
                meta: {
                    title: '订单'
                }
            },
            {
                path: 'orderDetail',
                name: 'orderDetail',
                component: () => import('../views/order/Detail.vue'),
                meta: {}
            },
            {
                path: 'activityOrders',
                name: 'activityOrders',
                component: () => import('../views/order/ActivityOrders.vue'),
                meta: {
                    title: '艺术品订单'
                }
            },
            {
                path: 'activityOrderDetail',
                name: 'activityOrderDetail',
                component: () => import('../views/order/ActivityDetail.vue'),
                meta: {}
            },
            {
                path: 'auctionOrders',
                name: 'auctionOrders',
                component: () => import('../views/order/AuctionOrders.vue'),
                meta: {
                    title: '拍卖订单'
                }
            },
            {
                path: 'auctionOrderDetail',
                name: 'auctionOrderDetail',
                component: () => import('../views/order/AuctionDetail.vue'),
                meta: {
                    title: '宇宙'
                }
            },
            {
                path: 'activityList',
                name: 'activityList',
                component: () => import('../views/activity/List1.vue'),
                meta: {
                    pageType: Page.Every,
                    title: '铸造中心',
                    tabColor: '#181818'
                }
            },
            {
                path: 'activityDetail',
                name: 'activityDetail',
                component: () => import('../views/activity/Detail1.vue'),
                meta: {
                    pageType: Page.Every,

                    tabColor: '#181818'
                }
            },
            {
                path: 'activitySubmit',
                name: 'activitySubmit',
                component: () => import('../views/activity/Submit.vue'),
                meta: {}
            },
            {
                path: 'chooseProduct',
                name: 'chooseProduct',
                component: () => import('../views/activity/ChooseProduct.vue'),
                meta: {
                    needBack: true
                }
            },
            {
                path: 'mineFollows',
                name: 'mineFollows',
                component: () => import('../views/user/Follows.vue'),
                meta: {}
            },
            {
                path: 'mineFollowers',
                name: 'mineFollowers',
                component: () => import('../views/user/Followers.vue'),
                meta: {}
            },
            {
                path: 'mineLikes',
                name: 'mineLikes',
                component: () => import('../views/user/Likes.vue'),
                meta: {}
            },
            {
                path: 'bankPay',
                name: 'bankPay',
                component: () => import('../views/pay/BankPay.vue'),
                meta: {
                    title: '首信易快捷支付'
                }
            },
            {
                path: 'mineBanks',
                name: 'mineBanks',
                component: () => import('../views/user/Banks.vue'),
                meta: {}
            },
            {
                path: 'mineBanksAdd',
                name: 'mineBanksAdd',
                component: () => import('../views/user/BanksAdd.vue'),
                meta: {}
            },
            {
                path: 'mineAddress',
                name: 'mineAddress',
                component: () => import('../views/user/Address.vue'),
                meta: {}
            },
            {
                path: 'mineAddressEdit',
                name: 'mineAddressEdit',
                component: () => import('../views/user/AddressEdit.vue'),
                meta: {}
            },
            {
                path: 'mineWallet',
                name: 'mineWallet',
                component: () => import('../views/user/Wallet.vue'),
                meta: {}
            },
            {
                path: 'minePoint',
                name: 'minePoint',
                component: () => import('../views/user/Point.vue'),
                meta: {}
            },
            {
                path: 'mineExchange',
                name: 'mineExchange',
                component: () => import('../views/user/Exchange.vue'),
                meta: {}
            },
            {
                path: 'mineLevel',
                name: 'mineLevel',
                component: () => import('../views/user/Level.vue'),
                meta: {}
            },
            {
                path: 'rank',
                name: 'rank',
                component: () => import('../views/user/Rank.vue'),
                meta: {}
            },
            {
                path: 'activityRank',
                name: 'activityRank',
                component: () => import('../views/user/ActivityRank.vue'),
                meta: {}
            },
            {
                path: 'balanceRecord',
                name: 'balanceRecord',
                component: () => import('../views/user/BalanceRecord.vue'),
                meta: {}
            },
            {
                path: 'walletAgreement',
                name: 'walletAgreement',
                component: () => import('../views/user/WalletAgreement.vue'),
                meta: {}
            },
            {
                path: 'faceAuth',
                name: 'faceAuth',
                component: () => import('../views/account/FaceAuth.vue'),
                meta: {}
            }
        ]
    }
];

const router = createRouter({
    history:
        process.env.VUE_APP_CORDOVA === 'true'
            ? createWebHashHistory()
            : createWebHistory(process.env.VUE_APP_PUBLIC_PATH),
    routes,
    scrollBehavior(to, from, savedPosition) {
        return { x: 0, y: 0 };
    }
});

router.beforeEach((to, from, next) => {
    if (/^\/http/.test(to.path)) {
        let url = to.path.replace('/', '');
        let params = [];
        if (to.query) {
            for (let key in to.query) {
                // eslint-disable-next-line no-prototype-builtins
                if (to.query.hasOwnProperty(key)) {
                    params.push(`${key}=${to.query[key]}`);
                }
            }
        }
        if (params.length > 0) {
            url += `?${params.join('&')}`;
        }
        window.open(url);
        return;
    }
    console.log(to);

    if (to.query.invitor) {
        sessionStorage.setItem('invitor', to.query.invitor);
    }
    if (to.meta.pageType != Page.Every && to.path !== '/collectionDetail' && to.path !== '/castingDetail') {
        if (!store.state.userInfo && to.meta.pageType !== Page.Login) {
            store
                .dispatch('getUserInfo')
                .then(() => {
                    next(backNext(to));
                })
                .catch(() => {
                    Dialog.confirm({
                        title: '提示',
                        message: '用户未登录,是否立即登录'
                    })
                        .then(() => {
                            let url = '/login';
                            if (to.meta.needBack) {
                                store.commit('setLoginBackUrl', to.fullPath);
                            }
                            next({
                                name: 'userLogin'
                            });
                        })
                        .catch(() => {
                            next(false);
                        });
                });
        } else {
            next(backNext(to));
        }
    } else {
        next(backNext(to));
        !!store.state.userInfo || store.dispatch('getUserInfo');
    }
});
router.afterEach((to, from) => {
    jsapiSign();
});

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