import { createRouter, createWebHistory } from '@ionic/vue-router'
import TabsPage from '../views/TabsPage.vue'
import { Page } from './Page'
import { useUserStore } from '../stores/user'
import { showConfirmDialog } from 'vant'
console.log(import.meta.env.BASE_URL)
const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: '/',
            redirect: '/home',
            meta: {
                allowGuest: true
            }
        },
        {
            path: '/',
            component: TabsPage,
            meta: {
                allowGuest: true
            },
            children: [
                {
                    path: '',
                    redirect: '/home',
                    meta: {
                        allowGuest: true
                    }
                },
                {
                    path: 'home',
                    name: 'home',
                    component: () => import('@/views/HomePage.vue'),
                    meta: {
                        allowGuest: true
                    }
                },
                {
                    path: 'mine',
                    name: 'mine',
                    component: () => import('@/views/MinePage.vue'),
                    meta: {
                        allowGuest: true
                    }
                }
            ]
        },
        {
            path: '/login',
            name: 'login',
            component: () => import('@/views/LoginPage.vue'),
            meta: {
                allowGuest: true,
                guestOnly: true
            }
        },
        {
            path: '/loginPhone',
            name: 'loginPhone',
            component: () => import('@/views/LoginPhonePage.vue'),
            meta: {
                allowGuest: true,
                guestOnly: true
            }
        },
        {
            path: '/register',
            name: 'register',
            component: () => import('@/views/RegisterPage.vue'),
            meta: {
                allowGuest: true,
                guestOnly: true
            }
        },
        {
            path: '/order',
            name: 'order',
            component: () => import('@/views/OrderPage.vue')
        },
        {
            path: '/orderDetail',
            name: 'orderDetail',
            component: () => import('@/views/OrderDetailPage.vue')
        },
        {
            path: '/wallet',
            name: 'wallet',
            component: () => import('@/views/WalletPage.vue')
        },
        {
            path: '/balanceRecord',
            name: 'balanceRecord',
            component: () => import('@/views/BalanceRecordPage.vue')
        },
        {
            path: '/productList',
            name: 'productList',
            component: () => import('@/views/ProductListPage.vue'),
            meta: {
                allowGuest: true
            }
        },
        {
            path: '/productDetail',
            name: 'productDetail',
            component: () => import('@/views/ProductDetailPage.vue'),
            meta: {
                allowGuest: true
            }
        },
        {
            path: '/productSearch',
            name: 'productSearch',
            component: () => import('@/views/ProductSearchPage.vue'),
            meta: {
                allowGuest: true
            }
        },
        {
            path: '/setting',
            name: 'setting',
            component: () => import('@/views/SettingPage.vue')
        },
        {
            path: '/changeText',
            name: 'changeText',
            component: () => import('@/views/ChangeTextPage.vue')
        },
        {
            path: '/distribution',
            name: 'distribution',
            component: () => import('@/views/DistributionPage.vue')
        },
        {
            path: '/chat',
            name: 'chat',
            component: () => import('@/views/ChatPage.vue')
        },
        {
            path: '/commissionList',
            name: 'commissionList',
            component: () => import('@/views/CommissionListPage.vue')
        },
        {
            path: '/myTeam',
            name: 'myTeam',
            component: () => import('@/views/MyTeamPage.vue')
        }
    ]
})
router.beforeEach(async (to, from, next) => {
    const { user, get: getUser } = useUserStore()
    if (!to.meta.allowGuest && !user) {
        try {
            await getUser()
            next()
        } catch (error) {
            const confirm = await showConfirmDialog({
                title: '提示',
                message: '用户未登录,是否立即登录'
            }).catch(() => {
                next(false)
            })
            if (confirm) {
                next('/login')
            }
        }
    } else {
        if (!user) {
            getUser().catch(() => {})
        }
        next()
    }
})

export default router
