import * as React from 'react';
import * as PropTypes from 'prop-types';
import weui from '../../utils/weui';

import classNames from '../../utils/classnames';

export default function DatePicker(props) {
  const { start, end, defaultValue, title, children, className } = props;
  const click = function () {
    let fixedDefaultValue = undefined;
    if (defaultValue) {
      const defaultDate = new Date(defaultValue);
      fixedDefaultValue = [
        defaultDate.getFullYear(),
        defaultDate.getMonth() + 1,
        defaultDate.getDate(),
      ];
    }

    weui.datePicker({
      ...{
        start,
        end,
        title,
      },
      ...(fixedDefaultValue ? { defaultValue: fixedDefaultValue } : {}),
    });
  };
  const cls = classNames({
    'weda-ui': true,
    'weda-date-picker': true,
    [className]: className,
  });

  return (
    <div className={cls} onClick={click}>
      {children}
    </div>
  );
}
