import { putAlgorithmModelUpdateAlgorithmModel } from '@/services/resource/AlgorithmModel';
import { postModelCategoryGetModelCategoryFkSelect } from '@/services/resource/ModelCategory';
import {
  ModalForm,
  ProForm,
  ProFormDateTimePicker,
  ProFormSelect,
  ProFormText,
} from '@ant-design/pro-components';
import { FormattedMessage, useIntl } from '@umijs/max';
import { Form, message } from 'antd';
import React from 'react';
import { proFormItemStyleProps, proFormModelWidth } from '../../../../../config/defaultForm';

export type FormValueType = {
  target?: string;
  template?: string;
  type?: string;
  time?: string;
  frequency?: string;
} & Partial<API.AlgorithmModel>;

export type UpdateFormProps = {
  updateModalOpen: boolean;
  handleModal: () => void;
  values: Partial<API.AlgorithmModel>;
  reload: any;
};
const UpdateForm: React.FC<UpdateFormProps> = (props) => {
  const intl = useIntl();
  const [form] = Form.useForm<API.AlgorithmModel>();

  return (
    <ModalForm<API.AlgorithmModel>
      width={proFormModelWidth}
      title={intl.formatMessage({
        id: 'resource.algorithm_model.table.list.update',
        defaultMessage: '$$$',
      })}
      open={props.updateModalOpen}
      form={form}
      autoFocusFirstInput
      modalProps={{
        destroyOnClose: true,
        onCancel: () => props.handleModal(),
      }}
      submitTimeout={2000}
      onFinish={async (values) => {
        putAlgorithmModelUpdateAlgorithmModel(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="resource.algorithm_model.table.list.name" defaultMessage="$$$" />
          }
          placeholder={`${intl.formatMessage({
            id: 'common.please_input',
            defaultMessage: '$$$',
          })}${intl.formatMessage({
            id: 'resource.algorithm_model.table.list.name',
            defaultMessage: '$$$',
          })}`}
          required={true}
          initialValue={props.values.name}
          disabled={false}
          rules={[
            {
              required: true,
              message: (
                <FormattedMessage
                  id="resource.algorithm_model.table.rule.required.name"
                  defaultMessage="name is required"
                />
              ),
            },
          ]}
        />
        <ProFormSelect
          width={proFormItemStyleProps.column2Width}
          name="categoryFkId"
          label={
            <FormattedMessage
              id="resource.algorithm_model.table.list.categoryFkId"
              defaultMessage="$$$"
            />
          }
          placeholder={`${intl.formatMessage({
            id: 'common.please_select',
            defaultMessage: '$$$',
          })}${intl.formatMessage({
            id: 'resource.algorithm_model.table.list.categoryFkId',
            defaultMessage: '$$$',
          })}`}
          required={true}
          initialValue={props.values.categoryFkId}
          showSearch
          debounceTime={1000}
          request={async (keyWord) => {
            const resp = await postModelCategoryGetModelCategoryFkSelect({
              keyword: keyWord?.keyWords || '',
            });
            return resp.data.list.map((v: any) => {
              return {
                label: v.name,
                value: v.id,
              };
            });
          }}
        />
        <ProFormText
          width={proFormItemStyleProps.column2Width}
          name="remark"
          label={
            <FormattedMessage
              id="resource.algorithm_model.table.list.remark"
              defaultMessage="$$$"
            />
          }
          placeholder={`${intl.formatMessage({
            id: 'common.please_input',
            defaultMessage: '$$$',
          })}${intl.formatMessage({
            id: 'resource.algorithm_model.table.list.remark',
            defaultMessage: '$$$',
          })}`}
          required={false}
          initialValue={props.values.remark}
          disabled={false}
        />
        <ProFormDateTimePicker
          width={proFormItemStyleProps.column2Width}
          name="createTime"
          label={
            <FormattedMessage
              id="resource.algorithm_model.table.list.createTime"
              defaultMessage="$$$"
            />
          }
          placeholder={`${intl.formatMessage({
            id: 'common.please_input',
            defaultMessage: '$$$',
          })}${intl.formatMessage({
            id: 'resource.algorithm_model.table.list.createTime',
            defaultMessage: '$$$',
          })}`}
          required={false}
          initialValue={props.values.createTime}
          disabled={true}
        />
        <ProFormDateTimePicker
          width={proFormItemStyleProps.column2Width}
          name="updateTime"
          label={
            <FormattedMessage
              id="resource.algorithm_model.table.list.updateTime"
              defaultMessage="$$$"
            />
          }
          placeholder={`${intl.formatMessage({
            id: 'common.please_input',
            defaultMessage: '$$$',
          })}${intl.formatMessage({
            id: 'resource.algorithm_model.table.list.updateTime',
            defaultMessage: '$$$',
          })}`}
          required={false}
          initialValue={props.values.updateTime}
          disabled={true}
        />
      </ProForm.Group>
    </ModalForm>
  );
};
export default UpdateForm;
