import pinia, { useAccountStore } from '@/stores';
import { createRouter, createWebHistory } from 'vue-router';

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    { path: '/login', component: () => import('@/pages/login.vue') },
    {
      path: '/',
      component: () => import(`@/pages/index.vue`),
      children: [
        { path: 'patient/history', component: () => import(`@/pages/index/patient/history.vue`) },
        {
          path: 'patient/room',
          components: {
            default: () => import(`@/pages/index/patient/room@default.vue`),
            aside: () => import(`@/pages/index/patient/room@aside.vue`),
          },
        },
        {
          path: 'system',
          children: [
            { path: 'user', component: () => import(`@/pages/index/system/user.vue`) },
            { path: 'role', component: () => import(`@/pages/index/system/role.vue`) },
            { path: 'tag', component: () => import(`@/pages/index/system/tag.vue`) },
            { path: 'organization', component: () => import(`@/pages/index/system/organization.vue`) },
            { path: 'institution', component: () => import(`@/pages/index/system/institution.vue`) },
          ],
        },
        { path: '', redirect: '/patient/history' },
        {
          path: 'follow',
          children: [
            { path: 'plan', component: () => import(`@/pages/index/follow/plan.vue`) },
            { path: 'task', component: () => import(`@/pages/index/follow/task.vue`) },
            { path: 'assessment', component: () => import(`@/pages/index/follow/assessment.vue`) },
          ],
        },
        {
          path: 'care',
          children: [
            { path: 'supplier', component: () => import(`@/pages/index/care/supplier.vue`) },
            { path: 'serviceItems', component: () => import(`@/pages/index/care/serviceItems.vue`) },
            { path: 'systemService', component: () => import(`@/pages/index/care/systemService.vue`) },
            { path: 'institutionService', component: () => import(`@/pages/index/care/institutionService.vue`) },
            { path: 'issueService', component: () => import(`@/pages/index/care/issueService.vue`) },
            { path: 'conditioningRecord', component: () => import(`@/pages/index/care/conditioningRecord.vue`) },
            { path: 'configured', component: () => import(`@/pages/index/care/configured.vue`) },
          ],
        },
        {
          path: 'equipment',
          children: [
            { path: 'registe', component: () => import(`@/pages/index/equipment/registe.vue`) },
            { path: 'configured', component: () => import(`@/pages/index/equipment/configured.vue`) },
            { path: 'reportManagement', component: () => import(`@/pages/index/equipment/reportManagement.vue`) },
          ],
        },
        {
          path: 'tcmRecuperation',
          children: [
            { path: 'preserve', component: () => import(`@/pages/index/tcmRecuperation/preserve.vue`) },
            { path: 'system', component: () => import(`@/pages/index/tcmRecuperation/system.vue`) },
            { path: 'institution', component: () => import(`@/pages/index/tcmRecuperation/institution.vue`) },

          ],
        },
        {
          path: 'online', children: [
            { path: 'onlineConsult', component: () => import(`@/pages/index/online/onlineConsult.vue`) },
          ],
        },
        {
          path: 'healthy',
          children: [{ path: 'education', component: () => import(`@/pages/index/healthy/education.vue`) }],
        },
        {
          path: 'notify',
          children: [{ path: 'manage', component: () => import(`@/pages/index/notify/manage.vue`) }],
        },
        {
          path: 'satisfaction',
          children: [{ path: 'survey', component: () => import(`@/pages/index/satisfaction/survey.vue`) }],
        },
        {
          path: 'order',
          children: [
            { path: 'management', component: () => import(`@/pages/index/order/management.vue`) },
            { path: 'dispatchOrder', component: () => import(`@/pages/index/order/dispatchOrder.vue`) },
            { path: 'shipment', component: () => import(`@/pages/index/order/shipment.vue`) },
            { path: 'revenueSharing', component: () => import(`@/pages/index/order/revenueSharing.vue`) },
          ],
        },
      ],
      beforeEnter(to, from, next) {
        if (useAccountStore(pinia).token) {
          next();
        } else {
          next({ path: '/login' });
        }
      },
    },
  ],
});

export default router;
