import { getPersonSummaryInfo } from "@/api/common/index";
import router from "@/router/index";

// 跳转法案详情
export const goToBill = (id, tabName) => {
  window.sessionStorage.setItem("billId", id);
  window.sessionStorage.setItem("curTabName", tabName);

  const param = {
    billId: id
  }
  const jsonStr = JSON.stringify(param);
  const base64 = btoa(encodeURIComponent(jsonStr));

  const route = router.resolve({
    path: "/billLayout",
    query: {
      data: base64
    }
  });
  window.open(route.href, "_blank");
};

// 跳转政令详情
export const goToDecree = (id, tabName) => {
  window.sessionStorage.setItem("decreeId", id);
  window.sessionStorage.setItem("curTabName", tabName);
  const param = {
    id: id
  }
  const jsonStr = JSON.stringify(param);
  const base64 = btoa(encodeURIComponent(jsonStr));
  const route = router.resolve({
    path: "/decreeLayout",
    query: {
      data: base64
    }
  });
  window.open(route.href, "_blank");
};

// 跳转智库详情
export const goToThinkTank = (id, tabName) => {
  window.sessionStorage.setItem("curTabName", tabName);
  const base64 = btoa(encodeURIComponent(id));
  const curRoute = router.resolve({
    name: "ThinkTankDetail",
    params: {
      id: base64
    }
  });
  window.open(curRoute.href, "_blank");
}

// 跳转智库报告详情
export const goToThinkTankReport = (id, tabName) => {
  window.sessionStorage.setItem("curTabName", tabName);
  const route = router.resolve({
    name: "ReportDetail",
    params: {
      id: id
    }
  });
  window.open(route.href, "_blank");
}

// 跳转实体清单
export const goToEntityList = () => {
  const param = {
    sanTypeId: 1
  }
  const jsonStr = JSON.stringify(param);
  const base64 = btoa(encodeURIComponent(jsonStr));
  const route = router.resolve({
    path: '/exportControl/entityList',
    query: {
      data: base64
    }
  });
  window.open(route.href, "_blank");
}

// 跳转实体清单事件
export const goToSingleEntityEvent = (id, tabName, date) => {
  window.sessionStorage.setItem("curTabName", tabName);

  const param = {
    id: id,
    sanTypeId: 1,
    date: date
  }
  const jsonStr = JSON.stringify(param);
  const base64 = btoa(encodeURIComponent(jsonStr));

  const route = router.resolve({
    path: "/exportControl/singleSanction",
    query: {
      data: base64
    }
  });
  window.open(route.href, "_blank");
}

// 跳转商业管制清单
export const goToCCLList = () => {
  const param = {
    sanTypeId: 13,
  }
  const jsonStr = JSON.stringify(param);
  const base64 = btoa(encodeURIComponent(jsonStr));
  const route = router.resolve({
    path: "/exportControl/commercialControlList",
    query: {
      data: base64
    }
  });
  window.open(route.href, "_blank");
}

// 跳转SDN清单
export const goToSDNList = () => {
  const param = {
    sanTypeId: 2,
  }
  const jsonStr = JSON.stringify(param);
  const base64 = btoa(encodeURIComponent(jsonStr));
  const route = router.resolve({
    path: "/finance/sdnlistoverview",
    query: {
      data: base64
    }
  });
  window.open(route.href, "_blank");
}

// 跳转SDN清单事件
export const goToSingleSDNEvent = (id, tabName, date) => {
  window.sessionStorage.setItem("curTabName", tabName);
  const param = {
    id: id,
    sanTypeId: 2,
    date: date
  }
  const jsonStr = JSON.stringify(param);
  const base64 = btoa(encodeURIComponent(jsonStr));
  const route = router.resolve({
    path: "/finance/singleSanction",
    query: {
      data: base64
    }
  });
  window.open(route.href, "_blank");
}

// 跳转232调查概览页
export const goToCase232 = () => {
  window.sessionStorage.setItem("curTabName", '232调查');
  const param = {
    id: 232,
  }
  const jsonStr = JSON.stringify(param);
  const base64 = btoa(encodeURIComponent(jsonStr));

  const route = router.resolve({
    path: "/marketAccessLayout/case",
    query: {
      data: base64
    }
  });
  window.open(route.href, "_blank");
}

// 跳转232调查详情页
export const goToCaseDetail232 = (searchId, tabName) => {
  window.sessionStorage.setItem("curTabName", tabName);
  const param = {
    id: 232,
    searchId: searchId
  }
  const jsonStr = JSON.stringify(param);
  const base64 = btoa(encodeURIComponent(jsonStr));

  const route = router.resolve({
    path: "/marketSingleCaseLayout/overview",
    query: {
      data: base64
    }
  });
  window.open(route.href, "_blank");
}

