// form
import { useFormContext, Controller } from 'react-hook-form';
import * as React from 'react';

// @mui
import { IconButton, TextField, TextFieldProps } from '@mui/material';
import { LocalizationProvider, MobileDatePicker } from '@mui/x-date-pickers';
import { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFns';

import { DesktopDatePicker } from '@mui/x-date-pickers/DesktopDatePicker';

import InputAdornment from '@mui/material/InputAdornment';
import EventIcon from '@mui/icons-material/Event';
import { fPostFormat } from '@/utils/formatTime';

// ----------------------------------------------------------------------

interface IProps {
  name: string;
}

export default function RHFDatepicker({ name, ...other }: IProps & TextFieldProps) {
  const { control } = useFormContext();

  return (
    <Controller
      name={name}
      control={control}
      render={({ field, fieldState: { error } }) => (
        <LocalizationProvider dateAdapter={AdapterDateFns}>
          <MobileDatePicker
            inputFormat="yyyy-MM-dd"
            value={field.value}
            onChange={(value) => {
              field.onChange(fPostFormat(value));
            }}
            renderInput={(field) => (
              <TextField
                {...field}
                fullWidth
                InputProps={{
                  endAdornment: (
                    <InputAdornment position="end">
                      <EventIcon />
                    </InputAdornment>
                  ),
                }}
                error={!!error}
                helperText={error?.message}
                {...other}
              />
            )}
          />
          {/* <DesktopDatePicker
            value={field.value}
            onChange={(value) => {
              field.onChange(fPostFormat(value));
            }}
            renderInput={(params) => <TextField {...params} fullWidth />}
          /> */}
        </LocalizationProvider>
      )}
    />
  );
}
