import { useRouter } from "vue-router";
import { getPersonSummaryInfo } from "@/api/common/index";
const router = useRouter()

// 跳转法案详情
export const goToBill = (id, tabName) => {
  window.sessionStorage.setItem("billId", id);
  window.sessionStorage.setItem("curTabName", tabName);
  const route = router.resolve({
    path: "/billLayout",
    query: {
      billId: id
    }
  });
  window.open(route.href, "_blank");
};

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

// 跳转智库详情
export const goToThinkTank = (id, tabName) => {
  window.sessionStorage.setItem("curTabName", tabName);
  const curRoute = router.resolve({
    name: "ThinkTankDetail",
    params: {
      id: id
    }
  });
  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 goToInstitution = (id, tabName) => {
  window.sessionStorage.setItem('curTabName', tabName)
  const curRoute = router.resolve({
    path: "/institution",
    query: {
      id: id
    }
  });
  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
  }
  // 先获取人物全局信息
  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: {
              personId: item.id
            }
          });
          window.open(route.href, "_blank");
          return;
        }
        const route = router.resolve({
          path: "/characterPage",
          query: {
            type: type, // type=1为科技企业领袖，2为国会议员，3为智库研究人员
            personId: item.id
          }
        });
        window.open(route.href, "_blank");
      } else {
        personTypeName = "";
        const route = router.resolve({
          path: "/characterPage",
          query: {
            personId: item.id
          }
        });
        window.open(route.href, "_blank");
        return;
      }
    } else {
      const route = router.resolve({
        path: "/characterPage",
        query: {
          personId: item.id
        }
      });
      window.open(route.href, "_blank");
      return;
    }
  } catch (error) {
    const route = router.resolve({
      path: "/characterPage",
      query: {
        personId: item.id
      }
    });
    window.open(route.href, "_blank");
    return;
  }
}

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

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

// 跳转搜索详情页
export const goToSearch = (tabName, areaName, billSearchType) => {
  window.sessionStorage.setItem("curTabName", tabName);
	// if (!areaName) return;
	const query = {
		searchText: tabName,
		areaName: areaName
	};
	if (enableBillTypeSwitch) {
		query.billSearchType = billSearchType
	}
	const curRoute = router.resolve({
		path: "/searchResults",
		query
	});
	window.open(curRoute.href, "_blank");
}


// 跳转数据资源库