// 跳转301调查概览页
export const goToCase301 = () => {
  window.sessionStorage.setItem("curTabName", '301调查');
  const param = {
    id: 301,

  }
  const jsonStr = JSON.stringify(param);
  const base64 = btoa(encodeURIComponent(jsonStr));
  const route = router.resolve({
    path: "/marketAccessLayout/case",
    query: {
      data: base64
    }
  });
  window.open(route.href, "_blank");
}

// 跳转301调查详情页
export const goToCaseDetail301 = (searchId, tabName) => {
  window.sessionStorage.setItem("curTabName", tabName);
  const param = {
    id: 301,
    searchId: searchId
  }
  const jsonStr = JSON.stringify(param);
  const base64 = btoa(encodeURIComponent(jsonStr));
  const route = router.resolve({
    path: "/marketSingleCaseLayout/overview",
    query: {
      data: base64
    }
  });
  window.open(route.href, "_blank");
}

// 跳转337调查概览页
export const goToCase337 = () => {
  window.sessionStorage.setItem("curTabName", '337调查');
  const param = {
    id: 337,
  }
  const jsonStr = JSON.stringify(param);
  const base64 = btoa(encodeURIComponent(jsonStr));
  const route = router.resolve({
    path: "/marketAccessLayout/case",
    query: {
      data: base64
    }
  });
  window.open(route.href, "_blank");
}

// 跳转337调查详情页
export const goToCaseDetail337 = (searchId, tabName) => {
  window.sessionStorage.setItem("curTabName", tabName);
  const param = {
    id: 337,
    searchId: searchId
  }
  const jsonStr = JSON.stringify(param);
  const base64 = btoa(encodeURIComponent(jsonStr));
  const route = router.resolve({
    path: "/marketSingleCaseLayout/overview",
    query: {
      data: base64
    }
  });
  window.open(route.href, "_blank");
}

// 跳转机构详情
export const goToInstitution = (id, tabName) => {
  window.sessionStorage.setItem('curTabName', tabName)
  const param = {
    id: id
  }
  const jsonStr = JSON.stringify(param);
  const base64 = btoa(encodeURIComponent(jsonStr));
  const curRoute = router.resolve({
    path: "/institution",
    query: {
      data: base64
    }
  });
  window.open(curRoute.href, "_blank");
}

// 跳转人物详情
export const goToCharacterPage = async (id, tabName) => {
  window.sessionStorage.setItem('curTabName', tabName)
  const personTypeList = JSON.parse(window.sessionStorage.getItem("personTypeList"));
  let type
  let personTypeName
  const params = {
    personId: id
  }
  const param1 = {
    personId: item.id
  }
  const jsonStr1 = JSON.stringify(param1);
  const base64_1 = btoa(encodeURIComponent(jsonStr1));

  const param2 = {
    personId: item.id
  }
  const jsonStr2 = JSON.stringify(param2);
  const base64_2 = btoa(encodeURIComponent(jsonStr2));

  // 先获取人物全局信息
  try {
    const res = await getPersonSummaryInfo(params)
    if (res.code === 200 && res.data) {
      const arr = personTypeList.filter(item => {
        return item.typeId === res.data.personType;
      })
      if (arr && arr.length) {
        personTypeName = arr[0].typeName;
        if (personTypeName === "科技企业领袖") {
          type = 1;
        } else if (personTypeName === "国会议员") {
          type = 2;
        } else if (personTypeName === "智库研究人员") {
          type = 3;
        } else {
          personTypeName = "其他类型";
          const route = router.resolve({
            path: "/characterPage",
            query: {
              data: base64_1
            }
          });
          window.open(route.href, "_blank");
          return;
        }
        const route = router.resolve({
          path: "/characterPage",
          query: {
            data: base64_2
          }
        });
        window.open(route.href, "_blank");
      } else {
        personTypeName = "";
        const route = router.resolve({
          path: "/characterPage",
          query: {
            data: base64_1
          }
        });
        window.open(route.href, "_blank");
        return;
      }
    } else {
      const route = router.resolve({
        path: "/characterPage",
        query: {
          data: base64_1
        }
      });
      window.open(route.href, "_blank");
      return;
    }
  } catch (error) {
    const route = router.resolve({
      path: "/characterPage",
      query: {
        data: base64_1
      }
    });
    window.open(route.href, "_blank");
    return;
  }
}

// 跳转企业
export const goToCompanyPage = (id, tabName) => {
  window.sessionStorage.setItem('curTabName', tabName)
  const param = {
    id: id
  }
  const jsonStr = JSON.stringify(param);
  const base64 = btoa(encodeURIComponent(jsonStr));
  const route = router.resolve({
    name: "companyPages",
    params: base64
  });
  window.open(route.href, "_blank");
}

