import { createWebHashHistory, RouteRecordRaw } from 'vue-router'
import { useLoginStore } from '@/stores'
import service from '@/services'
import Page from '@mobile/components/layouts/page/index.vue'
import animateRouter from './animateRouter'

const { getToken } = useLoginStore()

const routes: Array<RouteRecordRaw> = [
  {
    path: '/:pathMatch(.*)*',
    name: 'Error',
    component: () => import('../views/error/404.vue'),
    meta: {
      ignoreAuth: true,
    },
  },
  {
    path: '/boot',
    name: 'Boot',
    component: () => import('../views/boot/index.vue'),
    meta: {
      ignoreAuth: true,
    },
  },
  {
    path: '/',
    component: Page,
    children: [
      {
        path: '',
        name: 'Home',
        component: () => import('../views/home/index.vue'),
      }
    ]
  },
  {
    path: '/user',
    component: Page,
    children: [
      {
        path: 'login',
        name: 'UserLogin',
        component: () => import('../views/user/login/index.vue'),
        meta: {
          ignoreAuth: true,
        },
      },
      {
        path: 'register',
        name: 'UserRegister',
        component: () => import('../views/user/register/index.vue'),
        meta: {
          ignoreAuth: true,
        },
      },
      {
        path: 'forget',
        name: 'UserForget',
        component: () => import('../views/user/forget/index.vue'),
        meta: {
          ignoreAuth: true,
        },
      },
      {
        path: 'logoff',
        name: 'UserLogoff',
        component: () => import('../views/user/logoff/index.vue'),
      },
      {
        path: 'password',
        name: 'UserPassword',
        component: () => import('../views/user/password/index.vue'),
      },
    ]
  },
  {
    path: '/account',
    component: Page,
    children: [
      {
        path: 'certification',
        name: 'account-certification',
        component: () => import('../views/account/certification/index.vue'),
      }
    ]
  },
  {
    path: '/news',
    component: Page,
    children: [
      {
        path: 'details',
        name: 'news-details',
        component: () => import('../views/news/details/index.vue'),
      },
      {
        path: 'list',
        name: 'news-list',
        component: () => import('../views/news/list/index.vue'),
      }
    ]
  },
  {
    path: '/market',
    component: Page,
    children: [
      {
        path: '',
        name: 'Market',
        component: () => import('../views/market/list/index.vue'),
      }
    ]
  },
  {
    path: '/product',
    component: Page,
    children: [
      {
        path: '',
        name: 'product',
        component: () => import('../views/product/list/index.vue'),
      },
      {
        path: 'detail',
        name: 'product-detail',
        component: () => import('../views/product/detail/index.vue'),
      }
    ]
  },
  {
    path: '/purchase',
    component: Page,
    children: [
      {
        path: 'detail',
        name: 'PurchaseDetail',
        component: () => import('../views/purchase/detail/index.vue'),
      }
    ]
  },
  {
    path: '/supply-demand',
    component: Page,
    children: [
      {
        path: 'detail',
        name: 'SupplyDemandDetail',
        component: () => import('../views/supply-demand/detail/index.vue'),
      }
    ]
  },
  {
    path: '/contract',
    component: Page,
    children: [
      {
        path: '',
        name: 'contract',
        component: () => import('../views/contract/list/index.vue'),
      },
      {
        path: 'details',
        name: 'contract-details',
        component: () => import('../views/contract/details/index.vue'),
      }
    ]
  },
  {
    path: '/bank',
    component: Page,
    children: [
      {
        path: 'bank',
        name: 'bank-wallet',
        component: () => import('../views/bank/wallet/index.vue'),
      },
      {
        path: 'sign',
        name: 'bank-sign',
        component: () => import('../views/bank/sign/index.vue'),
      },
      {
        path: 'edit',
        name: 'add-banksign',
        component: () => import('../views/bank/sign/components/edit/index.vue'),
      },
      {
        path: 'statement',
        name: 'bank-statement',
        component: () => import('../views/bank/statement/index.vue'),
      },
      {
        path: 'hisstatement',
        name: 'bank-hisstatement',
        component: () => import('../views/bank/hisstatement/index.vue'),
      },
    ]
  },
  {
    path: '/order',
    component: Page,
    children: [
      {
        path: '',
        name: 'my-order',
        component: () => import('../views/mine/order/list/index.vue'),
      },
      {
        path: 'purchasetradedetail',
        name: 'purchase-trade-detail',
        component: () => import('../views/mine/order/detail/purchasetrade/index.vue'),
      },
      {
        path: 'wrtradedetail',
        name: 'wrtrade-detail',
        component: () => import('../views/mine/order/detail/wrtrade/index.vue'),
      },
      {
        path: 'wrorderdetail',
        name: 'wrorder-detail',
        component: () => import('../views/mine/order/detail/wrorder/index.vue'),
      },
      {
        path: 'transferdetail',
        name: 'transfer-detail',
        component: () => import('../views/mine/order/detail/transfer/index.vue'),
      }
      ,
      {
        path: 'hispurchasetradedetail',
        name: 'his-purchase-trade-detail',
        component: () => import('../views/mine/order/his/purchasetrade/index.vue'),
      },
      {
        path: 'hiswrtradedetail',
        name: 'his-wrtrade-detail',
        component: () => import('../views/mine/order/his/wrtrade/index.vue'),
      },
      {
        path: 'hiswrorderdetail',
        name: 'his-wrorder-detail',
        component: () => import('../views/mine/order/his/wrorder/index.vue'),
      },
      {
        path: 'histransferdetail',
        name: 'his-transfer-detail',
        component: () => import('../views/mine/order/his/transfer/index.vue'),
      }
    ]
  },
  {
    path: '/wareorder',
    component: Page,
    children: [
      {
        path: '',
        name: 'my-wareorder',
        component: () => import('../views/mine/wareorder/list/index.vue'),
      },
      {
        path: 'wroutinapplydetail',
        name: 'wroutinapply-detail',
        component: () => import('../views/mine/wareorder/wroutinapplydetail/index.vue'),
      },
    ]
  },
  {
    path: '/rules',
    component: Page,
    children: [
      {
        path: 'ptgz',
        name: 'rules-ptgz',
        component: () => import('../views/rules/ptgz/index.vue'),
      },
      {
        path: 'myrz',
        name: 'rules-myrz',
        component: () => import('../views/rules/myrz/index.vue'),
      },
      {
        path: 'ccwl',
        name: 'rules-ccwl',
        component: () => import('../views/rules/ccwl/index.vue'),
      },
      {
        path: 'zcxy',
        name: 'rules-zcxy',
        component: () => import('../views/rules/zcxy/index.vue'),
        meta: {
          ignoreAuth: true,
        },
      },
      {
        path: 'yhkhfxgzs',
        name: 'rules-yhkhfxgzs',
        component: () => import('../views/rules/fxgzs/index.vue'),
        meta: {
          ignoreAuth: true,
        },
      },
      {
        path: 'yszc',
        name: 'rules-yszc',
        component: () => import('../views/rules/yszc/index.vue'),
        meta: {
          ignoreAuth: true,
        },
      },
      {
        path: 'gywm',
        name: 'rules-gywm',
        component: () => import('../views/rules/gywm/index.vue'),
      },
      {
        path: 'fpsm',
        name: 'rules-fpsm',
        component: () => import('../views/rules/fpsm/index.vue'),
      },
      {
        path: 'fwrx',
        name: 'rules-fwrx',
        component: () => import('../views/rules/fwrx/index.vue'),
      },
    ]
  },
  {
    path: '/credit',
    component: Page,
    children: [
      {
        path: 'signin',
        name: 'credit-signin',
        component: () => import('../views/credit/signin/index.vue'),
      },
      {
        path: 'statement',
        name: 'credit-statement',
        component: () => import('../views/credit/statement/index.vue'),
      },
      {
        path: 'lottery',
        name: 'credit-lottery',
        component: () => import('../views/credit/lottery/index.vue'),
      },
    ]
  },
  {
    path: '/mine',
    component: Page,
    children: [
      {
        path: 'generalize',
        name: 'mine-generalize',
        component: () => import('../views/mine/generalize/index.vue'),
      },
      {
        path: 'promotion',
        name: 'promotion-detail',
        component: () => import('../views/mine/generalize/detail/index.vue'),
      },
      {
        path: 'service',
        name: 'mine-service',
        component: () => import('../views/mine/service/index.vue'),
      },
      {
        path: 'address',
        name: 'mine-address',
        component: () => import('../views/mine/address/index.vue'),
      },
      {
        path: 'invoice',
        name: 'mine-invoice',
        component: () => import('../views/mine/invoice/index.vue'),
      },
    ]
  },
  {
    path: '/navigation',
    component: Page,
    children: [
      {
        path: 'setting',
        name: 'NavigationSetting',
        component: () => import('../views/navigation/setting/index.vue'),
      },
      {
        path: 'invoice',
        name: 'NavigationInvoice',
        component: () => import('../views/navigation/invoice/index.vue'),
      },
    ]
  }
]

const router = animateRouter.create({
  history: createWebHashHistory(),
  routes,
})

// 路由跳转拦截
router.beforeEach((to, from, next) => {
  // 判断服务是否加载完成
  if (service.isReady) {
    if (to.meta.ignoreAuth || getToken()) {
      next();
    } else {
      next({
        name: 'UserLogin',
        query: { redirect: to.fullPath },
      });
    }
  } else {
    if (to.name === 'Boot' || to.name === 'UserLogin') {
      next();
    } else {
      next({
        name: 'Boot',
        query: { redirect: to.fullPath },
      });
    }
  }
})

export default router
