import { postProjectGetProjectFkSelect } from '@/services/project/Project';
import { postBusinessImageCreateBusinessImage } from '@/services/resource/BusinessImage';
import { ModalForm, ProForm, ProFormSelect, ProFormText } from '@ant-design/pro-components';
import { FormattedMessage, useIntl } from '@umijs/max';
import { Form, message } from 'antd';
import { proFormItemStyleProps, proFormModelWidth } from '../../../../../config/defaultForm';
import React from 'react';
export type FormValueType = {
  target?: string;
  template?: string;
  type?: string;
  time?: string;
  frequency?: string;
} & Partial<API.BusinessImage>;

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

  return (
    <ModalForm<API.BusinessImage>
      width={proFormModelWidth}
      title={intl.formatMessage({
        id: 'resource.business_image.table.list.add',
        defaultMessage: '$$$',
      })}
      open={props.createModalOpen}
      form={form}
      autoFocusFirstInput
      modalProps={{
        destroyOnClose: true,
        onCancel: () => props.handleModal(),
      }}
      submitTimeout={2000}
      onFinish={async (values) => {
        postBusinessImageCreateBusinessImage(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="name"
          label={
            <FormattedMessage id="resource.business_image.table.list.name" defaultMessage="$$$" />
          }
          placeholder={`${intl.formatMessage({
            id: 'common.please_input',
            defaultMessage: '$$$',
          })}${intl.formatMessage({
            id: 'resource.business_image.table.list.name',
            defaultMessage: '$$$',
          })}`}
          required={true}
          rules={[
            {
              required: true,
              message: (
                <FormattedMessage
                  id="resource.business_image.table.rule.required.name"
                  defaultMessage="name is required"
                />
              ),
            },
          ]}
        />
        <ProFormText
          width={proFormItemStyleProps.column2Width}
          name="version"
          label={
            <FormattedMessage
              id="resource.business_image.table.list.version"
              defaultMessage="$$$"
            />
          }
          placeholder={`${intl.formatMessage({
            id: 'common.please_input',
            defaultMessage: '$$$',
          })}${intl.formatMessage({
            id: 'resource.business_image.table.list.version',
            defaultMessage: '$$$',
          })}`}
          required={true}
          rules={[
            {
              required: true,
              message: (
                <FormattedMessage
                  id="resource.business_image.table.rule.required.version"
                  defaultMessage="version is required"
                />
              ),
            },
          ]}
        />
        <ProFormSelect
          width={proFormItemStyleProps.column2Width}
          name="projectFkId"
          label={
            <FormattedMessage
              id="resource.business_image.table.list.projectFkId"
              defaultMessage="$$$"
            />
          }
          placeholder={`${intl.formatMessage({
            id: 'common.please_input',
            defaultMessage: '$$$',
          })}${intl.formatMessage({
            id: 'resource.business_image.table.list.projectFkId',
            defaultMessage: '$$$',
          })}`}
          required={true}
          showSearch
          debounceTime={1000}
          request={async (keyWord) => {
            const resp = await postProjectGetProjectFkSelect({ keyword: keyWord?.keyWords || '' });
            return resp.data.list.map((v: any) => {
              return {
                label: v.name,
                value: v.id,
              };
            });
          }}
        />
        <ProFormText
          width={proFormItemStyleProps.column2Width}
          name="path"
          label={
            <FormattedMessage id="resource.business_image.table.list.path" defaultMessage="$$$" />
          }
          placeholder={`${intl.formatMessage({
            id: 'common.please_input',
            defaultMessage: '$$$',
          })}${intl.formatMessage({
            id: 'resource.business_image.table.list.path',
            defaultMessage: '$$$',
          })}`}
          required={false}
        />
        <ProFormText
          width={proFormItemStyleProps.column2Width}
          name="startCode"
          label={
            <FormattedMessage
              id="resource.business_image.table.list.startCode"
              defaultMessage="$$$"
            />
          }
          placeholder={`${intl.formatMessage({
            id: 'common.please_input',
            defaultMessage: '$$$',
          })}${intl.formatMessage({
            id: 'resource.business_image.table.list.startCode',
            defaultMessage: '$$$',
          })}`}
          required={false}
        />
        <ProFormText
          width={proFormItemStyleProps.column2Width}
          name="configHash"
          label={
            <FormattedMessage
              id="resource.business_image.table.list.configHash"
              defaultMessage="$$$"
            />
          }
          placeholder={`${intl.formatMessage({
            id: 'common.please_input',
            defaultMessage: '$$$',
          })}${intl.formatMessage({
            id: 'resource.business_image.table.list.configHash',
            defaultMessage: '$$$',
          })}`}
          required={false}
        />
        <ProFormText
          width={proFormItemStyleProps.column2Width}
          name="remark"
          label={
            <FormattedMessage id="resource.business_image.table.list.remark" defaultMessage="$$$" />
          }
          placeholder={`${intl.formatMessage({
            id: 'common.please_input',
            defaultMessage: '$$$',
          })}${intl.formatMessage({
            id: 'resource.business_image.table.list.remark',
            defaultMessage: '$$$',
          })}`}
          required={false}
        />
      </ProForm.Group>
    </ModalForm>
  );
};
export default CreateForm;
