// 每个站点允许的路由 (保持不变)
export const SITE_ROUTES: Record<any, string[]> = {
  www: ['/', '/index', '/indexData', '/theme', '/diy'], //优易365主站
  b: ['/indexB'], //企业购商城
  mro: ['/indexMro'], //工业品商城
  fuli: ['/indexFuli'], //福礼商城
  reg: ['/reg'], //个人注册
  breg: ['/breg'], //企业注册
  greg: ['/greg'], //供应商注册
  passport: ['/login'], //登录页
  search: ['/search', '/search/special', '/search/brand'], //搜索
  item: ['/item'], //商品详情,
  cart: ['/cart'], //商品详情
  trad: ['/trad'], //确认订单信息
  payc: ['/payc'], //支付订单

  plan: ['/plan', '/plan/procure', '/plan/guide', '/plan/project'], //解决方案
  plan_info: ['/plan_info', '/plan_info/procure', '/plan_info/guide', '/plan_info/project', '/solve/real'], //信息展示

  order: [
    '/order/orderManage',
    '/order/orderManage/detail',
    '/order/orderAudit',
    '/order/afterSale',
    '/order/batchOrder',
    '/order/orderEvaluation',
    '/enterprise/companyInfo',
    '/enterprise/companyInfo/edit',
    '/enterprise/purchaseHabit',
    '/enterprise/invoiceManage',
    '/enterprise/agreementSupply',
    '/enterprise/companyInfo',
    '/enterprise/invoiceManage',
    '/enterprise/myCollection',
    '/enterprise/myFootprint',
    '/enterprise/purchaseHabit',
    '/enterprise/purchasePlan',
    '/enterprise/purchaseHistory',
    '/reconciliation/billManage',
    '/reconciliation/invoiceManage',
    '/organization/deptManage',
    '/organization/staffManage',
    '/organization/roleManage',
    '/valueAdded/maintenance',
    '/valueAdded/complaint',
    '/cost/itemExpense',
    '/cost/quotaControl',
    '/cost/quotaControl/apply',
    '/enterprise/purchasePlan',
    '/organization/approvalFlow',
    '/organization/approvalFlow/create',
    '/order/orderManage/detail/:orderNo',
    '/order/orderManage/applyAfter',
    '/valueAdded/maintenanceApply',
    '/enterprise/messageNotice',
    '/enterprise/securitySetting',
    '/enterprise/securitySetting/resetPassword',
    '/enterprise/securitySetting/changePhone',
    '/enterprise/changePerson',
    '/order/orderEvaluation/evaluation'
  ], //订单列表

  i: ['/i'], //个人信息
  easybuv: ['/easybuv'] //地址管理
};

// 获取当前站点
export function getCurrentSite(): any {
  // 无论是生产还是开发，现在都统一通过域名判断
  const host = window.location.hostname;

  // 定义本地开发环境的域名映射关系
  // 确保你的 hosts 文件已经配置了这些域名指向 127.0.0.1
  if (host === 'index.xiaoluwebsite.xyz' || host === 'localhost') return 'www'; // 兼容未配hosts的情况
  if (host === 'b.xiaoluwebsite.xyz') return 'b';
  if (host === 'mro.xiaoluwebsite.xyz') return 'mro';
  if (host === 'fuli.xiaoluwebsite.xyz') return 'fuli';
  if (host === 'reg.xiaoluwebsite.xyz') return 'reg';
  if (host === 'breg.xiaoluwebsite.xyz') return 'breg';
  if (host === 'greg.xiaoluwebsite.xyz') return 'greg';
  if (host === 'pass.xiaoluwebsite.xyz') return 'passport';
  if (host === 'search.xiaoluwebsite.xyz') return 'search';
  if (host === 'item.xiaoluwebsite.xyz') return 'item';
  if (host === 'cart.xiaoluwebsite.xyz') return 'cart';
  if (host === 'trad.xiaoluwebsite.xyz') return 'trad';
  if (host === 'payc.xiaoluwebsite.xyz') return 'payc';
  if (host === 'order.xiaoluwebsite.xyz') return 'order';
  if (host === 'plan.xiaoluwebsite.xyz') return 'plan';
  if (host === 'planinfo.xiaoluwebsite.xyz') return 'plan_info';
  if (host === 'i.xiaoluwebsite.xyz') return 'i';
  if (host === 'easybuv.xiaoluwebsite.xyz') return 'easybuv';

  // 生产环境逻辑 (保持不变，或者合并到上面的判断中)
  if (import.meta.env.PROD) {
    // 如果上面没匹配到，且是生产环境，可以尝试原有的逻辑或默认返回 www
    // 这里建议直接复用上面的 hostname 判断，因为生产环境也是域名
    return 'www';
  }

  return 'www'; // 默认 fallback
}

// ... PATH_TO_SITE_MAP 和 getSiteByPath 保持不变 ...
const PATH_TO_SITE_MAP: Record<string, any> = {};
for (const [site, paths] of Object.entries(SITE_ROUTES)) {
  for (const path of paths) {
    PATH_TO_SITE_MAP[path] = site as any;
  }
}
export function getSiteByPath(path: string): any | null {
  const cleanPath = path.split('?')[0];
  return PATH_TO_SITE_MAP[cleanPath] || null;
}

// 跨站跳转逻辑
import router from '@/router';
export function onPath(path: string) {
  console.log('[跨站跳转]', path);
  // return
  if (import.meta.env.VITE_DOMAIN_NAME == 'true') {
    const targetSite = getSiteByPath(path);

    if (!targetSite) {
      console.error(`[跨站跳转失败] 路径 "${path}" 未关联任何站点`);
      return;
    }

    const currentSite = getCurrentSite();

    // 【新增】判断是否是同域名
    const isSameSite = currentSite === targetSite;

    let url = '';
    // 域名映射表 (保持不变)
    const domainMap: Record<string, string> = {
      www: 'index.xiaoluwebsite.xyz',
      b: 'b.xiaoluwebsite.xyz',
      mro: 'mro.xiaoluwebsite.xyz',
      fuli: 'fuli.xiaoluwebsite.xyz',
      reg: 'reg.xiaoluwebsite.xyz',
      breg: 'breg.xiaoluwebsite.xyz',
      greg: 'greg.xiaoluwebsite.xyz',
      passport: 'pass.xiaoluwebsite.xyz',
      search: 'search.xiaoluwebsite.xyz',
      item: 'item.xiaoluwebsite.xyz',
      cart: 'cart.xiaoluwebsite.xyz',
      trad: 'trad.xiaoluwebsite.xyz',
      payc: 'payc.xiaoluwebsite.xyz',
      order: 'order.xiaoluwebsite.xyz',
      plan: 'plan.xiaoluwebsite.xyz',
      plan_info: 'planinfo.xiaoluwebsite.xyz',
      i: 'i.xiaoluwebsite.xyz',
      easybuv: 'easybuv.xiaoluwebsite.xyz'
    };

    const baseDomain = domainMap[targetSite];

    if (import.meta.env.PROD) {
      url = `https://${baseDomain}${path}`;
    } else {
      const devPort = window.location.port || import.meta.env.VITE_APP_PORT;
      url = `https://${baseDomain}:${devPort}${path}`;
    }
    // 【修改】根据是否同域名决定跳转方式
    if (isSameSite) {
      // 同域名且传入了 router，使用 Vue Router 内部跳转
      router.push(path);
    } else {
      // 跨域名 或 未传入 router，使用 window.open 打开新窗口
      window.open(url, '_blank');
    }
  } else {
    router.push(path);
  }
}
