import { Controller } from 'react-hook-form'
import DateTimePicker, { IDateTimePicker } from '../Input/DateTimePicker'

interface Props extends Omit<IDateTimePicker, 'onChange' | 'value'> {
  name: string
  control: any
}

export default function FormDateTimePicker({
  name,
  control,
  label,
  required,
  minDate,
  maxDate,
  minTime,
  maxTime,
  ...rest
}: Props) {
  return (
    <Controller
      name={name}
      control={control}
      render={({ field, fieldState: { error } }) => (
        <DateTimePicker
          name={name}
          label={label}
          required={required}
          onChange={(v) => {
            field.onChange(v)
          }}
          value={field.value}
          minDate={minDate}
          maxDate={maxDate}
          minTime={minTime}
          maxTime={maxTime}
          inputProps={{
            ...rest.inputProps,
            error: !!error,
            helperText: error?.message ?? '',
          }}
          {...rest}
        />
      )}
    />
  )
}