// 跳转新闻详情
export const goToNewsPage = (id, tabName) => {
  window.sessionStorage.setItem("curTabName", tabName);
  const param = {
    newsId: id
  }
  const jsonStr = JSON.stringify(param);
  const base64 = btoa(encodeURIComponent(jsonStr));
  const route = router.resolve({
    path: "/newsAnalysis",
    query: {
      data: base64
    }
  });
  window.open(route.href, "_blank");
}

// 跳转搜索详情页
export const goToSearch = (tabName, areaName, billSearchType) => {
  window.sessionStorage.setItem("curTabName", `搜索-${tabName}`);

  let selectParam
  if (billSearchType) {
    selectParam = {
      searchText: tabName,
      areaName: areaName,
      billSearchType: billSearchType
    }
  } else {
    selectParam = {
      searchText: tabName,
      areaName: areaName,
    }
  }
  // const codeParam = new URLSearchParams(selectParam)
  const jsonStr = JSON.stringify(selectParam);
  const base64 = btoa(encodeURIComponent(jsonStr));

  const curRoute = router.resolve({
    path: "/searchResults",
    query: {
      data: base64
    }
  });
  window.open(curRoute.href, "_blank");

  // const codeParam = new URLSearchParams(selectParam)

}


// 跳转数据资源库-国家法案
export const goToDataCountryBill = (selectParam) => {
  // const codeParam = new URLSearchParams(selectParam)
  // JSON -> Base64
  const jsonStr = JSON.stringify(selectParam);
  // 处理中文：先 encodeURIComponent
  const base64 = btoa(encodeURIComponent(jsonStr));
  const route = router.resolve({
    path: "/dataLibrary/countryBill",
    query: {
      data: base64
    }
  });
  window.open(route.href, "_blank");
}

// 跳转数据资源库-政令
export const goToDataDecree = (selectParam) => {
  // JSON -> Base64
  const jsonStr = JSON.stringify(selectParam);
  // 处理中文：先 encodeURIComponent
  const base64 = btoa(encodeURIComponent(jsonStr));
  const route = router.resolve({
    path: "/dataLibrary/dataDecree",
    query: {
      data: base64
    }
  });
  window.open(route.href, "_blank");
}

// 跳转数据资源库-智库
export const goToDataThinkTank = (selectParam) => {
  // JSON -> Base64
  const jsonStr = JSON.stringify(selectParam);
  // 处理中文：先 encodeURIComponent
  const base64 = btoa(encodeURIComponent(jsonStr));
  const route = router.resolve({
    path: "/dataLibrary/dataThinkTank",
    query: {
      data: base64
    }
  });
  window.open(route.href, "_blank");
}

// 跳转数据资源库-出口管制-实体清单
export const goToDataEntityList = (selectParam) => {
  // JSON -> Base64
  const jsonStr = JSON.stringify(selectParam);
  // 处理中文：先 encodeURIComponent
  const base64 = btoa(encodeURIComponent(jsonStr));
  const route = router.resolve({
    path: "/dataLibrary/dataEntityList",
    query: {
      data: base64
    }
  });
  window.open(route.href, "_blank");
}

// 跳转数据资源库-出口管制-商业管制清单
export const goToDataCCLList = (selectParam) => {
  // JSON -> Base64
  const jsonStr = JSON.stringify(selectParam);
  // 处理中文：先 encodeURIComponent
  const base64 = btoa(encodeURIComponent(jsonStr));
  const route = router.resolve({
    path: "/dataLibrary/dataCommerceControlList",
    query: {
      data: base64
    }
  });
  window.open(route.href, "_blank");
}

// 跳转数据资源库-出口管制-实体清单事件
export const goToDataSingleEntityEvent = (selectParam) => {
  // JSON -> Base64
  const jsonStr = JSON.stringify(selectParam);
  // 处理中文：先 encodeURIComponent
  const base64 = btoa(encodeURIComponent(jsonStr));
  const route = router.resolve({
    path: "/dataLibrary/dataEntityListEvent",
    query: {
      data: base64
    }
  });
  window.open(route.href, "_blank");
}

// 跳转数据资源库-出口管制-商业管制清单事件
export const goToDataSingleCCLEvent = (selectParam) => {
  // JSON -> Base64
  const jsonStr = JSON.stringify(selectParam);
  // 处理中文：先 encodeURIComponent
  const base64 = btoa(encodeURIComponent(jsonStr));
  const route = router.resolve({
    path: "/dataLibrary/dataCommerceControlListEvent",
    query: {
      data: base64
    }
  });
  window.open(route.href, "_blank");
}


