import React, { useEffect } from 'react';
import { Form, Input, Modal, Radio } from 'antd';
import { TagListItem } from '../data';

export interface CreateFormProps {
  onCancel: () => void;
  onSubmit: (values: TagListItem) => void;
  createModalVisible: boolean;
}

const FormItem = Form.Item;

const formLayout = {
  labelCol: { span: 7 },
  wrapperCol: { span: 13 },
};

const CreateTagForm: React.FC<CreateFormProps> = (props) => {
  const [form] = Form.useForm();


  const {
    onSubmit,
    onCancel,
    createModalVisible,
  } = props;

  useEffect(() => {
    if (form && !createModalVisible) {
      form.resetFields();
    }
  }, [props.createModalVisible]);


  const handleSubmit = () => {
    if (!form) return;
    form.submit();
  };

  const handleFinish = (values: TagListItem) => {
    if (onSubmit) {
      onSubmit(values);
    }
  };

  const renderContent = () => {
    return (
      <>
        <FormItem
          name="name"
          label="名称"
          rules={[{ required: true, message: '请输入标签' }]}
        >
          <Input id="update-role_name" placeholder={'请输入标签'} />
        </FormItem>

        <FormItem
          name="name_cn"
          label="中文名称"
          rules={[{ required: true, message: '请输入中文标签' }]}
        >
          <Input id="update-role_name_cn" placeholder={'请输入中文标签'} />
        </FormItem>
        {/* <FormItem
          name="remark"
          label="备注"
        >
          <Input.TextArea id="update-remark" placeholder={'请输入备注'} rows={2} />
        </FormItem> */}

      </>
    );
  };


  const modalFooter = { okText: '保存', onOk: handleSubmit, onCancel };

  return (
    <Modal
      forceRender
      destroyOnClose
      title="新建标签"
      open={createModalVisible}
      {...modalFooter}
    >
      <Form
        {...formLayout}
        form={form}
        onFinish={handleFinish}
      >
        {renderContent()}
      </Form>
    </Modal>
  );
};

export default CreateTagForm;
