/**
 * Field Date Picker
 *
 * @author: Brauer Ilya
 * @date: 2020-11-13
 */

import * as React from 'react';
import {Field, IFieldProps} from './Field';
import {DatePicker, DatePickerProps} from '../..';
import {Moment} from 'moment';

type IFieldInputProps = IFieldProps & {
    name?: string;
    type: 'dateTime' | 'date';
    disabled: boolean;
    readOnly: boolean;
    valueFormat: string;
    viewFormat: string;
    disabledDate?: (date: Moment) => boolean;
    onChange: (name: string, value: string) => void;
    value?: string;
    defaultValue?: string;
    error?: any;
    placeholder?: string;
    suffix?: any;
    allowClear?: boolean;
    i18nLocale: string;
    errorTimeMessage: string;
    errorDateMessage: string;
}

const defaultProps = {
    type: 'date',
    disabled: false as boolean,
    readOnly: false as boolean
};

export class FieldDatePicker extends React.PureComponent<IFieldInputProps> {

    static defaultProps = defaultProps;

    get inputChildren () {
        return (
            <DatePicker {...this.inputProps}/>
        );
    }

    get inputProps () {
        let props: DatePickerProps = {
            pickerType: this.props.type,
            placeholder: this.props.placeholder,
            value: this.props.value,
            isDisabled: this.props.disabled,
            isReadOnly: this.props.readOnly,
            onChange: this.onChange,
            viewFormat: this.props.viewFormat,
            defaultValue: this.props.defaultValue,
            valueFormat: this.props.valueFormat,
            disabledDate: this.props.disabledDate,
            i18nLocale: this.props.i18nLocale,
            errorTimeMessage: this.props.errorTimeMessage,
            errorDateMessage: this.props.errorDateMessage
        };

        return props;
    }

    onChange = (date: string) => {
        this.props.onChange(this.props.name || '', date);
    };

    override render () {
        return (
            <Field
                {...this.props}
            >
                {this.inputChildren}
            </Field>
        );
    }
}