// 跳转数据资源库-投融资限制-SDN清单
export const goToDataSDNList = (selectParam) => {
  // JSON -> Base64
  const jsonStr = JSON.stringify(selectParam);
  // 处理中文：先 encodeURIComponent
  const base64 = btoa(encodeURIComponent(jsonStr));
  const route = router.resolve({
    path: "/dataLibrary/sDNList",
    query: {
      data: base64
    }
  });
  window.open(route.href, "_blank");
}

// 跳转数据资源库-投融资限制-涉军企业清单
export const goToDataMREList = (selectParam) => {
  // JSON -> Base64
  const jsonStr = JSON.stringify(selectParam);
  // 处理中文：先 encodeURIComponent
  const base64 = btoa(encodeURIComponent(jsonStr));
  const route = router.resolve({
    path: "/dataLibrary/mREList",
    query: {
      data: base64
    }
  });
  window.open(route.href, "_blank");
}

// 跳转数据资源库-投融资限制-SDN清单事件
export const goToDataSingleSDNEvent = (selectParam) => {
  // JSON -> Base64
  const jsonStr = JSON.stringify(selectParam);
  // 处理中文：先 encodeURIComponent
  const base64 = btoa(encodeURIComponent(jsonStr));
  const route = router.resolve({
    path: "/dataLibrary/sDNListEvent",
    query: {
      data: base64
    }
  });
  window.open(route.href, "_blank");
}

// 跳转数据资源库-投融资限制-涉军企业清单事件
export const goToDataSingleMREEvent = (selectParam) => {
  // JSON -> Base64
  const jsonStr = JSON.stringify(selectParam);
  // 处理中文：先 encodeURIComponent
  const base64 = btoa(encodeURIComponent(jsonStr));
  const route = router.resolve({
    path: "/dataLibrary/mREListEvent",
    query: {
      data: base64
    }
  });
  window.open(route.href, "_blank");
}

// 跳转数据资源库-科技人物
export const goToDataTechnologyFigures = (selectParam) => {
  // JSON -> Base64
  const jsonStr = JSON.stringify(selectParam);
  // 处理中文：先 encodeURIComponent
  const base64 = btoa(encodeURIComponent(jsonStr));
  const route = router.resolve({
    path: "/dataLibrary/dataTechnologyFigures",
    query: {
      data: base64
    }
  });
  window.open(route.href, "_blank");
}

// 跳转数据资源库-重要实体-科技企业
export const goToDataCompany = (selectParam) => {
  // JSON -> Base64
  const jsonStr = JSON.stringify(selectParam);
  // 处理中文：先 encodeURIComponent
  const base64 = btoa(encodeURIComponent(jsonStr));
  const route = router.resolve({
    path: "/dataLibrary/dataCompany",
    query: {
      data: base64
    }
  });
  window.open(route.href, "_blank");
}

// 跳转数据资源库-重要实体-机构
export const goToDataInstitution = (selectParam) => {
  // JSON -> Base64
  const jsonStr = JSON.stringify(selectParam);
  // 处理中文：先 encodeURIComponent
  const base64 = btoa(encodeURIComponent(jsonStr));
  const route = router.resolve({
    path: "/dataLibrary/dataInstitution",
    query: {
      data: base64
    }
  });
  window.open(route.href, "_blank");
}

// 跳转数据资源库-新闻
export const goToDataNews = (selectParam) => {
  // JSON -> Base64
  const jsonStr = JSON.stringify(selectParam);
  // 处理中文：先 encodeURIComponent
  const base64 = btoa(encodeURIComponent(jsonStr));
  const route = router.resolve({
    path: "/dataLibrary/dataNews",
    query: {
      data: base64
    }
  });
  window.open(route.href, "_blank");
}

// 解码-------------------------------------------!!!!!
export const getDecodedParams = () => {
  if (window.location.search) {
    const urlParams = new URLSearchParams(window.location.search)
    const encoded = urlParams.get('data')
    if (!encoded) return null;
    try {
      // Base64 解码
      const decoded = atob(encoded);
      const jsonStr = decodeURIComponent(decoded);
      return JSON.parse(jsonStr);
    } catch (e) {
      console.error('解码失败', e);
      return null;
    }
  } else {
    const obj = router.currentRoute.value.params
    const encoded = Object.values(obj)[0]
    if (!encoded) return null;
    try {
      // Base64 解码
      const decoded = atob(encoded);
      const jsonStr = decodeURIComponent(decoded);
      return jsonStr;
    } catch (e) {
      console.error('解码失败', e);
      return null;
    }
  }
}




