// 修改弹窗
import React, { useRef, useEffect } from 'react';
import { createForm } from '@formily/core';
import { createSchemaField } from '@formily/react';
import Modal, { ModalProps } from '@/components/Modal';
import { Form, FormItem, Input, Select } from '@formily/antd';

interface EditNoticeModalPropsType extends ModalProps {
  editModalData: any;
  onOk: (val: any) => void;
  onCancel: () => void;
}

const SchemaField = createSchemaField({
  components: {
    FormItem,
    Input,
    Select,
  },
});

const form = createForm({});

const EditNoticeModal = ({ onOk, onCancel, editModalData, ...rest }: EditNoticeModalPropsType) => {
  useEffect(() => {
    form.setInitialValues(editModalData);
  });

  const handleOk = () => {
    form.submit(async () => {
      const formState = form.getFormState();
      onOk(formState.values);
    });
  };

  const handleCancel = () => {
    onCancel();
  };

  return (
    <Modal title="修改通知" onOk={handleOk} onCancel={handleCancel} width={800} {...rest}>
      <Form form={form} labelCol={4} wrapperCol={18}>
        <SchemaField>
          <SchemaField.String
            name="code"
            title="通知码"
            x-disabled
            required
            x-decorator="FormItem"
            x-component="Input"
          />
          <SchemaField.String
            name="addr"
            title="通知地址"
            required
            x-decorator="FormItem"
            x-component="Input"
          />
        </SchemaField>
      </Form>
    </Modal>
  );
};

export default EditNoticeModal;
