/*
 * @Author: zhoux zhouxia@supervision.ltd
 * @Date: 2023-11-01 13:56:33
 * @LastEditors: zhoux zhouxia@supervision.ltd
 * @LastEditTime: 2023-11-22 13:50:07
 * @FilePath: \general-ai-platform-web\src\pages\Resource\ModelVersionList\components\CreateForm.tsx
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { postAlgorithmModelGetAlgorithmModelFkSelect } from '@/services/resource/AlgorithmModel';
import { postModelVersionCreateModelVersion } from '@/services/resource/ModelVersion';
import {
  ModalForm,
  ProForm,
  ProFormSelect,
  ProFormSwitch,
  ProFormText,
} from '@ant-design/pro-components';
import { FormattedMessage, useIntl } from '@umijs/max';
import { Form, message } from 'antd';
import { proFormMaxItemStyleProps, proFormMaxModelWidth } from '../../../../../config/defaultForm';
import React from 'react';
export type FormValueType = {
  target?: string;
  template?: string;
  type?: string;
  time?: string;
  frequency?: string;
} & Partial<API.ModelVersion>;

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

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