import React, {useEffect, useState, useRef, memo} from 'react';
import ZdyTable from './ZdyTable';
import {isJSON} from '@/webPublic/one_stop_public/copy';
import {uaaRequest} from '@/webPublic/one_stop_public/utils/request';
import {Modal, message, Spin} from 'antd';

export default function PrentIndex(props) {
  const [prepareShow, setPrepareShow] = useState(false);

  useEffect(() => { // 一个页面只能执行一次. 因为是全局共享的数据
    let data = props?.postData?.unifiedServicePatternModel?.content;
    if (data && isJSON(data)) {
      // 模板挂载时公式执行
      let g = JSON.parse(data)?.templateWillMount;
      if (g && typeof g === 'string' && g.length > 10 && !window.zdyTableTemplateWillMountProps) {
        window.zdyTableTemplateWillMountProps = 'init';
        let p = {
          message,
          Modal,
          uaaRequest,
          userNowInfo:
            (isJSON(localStorage.getItem('user')) && JSON.parse(localStorage.getItem('user'))) ||
            {},
          templateData: props.postData,
          routerState: props?.routerState,
        };
        let newFunc = new Function('props', g);
        console.log('开始执行templateWillMount公式');
        try {
          newFunc(p)
            .then(res => {
              if (res) {
                window.zdyTableTemplateWillMountProps = res;
                setPrepareShow(true);
              }
            })
            .catch(error => {
              window.zdyTableTemplateWillMountProps = undefined;
              setPrepareShow(true);
              console.log('模板挂载时公式执行失败', error);
            });
        } catch (e) {
          console.log(e);
          setPrepareShow(true);
        }
      } else {
        setPrepareShow(true);
      }
    } else {
      setPrepareShow(true);
    }
  }, []);

  return prepareShow ? <ZdyTable {...props} /> : <Spin size={'large'}/>;
}
