import { formatDate } from "date-fns";

export const DateInput = ({
	value,
	onChange,
	...props
}: Omit<React.InputHTMLAttributes<HTMLInputElement>, "value" | "onChange"> & {
	value?: Date | null;
	onChange?: (date: Date) => void;
}) => {
	return (
		<input
			type="datetime-local"
			className="input"
			value={formatDate(value || new Date(), "yyyy-MM-dd HH:mm")}
			onChange={(e) => {
				const date = e.target.value ? new Date(e.target.value) : null;
				if (!date) return;
				onChange?.(date);
			}}
			{...props}
		/>
	);
};
