import { eRole } from '@/types/roleType'
import { chatAuthCheck } from '@/utils/authCheck'
import { getUserMsg } from '@/utils/userMsg'
import { Toast } from 'vant'
import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    redirect: { name: 'Home' },
    component: () =>
      import(/* webpackChunkName: "withMenu" */ '@/views/withMenu/index.vue'),
    children: [
      {
        path: '/home',
        name: 'Home',
        component: () =>
          import(
            /* webpackChunkName: "Home" */ '@/views/withMenu/Home/index.vue'
          ),
      },
      {
        path: '/mine',
        name: 'Mine',
        component: () =>
          import(
            /* webpackChunkName: "mine" */ '@/views/withMenu/Mine/index.vue'
          ),
      },
      {
        path: '/loan',
        name: 'Loan',
        component: () =>
          import(
            /* webpackChunkName: "mine" */ '@/views/withMenu/Loan/index.vue'
          ),
      },
      {
        path: '/chatList',
        name: 'ChatList',
        meta: {
          needAuth: true,
        },
        component: () =>
          import(
            /* webpackChunkName: "chatList" */ '@/views/withMenu/ChatList/index.vue'
          ),
      },
    ],
  },
  {
    path: '/loanList/:mode',
    name: 'LoanList',
    component: () =>
      import(/* webpackChunkName: "LoanList" */ '@/views/LoanList/index.vue'),
  },
  {
    path: '/loanDetail/:uuid',
    name: 'LoanDetail',
    component: () =>
      import(
        /* webpackChunkName: "LoanDetail" */ '@/views/LoanDetail/index.vue'
      ),
  },
  {
    path: '/activityList',
    name: 'ActivityList',
    component: () =>
      import(
        /* webpackChunkName: "ActivityList" */ '@/views/ActivityList/index.vue'
      ),
  },
  {
    path: '/activityDetail/:uuid',
    name: 'ActivityDetail',
    component: () =>
      import(
        /* webpackChunkName: "ActivityDetail" */ '@/views/ActivityDetail/index.vue'
      ),
  },
  {
    path: '/policySupport',
    name: 'PolicySupport',
    component: () =>
      import(
        /* webpackChunkName: "PolicySupport" */ '@/views/PolicySupport/index.vue'
      ),
  },
  {
    path: '/news',
    name: 'News',
    component: () =>
      import(/* webpackChunkName: "News" */ '@/views/News/index.vue'),
  },
  {
    path: '/newsDetail',
    name: 'NewsDetail',
    component: () =>
      import(
        /* webpackChunkName: "NewsDetail" */ '@/views/NewsDetail/index.vue'
      ),
  },
  {
    path: '/search',
    name: 'Search',
    component: () =>
      import(/* webpackChunkName: "Search" */ '@/views/Search/index.vue'),
  },
  {
    path: '/auth',
    component: () =>
      import(/* webpackChunkName: "auth" */ '@/views/Auth/index.vue'),
    redirect: {
      name: 'Login',
    },
    children: [
      {
        path: '/auth/Login',
        name: 'Login',
        component: () =>
          import(
            /* webpackChunkName: "Login" */ '@/views/Auth/Login/index.vue'
          ),
      },
      {
        path: '/auth/UserAgreement',
        name: 'UserAgreement',
        component: () =>
          import(
            /* webpackChunkName: "UserAgreement" */ '@/views/Auth/UserAgreement/index.vue'
          ),
      },
      {
        path: '/auth/PwdSetting',
        name: 'PwdSetting',
        component: () =>
          import(
            /* webpackChunkName: "PwdSetting" */ '@/views/Auth/PwdSetting/index.vue'
          ),
      },
      {
        path: '/auth/PwdFind',
        name: 'PwdFind',
        component: () =>
          import(
            /* webpackChunkName: "PwdFind" */ '@/views/Auth/PwdFind/index.vue'
          ),
      },
      {
        path: '/auth/PwdModify',
        name: 'PwdModify',
        component: () =>
          import(
            /* webpackChunkName: "PwdModify" */ '@/views/Auth/PwdModify/index.vue'
          ),
      },
    ],
  },
  {
    path: '/setting',
    name: 'Setting',
    component: () =>
      import(
        /* webpackChunkName: "Setting" */ '@/views/withMenu/Mine/setting.vue'
      ),
    meta: {
      needAuth: true,
    },
  },
  {
    path: '/aboutUs',
    name: 'AboutUs',
    component: () =>
      import(
        /* webpackChunkName: "AboutUs" */ '@/views/withMenu/Mine/aboutUs.vue'
      ),
  },
  {
    path: '/msgManage',
    name: 'MsgManage',
    component: () =>
      import(
        /* webpackChunkName: "MsgManage" */ '@/views/withMenu/Mine/msg.vue'
      ),
  },
  {
    path: '/msgDetail',
    name: 'MsgDetail',
    component: () =>
      import(
        /* webpackChunkName: "MsgDetail" */ '@/views/withMenu/Mine/msgDetail.vue'
      ),
  },
  {
    path: '/branch',
    name: 'Branch',
    component: () =>
      import(
        /* webpackChunkName: "Branch" */ '@/views/withMenu/Mine/branch.vue'
      ),
  },
  {
    path: '/chat',
    name: 'Chat',
    component: () =>
      import(/* webpackChunkName: "Chat" */ '@/views/Chat/Chat.vue'),
  },
  {
    path: '/chatDetailMsg',
    name: 'ChatDetailMsg',
    component: () =>
      import(
        /* webpackChunkName: "ChatDetailMsg" */ '@/views/Chat/DetailMsg.vue'
      ),
  },
  {
    path: '/crop',
    name: 'Crop',
    component: () =>
      import(/* webpackChunkName: "Crop" */ '@/views/Crop/index.vue'),
  },
  {
    path: '/:pathMatch(.*)*',
    redirect: {
      name: 'Home',
    },
  },
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes,
})

router.beforeEach((to, from, next) => {
  const userMsg = getUserMsg()
  if (to.meta.needAuth) {
    if (!!userMsg) {
      if (userMsg.role === eRole.user) next()
      else if (userMsg.role === eRole.staff) {
        if (to.name === 'ChatList') {
          chatAuthCheck(
            () => {
              next()
            },
            (mark: boolean | undefined) => {
              /* 如果有mark为false，则表示检查聊天权限的逻辑走到了调用接口，但是返回auth为false */
              mark === false &&
                Toast('由于您现在是不在岗状态所以无法进入聊天！')
            },
          )
        } else {
          next()
        }
      }
    } else {
      next({ name: 'Login' })
    }
  } else {
    next()
  }
})

export default router
