import { postDeviceCategoryCreateDeviceCategory } from '@/services/device/DeviceCategory';
import { ModalForm, ProForm, ProFormText } from '@ant-design/pro-components';
import { FormattedMessage, useIntl } from '@umijs/max';
import { Form, Switch, message } from 'antd';
import React, { useState } from 'react';
import {
  proFormSmallItemStyleProps,
  proFormSmallModelWidth,
} from '../../../../../config/defaultForm';
export type FormValueType = {
  target?: string;
  template?: string;
  type?: string;
  time?: string;
  frequency?: string;
} & Partial<API.DeviceCategory>;

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

  return (
    <ModalForm<API.DeviceCategory>
      width={proFormSmallModelWidth}
      title={intl.formatMessage({
        id: 'contact.contact.table.list.add',
        defaultMessage: '$$$',
      })}
      open={props.createModalOpen}
      form={form}
      autoFocusFirstInput
      modalProps={{
        destroyOnClose: true,
        onCancel: () => props.handleModal(),
      }}
      submitTimeout={2000}
      onFinish={async (values) => {
        postDeviceCategoryCreateDeviceCategory(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={proFormSmallItemStyleProps.width}
          name="name"
          label={
            <FormattedMessage id="contact.contact.table.list.name" defaultMessage="$$$" />
          }
          placeholder={`${intl.formatMessage({
            id: 'common.please_input',
            defaultMessage: '$$$',
          })}${intl.formatMessage({
            id: 'contact.contact.table.list.name',
            defaultMessage: '$$$',
          })}`}
          required={true}
          rules={[
            {
              required: true,
              message: (
                <FormattedMessage
                  id="contact.contact.table.rule.required.name"
                  defaultMessage="name is required"
                />
              ),
            },
          ]}
        />
        {/* <ProFormText
          width="lg"
          name="code"
          label={
            <FormattedMessage id="device.device_category.table.list.code" defaultMessage="$$$" />
          }
          placeholder={`${intl.formatMessage({
            id: 'common.please_input',
            defaultMessage: '$$$',
          })}${intl.formatMessage({
            id: 'device.device_category.table.list.code',
            defaultMessage: '$$$',
          })}`}
          required={!isAuto}
          initialValue=""
          disabled={isAuto}
          rules={
            isAuto
              ? []
              : [
                  {
                    required: true,
                    message: (
                      <FormattedMessage
                        id="device.device_category.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={proFormSmallItemStyleProps.width}
          name="phone"
          label={
            <FormattedMessage id="contact.contact.table.list.phone" defaultMessage="$$$" />
          }
          placeholder={`${intl.formatMessage({
            id: 'common.please_input',
            defaultMessage: '$$$',
          })}${intl.formatMessage({
            id: 'contact.contact.table.list.phone',
            defaultMessage: '$$$',
          })}`}
          required={true}
        />
        <ProFormText
          width={proFormSmallItemStyleProps.width}
          name="mail"
          label={
            <FormattedMessage id="contact.contact.table.list.mail" defaultMessage="$$$" />
          }
          placeholder={`${intl.formatMessage({
            id: 'common.please_input',
            defaultMessage: '$$$',
          })}${intl.formatMessage({
            id: 'contact.contact.table.list.mail',
            defaultMessage: '$$$',
          })}`}
          required={true}
        />
        <ProFormText
          width={proFormSmallItemStyleProps.width}
          name="WeChatID"
          label={
            <FormattedMessage id="contact.contact.table.list.WeChatID" defaultMessage="$$$" />
          }
          placeholder={`${intl.formatMessage({
            id: 'common.please_input',
            defaultMessage: '$$$',
          })}${intl.formatMessage({
            id: 'contact.contact.table.list.WeChatID',
            defaultMessage: '$$$',
          })}`}
          required={false}
        />
        <ProFormText
          width={proFormSmallItemStyleProps.width}
          name="remark"
          label={
            <FormattedMessage id="contact.contact.table.list.remark" defaultMessage="$$$" />
          }
          placeholder={`${intl.formatMessage({
            id: 'common.please_input',
            defaultMessage: '$$$',
          })}${intl.formatMessage({
            id: 'contact.contact.table.list.remark',
            defaultMessage: '$$$',
          })}`}
          required={false}
        />
      </ProForm.Group>
    </ModalForm>
  );
};
export default CreateForm;
