import css from './index.module.css';
import {Row, Col, Input} from 'antd';
import classNames from 'classnames';
import {FC} from 'react';
import {Controller, UseControllerProps} from 'react-hook-form';

type Props = {
  label: string;
  placeholder?: string;
  width: string;
  height: string;
  required?: boolean;
} & UseControllerProps<any, any>;

const OperationField: FC<Props> = function ({
  control,
  name,
  label,
  placeholder,
  height,
  required,
}) {
  return (
    <Row gutter={12}>
      <Col span={6} offset={2}>
        <label
          className={classNames([
            css.textRight,
            {[css.fieldRequired]: required ?? true},
          ])}
          htmlFor={`operation_${name}`}
        >
          {label}
        </label>
      </Col>
      <Col span={12}>
        <Controller
          name={name}
          control={control}
          render={function ({
            field: {value, onChange, ref},
            formState: {errors},
          }) {
            const errorTips = errors[name]?.message?.toString() ?? '';
            return (
              <>
                <Input.TextArea
                  name={name}
                  placeholder={placeholder ?? `请输入${label}`}
                  data-testid={`field_${name}`}
                  id={`field_${name}`}
                  value={value}
                  onChange={onChange}
                  ref={ref}
                  className={css.areaFiled}
                  style={{height}}
                />
                <p
                  className={classNames([
                    css.errorTips,
                    {[css.errorTipsHidden]: !errorTips},
                  ])}
                >
                  {errorTips}
                </p>
              </>
            );
          }}
        />
      </Col>
    </Row>
  );
};

export default OperationField;
