import {ModalForm,ProForm} from '@ant-design/pro-components';import {ProFormText} from '@ant-design/pro-components';import {ProFormSwitch} from '@ant-design/pro-components';
import {FormattedMessage, useIntl} from '@umijs/max';
import {postProjectCreateProject} from "@/services/project/Project";
import React, {useState} from 'react';
import {Button, Form, message} from 'antd';
import {Switch} from 'antd';
export type FormValueType = {
        target?: string;
        template?: string;
        type?: string;
        time?: string;
        frequency?: string;
      } & Partial<API.Project>;

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

    const intl = useIntl();
    const [isAuto, setIsAuto] = useState(true);
    const [form] = Form.useForm<API.Project>();

    return (
      <ModalForm<API.Project>
        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) => {
        postProjectCreateProject(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="md" name="name" label={<FormattedMessage id="project.project.table.list.name" defaultMessage="$$$"/>} placeholder={`${intl.formatMessage({id: 'common.please_input', defaultMessage: '$$$'})}${intl.formatMessage({id: 'project.project.table.list.name', defaultMessage: '$$$'})}`} required={true}
                      rules={[
                      {
                        required: true,
                        message: (
                          <FormattedMessage
                            id="project.project.table.rule.required.name"
                            defaultMessage="name is required"
                          />
                        ),
                      },
                    ]}
                  />
<ProFormText width="md" name="code" label={<FormattedMessage id="project.project.table.list.code" defaultMessage="$$$"/>} placeholder={`${intl.formatMessage({id: 'common.please_input', defaultMessage: '$$$'})}${intl.formatMessage({id: 'project.project.table.list.code', defaultMessage: '$$$'})}`} required={!isAuto} initialValue='' disabled={isAuto}
                                                      rules={isAuto?[]:[
                                                      {
                                                        required: true,
                                                        message: (
                                                          <FormattedMessage
                                                            id="project.project.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="md" name="info" label={<FormattedMessage id="project.project.table.list.info" defaultMessage="$$$"/>} placeholder={`${intl.formatMessage({id: 'common.please_input', defaultMessage: '$$$'})}${intl.formatMessage({id: 'project.project.table.list.info', defaultMessage: '$$$'})}`} required={true}
                      rules={[
                      {
                        required: true,
                        message: (
                          <FormattedMessage
                            id="project.project.table.rule.required.info"
                            defaultMessage="info is required"
                          />
                        ),
                      },
                    ]}
                  />
<ProFormSwitch width="md" name="isEnable" label={<FormattedMessage id="project.project.table.list.isEnable" defaultMessage="$$$"/>} initialValue={true}/>
<ProFormText width="md" name="remark" label={<FormattedMessage id="project.project.table.list.remark" defaultMessage="$$$"/>} placeholder={`${intl.formatMessage({id: 'common.please_input', defaultMessage: '$$$'})}${intl.formatMessage({id: 'project.project.table.list.remark', defaultMessage: '$$$'})}`} required={false}/>
</ProForm.Group>
</ModalForm>)}
export default CreateForm;
