import { postAlgorithmModelGetAlgorithmModelFkSelect } from '@/services/resource/AlgorithmModel';
import { putModelVersionUpdateModelVersion } from '@/services/resource/ModelVersion';
import {
  ModalForm,
  ProForm,
  ProFormDateTimePicker,
  ProFormSelect,
  ProFormSwitch,
  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, { useEffect , useState} from 'react';
export type FormValueType = {
  target?: string;
  template?: string;
  type?: string;
  time?: string;
  frequency?: string;
} & Partial<API.ModelVersion>;

export type UpdateFormProps = {
  updateModalOpen: boolean;
  handleModal: () => void;
  values: Partial<API.ModelVersion>;
  reload: any;
  currentDefaultFieldsData?: Record<string,any>
};
const UpdateForm: React.FC<UpdateFormProps> = (props) => {
  const intl = useIntl();
  const [isHasModelFkId, setIsHasModelFkId] = useState<boolean>(false)

  const [form] = Form.useForm<API.ModelVersion>();
  useEffect(()=>{
    if(props.currentDefaultFieldsData){
      // 如果是在模型详情新增版本，ModelFkId不可编辑
      console.log(props.values,'currentDefaultFieldsData')
      setIsHasModelFkId(true)
    } else {
      setIsHasModelFkId(false)
    }
  }, [])
  return (
    <ModalForm<API.ModelVersion>
      width={proFormModelWidth}
      title={intl.formatMessage({
        id: 'resource.model_version.table.list.update',
        defaultMessage: '$$$',
      })}
      open={props.updateModalOpen}
      form={form}
      autoFocusFirstInput
      modalProps={{
        destroyOnClose: true,
        onCancel: () => props.handleModal(),
      }}
      submitTimeout={2000}
      onFinish={async (values) => {
        putModelVersionUpdateModelVersion(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}
        />
        <ProFormSelect
          width={proFormItemStyleProps.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}
          initialValue={props.values.modelFkId}
          showSearch
          debounceTime={1000}
          disabled={isHasModelFkId}
          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={proFormItemStyleProps.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}
          initialValue={props.values.version}
          disabled={false}
        />
        <ProFormText
          width={proFormItemStyleProps.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}
          initialValue={props.values.path}
          disabled={false}
          rules={[
            {
              required: true,
              message: (
                <FormattedMessage
                  id="resource.model_version.table.rule.required.path"
                  defaultMessage="path is required"
                />
              ),
            },
          ]}
        />
        <ProFormText
          width={proFormItemStyleProps.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}
          initialValue={props.values.startCode}
          disabled={false}
        />
        <ProFormSwitch
          width={proFormItemStyleProps.column2Width}
          name="isEnable"
          label={
            <FormattedMessage
              id="resource.model_version.table.list.isEnable"
              defaultMessage="$$$"
            />
          }
          initialValue={props.values.isEnable}
          disabled={false}
        />
        <ProFormText
          width={proFormItemStyleProps.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}
          initialValue={props.values.remark}
          disabled={false}
        />
        <ProFormDateTimePicker
          width={proFormItemStyleProps.column2Width}
          name="createTime"
          label={
            <FormattedMessage
              id="resource.model_version.table.list.createTime"
              defaultMessage="$$$"
            />
          }
          placeholder={`${intl.formatMessage({
            id: 'common.please_input',
            defaultMessage: '$$$',
          })}${intl.formatMessage({
            id: 'resource.model_version.table.list.createTime',
            defaultMessage: '$$$',
          })}`}
          required={false}
          initialValue={props.values.createTime}
          disabled={true}
        />
        <ProFormDateTimePicker
          width={proFormItemStyleProps.column2Width}
          name="updateTime"
          label={
            <FormattedMessage
              id="resource.model_version.table.list.updateTime"
              defaultMessage="$$$"
            />
          }
          placeholder={`${intl.formatMessage({
            id: 'common.please_input',
            defaultMessage: '$$$',
          })}${intl.formatMessage({
            id: 'resource.model_version.table.list.updateTime',
            defaultMessage: '$$$',
          })}`}
          required={false}
          initialValue={props.values.updateTime}
          disabled={true}
        />
      </ProForm.Group>
    </ModalForm>
  );
};
export default UpdateForm;
