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 UpdateTimeForm: 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.title1',
        defaultMessage: `编辑${props.values.warning_name}`,
      })}
      open={props.updateModalOpen}
      form={form}
      autoFocusFirstInput
      modalProps={{
        destroyOnClose: true,
        onCancel: () => props.handleModal(),
      }}
      submitTimeout={2000}
      initialValues={{
        list: [props.values.trigger_start_time,
          props.values.trigger_end_time],
      }}
      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>
        {/* <ProFormText
          width={proFormSmallItemStyleProps.width}
          name="name"
          label={<FormattedMessage id="alarm_rules.page.form.enabled" defaultMessage="$$$" />}
          placeholder={`${intl.formatMessage({
            id: 'common.please_input',
            defaultMessage: '$$$',
          })}${intl.formatMessage({
            id: 'alarm_rules.page.form.enabled',
            defaultMessage: '$$$',
          })}`}
          required={false}
          initialValue={props.values.enabled}
          disabled={false}
        /> */}
        <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"
                />
              ),
            },
          ]}
        />
          <ProFormFieldSet
            name="list"
            label="触发条件"
            type='space'
            transform={(value: any) => ({
              // list: value,
              // startTime: value[0],
              // endTime: value[1],
              trigger_start_time: value[0],
              trigger_end_time: value[1]
            })}
            className={styles}
          >
            当处于<ProFormText
            width={100}
            name="trigger_start_time"
            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={false}
            initialValue={props.values.trigger_start_time}
            disabled={false}
          />至
          <ProFormText
            width={100}
            name="trigger_end_time"
            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={false}
            initialValue={props.values.trigger_end_time}
            disabled={false}
          />时间内，发现风险人员立即告警。
          </ProFormFieldSet>
      </ProForm.Group>
    </ModalForm>
  );
};
export default UpdateTimeForm;
