import React, { useEffect, useState, createContext, useRef } from 'react';
import CheckRecord from '@/webPublic/FormInsertDiy/ExportComponent/CheckRecord'; // 查询类
import Apply from '@/webPublic/FormInsertDiy/AffairPage/ApplyPage'; // 申请
import BatchAudit from '@/webPublic/FormInsertDiy/AffairPage/AuditPage/BatchAudit'; // 批量审批
import Audit from '@/webPublic/FormInsertDiy/AffairPage/AuditPage/index.js'; // 审批
import Detail from '@/webPublic/FormInsertDiy/AffairPage/Detail'; //详情
import { Modal, message, notification, Popconfirm } from 'antd';
import { connect } from 'dva';
import config from '@/config/config';
import { getToken } from '@/webPublic/one_stop_public/utils/getToken';

export const CreateC = createContext({
	routerConfig: {},
});

window.iframeParentComponent = {
	Modal,
	message,
	notification,
	Popconfirm,
};

function ActiveMenuComponent({ routerConfig, ...otherProps }) {
	const { component = '', appId } = routerConfig;
	const refType = useRef();
	const { location } = otherProps;
	const record = location?.state?.record;
	const [show, setShow] = useState(false);
	useEffect(
		() => {
			if ((appId || record.id) && refType.current?.component === component) { // 当同一个组件渲染时 切换show 保证重新调接口并渲染页面
				setShow(false);
				setTimeout(() => {
					setShow(true);
				}, 100);
			} else {
				setShow(true);
			}
			refType.current = routerConfig;
		},
		[appId, record],
	);
	if (!show) {
		return null;
	}

	let Res = <div>暂无此功能</div>;

	switch (component) {
		case 'CheckRecord': // 查询类应用
			Res = <CheckRecord workId={appId} />;
			break;
		case 'Apply': // 申请类
			Res = <Apply workId={appId}
                   {...otherProps}
      />;
			break;
		case 'Audit': // 审批类
			Res = <Audit workId={appId} {...otherProps} />;
			break;
		case 'BatchAudit': // 批量审批
			Res = <BatchAudit workId={appId} {...otherProps} />;
			break;
		case 'Detail': // 详情
			Res = <Detail {...otherProps} />;
			break;
		default:
			break;
	}
	return (
		<CreateC.Provider
			value={{
				routerConfig,
			}}>
			{Res}
		</CreateC.Provider>
	);
}

export default connect(({}) => {
	return {};
})(ActiveMenuComponent);
