/** @format */

import * as React from 'react';
import Apifrom, {formValuesProps} from './datApiForm';
import message from '../../../../../components/message';
import {useInsertApi} from '../../../../../service/dataApi';
interface Iprops {
    onOk?: () => void;
}
const DataApiSet: React.FC<Iprops> = props => {
    const {children, onOk} = props;
    const [state, setState] = React.useState({visible: false});
    const [insertState, requestInsert] = useInsertApi();
    const handleUpdateModalVisible = () => {
        setState(preState => ({...preState, visible: !preState.visible}));
    };
    const handleSubmit = (values: formValuesProps) => {
        requestInsert(values).then(error => {
            if (error) return message.error(error);
            handleUpdateModalVisible();
            onOk && onOk();
        });
    };
    return (
        <>
            {React.isValidElement(children) &&
                React.cloneElement(children, {
                    onClick: handleUpdateModalVisible,
                })}
            <Apifrom
                loading={insertState.loading}
                title='新增'
                visible={state.visible}
                onCancel={handleUpdateModalVisible}
                onSubmit={handleSubmit}
            />
        </>
    );
};
export default DataApiSet;
