import {ModalForm, ProForm} from '@ant-design/pro-components';
import {ProFormText} from '@ant-design/pro-components';
import {ProFormSelect} from '@ant-design/pro-components';
import {FormattedMessage, useIntl} from '@umijs/max';
import React from 'react';
import {postApiCreateApi} from "@/services/system/Api";
import {Form, message} from 'antd';
// 表单宽度 804 两列
import { proFormItemStyleProps, proFormModelWidth } from '../../../../../config/defaultForm';

export type CreateFormProps = {
  createModalOpen: boolean;
  handleModal: () => void;
  values: Partial<API.Api>;
  reload: any;
};
const CreateForm: React.FC<CreateFormProps> = (props) => {

  const intl = useIntl();
  const [form] = Form.useForm<API.Api>();

  return (
    <ModalForm<API.Api>
      width={proFormModelWidth}
      title={intl.formatMessage({
        id: 'system.interface.table.list.add',
        defaultMessage: '$$$',
      })}
      open={props.createModalOpen}
      form={form}
      autoFocusFirstInput
      modalProps={{
        destroyOnClose: true,
        onCancel: () => props.handleModal(),
      }}
      submitTimeout={2000}
      onFinish={async (values) => {
        postApiCreateApi(values).then(() => {
          message.success(intl.formatMessage({id: 'common.success', defaultMessage: '$$$'}))
          props.reload()
        }).catch(() => {
          message.error(intl.formatMessage({id: 'common.error', defaultMessage: '$$$'}))
        })
        props.handleModal();
        return true;
      }}
    >

      <ProForm.Group>
        <ProFormText width={proFormItemStyleProps.column2Width} name="path"
                     label={<FormattedMessage id="system.api.table.list.path" defaultMessage="$$$"/>}
                     placeholder="请输入接口路径" required={true}
                     rules={[
                       {
                         required: true,
                         message: (
                           <FormattedMessage
                             id="system.api.table.rule.required.path"
                             defaultMessage="path is required"
                           />
                         ),
                       },
                     ]}
        />
        <ProFormSelect width={proFormItemStyleProps.column2Width} name="method"
                       label={<FormattedMessage id="system.api.table.list.method" defaultMessage="$$$"/>}
                       placeholder="请选择请求方式" required={false} initialValue="GET" valueEnum={{
          'GET': intl.formatMessage({id: 'system.api.enum.GET', defaultMessage: '$$$',}),
          'POST': intl.formatMessage({id: 'system.api.enum.POST', defaultMessage: '$$$',}),
          'DELETE': intl.formatMessage({id: 'system.api.enum.DELETE', defaultMessage: '$$$',}),
          'PUT': intl.formatMessage({id: 'system.api.enum.PUT', defaultMessage: '$$$',}),
          'PATCH': intl.formatMessage({id: 'system.api.enum.PATCH', defaultMessage: '$$$',})
        }}/>
        <ProFormText width={proFormItemStyleProps.column2Width} name="apiGroup"
                     label={<FormattedMessage id="system.api.table.list.apiGroup" defaultMessage="$$$"/>}
                     placeholder="请输入接口分组" required={false}/>
        <ProFormText width={proFormItemStyleProps.column2Width} name="description"
                     label={<FormattedMessage id="system.api.table.list.description" defaultMessage="$$$"/>}
                     placeholder="请输入接口描述" required={false}/>
      </ProForm.Group>
    </ModalForm>)
}
export default CreateForm;
