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

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

  return (
    <ModalForm<API.Device>
      width={proFormModelWidth}
      title={intl.formatMessage({
        id: 'device.device.table.list.add',
        defaultMessage: '$$$',
      })}
      open={props.createModalOpen}
      form={form}
      autoFocusFirstInput
      modalProps={{
        destroyOnClose: true,
        onCancel: () => props.handleModal(),
      }}
      submitTimeout={2000}
      onFinish={async (values) => {
        postDeviceCreateDevice(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="name"
          label={<FormattedMessage id="device.device.table.list.name" defaultMessage="$$$" />}
          placeholder={`${intl.formatMessage({
            id: 'common.please_input',
            defaultMessage: '$$$',
          })}${intl.formatMessage({ id: 'device.device.table.list.name', defaultMessage: '$$$' })}`}
          required={true}
          rules={[
            {
              required: true,
              message: (
                <FormattedMessage
                  id="device.device.table.rule.required.name"
                  defaultMessage="name is required"
                />
              ),
            },
          ]}
        />
        <ProFormSelect
          width={proFormItemStyleProps.column2Width}
          name="groupFkId"
          label={<FormattedMessage id="device.device.table.list.groupFkId" defaultMessage="$$$" />}
          placeholder={`${intl.formatMessage({
            id: 'common.please_select',
            defaultMessage: '$$$',
          })}${intl.formatMessage({
            id: 'device.device.table.list.groupFkId',
            defaultMessage: '$$$',
          })}`}
          required={false}
          showSearch
          debounceTime={1000}
          request={async (keyWord) => {
            const resp = await postDeviceGroupGetDeviceGroupFkSelect({
              keyword: keyWord?.keyWords || '',
            });
            return resp.data.list.map((v: any) => {
              return {
                label: v.name,
                value: v.id,
              };
            });
          }}
        />
        <ProFormText
          width={proFormItemStyleProps.width - 80}
          name="code"
          label={<FormattedMessage id="device.device.table.list.code" defaultMessage="$$$" />}
          placeholder={`${intl.formatMessage({
            id: 'common.please_input',
            defaultMessage: '$$$',
          })}${intl.formatMessage({ id: 'device.device.table.list.code', defaultMessage: '$$$' })}`}
          required={!isAuto}
          initialValue=""
          disabled={isAuto}
          rules={
            isAuto
              ? []
              : [
                  {
                    required: true,
                    message: (
                      <FormattedMessage
                        id="device.device.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={proFormItemStyleProps.column2Width}
          name="position"
          label={<FormattedMessage id="device.device.table.list.position" defaultMessage="$$$" />}
          placeholder={`${intl.formatMessage({
            id: 'common.please_input',
            defaultMessage: '$$$',
          })}${intl.formatMessage({
            id: 'device.device.table.list.position',
            defaultMessage: '$$$',
          })}`}
          required={false}
        />
        <ProFormText
          width={proFormItemStyleProps.column2Width}
          name="param"
          label={<FormattedMessage id="device.device.table.list.param" defaultMessage="$$$" />}
          placeholder={`${intl.formatMessage({
            id: 'common.please_input',
            defaultMessage: '$$$',
          })}${intl.formatMessage({
            id: 'device.device.table.list.param',
            defaultMessage: '$$$',
          })}`}
          required={false}
        />
        <ProFormText
          width={proFormItemStyleProps.column2Width}
          name="spec"
          label={<FormattedMessage id="device.device.table.list.spec" defaultMessage="$$$" />}
          placeholder={`${intl.formatMessage({
            id: 'common.please_input',
            defaultMessage: '$$$',
          })}${intl.formatMessage({ id: 'device.device.table.list.spec', defaultMessage: '$$$' })}`}
          required={false}
        />
        <ProFormSelect
          width={proFormItemStyleProps.column2Width}
          name="categoryFkId"
          label={
            <FormattedMessage id="device.device.table.list.categoryFkId" defaultMessage="$$$" />
          }
          placeholder={`${intl.formatMessage({
            id: 'common.please_select',
            defaultMessage: '$$$',
          })}${intl.formatMessage({
            id: 'device.device.table.list.categoryFkId',
            defaultMessage: '$$$',
          })}`}
          required={false}
          showSearch
          debounceTime={1000}
          request={async (keyWord) => {
            const resp = await postDeviceCategoryGetDeviceCategoryFkSelect({
              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="device.device.table.list.remark" defaultMessage="$$$" />}
          placeholder={`${intl.formatMessage({
            id: 'common.please_input',
            defaultMessage: '$$$',
          })}${intl.formatMessage({
            id: 'device.device.table.list.remark',
            defaultMessage: '$$$',
          })}`}
          required={false}
        />
        <ProFormSwitch
          width={proFormItemStyleProps.column2Width}
          name="isEnable"
          label={<FormattedMessage id="device.device.table.list.isEnable" defaultMessage="$$$" />}
          initialValue={true}
        />
      </ProForm.Group>
    </ModalForm>
  );
};
export default CreateForm;
