import React, { useRef } from 'react';
import { DatePicker } from 'antd';
import { onBlurFn } from '../onBlurFn';
import { antiAssign } from '../../../../utils/utils';
import styles from './styles.less';

export const ApolloDate = (props: any) => {
    const { origin, onChange, showTime } = props;
    const selfProps = antiAssign(props, [
        'onChange',
        'columnConfig',
        'tableId',
        'cellRenderProps',
        'maxPopHeight',
        'getPopupContainer',
        'origin',
    ]);
    const isOpen = useRef();
    const changeValue = (date, dateString) => {
        if (typeof onChange === 'function') {
            if (typeof onBlurFn === 'function' && !isOpen.current) {
                onBlurFn({ ...props, value: date });
            }
            onChange(date, dateString);
        }
    };
    const dateOnBlurFn = (e: boolean) => {
        isOpen.current = e;
    };
    const timeOnBlurFn = (e: boolean) => {
        isOpen.current = e;
        if (typeof onBlurFn === 'function' && !e) {
            onBlurFn(props);
        }
    };
    return origin === 'editForm' ? (
        <DatePicker
            className={styles.date}
            // value={inputVal}
            dropdownClassName={styles.dropdownClassName}
            popupStyle={{ width: '350px' }}
            {...selfProps}
            onChange={changeValue}
            onOpenChange={showTime ? timeOnBlurFn : dateOnBlurFn}
        />
    ) : (
        <DatePicker
            className={styles.date}
            dropdownClassName={styles.dropdownClassName}
            popupStyle={{ width: '350px' }}
            {...selfProps}
            onChange={changeValue}
        />
    );
};
