import RenderForm from "@/webPublic/FormInsertDiy/RenderForm";
import { fetchTemplateById } from '@/webPublic/Services';
import { useState, useEffect } from "react";
import { Form } from 'antd';
import { getOneStopConfig } from "@/baseComponent/utils";
import { setToken } from '@/utils/authority';

const getUrlInfo = (param) => {
  let url = window.document.location.href.toString();
  let u = url.split("?");
  if (typeof(u[1]) == "string") {
    u = u[1].split("&");
    let get = {};
    for (let i in u) {
      let j = u[i].split("=");
      get[j[0]] = decodeURIComponent(j[1]);
    }
    return get;
  } else {
    return {};
  }
};

function OtherCheckRecord({ form, location: { pathname }, ...props }) {
  const [data, setData]  = useState();
  useEffect(() => {
    let params = getUrlInfo();
    let id = params.id || props.id;
    const { token, isMobile} = params;
    if(token){
      setToken(token);
    }
    if(!id){
      console.error('没有找到id');
    }
    if(isMobile){
      document.body.style.minWidth = 'auto'; // 移动端宽度自适应
    }
    setData(undefined);
    fetchTemplateById(id).then(res => {
      if (res) setData({...res, content: "{}"});
    });
  }, [pathname]);

  return !!data && <RenderForm form={form}
                               postData={data}
                               style={{ padding: 0 }} />;
}

export default  Form.create()(OtherCheckRecord)
