import { createRouter, createWebHistory } from 'vue-router'
import AdminLayout from '../components/layout/AdminLayout.vue'
import OperationLog from '@/views/system/OperationLog.vue'

const routes = [
  {
    path: '/',
    redirect: '/dashboard'
  },
  {
    path: '/redirect',
    component: AdminLayout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('../views/Redirect.vue'),
        meta: { title: '重定向' }
      }
    ]
  },
  {
    path: '/',
    component: AdminLayout,
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('../views/dashboard/Dashboard.vue'),
        meta: {
          title: '仪表盘',
          icon: 'DataBoard'
        }
      },
      {
        path: 'ticket',
        name: 'Ticket',
        meta: {
          title: '工单管理',
          icon: 'Tickets'
        },
        redirect: '/ticket/list',
        children: [
          {
            path: 'list',
            name: 'TicketList',
            component: () => import('../views/ticket/TicketList.vue'),
            meta: {
              title: '工单列表'
            }
          },
          {
            path: 'detail',
            name: 'TicketDetail',
            component: () => import('../views/ticket/TicketDetail.vue'),
            meta: {
              title: '工单详情',
              hidden: true,
              activeMenu: '/ticket/list'
            }
          },
          {
            path: 'types',
            name: 'TicketTypes',
            component: () => import('../views/ticket/TicketTypes.vue'),
            meta: {
              title: '工单类型管理'
            }
          },
          {
            path: 'templates',
            name: 'TicketTemplates',
            component: () => import('../views/ticket/TicketTemplates.vue'),
            meta: {
              title: '回复模板管理'
            }
          }
        ]
      },

      // ACS管理路由
      {
        path: 'acs',
        name: 'ACS',
        meta: {
          title: 'ACS管理',
          icon: 'Monitor'
        },
        redirect: '/acs/messages/announcements',
        children: [
          // 消息管理路由
          {
            path: 'messages',
            name: 'Messages',
            meta: {
              title: '消息管理'
            },
            redirect: '/acs/messages/announcements',
            children: [
              {
                path: 'announcements',
                name: 'Announcements',
                component: () => import('../views/acs/messages/Announcements.vue'),
                meta: {
                  title: '官方公告'
                }
              },
              {
                path: 'policies',
                name: 'Policies',
                component: () => import('../views/acs/messages/Policies.vue'),
                meta: {
                  title: '官方政策'
                }
              },
              {
                path: 'news',
                name: 'News',
                component: () => import('../views/acs/messages/News.vue'),
                meta: {
                  title: '新闻咨询'
                }
              }
            ]
          },
          // 镜像管理路由
          {
            path: 'images',
            name: 'Images',
            meta: {
              title: '镜像管理'
            },
            redirect: '/acs/images/vm',
            children: [
              {
                path: 'vm',
                name: 'VmImages',
                component: () => import('../views/acs/images/VmImages.vue'),
                meta: {
                  title: '虚拟机镜像'
                }
              },
              {
                path: 'container',
                name: 'ContainerImages',
                component: () => import('../views/acs/images/ContainerImages.vue'),
                meta: {
                  title: '容器镜像'
                }
              },
              {
                path: 'categories',
                name: 'ImageCategories',
                component: () => import('../views/acs/images/ImageCategories.vue'),
                meta: {
                  title: '镜像分类'
                }
              }
            ]
          },
          // 节点管理路由
          {
            path: 'nodes',
            name: 'Nodes',
            component: () => import('../views/acs/nodes/Nodes.vue'),
            meta: {
              title: '节点管理'
            }
          },
          {
            path: 'nodes/form',
            name: 'ServerForm',
            component: () => import('@/views/acs/nodes/ServerForm.vue'),
            meta: { title: '服务器表单', activeMenu: '/acs/nodes', hidden: true }
          },
          {
            path: 'images/form',
            name: 'ImageForm',
            component: () => import('@/views/acs/images/ImageForm.vue'),
            meta: { title: '镜像表单', activeMenu: '/acs/images/vm', hidden: true }
          }, {
            path: 'guacamole',
            name: 'Guacamole',
            component: () => import('../views/acs/guacamole/Guacamole.vue'),
            meta: {
              title: 'Guacamole管理'
            }
          }
        ]
      },
      // 用户管理路由
      {
        path: 'user',
        name: 'User',
        meta: {
          title: '用户管理',
          icon: 'User'
        },
        redirect: '/user/list',
        children: [
          {
            path: 'list',
            name: 'UserList',
            component: () => import('../views/user/UserList.vue'),
            meta: {
              title: '用户列表'
            }
          },
          {
            path: 'detail',
            name: 'UserDetail',
            component: () => import('../views/user/UserDetail.vue'),
            meta: {
              title: '用户详情'
            }
          },
          {
            path: 'balance',
            name: 'UserBalance',
            component: () => import('../views/user/UserBalance.vue'),
            meta: {
              title: '用户余额管理'
            }
          },
          {
            path: 'group',
            name: 'UserGroup',
            component: () => import('../views/user/UserGroup.vue'),
            meta: {
              title: '用户组管理'
            }
          },
          {
            path: 'admin-group',
            name: 'AdminGroup',
            component: () => import('../views/user/AdminGroup.vue'),
            meta: {
              title: '管理员组管理'
            }
          }
        ]
      },
      // 商品管理路由
      {
        path: 'product',
        name: 'Product',
        meta: { title: '商品管理', icon: 'Goods' },
        redirect: '/product/manage',
        children: [
          {
            path: 'manage',
            name: 'ProductManage',
            component: () => import('../views/product/ProductGroup.vue'),
            meta: { title: '商品管理' }
          },
          {
            path: 'discount',
            name: 'DiscountManage',
            component: () => import('../views/product/DiscountManage.vue'),
            meta: { title: '优惠管理' }
          },
          {
            path: 'discount/voucher/:id/manage',
            name: 'VoucherManagement',
            component: () => import('../views/marketing/VoucherManagement.vue'),
            meta: { title: '代金券分发管理', hidden: true, activeMenu: '/product/discount' }
          },
          { path: 'list', redirect: '/product/manage' },
          { path: 'group', redirect: '/product/manage' }
        ]
      },
      // 用户商品管理路由
      {
        path: 'user-goods',
        name: 'UserGoods',
        meta: { title: '用户商品管理', icon: 'ShoppingCart' },
        redirect: '/user-goods/list',
        children: [
          {
            path: 'list',
            name: 'UserGoodsList',
            component: () => import('../views/product/UserGoodsList.vue'),
            meta: { title: '所有商品' }
          },
          {
            path: 'detail/:id',
            name: 'UserGoodsDetail',
            component: () => import('../views/product/UserGoodsDetail.vue'),
            meta: { title: '用户商品详情', hidden: true, activeMenu: '/user-goods/list' }
          },
          {
            path: 'vm-list',
            name: 'UserVmList',
            component: () => import('../views/user-vm/UserVmList.vue'),
            meta: { title: '云服务器' }
          },
        ]
      },
      // 订单管理路由
      {
        path: 'order',
        name: 'Order',
        meta: {
          title: '订单管理',
          icon: 'Document'
        },
        redirect: '/order/list',
        children: [
          {
            path: 'list',
            name: 'OrderList',
            component: () => import('../views/order/OrderList.vue'),
            meta: {
              title: '订单列表'
            }
          }
        ]
      },
      // 优惠营销路由（已合并至 /product/discount，保留重定向兼容旧链接）
      {
        path: 'marketing',
        name: 'Marketing',
        meta: {
          title: '优惠营销',
          icon: 'Present',
          hidden: true
        },
        redirect: '/product/discount',
        children: [
          { path: 'discount', redirect: '/product/discount' },
          { path: 'voucher', redirect: '/product/discount' },
          { path: 'voucher/:id/manage', redirect: to => `/product/discount/voucher/${to.params.id}/manage` }
        ]
      },
      // 活动管理路由
      {
        path: 'activity',
        name: 'Activity',
        meta: {
          title: '活动管理',
          icon: 'TrophyBase'
        },
        redirect: '/activity/signin',
        children: [
          {
            path: 'signin',
            name: 'SigninActivity',
            component: () => import('../views/activity/SigninActivity.vue'),
            meta: {
              title: '签到活动'
            }
          },
          {
            path: '/activity/groupbuy',
            name: 'GroupBuyManage',
            component: () => import('../views/activity/GroupBuyManage.vue'),
            meta: {
              title: '拼团管理'
            }
          }
        ]
      },
      {
        path: 'system',
        name: 'System',
        meta: {
          title: '系统管理',
          icon: 'Setting'
        },
        redirect: '/system/permission/route',
        children: [
          {
            path: 'permission/route',
            name: 'PermissionRoute',
            component: () => import('../views/system/PermissionRoute.vue'),
            meta: {
              title: '路由权限'
            }
          },
          {
            path: 'permission/admin',
            name: 'PermissionAdmin',
            component: () => import('../views/system/PermissionAdmin.vue'),
            meta: {
              title: '管理员权限'
            }
          },

          {
            path: 'file',
            name: 'SystemFile',
            component: () => import('../views/system/SystemFile.vue'),
            meta: {
              title: '文件管理'
            }
          },

          {
            path: 'domain-whitelist',
            name: 'DomainWhitelist',
            component: () => import('../views/system/DomainWhitelist.vue'),
            meta: { title: '域名白名单' }
          },
          {
            path: 'setting-manage',
            name: 'SettingManage',
            component: () => import('../views/system/SettingManage.vue'),
            meta: { title: '配置管理' }
          },
          {
            path: 'notice-channel',
            name: 'NoticeChannel',
            component: () => import('../views/system/NoticeChannel.vue'),
            meta: { title: '通知管理' }
          },
          {
            path: 'menu-manage',
            name: 'MenuManage',
            component: () => import('../views/system/MenuManage.vue'),
            meta: { title: '菜单管理' }
          },
          {
            path: 'menu-permission',
            name: 'MenuPermission',
            component: () => import('../views/system/MenuPermission.vue'),
            meta: { title: '菜单权限' }
          }
        ]
      },
      {
        path: 'virtualization',
        name: 'Virtualization',
        meta: {
          title: '虚拟化平台管理',
          icon: 'Platform'
        },
        redirect: '/virtualization/kvm-service',
        children: [
          {
            path: 'kvm-service',
            name: 'KvmService',
            component: () => import('../views/virtualization/KvmService.vue'),
            meta: {
              title: '主控服务管理'
            }
          },
          {
            path: 'kvm-service-detail',
            name: 'KvmServiceDetail',
            component: () => import('../views/virtualization/KvmServiceDetail.vue'),
            meta: {
              title: '主控服务详情',
              hidden: true,
              activeMenu: '/virtualization/kvm-service'
            }
          },
          {
            path: 'host-group-mapping',
            name: 'HostGroupMapping',
            component: () => import('../views/virtualization/HostGroupMapping.vue'),
            meta: {
              title: '宿主机组映射管理'
            }
          },
          {
            path: 'host-manage',
            name: 'HostManage',
            component: () => import('../views/virtualization/HostManage.vue'),
            meta: {
              title: '宿主机管理',
              hidden: true,
              activeMenu: '/virtualization/kvm-service'
            }
          },
          {
            path: 'image-manage',
            name: 'ImageManage',
            component: () => import('../views/virtualization/ImageManage.vue'),
            meta: {
              title: '镜像管理',
              hidden: true,
              activeMenu: '/virtualization/kvm-service'
            }
          },
          {
            path: 'network-manage',
            name: 'NetworkManage',
            component: () => import('../views/virtualization/NetworkManage.vue'),
            meta: {
              title: '网络管理',
              hidden: true,
              activeMenu: '/virtualization/kvm-service'
            }
          },
          {
            path: 'volume-manage',
            name: 'VolumeManage',
            component: () => import('../views/virtualization/VolumeManage.vue'),
            meta: {
              title: '数据卷管理',
              hidden: true,
              activeMenu: '/virtualization/kvm-service'
            }
          },
          {
            path: 'vm-manage',
            name: 'VmManage',
            component: () => import('../views/virtualization/VmManage.vue'),
            meta: {
              title: '虚拟机管理',
              hidden: true,
              activeMenu: '/virtualization/kvm-service'
            }
          },
          {
            path: 'security-group',
            name: 'SecurityGroupManage',
            component: () => import('../views/virtualization/SecurityGroupManage.vue'),
            meta: {
              title: '安全组管理',
              hidden: true,
              activeMenu: '/virtualization/kvm-service'
            }
          },
          {
            path: 'vnc-node',
            name: 'VncNodeManage',
            component: () => import('../views/virtualization/VncNodeManage.vue'),
            meta: {
              title: 'VNC节点管理',
              hidden: true,
              activeMenu: '/virtualization/kvm-service'
            }
          },
          {
            path: 'vnc-command',
            name: 'VncCommandManage',
            component: () => import('../views/virtualization/VncCommandManage.vue'),
            meta: {
              title: 'VNC指令管理'
            }
          },
          {
            path: 'host-detail',
            name: 'VirtHostDetail',
            component: () => import('../views/virtualization/HostDetail.vue'),
            meta: {
              title: '宿主机详情',
              hidden: true,
              activeMenu: '/virtualization/kvm-service'
            }
          },
          {
            path: 'image-detail',
            name: 'VirtImageDetail',
            component: () => import('../views/virtualization/ImageDetail.vue'),
            meta: {
              title: '镜像详情',
              hidden: true,
              activeMenu: '/virtualization/kvm-service'
            }
          },
          {
            path: 'vm-detail',
            name: 'VirtVmDetail',
            component: () => import('../views/virtualization/VmDetail.vue'),
            meta: {
              title: '虚拟机详情',
              hidden: true,
              activeMenu: '/virtualization/kvm-service'
            }
          },
          {
            path: 'security-group-detail',
            name: 'VirtSecurityGroupDetail',
            component: () => import('../views/virtualization/SecurityGroupDetail.vue'),
            meta: {
              title: '安全组详情',
              hidden: true,
              activeMenu: '/virtualization/kvm-service'
            }
          },
          {
            path: 'volume-detail',
            name: 'VirtVolumeDetail',
            component: () => import('../views/virtualization/VolumeDetail.vue'),
            meta: {
              title: '数据卷详情',
              hidden: true,
              activeMenu: '/virtualization/kvm-service'
            }
          }
        ]
      },
      // 短信平台管理路由
      {
        path: 'sms',
        name: 'Sms',
        meta: {
          title: '短信平台管理',
          icon: 'ChatDotRound'
        },
        redirect: '/sms/service',
        children: [
          {
            path: 'service',
            name: 'SmsService',
            component: () => import('../views/sms/SmsService.vue'),
            meta: { title: '主控服务管理' }
          },
          {
            path: 'goods',
            name: 'SmsGoods',
            component: () => import('../views/sms/SmsGoods.vue'),
            meta: { title: '额度商品管理' }
          },
          {
            path: 'signature',
            name: 'SmsSignature',
            component: () => import('../views/sms/SmsSignature.vue'),
            meta: { title: '签名管理' }
          },
          {
            path: 'template',
            name: 'SmsTemplateMgr',
            component: () => import('../views/sms/SmsTemplate.vue'),
            meta: { title: '模板管理' }
          }
        ]
      },
      // 邮箱平台管理路由
      {
        path: 'mail',
        name: 'Mail',
        meta: {
          title: '邮箱平台管理',
          icon: 'Message'
        },
        redirect: '/mail/service',
        children: [
          {
            path: 'service',
            name: 'MailService',
            component: () => import('../views/mail/MailService.vue'),
            meta: { title: '主控服务管理' }
          }
        ]
      },
      // 站点审计路由
      {
        path: 'audit',
        name: 'Audit',
        meta: {
          title: '站点审计',
          icon: 'Monitor'
        },
        redirect: '/audit/all',
        children: [
          {
            path: 'all',
            name: 'AuditAll',
            component: () => import('../views/audit/AllSites.vue'),
            meta: {
              title: '所有站点'
            }
          },
          {
            path: 'violation',
            name: 'AuditViolation',
            component: () => import('../views/audit/ViolationSites.vue'),
            meta: {
              title: '违规站点'
            }
          }
        ]
      },
      // 全局设置管理路由
      {
        path: 'setting',
        name: 'Setting',
        meta: {
          title: '全局设置管理',
          icon: 'Setting'
        },
        redirect: '/setting/global',
        children: [
          {
            path: 'global',
            name: 'GlobalSetting',
            component: () => import('../views/setting/GlobalSetting.vue'),
            meta: {
              title: '全局设置'
            }
          }
        ]
      },
      // 个人中心路由
      {
        path: 'profile',
        name: 'Profile',
        component: () => import('../views/profile/UserInfo.vue'),
        meta: {
          title: '个人信息',
          hidden: true
        }
      },
      // 修改密码路由
      {
        path: 'change-password',
        name: 'ChangePassword',
        component: () => import('../views/profile/ChangePassword.vue'),
        meta: {
          title: '修改密码',
          hidden: true
        }
      },
      // 服务器详情页面路由
      {
        path: 'servers/server',
        name: 'ServerDetail',
        component: () => import('../views/acs/nodes/server.vue'),
        meta: {
          title: '服务器详情',
          hidden: true
        }
      },
      // 虚拟机详情页面路由
      {
        path: 'servers/vm',
        name: 'VmDetail',
        component: () => import('../views/acs/nodes/VmDetail.vue'),
        meta: {
          title: '虚拟机详情',
          hidden: true
        }
      },
      // 容器详情页面路由
      {
        path: 'servers/container',
        name: 'ContainerDetail',
        component: () => import('../views/acs/nodes/containDetail.vue'),
        meta: {
          title: '容器详情',
          hidden: true
        }
      }, {
        path: 'servers/container/console',
        name: 'ContainerConsole',
        component: () => import('../views/acs/nodes/containerConsole.vue'),
        meta: {
          title: '终端容器',
          hidden: true
        }
      }, {
        path: 'servers/container/files',
        name: 'ContainerFiles',
        component: () => import('../views/acs/nodes/containFile.vue'),
        meta: {
          title: '容器文件管理',
          hidden: true
        }
      }
    ]
  },
  // 登录页
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue'),
    meta: {
      title: '登录'
    }
  },
  // 404 页面
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('../views/NotFound.vue'),
    meta: {
      title: '页面不存在'
    }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 全局前置守卫
router.beforeEach((to, from, next) => {
  // 设置页面标题
  document.title = to.meta.title ? `${to.meta.title} - 007UI管理系统` : '007UI管理系统'

  // 这里可以添加登录验证逻辑
  const isAuthenticated = localStorage.getItem('token')
  if (to.path !== '/login' && !isAuthenticated) {
    next({ path: '/login' })
  } else {
    next()
  }
})

// 全局后置钩子
router.afterEach(() => {
  window.scrollTo(0, 0)
})

export default router 