import {ModalForm, ProForm, ProFormDigit, ProFormSwitch} 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 {postPostCreatePost} from "@/services/system/Post";
import {Form, message} from 'antd';

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

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

  return (
    <ModalForm<API.Post>
      title={intl.formatMessage({
        id: 'common.modal.table.create.title',
        defaultMessage: '$$$',
      })}
      open={props.createModalOpen}
      form={form}
      autoFocusFirstInput
      modalProps={{
        destroyOnClose: true,
        onCancel: () => props.handleModal(),
      }}
      submitTimeout={2000}
      onFinish={async (values) => {
        postPostCreatePost(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="md" name="name"
                     label={<FormattedMessage id="system.post.table.list.name" defaultMessage="$$$"/>}
                     placeholder={`${intl.formatMessage({id: 'common.please_input', defaultMessage: '$$$'})}${intl.formatMessage({id: 'system.post.table.list.name', defaultMessage: '$$$'})}`}
                     required={true}
                     rules={[
                       {
                         required: true,
                         message: (
                           <FormattedMessage
                             id="system.post.table.rule.required.name"
                             defaultMessage="name is required"
                           />
                         ),
                       },
                     ]}
        />
        <ProFormText width="md" name="code"
                     label={<FormattedMessage id="system.post.table.list.code" defaultMessage="$$$"/>}
                     placeholder={`${intl.formatMessage({id: 'common.please_input', defaultMessage: '$$$'})}${intl.formatMessage({id: 'system.post.table.list.code', defaultMessage: '$$$'})}`}
                     required={true}
                     rules={[
                       {
                         required: true,
                         message: (
                           <FormattedMessage
                             id="system.post.table.rule.required.code"
                             defaultMessage="code is required"
                           />
                         ),
                       },
                     ]}
        />
        <ProFormDigit width="md" fieldProps={{precision: 0}} name="sort"
                      label={<FormattedMessage id="system.post.table.list.sort" defaultMessage="$$$"/>}
                      placeholder={`${intl.formatMessage({id: 'common.please_input', defaultMessage: '$$$'})}${intl.formatMessage({id: 'system.post.table.list.sort', defaultMessage: '$$$'})}`}
                      initialValue={1}
        />
      </ProForm.Group>
      <ProForm.Group>
        <ProFormText width="md" name="remark"
                     label={<FormattedMessage id="system.post.table.list.remark" defaultMessage="$$$"/>}
                     placeholder={`${intl.formatMessage({id: 'common.please_input', defaultMessage: '$$$'})}${intl.formatMessage({id: 'system.post.table.list.remark', defaultMessage: '$$$'})}`}
                     required={false}/>
        <ProFormSwitch width="md" name="status" initialValue={true}
                       label={<FormattedMessage id="system.post.table.list.status" defaultMessage="$$$"/>}
                       required={false}>

        </ProFormSwitch>
      </ProForm.Group>
    </ModalForm>)
}
export default CreateForm;
