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 {Form, message} from 'antd';
import {putApiUpdateApi} from "@/services/system/Api";

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

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

  return (
    <ModalForm<API.Api>
      title={intl.formatMessage({
        id: 'common.modal.table.update.title',
        defaultMessage: '$$$',
      })}
      open={props.updateModalOpen}
      form={form}
      autoFocusFirstInput
      modalProps={{
        destroyOnClose: true,
        onCancel: () => props.handleModal(),
      }}
      submitTimeout={2000}
      onFinish={async (values) => {
        putApiUpdateApi(values).then(() => {
          message.success(intl.formatMessage({id: 'common.success', defaultMessage: '$$$'}))
          props.reload()
        }).catch(() => {
          message.error(intl.formatMessage({id: 'common.failure', defaultMessage: '$$$'}))
        })

        props.handleModal();
        return true;
      }}
    >

      <ProForm.Group>
        <ProFormText width="md" name="id" label="id" disabled={true} initialValue={props.values.id}/>
        <ProFormText width="md" name="path"
                     label={<FormattedMessage id="system.api.table.list.path" defaultMessage="$$$"/>}
                     placeholder="请输入接口路径" required={true} initialValue={props.values.path} disabled={false}
                     rules={[
                       {
                         required: true,
                         message: (
                           <FormattedMessage
                             id="system.api.table.rule.required.path"
                             defaultMessage="path is required"
                           />
                         ),
                       },
                     ]}
        />
        <ProFormSelect width="md" name="method"
                       label={<FormattedMessage id="system.api.table.list.method" defaultMessage="$$$"/>}
                       placeholder="请输入请求方式" required={false} initialValue={props.values.method} 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: '$$$',})
        }} disabled={false}/>
        <ProFormText width="md" name="apiGroup"
                     label={<FormattedMessage id="system.api.table.list.apiGroup" defaultMessage="$$$"/>}
                     placeholder="请输入接口分组" required={false} initialValue={props.values.apiGroup}
                     disabled={false}/>
        <ProFormText width="md" name="description"
                     label={<FormattedMessage id="system.api.table.list.description" defaultMessage="$$$"/>}
                     placeholder="请输入接口描述" required={false} initialValue={props.values.description}
                     disabled={false}/>
      </ProForm.Group>
    </ModalForm>)
}
export default UpdateForm;
