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

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

const Date: FC<Props> = function ({control, name, label, required}) {
  return (
    <Row gutter={12}>
      <Col span={6} offset={2}>
        <label
          className={classNames([
            css.textRight,
            {[css.fieldRequired]: required ?? true},
          ])}
          htmlFor={`field_${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 (
              <>
                <DatePicker
                  data-testid={`field_${name}`}
                  allowClear={false}
                  ref={ref}
                  value={value}
                  onChange={onChange}
                  className={classNames(['width-full', css.modalDate])}
                />
                <p
                  className={classNames([
                    css.errorTips,
                    {[css.errorTipsHidden]: !errorTips},
                  ])}
                >
                  {errorTips}
                </p>
              </>
            );
          }}
        />
      </Col>
    </Row>
  );
};

export default Date;
