import { postDeviceGetDeviceFkSelect } from '@/services/device/Device';
import { postDeviceRelationCreateDeviceRelation } from '@/services/device/DeviceRelation';
import { ModalForm, ProForm, ProFormSelect } from '@ant-design/pro-components';
import { FormattedMessage, useIntl } from '@umijs/max';
import { Form, message } from 'antd';
import React from 'react';
export type FormValueType = {
  target?: string;
  template?: string;
  type?: string;
  time?: string;
  frequency?: string;
} & Partial<API.DeviceRelation>;

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

  return (
    <ModalForm<API.DeviceRelation>
      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) => {
        postDeviceRelationCreateDeviceRelation(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="md"
          name="deviceParentFkId"
          label={
            <FormattedMessage
              id="device.device_relation.table.list.deviceParentFkId"
              defaultMessage="$$$"
            />
          }
          placeholder={`${intl.formatMessage({
            id: 'common.please_select',
            defaultMessage: '$$$',
          })}${intl.formatMessage({
            id: 'device.device_relation.table.list.deviceParentFkId',
            defaultMessage: '$$$',
          })}`}
          required={true}
          showSearch
          debounceTime={1000}
          request={async (keyWord) => {
            const resp = await postDeviceGetDeviceFkSelect({ keyword: keyWord?.keyWords || '' });
            return resp.data.list.map((v: any) => {
              return {
                label: v.name,
                value: v.id,
              };
            });
          }}
        />
        <ProFormSelect
          width="md"
          name="deviceSonFkId"
          label={
            <FormattedMessage
              id="device.device_relation.table.list.deviceSonFkId"
              defaultMessage="$$$"
            />
          }
          placeholder={`${intl.formatMessage({
            id: 'common.please_select',
            defaultMessage: '$$$',
          })}${intl.formatMessage({
            id: 'device.device_relation.table.list.deviceSonFkId',
            defaultMessage: '$$$',
          })}`}
          required={true}
          showSearch
          debounceTime={1000}
          request={async (keyWord) => {
            const resp = await postDeviceGetDeviceFkSelect({ keyword: keyWord?.keyWords || '' });
            return resp.data.list.map((v: any) => {
              return {
                label: v.name,
                value: v.id,
              };
            });
          }}
        />
      </ProForm.Group>
    </ModalForm>
  );
};
export default CreateForm;
