// 自定义时间组件
import React, { useState, useEffect, useMemo } from 'react'
import { DatePicker, Input } from 'antd'
import moment from 'moment'
import locale from 'antd/lib/date-picker/locale/zh_CN'

const DateTime = (props) => {

  const { value, onChange, schema, addons } = props
  const { disabled, format, presetValue, placeholder, options, currentDate, defaultCurrent } = schema

  useEffect(() => {
    if (addons) {
      let value = presetValue || (options === '默认为当前时间' ? moment(new Date()).format('YYYY-MM-DD HH:ss:mm') : null)
      addons.setValue(addons.dataPath, value)
    } else {
      onChange(presetValue)
    }
  }, [presetValue, options])

  const disabledDate = (current) => {
    if (options === '不超过当前时间') {
      return current && current > moment()
    } else {
      return false
    }
  }

  const dateChange = (date, dateStr) => {
    if (date && date._d) {
      onChange(moment(date._d).format('YYYY-MM-DD HH:ss:mm'))
    } else {
      onChange(null)
    }
  }

  return (
    <DatePicker
      disabledDate={disabledDate}
      picker={format}
      disabled={disabled}
      placeholder={!disabled ? placeholder : ''}
      value={value ? moment(value) : null}
      onChange={dateChange}
      showNow
      showTime={format === 'dateTime'}
      style={{ width: '100%' }}
      locale={locale}
      getPopupContainer={(targterNode) => targterNode.parentElement || document.body}
    />
  );

};

export default DateTime