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

export default function prepareShow(postData = {} , content = '') { // 模板挂载前执行的公式
  let agg = deepCopy(postData);
  return new Promise((resolve, reject) => {
    let data = agg?.unifiedServicePatternModel?.content || content;
    let g = !!data && isJSON(data) && JSON.parse(data)?.templateWillMount;
    if (g && typeof g === 'string' && g.length > 10) {
      let p = { // 函数参数
        message,
        Modal,
        uaaRequest,
        userNowInfo:
          (isJSON(localStorage.getItem('user')) && JSON.parse(localStorage.getItem('user'))) ||
          {},
        templateData: agg,
      };
      let newFunc = new Function('props', g);
      console.log('开始执行templateWillMount公式');
      window.zdyTableTemplateWillMountInit = true;
      try {
        newFunc(p) // 返回一个promise
          .then(res => {
            if (res) {
              console.log('执行templateWillMount公式完成', res);
              window.zdyTableTemplateWillMountProps = res;
              resolve(true);
            }
          })
          .catch(error => {
            window.zdyTableTemplateWillMountProps = undefined;
            resolve(true);
            console.log('模板挂载时公式执行失败', error);
          });
      } catch (e) {
        resolve(true);
        console.log(e);
      }
    }else{
      resolve(true);
    }
  });
}
