import { createWebHistory, createRouter, RouteRecordRaw } from 'vue-router';
/* Layout */
import Layout from '@/layout/index.vue';

/**
 * Note: 路由配置项
 *
 * hidden: true                     // 当设置 true 的时候该路由不会再侧边栏出现 如401，login等页面，或者如一些编辑页面/edit/1
 * alwaysShow: true                 // 当你一个路由下面的 children 声明的路由大于1个时，自动会变成嵌套的模式--如组件页面
 *                                  // 只有一个时，会将那个子路由当做根路由显示在侧边栏--如引导页面
 *                                  // 若你想不管路由下面的 children 声明的个数都显示你的根路由
 *                                  // 你可以设置 alwaysShow: true，这样它就会忽略之前定义的规则，一直显示根路由
 * redirect: noRedirect             // 当设置 noRedirect 的时候该路由在面包屑导航中不可被点击
 * name:'router-name'               // 设定路由的名字，一定要填写不然使用<keep-alive>时会出现各种问题
 * query: '{"id": 1, "name": "ry"}' // 访问路由的默认传递参数
 * roles: ['admin', 'common']       // 访问路由的角色权限
 * permissions: ['a:a:a', 'b:b:b']  // 访问路由的菜单权限
 * meta : {
 noCache: true                   // 如果设置为true，则不会被 <keep-alive> 缓存(默认 false)
 title: 'title'                  // 设置该路由在侧边栏和面包屑中展示的名字
 icon: 'svg-name'                // 设置该路由的图标，对应路径src/assets/icons/svg
 breadcrumb: false               // 如果设置为false，则不会在breadcrumb面包屑中显示
 activeMenu: '/system/user'      // 当路由设置了该属性，则会高亮相对应的侧边栏。
 }
 */

// 公共路由
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: '/test',
    hidden: true,
    component: () => import('@/views/print/test.vue')
  },
  {
    path: '/externalPrint',
    hidden: true,
    component: () => import('@/views/print/externalPrint.vue')
  },
  {
    path: '/print',
    hidden: true,
    component: () => import('@/views/print/index.vue')
  },
  {
    path: '/boardGenerate',
    hidden: true,
    component: () => import('@/views/boardGenerate/index.vue')
  },
  {
    path: '/boardGenerate1',
    hidden: true,
    component: () => import('@/views/boardGenerate/index1.vue')
  },
  {
    path: '/boardGenerate2',
    hidden: true,
    component: () => import('@/views/boardGenerate/index2.vue')
  },
  {
    path: '/boardView',
    hidden: true,
    component: () => import('@/views/boardGenerate/view.vue')
  },
  {
    path: '/boardConstruction',
    hidden: true,
    component: () => import('@/views/boardGenerate/construction.vue')
  },
  {
    path: '/board1',
    hidden: true,
    component: () => import('@/views/board/index.vue')
  },
  {
    path: '/formCs',
    hidden: true,
    component: () => import('@/views/tool/draggable/demo.vue')
  },
  {
    path: '/board2',
    hidden: true,
    component: () => import('@/views/board/index2.vue')
  },
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index.vue')
      }
    ]
  },
  {
    path: '/social-callback',
    hidden: true,
    component: () => import('@/layout/components/SocialCallback/index.vue')
  },
  {
    path: '/login',
    component: () => import('@/views/login.vue'),
    hidden: true
  },
  {
    path: '/register',
    component: () => import('@/views/register.vue'),
    hidden: true
  },
  {
    path: '/:pathMatch(.*)*',
    component: () => import('@/views/error/404.vue'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error/401.vue'),
    hidden: true
  },
  {
    path: '',
    component: Layout,
    redirect: '/index',
    children: [
      {
        path: '/index',
        component: () => import('@/views/index.vue'),
        name: 'Index',
        meta: { title: '首页', icon: 'dashboard', affix: true }
      }
    ]
  },
  {
    path: '/wms',
    component: Layout,
    redirect: '/index',
    children: [
      {
        path: 'instockDetail',
        component: () => import('@/views/wms/instockDetail/index.vue'),
        name: 'instockDetail',
        meta: { title: '入库明细', icon: 'dashboard' },
        hidden: true
      }
    ]
  },
  {
    path: '/wms',
    component: Layout,
    redirect: '/index',
    children: [
      {
        path: 'allocateOrderDetail',
        component: () => import('@/views/wms/allocateOrderDetail/index.vue'),
        name: 'allocateOrderDetail',
        meta: { title: '调拨单明细', icon: 'dashboard' },
        hidden: true
      }
    ]
  },
  {
    path: '/wms',
    component: Layout,
    redirect: '/index',
    children: [
      {
        path: 'outStockDetail',
        component: () => import('@/views/wms/outstockDetail/index.vue'),
        name: 'outstockDetail',
        meta: { title: '出库明细', icon: 'dashboard' },
        hidden: true
      }
    ]
  },
  {
    path: '/wms',
    component: Layout,
    redirect: '/index',
    children: [
      {
        path: 'psmInLoadDetail',
        component: () => import('@/views/wms/psmInLoadDetail/index.vue'),
        name: 'psmInLoadDetail',
        meta: { title: '装车架子明细', icon: 'dashboard' },
        hidden: true
      }
    ]
  },
  {
    path: '/wms',
    component: Layout,
    redirect: '/index',
    children: [
      {
        path: 'psmInStockDetail',
        component: () => import('@/views/wms/psmInStockDetail/index.vue'),
        name: 'psmInStockDetail',
        meta: { title: '成品入库明细', icon: 'dashboard' },
        hidden: true
      }
    ]
  },
  {
    path: '/user',
    component: Layout,
    hidden: true,
    redirect: 'noredirect',
    children: [
      {
        path: 'profile',
        component: () => import('@/views/system/user/profile/index.vue'),
        name: 'Profile',
        meta: { title: '个人中心', icon: 'user' }
      }
    ]
  },
  {
    path: '/qms/qcUnqualifiedApproval',
    component: Layout,
    hidden: true,
    children: [
      {
        path: 'approval',
        component: () => import('@/views/qms/qcUnqualifiedApproval/approval.vue'),
        name: 'qcUnqualifiedApproval',
        meta: { title: '不合格品主管审批', activeMenu: '/qms/qcUnqualifiedApproval', noCache: true }
      }
    ]
  }
];

