/*
 * @Author: code4eat awesomedema@gmail.com
 * @Date: 2023-03-03 11:30:33
 * @LastEditors: code4eat awesomedema@gmail.com
 * @LastEditTime: 2024-09-13 12:13:10
 * @FilePath: /KC-MiddlePlatform/src/pages/platform/setting/pubDicTypeMana/index.tsx
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

import { createFromIconfontCN } from '@ant-design/icons';

import { Skeleton } from 'antd';
import { useEffect, useRef, useState } from 'react';
import { useLocation } from 'umi';

import KCIMPagecontainer from '@/components/KCIMPageContainer';

const IconFont = createFromIconfontCN({
  scriptUrl: '',
});

function getQueryParams() {
  const queryString = window.location.search; // 获取 location.search 的值

  // 使用正则表达式匹配 softUrl 的完整值
  const softUrlMatch = queryString.match(/softUrl=([^&]+(?:&[^=]+=true)*)/); // 改进后的正则表达式

  let softUrl = ''; // 用于存储 softUrl 的值

  if (softUrlMatch) {
    // 获取匹配到的第一个捕获组（即 softUrl 的值），并进行解码
    softUrl = decodeURIComponent(softUrlMatch[1]);
  }

  // console.log({ softUrl, queryString, softUrlMatch }); // 打印完整的 softUrl

  return { softUrl }; // 返回带有完整 softUrl 的对象
}

export default function StaticPage() {
  const [specialPageUrl, setspecialPageUrl] = useState<string | undefined>(
    undefined,
  );
  const [loading, setloading] = useState(false);
  const { softUrl } = getQueryParams();
  const location = useLocation();
  const [scale, set_scale] = useState(0.5);
  const [translate, set_translate] = useState({ x: 0, y: 0 });

  const onLoadhandle = () => {
    setloading(false);
  };

  const adjustIframeScale = () => {
    const desiredWidth = 1200;
    const windowWidth = window.innerWidth - 232;
    let scale = windowWidth / desiredWidth;
    // scale = Math.min(scale, 1); // 限制最大缩放比例为1
    const translateX = (windowWidth - desiredWidth * scale) / 2 / scale;
    const translateY = 0; // 如果需要，也可以计算垂直平移
    set_translate({ x: translateX, y: translateY });
    set_scale(scale);
  };

  useEffect(() => {
    setloading(true);
    setspecialPageUrl(softUrl ? softUrl : '');
    console.log({ softUrl });
  }, [location]);

  useEffect(() => {
    adjustIframeScale();
    window.addEventListener('resize', adjustIframeScale);
    return () => {
      window.removeEventListener('resize', adjustIframeScale);
    };
  }, []);

  return (
    <KCIMPagecontainer className="StaticPage" title={false}>
      <Skeleton loading={loading} paragraph={{ rows: 50 }} active />
      <iframe
        onLoad={() => onLoadhandle()}
        style={{
          width: '100%',
          height: 'calc(100vh - 80px)',
          border: 'none',
          overflow: 'hidden',
        }}
        src={specialPageUrl}
      ></iframe>
    </KCIMPagecontainer>
  );
}
