/*
 * @Author: zhoux zhouxia@supervision.ltd
 * @Date: 2023-11-01 13:56:33
 * @LastEditors: zhoux zhouxia@supervision.ltd
 * @LastEditTime: 2023-11-22 13:51:06
 * @FilePath: \general-ai-platform-web\src\pages\System\ApiList\components\UpdateForm.tsx
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
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";
// 表单宽度 804 两列
import { proFormItemStyleProps, proFormModelWidth } from '../../../../../config/defaultForm';

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>
      width={proFormModelWidth}
      title={intl.formatMessage({
        id: 'system.interface.table.list.update',
        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={proFormItemStyleProps.column2Width} name="id" label="id" disabled={true} initialValue={props.values.id}/>
        <ProFormText width={proFormItemStyleProps.column2Width} 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={proFormItemStyleProps.column2Width} 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={proFormItemStyleProps.column2Width} name="apiGroup"
                     label={<FormattedMessage id="system.api.table.list.apiGroup" defaultMessage="$$$"/>}
                     placeholder="请输入接口分组" required={false} initialValue={props.values.apiGroup}
                     disabled={false}/>
        <ProFormText width={proFormItemStyleProps.column2Width} 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;
