/** @format */
import * as React from 'react';
import message from '../../../../components/message';
import ProviderForm, {formDataProps} from './provideForm';
import {useInsertSource} from '../../../../service/provider';

interface Iprops {
    onOk?: () => void;
}
const CreateProvider: React.FC<Iprops> = props => {
    const {children, onOk} = props;
    const [state, setState] = React.useState({
        visible: false,
    });
    const [insertState, requestInsert] = useInsertSource();
    const handleUpdateVisible = () => {
        setState(preState => ({...preState, visible: !preState.visible}));
    };
    const handleSubmit = (values: formDataProps) => {
        requestInsert(values).then(error => {
            if (error) return message.error(error);
            handleUpdateVisible();
            onOk && onOk();
        });
    };
    return (
        <>
            {React.isValidElement(children) &&
                React.cloneElement(children, {
                    onClick: handleUpdateVisible,
                })}
            <ProviderForm
                onCancel={handleUpdateVisible}
                loading={insertState.loading}
                onSubmit={handleSubmit}
                visible={state.visible}
                title='添加'
            />
        </>
    );
};
export default CreateProvider;
