import {createRouter, createWebHashHistory, RouteRecordRaw} from 'vue-router';
import {useUserStore} from "@/store/modules/user";
import {domainStoreWidthOut} from "@/store/modules/domain";
import {getDomainList} from "@/api/login";

export const constantRouter: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'Root',
    redirect: '/home',
    meta: {
      title: 'Root',
    },
  },
  {
    path: '/',
    name: 'index',
    component: () => import('@/views/index/index.vue'),
    meta: {
      title: '首页',
    },
    children: [
      {
        path: '/home',
        name: 'Home',
        component: () => import('@/views/index/home/index.vue'),
        meta: {
          title: '首页',
          showBar: false
        },
      },
      {
        path: '/serveList',
        name: 'serveList',
        component: () => import('@/views/index/serveList/index.vue'),
        meta: {
          isPermissions: true,
          title: '钱包',
          showBar: false
        },
      },
      {
        path: '/message',
        name: 'message',
        component: () => import('@/views/index/message/index.vue'),
        meta: {
          isPermissions: true,
          title: '聊天',
          showBar: false
        },
      },
      {
        path: '/my',
        name: 'my',
        component: () => import('@/views/index/my/index.vue'),
        meta: {
          isPermissions: true,
          title: '我的',
          showBar: false
        },
      }
    ]
  },
  {
    path: '/userInfo',
    name: 'userInfo',
    component: () => import('@/views/my/userInfo/index.vue'),
    meta: {
      title: '我的资料',
      isPermissions: true,
      showBar: true
    },
  },
  {
    path: '/userInfo1',
    name: 'userInfo1',
    component: () => import('@/views/my/userInfo1/index.vue'),
    meta: {
      title: '基本信息',
      isPermissions: true,
      showBar: true
    },
  },
  {
    path: '/signature',
    name: 'signature',
    component: () => import('@/views/my/signature/index.vue'),
    meta: {
      title: '签名',
      isPermissions: true,
      showBar: true
    },
  },
  {
    path: '/contract',
    name: 'contract',
    component: () => import('@/views/my/contract/index.vue'),
    meta: {
      title: '合同',
      isPermissions: true,
      showBar: true
    },
  },
  {
    path: '/userInfo2',
    name: 'userInfo2',
    component: () => import('@/views/my/userInfo2/index.vue'),
    meta: {
      title: '提交资料',
      isPermissions: true,
      showBar: true
    },
  },
  {
    path: '/userInfo3',
    name: 'userInfo3',
    component: () => import('@/views/my/userInfo3/index.vue'),
    meta: {
      title: '收款银行卡',
      isPermissions: true,
      showBar: true
    },
  },
  {
    path: '/loansInfo',
    name: 'loansInfo',
    component: () => import('@/views/loans/info/index.vue'),
    meta: {
      title: '借款详情',
      isPermissions: true,
      showBar: true
    },
  },
  {
    path: '/loansInfo1',
    name: 'loansInfo1',
    component: () => import('@/views/loans/info1/index.vue'),
    meta: {
      title: '提现',
      isPermissions: true,
      showBar: true
    },
  },
  {
    path: '/myLoan',
    name: 'myLoan',
    component: () => import('@/views/my/myLoan/index.vue'),
    meta: {
      title: '我的借款',
      isPermissions: true,
      showBar: true
    },
  },
  {
    path: '/myRepayment',
    name: 'myRepayment',
    component: () => import('@/views/my/myRepayment/index.vue'),
    meta: {
      title: '我的还款',
      isPermissions: true,
      showBar: true
    },
  },

  {
    path: '/borrowInfo',
    name: 'borrowInfo',
    component: () => import('@/views/borrowInfo/index.vue'),
    meta: {
      title: '贷款详情',
      isPermissions: true,
      showBar: true
    },
  },
  {
    path: '/uploadPassword',
    name: 'uploadPassword',
    component: () => import('@/views/uploadPassword/index.vue'),
    meta: {
      title: '修改密码',
      isPermissions: true,
      showBar: true
    },
  },



  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login/index.vue'),
    meta: {
      title: '登录',
      showBar: false
    },
  },
  {
    path: '/register',
    name: 'register',
    component: () => import('@/views/register/index.vue'),
    meta: {
      title: '注册',
      showBar: true
    },
  },
  {
    path: '/forget',
    name: 'forget',
    component: () => import('@/views/forget/index.vue'),
    meta: {
      title: '忘记密码',
      showBar: true
    },
  },
  {
    path: '/agreement',
    name: 'agreement',
    component: () => import('@/views/agreement/agreement.vue'),
    meta: {
      title: '协议',
      showBar: true
    },
  }
];

//需要验证权限

//普通路由 无需验证权限

const router = createRouter({
  history: createWebHashHistory(''),
  routes: constantRouter,
  strict: true,
  scrollBehavior: () => ({ left: 0, top: 0 }),
});


// @ts-ignore
router.beforeEach(async (to, from, next) => {
  const dsw = domainStoreWidthOut()
  if (!dsw.getDomain || dsw.getDomain.length == 0) {
    const response = await getDomainList()
    if (response && response.data) {
      const domain = response.data.split(',')
      dsw.setDomain(domain)
    }
  }
  if (to.meta.title) { // 判断是否有标题
    document.title = to.meta.title as string;
  }
  if (to.meta.isPermissions) {
    const userStore = useUserStore()
    if (!userStore.getToken) {
      next({
        path: '/login'
      })
      return
    }
  }
  next()
})

export default router;
