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 {Form, message} from 'antd';
import {putPostUpdatePost} from "@/services/system/Post";
// 表单宽度 804 两列
import { proFormItemStyleProps, proFormModelWidth } from '../../../../../config/defaultForm';

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

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

  return (
    <ModalForm<API.Post>
      width={proFormModelWidth}
      title={intl.formatMessage({
        id: 'system.post.table.list.update',
        defaultMessage: '$$$',
      })}
      open={props.updateModalOpen}
      form={form}
      autoFocusFirstInput
      modalProps={{
        destroyOnClose: true,
        onCancel: () => props.handleModal(),
      }}
      submitTimeout={2000}
      onFinish={async (values) => {
        putPostUpdatePost(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="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}
                     initialValue={props.values.name}
                     rules={[
                       {
                         required: true,
                         message: (
                           <FormattedMessage
                             id="system.post.table.rule.required.name"
                             defaultMessage="name is required"
                           />
                         ),
                       },
                     ]}
        />
        <ProFormText width={proFormItemStyleProps.column2Width} 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}
                     initialValue={props.values.code}
                     rules={[
                       {
                         required: true,
                         message: (
                           <FormattedMessage
                             id="system.post.table.rule.required.code"
                             defaultMessage="code is required"
                           />
                         ),
                       },
                     ]}
        />
        <ProFormDigit width={proFormItemStyleProps.column2Width} 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={props.values.sort}
        />
      </ProForm.Group>
      <ProForm.Group>
        <ProFormText width={proFormItemStyleProps.column2Width} 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}
                     initialValue={props.values.remark}
        />
        <ProFormSwitch width={proFormItemStyleProps.column2Width} name="status" initialValue={props.values.status}
                       label={<FormattedMessage id="system.post.table.list.status" defaultMessage="$$$"/>}
                       required={false}>

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