/*
 * @Author: zhoux zhouxia@supervision.ltd
 * @Date: 2023-11-01 13:56:33
 * @LastEditors: zhoux zhouxia@supervision.ltd
 * @LastEditTime: 2023-12-26 15:01:08
 * @FilePath: \general-ai-platform-web\src\pages\Device\DeviceCategoryList\components\UpdateForm.tsx
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { putDeviceCategoryUpdateDeviceCategory } from '@/services/device/DeviceCategory';
import { postDeviceGroupGetDeviceGroupFkSelect } from '@/services/device/DeviceGroup';
import { postInterfaces } from '@/services/alarm/Interfaces';
import { ModalForm, ProForm, ProFormFieldSet, ProFormSelect, ProFormSwitch, ProFormText, ProFormTextArea } from '@ant-design/pro-components';
import { FormattedMessage, useIntl } from '@umijs/max';
import { Form, message } from 'antd';
import React from 'react';
import {
  proFormSmallItemStyleProps,
  proFormSmallModelWidth,
} from '../../../../../config/defaultForm';
export type FormValueType = {
  target?: string;
  template?: string;
  type?: string;
  time?: string;
  frequency?: string;
} & Partial<API.UpdateInterfacesParams>;

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

  return (
    <ModalForm<any>
      width={proFormSmallModelWidth}
      title={intl.formatMessage({
        id: 'device.interface_manage.table.list.updatePic',
        defaultMessage: `编辑${props.values.device_name}`,
      })}
      open={props.updateModalOpen}
      form={form}
      autoFocusFirstInput
      modalProps={{
        destroyOnClose: true,
        onCancel: () => props.handleModal(),
      }}
      submitTimeout={2000}
      onFinish={async (values) => {
        console.log(values);
        values.id = props.values.id
        postInterfaces(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="device_name"
          label={
            <FormattedMessage id="device.interface_manage.table.list.name" defaultMessage="$$$" />
          }
          placeholder={`${intl.formatMessage({
            id: 'common.please_input',
            defaultMessage: '$$$',
          })}${intl.formatMessage({
            id: 'device.interface_manage.table.list.name',
            defaultMessage: '$$$',
          })}`}
          required={true}
          initialValue={props.values.device_name}
          rules={[
            {
              required: true,
              message: (
                <FormattedMessage
                  id="device.interface_manage.table.rule.required.name"
                  defaultMessage="name is required"
                />
              ),
            },
          ]}
        />
        <ProFormText
          width={proFormSmallItemStyleProps.width}
          name="device_api"
          label={<FormattedMessage id="device.interface_manage.table.list.address" defaultMessage="$$$" />}
          placeholder={`${intl.formatMessage({
            id: 'common.please_input',
            defaultMessage: '$$$',
          })}${intl.formatMessage({
            id: 'device.interface_manage.table.list.address',
            defaultMessage: '$$$',
          })}`}
          required={true}
          initialValue={props.values.device_api}
          disabled={false}
          rules={[
            {
              required: true,
              message: (
                <FormattedMessage
                  id="device.interface_manage.table.rule.required.address"
                  defaultMessage="name is required"
                />
              ),
            },
          ]}
        />
        <ProFormTextArea
          width={proFormSmallItemStyleProps.width}
          name="note"
          label={
            <FormattedMessage id="device.interface_manage.table.list.remark" defaultMessage="$$$" />
          }
          placeholder={`${intl.formatMessage({
            id: 'common.please_input',
            defaultMessage: '$$$',
          })}${intl.formatMessage({
            id: 'device.interface_manage.table.list.remark',
            defaultMessage: '$$$',
          })}`}
          initialValue={props.values.note}
          required={false}
        />
        {/* <ProFormSelect
          width={proFormSmallItemStyleProps.width}
          name="level"
          label={<FormattedMessage id="alarm.setting.table.list.level" defaultMessage="$$$" />}
          placeholder={`${intl.formatMessage({
            id: 'common.please_select',
            defaultMessage: '$$$',
          })}${intl.formatMessage({
            id: 'alarm.setting.table.list.level',
            defaultMessage: '$$$',
          })}`}
          required={false}
          // TODO 在types中增加类型注释
          initialValue={props.values?.level}
          debounceTime={1000}
          request={async () => {
            // TODO 此处需要使用告警级别接口联调
            const resp = await postDeviceGroupGetDeviceGroupFkSelect({});
            return resp.data.list.map((v: any) => {
              return {
                label: v.name,
                value: v.id,
              };
            });
          }}
        /> */}
        {/* <ProFormText
          width={proFormSmallItemStyleProps.width}
          name="triggerConditions"
          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.triggerConditions}
          disabled={false}
        /> */}
            
      </ProForm.Group>
    </ModalForm>
  );
};
export default UpdateForm;
