/*
 * @Author: your name
 * @Date: 2022-03-03 18:04:40
 * @LastEditTime: 2025-02-28 11:35:07
 * @LastEditors: code4eat awesomedema@gmail.com
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: /KC-MiddlePlatform/src/pages/platform/setting/reports/index.tsx
 */

import { useEffect, useState } from 'react';

import { Skeleton } from 'antd';

import { useModel } from '@umijs/max';
import { getUrlParams } from '@/utils/tooljs';
import { getPlatFormParams } from './service';
import KCIMPagecontainer from '@/components/KCIMPageContainer';


const findPage = (tree:any[], pathname:string):any => {
  for (let node of tree) {
    // 检查当前节点的路径是否匹配
    if (`/MediResourceManaSys${node.path}` === pathname) {
      return node; // 找到匹配的节点
    }
    // 如果当前节点有子节点，递归查找
    if (node.children && node.children.length > 0) {
      const found = findPage(node.children, pathname);
      if (found) return found;
    }
  }
  return null; // 如果没有找到匹配的节点
};

export default () => {
  const { initialState } = useModel('@@initialState');
  const [specialPageUrl, setspecialPageUrl] = useState<string | undefined>(undefined);
  const [loading, setloading] = useState(false);
  const { pathname } = location;

  
  

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

  const getHost = async ()=>{
       const resp = await getPlatFormParams('1547394914533380096','1788836132977512448');
       if(resp){
          return resp
       }
  }

  const loadPage = ()=>{
    
    if (initialState && initialState.spacicalPageParamsType && initialState.userData) {
      const {
        spacicalPageParamsType,
        userData: { youshuToken },
      } = initialState;

  
      
      
      const spacicalPage = findPage(spacicalPageParamsType, pathname);
      // console.log({pathname,spacicalPageParamsType,spacicalPage});
  
      if (spacicalPage) {
        //当前页面属于有数数据展示页面
        const url = `${spacicalPage.softUrl}`;

        const {noMenu=undefined,noTopbar=undefined} = getUrlParams(`http://localhost:8000${url}`);
        if(noMenu == 'true'&&noTopbar == 'true'){
          getHost().then((val)=>{
                setspecialPageUrl(`${val.value}${url}&userData=${localStorage.getItem('userData')}`);
          })
        }else{
          setspecialPageUrl(url);
        }
        setloading(false); 
      }
    }
  }


  useEffect(() => {
    setloading(true); 
    loadPage();
  }, [pathname,initialState]);

  return (
    <KCIMPagecontainer style={{padding:0}} title={false}>
      <Skeleton loading={loading} paragraph={{ rows: 50 }} active />
      <iframe
        onLoad={() => onLoadhandle()}
        style={{ width: '100%', height: '100vh', border: 'none' }}
        src={specialPageUrl}
      />
    </KCIMPagecontainer>
  );
};
