import { putDeviceCategoryUpdateDeviceCategory } from '@/services/device/DeviceCategory';
import { postDeviceGroupGetDeviceGroupFkSelect } from '@/services/device/DeviceGroup';
import { postAlarmRules } from '@/services/alarm/AlarmRules';
import { ModalForm, ProForm, ProFormFieldSet, ProFormSelect, ProFormSwitch, ProFormText } from '@ant-design/pro-components';
import { FormattedMessage, useIntl } from '@umijs/max';
import { Form, message } from 'antd';
import React, { useState } from 'react';
import {
  proFormSmallItemStyleProps,
  proFormSmallModelWidth,
} from '../../../../../config/defaultForm';
import styles from './updateForm.less'
export type FormValueType = {
  target?: string;
  template?: string;
  type?: string;
  time?: string;
  frequency?: string;
} & Partial<API.UpdateAlarmRulesForm>;

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

  return (
    <ModalForm<any>
      width={proFormSmallModelWidth}
      title={intl.formatMessage({
        id: 'alarm_rules.page.form.title',
        defaultMessage: `$$$`,
      })}
      open={props.updateModalOpen}
      form={form}
      autoFocusFirstInput
      modalProps={{
        destroyOnClose: true,
        onCancel: () => props.handleModal(),
      }}
      submitTimeout={2000}
      onFinish={async (values) => {
        values.id = props.values.id
        values.is_use = values.is_use ? 1 : 0
        // console.log(values);
        await postAlarmRules(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>
        <ProFormSwitch
          width={proFormSmallItemStyleProps.column2Width}
          name="is_use"
          label={
            <FormattedMessage
              id="alarm_rules.page.form.isEnabled"
              defaultMessage="$$$"
            />
          }
          checkedChildren="启用"
          unCheckedChildren="关闭"
          initialValue={props.values.is_use}
          disabled={false}
        />
        <ProFormText
          width={proFormSmallItemStyleProps.width}
          name="warning_name"
          label={<FormattedMessage id="alarm_rules.page.form.name" defaultMessage="$$$" />}
          placeholder={`${intl.formatMessage({
            id: 'common.please_input',
            defaultMessage: '$$$',
          })}${intl.formatMessage({
            id: 'alarm_rules.page.form.name',
            defaultMessage: '$$$',
          })}`}
          required={true}
          initialValue={props.values.warning_name}
          disabled={false}
          rules={[
            {
              required: true,
              message: (
                <FormattedMessage
                  id="alarm_rules.page.rule.required.name"
                  defaultMessage="name is required"
                />
              ),
            },
          ]}
        />
        <div>触发条件</div>
          <div style={{
            display: 'flex',
            flexWrap: 'wrap',
            justifyContent: 'flex-start',
            alignItems: 'baseline'
          }}>
            <span style={{ marginRight: 6 }}>当检测到风险人员单人，于</span>
            <ProFormText
              width={100}
              name="time_period"
              // label={<FormattedMessage id="alarm_rules.page.form.triggerConditions" defaultMessage="$$$" />}
              placeholder={`${intl.formatMessage({
                id: 'common.please_input',
                defaultMessage: '$$$',
              })}${intl.formatMessage({
                id: 'alarm_rules.page.form.triggerConditions',
                defaultMessage: '$$$',
              })}`}
              required={true}
              initialValue={props.values.time_period}
              disabled={false}
              rules={[
                {
                  required: true,
                  message: (
                    <FormattedMessage
                      id="alarm_rules.page.rule.required.time_interval"
                      defaultMessage="name is required"
                    />
                  ),
                },
              ]}
            />
            <span style={{ margin: '0 6px' }}>小时内，间隔</span>
            <ProFormText
              width={100}
              name="time_interval"
              // label={<FormattedMessage id="alarm_rules.page.form.triggerConditions" defaultMessage="$$$" />}
              placeholder={`${intl.formatMessage({
                id: 'common.please_input',
                defaultMessage: '$$$',
              })}${intl.formatMessage({
                id: 'alarm_rules.page.form.triggerConditions',
                defaultMessage: '$$$',
              })}`}
              required={true}
              initialValue={props.values.time_interval}
              disabled={false}
              rules={[
                {
                  required: true,
                  message: (
                    <FormattedMessage
                      id="alarm_rules.page.rule.required.time_interval"
                      defaultMessage="name is required"
                    />
                  ),
                },
              ]}
            />
            <span style={{ marginRight: 6 }}>小时，重复出现</span>
            <ProFormText
              width={100}
              name="appear_number"
              // label={<FormattedMessage id="alarm_rules.page.form.triggerConditions" defaultMessage="$$$" />}
              placeholder={`${intl.formatMessage({
                id: 'common.please_input',
                defaultMessage: '$$$',
              })}${intl.formatMessage({
                id: 'alarm_rules.page.form.triggerConditions',
                defaultMessage: '$$$',
              })}`}
              required={true}
              initialValue={props.values.appear_number}
              disabled={false}
              rules={[
                {
                  required: true,
                  message: (
                    <FormattedMessage
                      id="alarm_rules.page.rule.required.appear_number"
                      defaultMessage="name is required"
                    />
                  ),
                },
              ]}
            />
            <span style={{ marginLeft: 6 }}>次，则触发警告。</span>
        </div>
      </ProForm.Group>
    </ModalForm>
  );
};
export default UpdateForm;