// 动态路由，基于用户权限动态去加载
export const dynamicRoutes: RouteRecordRaw[] = [
  {
    path: '/system/user-auth',
    component: Layout,
    hidden: true,
    permissions: ['system:user:edit'],
    children: [
      {
        path: 'role/:userId(\\d+)',
        component: () => import('@/views/system/user/authRole.vue'),
        name: 'AuthRole',
        meta: { title: '分配角色', activeMenu: '/system/user', icon: '' }
      }
    ]
  },
  {
    path: '/system/role-auth',
    component: Layout,
    hidden: true,
    permissions: ['system:role:edit'],
    children: [
      {
        path: 'user/:roleId(\\d+)',
        component: () => import('@/views/system/role/authUser.vue'),
        name: 'AuthUser',
        meta: { title: '分配用户', activeMenu: '/system/role', icon: '' }
      }
    ]
  },
  {
    path: '/system/dict-data',
    component: Layout,
    hidden: true,
    permissions: ['system:dict:list'],
    children: [
      {
        path: 'index/:dictId(\\d+)',
        component: () => import('@/views/system/dict/data.vue'),
        name: 'Data',
        meta: { title: '字典数据', activeMenu: '/system/dict', icon: '' }
      }
    ]
  },
  {
    path: '/system/oss-config',
    component: Layout,
    hidden: true,
    permissions: ['system:ossConfig:list'],
    children: [
      {
        path: 'index',
        component: () => import('@/views/system/oss/config.vue'),
        name: 'OssConfig',
        meta: { title: '配置管理', activeMenu: '/system/oss', icon: '' }
      }
    ]
  },
  {
    path: '/tool/gen-edit',
    component: Layout,
    hidden: true,
    permissions: ['tool:gen:edit'],
    children: [
      {
        path: 'index/:tableId(\\d+)',
        component: () => import('@/views/tool/gen/editTable.vue'),
        name: 'GenEdit',
        meta: { title: '修改生成配置', activeMenu: '/tool/gen', icon: '', noCache: true }
      }
    ]
  },
  // {
  //   path: '/flowableWorkflow/leaveEdit',
  //   component: Layout,
  //   hidden: true,
  //   permissions: ['flowableWorkflow:leave:edit'],
  //   children: [
  //     {
  //       path: 'index',
  //       component: () => import('@/views/flowableWorkflow/leave/leaveEdit.vue'),
  //       name: 'leaveEdit',
  //       meta: { title: '请假申请', activeMenu: '/flowableWorkflow/leave', noCache: true }
  //     }
  //   ]
  // },
  {
    path: '/mes/product-plan',
    component: Layout,
    hidden: true,
    permissions: ['mes:orderInfo:edit'],
    children: [
      {
        path: 'index/:productOrderId(\\d+)',
        component: () => import('@/views/mes/orderInfo/editProductPlan.vue'),
        name: 'productPlanEdit',
        meta: { title: '生产派工', activeMenu: '/mes/productplan', noCache: true }
      }
    ]
  },
  // {
  //   path: '/mes/product-order',
  //   component: Layout,
  //   hidden: true,
  //   permissions: ['mes:orderInfo:edit'],
  //   children: [
  //     {
  //       path: 'index/:productOrderId(\\d+)',
  //       component: () => import('@/views/mes/orderInfo/editProductOrder.vue'),
  //       name: 'productOrderEdit',
  //       meta: { title: '生产任务', activeMenu: '/mes/orderInfo', noCache: true }
  //     }
  //   ]
  // },
  // {
  //   path: '/mes/product-order',
  //   component: Layout,
  //   hidden: true,
  //   permissions: ['mes:orderInfo:edit'],
  //   children: [
  //     {
  //       path: 'index',
  //       component: () => import('@/views/mes/orderInfo/batchAddOrder.vue'),
  //       name: 'batchAddOrder',
  //       meta: { title: '生产任务', activeMenu: '/mes/orderInfo', noCache: true }
  //     }
  //   ]
  // },
  {
    path: '/dms/deviceMode/mode-edit',
    component: Layout,
    hidden: true,
    permissions: ['dms:deviceMode:edit'],
    children: [
      {
        path: 'index/:deviceModeId(\\d+)',
        component: () => import('@/views/dms/deviceMode/editDeviceMode.vue'),
        name: 'ModeAdd',
        meta: { title: '添加设备模型', activeMenu: '/dms/deviceMode', noCache: true }
      }
    ]
  },
  {
    path: '/dms/dmsPlanLubeDetail',
    component: Layout,
    hidden: true,
    permissions: ['dms:dmsPlanLube:add'],
    children: [
      {
        path: 'index/:planLubeId/:planLubeCode',
        component: () => import('@/views/dms/dmsPlanLubeDetail/index.vue'),
        name: 'DmsPlanLubeDetail',
        meta: { title: '润滑计划明细', activeMenu: '/dms/dmsPlanLube', icon: '' }
      }
    ]
  },

  {
    path: '/dms/dmsBillsLubeInstanceActivity',
    component: Layout,
    hidden: true,
    permissions: ['dms:dmsBillsLubeInstance:add'],
    children: [
      {
        path: 'index',
        component: () => import('@/views/dms/dmsBillsLubeInstanceActivity/index.vue'),
        name: 'dmsBillsLubeInstanceActivity',
        meta: { title: '润滑工单明细', activeMenu: '/dms/dmsBillsLubeInstance', icon: '' }
      }
    ]
  },

  {
    path: '/dms/dmsMaintInstanceActivity',
    component: Layout,
    hidden: true,
    permissions: ['dms:dmsBillsMaintInstance:add'],
    children: [
      {
        path: 'index',
        component: () => import('@/views/dms/dmsMaintInstanceActivity/index.vue'),
        name: 'dmsMaintInstanceActivity',
        meta: { title: '保养工单明细', activeMenu: '/dms/dmsBillsMaintInstance', icon: '' }
      }
    ]
  },

  {
    path: '/dms/dmsBillsInspectInstanceActivity',
    component: Layout,
    hidden: true,
    permissions: ['dms:dmsBillsInspectInstance:add'],
    children: [
      {
        path: 'index',
        component: () => import('@/views/dms/dmsBillsInspectInstanceActivity/index.vue'),
        name: 'dmsBillsInspectInstanceActivity',
        meta: { title: '点巡检工单明细', activeMenu: '/dms/dmsBillsInspectInstance', icon: '' }
      }
    ]
  },

  {
    path: '/dms/dmsFaultInstanceActivity',
    component: Layout,
    hidden: true,
    permissions: ['dms:dmsBillsFaultInstance:add'],
    children: [
      {
        path: 'index',
        component: () => import('@/views/dms/dmsFaultInstanceActivity/index.vue'),
        name: 'dmsFaultInstanceActivity',
        meta: { title: '维修工单明细', activeMenu: '/dms/dmsBillsFaultInstance', icon: '' }
      }
    ]
  },

  {
    path: '/dms/dmsPlanMaintDetail',
    component: Layout,
    hidden: true,
    permissions: ['dms:dmsPlanMaint:add'],
    children: [
      {
        path: 'index/:planMaintId/:planMaintCode',
        component: () => import('@/views/dms/dmsPlanMaintDetail/index.vue'),
        name: 'dmsPlanMaintDetail',
        meta: { title: '润滑工单明细', activeMenu: '/dms/dmsPlanMaint', icon: '' }
      }
    ]
  },
  // 新增点巡检线路详情路由
  {
    path: '/dms/dmsInspectRouteDetail',
    component: Layout,
    hidden: true,
    permissions: ['dms:dmsBaseInspectRoute:add'], // 根据实际权限需求配置
    children: [
      {
        path: 'index/:inspectRouteId', // 参数名为 inspectRouteId
        component: () => import('@/views/dms/dmsInspectRouteDetail/index.vue'),
        name: 'DmsInspectRouteDetail',
        meta: { title: '巡检线路详情', activeMenu: '/dms/dmsBaseInspectRoute' }
      }
    ]
  },
  {
    path: '/workflow/leaveEdit',
    component: Layout,
    hidden: true,
    permissions: ['workflow:leave:edit'],
    children: [
      {
        path: 'index',
        component: () => import('@/views/workflow/leave/leaveEdit.vue'),
        name: 'leaveEdit',
        meta: { title: '请假申请', activeMenu: '/workflow/leave', noCache: true }
      }
    ]
  },
  {
    path: '/workflow/design',
    component: Layout,
    hidden: true,
    permissions: ['workflow:leave:edit'],
    children: [
      {
        path: 'index',
        component: () => import('@/views/workflow/processDefinition/design.vue'),
        name: 'design',
        meta: { title: '流程设计', activeMenu: '/workflow/processDefinition', noCache: true }
      }
    ]
  },
  {
    path: '/dms/maint/maintEdit',
    component: Layout,
    hidden: true,
    permissions: ['dms:dmsBillsMaintInstance:add'],
    children: [
      {
        path: 'index',
        component: () => import('@/views/dms/dmsBillsMaintInstance/maintEdit.vue'),
        name: 'maintEdit',
        meta: { title: '保养工单申请', activeMenu: '/dms/maint/maintEdit', noCache: true }
      }
    ]
  },

  {
    path: '/ai/skill/knowledgeContent',
    component: Layout,
    hidden: true,
    permissions: ['ai:aiKnowledgeContent:list'],
    children: [
      {
        path: 'index/:knowledgeBaseId/:modelId/:knowledgeBaseName',
        component: () => import('@/views/ai/skill/aiKnowledge/knowledgeContent.vue'),
        name: 'KnowledgeContent',
        meta: { title: '知识库内容', activeMenu: '/ai/skill/aiKnowledge', noCache: true }
      }
    ]
  },

  {
    path: '/ai/skill/knowledgeBaseQA',
    component: Layout,
    hidden: true,
    permissions: ['ai:aiKnowledgeBaseDocs:qa'],
    children: [
      {
        path: 'index/:knowledgeBaseId/:embeddingModelId/:retrieveLimit',
        component: () => import('@/views/ai/skill/aiChat/index.vue'),
        name: 'KnowledgeBaseQA',
        meta: { title: '知识库问答', activeMenu: '/ai/skill/aiChat', noCache: true }
      }
    ]
  },

  // {
  //   path: '/knowledge-base-preview',
  //   name: 'KnowledgeBasePreview',
  //   component: () => import('@/views/ai/skill/aiKnowledge/KnowledgeBasePreview.vue'),
  //   hidden: true
  // },
  // {
  //   path: '/knowledge-base-upload',
  //   name: 'KnowledgeBaseUpload',
  //   component: () => import('@/views/ai/skill/aiKnowledge/KnowledgeBaseUpload.vue'),
  //   hidden: true
  // },
  // {
  //   path: '/knowledge-base-qa',
  //   name: 'KnowledgeBaseQA',
  //   component: () => import('@/views/ai/skill/aiKnowledge/KnowledgeBaseQA.vue'),
  //   hidden: true
  // },
  // {
  //   path: '/ai-chat-settings',
  //   name: 'AiChatSettings',
  //   component: () => import('@/views/ai/base/aiApp/aiAppConfig.vue'),
  //   hidden: true
  // }


];

/**
 * 创建路由
 */
const router = createRouter({
  history: createWebHistory(import.meta.env.VITE_APP_CONTEXT_PATH),
  routes: constantRoutes,
  // 刷新时，滚动条位置还原
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition;
    } else {
      return { top: 0 };
    }
  }
});

export default router;
