import {  Prisma, StaffDto } from "@nice/common";
import React, {
	createContext,
	ReactNode,
	useContext,
	useMemo,
	useState,
} from "react";
// import { useDebounce } from "use-debounce";
import { Form, FormInstance } from 'antd';


interface MainContextType {
	searchValue?: string;
    setSearchValue: React.Dispatch<React.SetStateAction<string>>;
	formValue?: {[key: string]: any};
    setFormValue: React.Dispatch<React.SetStateAction<{[key: string]: any}>>;
    form: FormInstance; // 新增表单实例
    visible: boolean;
    setVisible: React.Dispatch<React.SetStateAction<boolean>>;
    editingRecord?: StaffDto | null;
    setEditingRecord: React.Dispatch<React.SetStateAction<StaffDto | null>>;
}

const MainContext = createContext<MainContextType | null>(null);
interface MainProviderProps {
	children: ReactNode;
}

export function MainProvider({ children }: MainProviderProps) {
	const [searchValue, setSearchValue] = useState<string>("");
	const [formValue, setFormValue] = useState<{[key: string]: any}>({});
    const [form] = Form.useForm(); // 添加AntD表单实例
    const [visible, setVisible] = useState<boolean>(false);
    const [editingRecord, setEditingRecord] = useState<StaffDto | null>(null);
	return (
		<MainContext.Provider
			value={{
				searchValue,
                setSearchValue,
				formValue,
				setFormValue,
				form, // 暴露表单实例
                visible,
                setVisible,
                editingRecord,
                setEditingRecord
			}}>
			<Form
				form={form}
				onValuesChange={(changed, all) => setFormValue(all)}
				initialValues={formValue}
			>
				{children}
			</Form>
		</MainContext.Provider>
	);
}
export const useMainContext = () => {
	const context = useContext(MainContext);
	if (!context) {
		throw new Error("useMainContext must be used within MainProvider");
	}
	return context;
};
