import { createRouter, createWebHashHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import type { App } from 'vue'
import { Layout } from '@/utils/routerHelper'
import { useI18n } from '@/hooks/web/useI18n'

const { t } = useI18n()

export const constantRouterMap: AppRouteRecordRaw[] = [
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard/analysis',
    name: 'Root',
    meta: {
      hidden: true
    }
  },
  {
    path: '/dashboard',
    component: Layout,
    redirect: '/dashboard/analysis',
    name: 'Dashboard',
    meta: {
      title: '首页',
      icon: 'ant-design:dashboard-filled',
      alwaysShow: true
    },
    children: [
      {
        path: 'analysis',
        component: () => import('@/views/Dashboard/Analysis.vue'),
        name: 'Analysis',
        meta: {
          title: '分析页',
          noCache: true,
          affix: true
        }
      }
    ]
  },
  {
    path: '/redirect',
    component: Layout,
    name: 'Redirect',
    children: [
      {
        path: '/redirect/:path(.*)',
        name: 'Redirect',
        component: () => import('@/views/Redirect/Redirect.vue'),
        meta: {}
      }
    ],
    meta: {
      hidden: true,
      noTagsView: true
    }
  },
  {
    path: '/login',
    component: () => import('@/views/Login/Login.vue'),
    name: 'Login',
    meta: {
      hidden: true,
      title: t('router.login'),
      noTagsView: true
    }
  },
  {
    path: '/404',
    component: () => import('@/views/Error/404.vue'),
    name: 'NoFind',
    meta: {
      hidden: true,
      title: '404',
      noTagsView: true
    }
  }
]

