import { postModelCategoryCreateModelCategory } from '@/services/resource/ModelCategory';
import { ModalForm, ProForm, ProFormText } from '@ant-design/pro-components';
import { FormattedMessage, useIntl } from '@umijs/max';
import { Form, Switch, message } from 'antd';
import { proFormSmallItemStyleProps,proFormSmallModelWidth } from '../../../../../config/defaultForm';
import React, { useState } from 'react';
export type FormValueType = {
  target?: string;
  template?: string;
  type?: string;
  time?: string;
  frequency?: string;
} & Partial<API.ModelCategory>;

export type CreateFormProps = {
  createModalOpen: boolean;
  handleModal: () => void;
  values: Partial<API.ModelCategory>;
  reload: any;
};
const CreateForm: React.FC<CreateFormProps> = (props) => {
  const intl = useIntl();
  const [isAuto, setIsAuto] = useState(true);
  const [form] = Form.useForm<API.ModelCategory>();

  return (
    <ModalForm<API.ModelCategory>
      width={proFormSmallModelWidth}
      title={intl.formatMessage({
        id: 'resource.model_category.table.list.add',
        defaultMessage: '$$$',
      })}
      open={props.createModalOpen}
      form={form}
      autoFocusFirstInput
      modalProps={{
        destroyOnClose: true,
        onCancel: () => props.handleModal(),
      }}
      submitTimeout={2000}
      onFinish={async (values) => {
        postModelCategoryCreateModelCategory(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={proFormSmallItemStyleProps.width}
          name="name"
          label={
            <FormattedMessage id="resource.model_category.table.list.name" defaultMessage="$$$" />
          }
          placeholder={`${intl.formatMessage({
            id: 'common.please_input',
            defaultMessage: '$$$',
          })}${intl.formatMessage({
            id: 'resource.model_category.table.list.name',
            defaultMessage: '$$$',
          })}`}
          required={true}
          rules={[
            {
              required: true,
              message: (
                <FormattedMessage
                  id="resource.model_category.table.rule.required.name"
                  defaultMessage="name is required"
                />
              ),
            },
          ]}
        />
        <ProFormText
          width='lg'
          name="code"
          label={
            <FormattedMessage id="resource.model_category.table.list.code" defaultMessage="$$$" />
          }
          placeholder={`${intl.formatMessage({
            id: 'common.please_input',
            defaultMessage: '$$$',
          })}${intl.formatMessage({
            id: 'resource.model_category.table.list.code',
            defaultMessage: '$$$',
          })}`}
          required={!isAuto}
          initialValue=""
          disabled={isAuto}
          rules={
            isAuto
              ? []
              : [
                  {
                    required: true,
                    message: (
                      <FormattedMessage
                        id="resource.model_category.table.rule.required.code"
                        defaultMessage="code is required"
                      />
                    ),
                  },
                ]
          }
          addonAfter={
            <Switch
              checked={isAuto}
              checkedChildren={<FormattedMessage id="common.auto" defaultMessage="$$$" />}
              unCheckedChildren={<FormattedMessage id="common.edit" defaultMessage="$$$" />}
              onChange={setIsAuto}
            />
          }
        />
        <ProFormText
          width={proFormSmallItemStyleProps.width}
          name="remark"
          label={
            <FormattedMessage id="resource.model_category.table.list.remark" defaultMessage="$$$" />
          }
          placeholder={`${intl.formatMessage({
            id: 'common.please_input',
            defaultMessage: '$$$',
          })}${intl.formatMessage({
            id: 'resource.model_category.table.list.remark',
            defaultMessage: '$$$',
          })}`}
          required={false}
        />
      </ProForm.Group>
    </ModalForm>
  );
};
export default CreateForm;
