/*
 * @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 { postUserList } from '@/services/system/User';
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.UserList>;

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

  return (
    <ModalForm<any>
      width={proFormSmallModelWidth}
      title={intl.formatMessage({
        id: 'system.user.table.list.editor',
        defaultMessage: `编辑用户`,
      })}
      open={props.updateModalOpen}
      form={form}
      autoFocusFirstInput
      modalProps={{
        destroyOnClose: true,
        onCancel: () => props.handleModal(),
        okText: intl.formatMessage({ id: 'common.yes', defaultMessage: '确认' }),
        cancelText: intl.formatMessage({ id: 'common.no', defaultMessage: '取消' }),
      }}
      submitTimeout={2000}
      onFinish={async (values) => {
        console.log(values);
        values.id = props.values.id
        postUserList(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="username"
          label={
            <FormattedMessage id="system.user.table.list.userName" defaultMessage="用户名" />
          }
          placeholder={`${intl.formatMessage({
            id: 'common.please_input',
            defaultMessage: '请输入',
          })}${intl.formatMessage({
            id: 'system.user.table.list.userName',
            defaultMessage: '用户名',
          })}`}
          required={true}
          initialValue={props.values.username}
          rules={[
            {
              required: true,
              message: (
                <FormattedMessage
                  id="system.user.table.rule.required.userName"
                  defaultMessage="用户名为必填项"
                />
              ),
            },
          ]}
        />
        <ProFormText
          width={proFormSmallItemStyleProps.width}
          name="password"
          label={<FormattedMessage id="system.user.table.list.password" defaultMessage="密码" />}
          placeholder={`${intl.formatMessage({
            id: 'common.please_input',
            defaultMessage: '请输入',
          })}${intl.formatMessage({
            id: 'system.user.table.list.password',
            defaultMessage: '密码',
          })}`}
          required={true}
          initialValue={props.values.password}
          disabled={false}
          rules={[
            {
              required: true,
              message: (
                <FormattedMessage
                  id="system.user.table.rule.required.password"
                  defaultMessage="密码为必填项"
                />
              ),
            },
          ]}
        />
        <ProFormText
          width={proFormSmallItemStyleProps.width}
          name="note"
          label={
            <FormattedMessage id="system.user.table.list.remark" defaultMessage="备注" />
          }
          placeholder={`${intl.formatMessage({
            id: 'common.please_input',
            defaultMessage: '请输入',
          })}${intl.formatMessage({
            id: 'system.user.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;