export const asyncRouterMap: AppRouteRecordRaw[] = [
  {
    path: '/dashboard',
    component: Layout,
    redirect: '/dashboard/analysis',
    name: 'Dashboard',
    meta: {
      title: t('router.dashboard'),
      icon: 'ant-design:dashboard-filled',
      alwaysShow: true
    },
    children: [
      {
        path: 'analysis',
        component: () => import('@/views/Dashboard/Analysis.vue'),
        name: 'Analysis',
        meta: {
          title: t('router.analysis'),
          noCache: true,
          affix: true
        }
      }
    ]
  },
  {
    path: '/data-manage',
    component: Layout,
    redirect: '/data-manage/data-source-manage',
    name: 'DataManage',
    menuId: 'DataManage',
    meta: {
      title: '数据管理',
      icon: 'tdesign:data-base',
      alwaysShow: true
    },
    children: [
      {
        path: 'data-model-manage',
        component: () => import('@/views/DataManage/DataModelManage/DataModelManage.vue'),
        name: 'DataModelManage',
        menuId: 'DataManage/DataModelManage',
        meta: {
          title: '数据模型管理'
        }
      },
      {
        path: 'data-set-manage',
        component: () => import('@/views/DataManage/DataSetManage/DataSetManage.vue'),
        name: 'DataSetManage',
        menuId: 'DataManage/DataSetManage',
        meta: {
          title: '数据集管理'
        }
      }
    ]
  },
  {
    path: '/data-review',
    component: Layout,
    redirect: '/data-review/data-review-detail-rule',
    name: 'DataReview',
    menuId: 'DataReview',
    meta: {
      title: t('router.dataReview'),
      icon: 'tdesign:data-checked',
      alwaysShow: true
    },
    children: [
      {
        path: 'data-review-detail-rule',
        component: () => import('@/views/srdb/SrdbKxqRule/SrdbKxqRule.vue'),
        name: 'SrdbKxqRule',
        menuId: 'DataReview/SrdbKxqRule',
        meta: {
          title: '规则配置'
        }
      },
      {
        path: 'data-review-detail-rule',
        component: () => import('@/views/DataReview/DataReviewDetailRule/DataReviewDetailRule.vue'),
        name: 'DataReviewDetailRule',
        menuId: 'DataReview/DataReviewDetailRule',
        meta: {
          title: '明细规则配置'
        }
      },
      {
        path: 'data-review-detail-rule-config/:id',
        component: () =>
          import('@/views/DataReview/DataReviewDetailRuleConfig/DataReviewDetailRuleConfig.vue'),
        name: 'DataReviewDetailRuleConfig',
        menuId: 'DataReview/DataReviewDetailRuleConfig',
        meta: {
          hidden: true,
          title: '规则配置',
          canTo: true,
          activeMenu: '/data-review/data-review-detail-rule'
        }
      },
      {
        path: 'data-review-detail',
        component: () => import('@/views/DataReview/DataReviewDetail.vue'),
        name: 'DataReviewDetail',
        menuId: 'DataReview/DataReviewDetail',
        meta: {
          title: t('router.dataReviewDetail')
        }
      },
      {
        path: 'data-review-detail-result',
        component: () => import('@/views/DataReview/DataReviewDetailResult.vue'),
        name: 'DataReviewDetailResult',
        menuId: 'DataReview/DataReviewDetailResult',
        meta: {
          title: t('router.dataReviewDetailResult')
        }
      }
    ]
  },
  {
    path: '/report-manage',
    component: Layout,
    redirect: '/report-manage/report-build-file',
    name: 'ReportManage',
    menuId: 'ReportManage',
    meta: {
      title: t('router.ReportManage'),
      icon: 'ep:document',
      alwaysShow: true
    },
    children: [
      {
        path: 'report-build-file',
        component: () => import('@/views/Template/ReportBuildFile/ReportBuildFile.vue'),
        name: 'ReportBuildFile',
        menuId: 'ReportManage/ReportBuildFile',
        meta: {
          title: t('router.ReportBuildFile')
        }
      },
      {
        path: 'report-file-push',
        component: () => import('@/views/Template/ReportFIlePush/ReportFilePush.vue'),
        name: 'ReportFilePush',
        menuId: 'ReportManage/ReportFilePush',
        meta: {
          title: t('router.ReportFilePush')
        }
      },
      {
        path: 'report-temp-config',
        component: () => import('@/views/Template/ReportTempConfig/ReportTempConfig.vue'),
        name: 'ReportTempConfig',
        menuId: 'ReportManage/ReportTempConfig',
        meta: {
          title: t('router.ReportTempConfig')
        }
      },
      {
        path: 'temp-submit-manage',
        component: () => import('@/views/Template/TempSubmitManage/TempSubmitManage.vue'),
        name: 'TempSubmitManage',
        menuId: 'ReportManage/TempSubmitManage',
        meta: {
          title: t('router.TempSubmitManage')
        }
      }
    ]
  },
  {
    path: '/trusted-zone',
    component: Layout,
    redirect: '/trusted-zone/database-table-query',
    name: 'trustedZone',
    menuId: 'trustedZone',
    meta: {
      title: '可信区管理',
      icon: 'tdesign:view-module',
      alwaysShow: true
    },
    children: [
      {
        path: 'database-table-query',
        component: () =>
          import('@/views/TrustedZoneManage/DatabaseTableQuery/DatabaseTableQuery.vue'),
        name: 'DatabaseTableQuery',
        menuId: 'trustedZone/DatabaseTableQuery',
        meta: {
          title: '库表信息查询'
        }
      },
      {
        path: 'field-info-query',
        component: () => import('@/views/TrustedZoneManage/FieldInfoQuery/FieldInfoQuery.vue'),
        name: 'FieldInfoQuery',
        menuId: 'trustedZone/FieldInfoQuery',
        meta: {
          title: '字段信息查询'
        }
      },
      {
        path: 'ledger-info-query',
        component: () => import('@/views/TrustedZoneManage/LedgerInfoQuery/LedgerInfoQuery.vue'),
        name: 'LedgerInfoQuery',
        menuId: 'trustedZone/LedgerInfoQuery',
        meta: {
          title: '台账信息查询'
        }
      },
      {
        path: 'action-log-query',
        component: () => import('@/views/TrustedZoneManage/ActionLogQuery/ActionLogQuery.vue'),
        name: 'ActionLogQuery',
        menuId: 'trustedZone/ActionLogQuery',
        meta: {
          title: '操作日志查询'
        }
      }
    ]
  },
  {
    path: '/multipleQuery',
    component: Layout,
    redirect: '/multipleQuery/serviceLog',
    name: 'multipleQuery',
    menuId: 'multipleQuery',
    meta: {
      title: '综合查询',
      icon: 'ep:data-analysis',
      alwaysShow: true
    },
    children: [
      {
        path: 'serviceLog',
        component: () => import('@/views/MultipleQuery/ServiceLog/ServiceLog.vue'),
        name: 'ServiceLog',
        menuId: 'multipleQuery/ServiceLog',
        meta: {
          title: t('router.servicelogqueryprototype')
        }
      },
      {
        path: 'submit-data-query',
        component: () => import('@/views/MultipleQuery/SubmitDataQuery/SubmitDataQuery.vue'),
        name: 'SubmitDataQuery',
        menuId: 'multipleQuery/SubmitDataQuery',
        meta: {
          title: '报送数据查询'
        }
      }
    ]
  },
  {
    path: '/job',
    component: Layout,
    redirect: '/job/rep-job',
    name: 'Job',
    menuId: 'Job',
    meta: {
      title: '作业管理',
      icon: 'tdesign:task-setting',
      alwaysShow: true
    },
    children: [
      {
        path: 'rep-job',
        component: () => import('@/views/job/RepJob/RepJob.vue'),
        name: 'RepJob',
        menuId: 'job/RepJob',
        meta: {
          title: '作业配置'
        }
      },
      {
        path: 'JobMonitor',
        component: () => import('@/views/job/RepTask/RepTask.vue'),
        name: 'JobMonitor',
        menuId: 'job/JobMonitor',
        meta: {
          title: '作业监控'
        }
      }
    ]
  },
  
  {
    path: '/system',
    component: Layout,
    redirect: '/system/sys-menu',
    name: 'System',
    menuId: 'System',
    meta: {
      title: '系统管理',
      icon: 'eos-icons:role-binding',
      alwaysShow: true
    },
    children: [
      {
        path: 'rep-data-source',
        component: () => import('@/views/param/RepDataSource/RepDataSource.vue'),
        name: 'RepDataSource',
        menuId: 'param/RepDataSource',
        meta: {
          title: '数据源管理'
        }
      },
      {
        path: 'sys-login-log',
        component: () => import('@/views/system/SysLoginLog/SysLoginLog.vue'),
        name: 'SysLoginLog',
        menuId: 'system/SysLoginLog',
        meta: {
          title: '登陆日志查询'
        }
      },
      {
        path: 'sys-menu',
        component: () => import('@/views/system/SysMenu/SysMenu.vue'),
        name: 'SysMenu',
        menuId: 'system/SysMenu',
        meta: {
          title: '菜单管理'
        }
      },
      {
        path: 'sys-role',
        component: () => import('@/views/system/SysRole/SysRole.vue'),
        name: 'SysRole',
        menuId: 'system/SysRole',
        meta: {
          title: '角色管理'
        }
      },
      {
        path: 'sys-user',
        component: () => import('@/views/system/SysUser/SysUser.vue'),
        name: 'SysUser',
        menuId: 'system/SysUser',
        meta: {
          title: '用户管理'
        }
      },
      {
        path: 'sys-organ',
        component: () => import('@/views/param/SysOrgan/SysOrgan.vue'),
        name: 'SysOrgan',
        menuId: 'param/SysOrgan',
        meta: {
          title: '机构管理'
        }
      },
      {
        path: 'sys-organ-tree',
        component: () => import('@/views/param/SysOrganTree/SysOrganTree.vue'),
        name: 'SysOrganTree',
        menuId: 'param/SysOrganTree',
        meta: {
          title: '机构树管理'
        }
      },
      {
        path: 'sys-org-tree-detail/:id',
        component: () => import('@/views/param/SysOrgTreeDetail/SysOrgTreeDetail.vue'),
        name: 'SysOrgTreeDetail',
        menuId: 'param/SysOrgTreeDetail',
        meta: {
          title: '机构树管理',
          hidden: true
        }
      }
    ]
  },

  {
    path: '/csck',
    component: Layout,
    redirect: '/csck/rep-job',
    name: 'csck',
    menuId: 'csck',
    meta: {
      title: '跨系统校验',
      icon: 'tdesign:task-setting',
      alwaysShow: true
    },
    children: [
      {
        path: 'CsckBusinessSystem',
        component: () => import('@/views/csck/CsckBusinessSystem/CsckBusinessSystem.vue'),
        name: 'CsckBusinessSystem',
        menuId: 'csck/CsckBusinessSystem',
        meta: {
          title: '业务系统管理',
          hidden: false
        }
      },
      {
        path: 'CsckBusinessSystem1',
        component: () => import('@/views/csck/CsckBusinessSystem/CsckBusinessSystem1.vue'),
        name: 'CsckBusinessSystem1',
        menuId: 'csck/CsckBusinessSystem1',
        meta: {
          title: '业务系统管理1',
          hidden: false
        }
      },
    ]
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  strict: true,
  routes: constantRouterMap as RouteRecordRaw[],
  scrollBehavior: () => ({ left: 0, top: 0 })
})

export const resetRouter = (): void => {
  const resetWhiteNameList = ['Redirect', 'Login', 'NoFind', 'Root']
  router.getRoutes().forEach((route) => {
    const { name } = route
    if (name && !resetWhiteNameList.includes(name as string)) {
      router.hasRoute(name) && router.removeRoute(name)
    }
  })
}

export const setupRouter = (app: App<Element>) => {
  app.use(router)
}

export default router
