import { FC, useEffect, useState } from "react";
import { Form, Input, Modal, ModalProps } from "antd";
import style from "@/components/AddIndexModal/index.module.scss";

export interface EvaluationFormModalProps extends Omit<ModalProps, "onOk"> {
  initContent?: string;
  onCancel?: () => void;
  onOk?: (val: any) => void;
}

const { TextArea } = Input;

export const EvaluationFormModal: FC<EvaluationFormModalProps> = ({
  initContent,
  open,
  onOk,
  onCancel,
  ...rest
}) => {
  const [form] = Form.useForm<any>();
  const [loading, setLoading] = useState(false);

  const handleCancel = () => {
    form.setFieldValue("opinion", initContent);
    onCancel?.();
  };

  const handleConfirm = () => {
    form.submit();
  };

  const handleSubmit = async (values: any) => {
    try {
      setLoading(true);
      await onOk?.(values);
      handleCancel();
    } finally {
      setLoading(false);
    }
  };

  useEffect(() => {
    form.setFieldValue("opinion", initContent);
  }, [initContent]);

  return (
    <Modal
      className={style.modal}
      title="评定意见"
      okText="提交"
      cancelText="取消"
      open={open}
      width={640}
      okButtonProps={{
        disabled: loading,
      }}
      forceRender
      maskClosable={false}
      onOk={handleConfirm}
      onCancel={handleCancel}
      {...rest}
    >
      <Form
        labelCol={{ span: 4, offset: 2 }}
        form={form}
        onFinish={handleSubmit}
      >
        <Form.Item name="opinion">
          <TextArea
            rows={20}
            maxLength={2000}
            placeholder="请输入内容，不超过2000字"
            showCount
          />
        </Form.Item>
      </Form>
    </Modal>
  );
};
