// eslint-disable-next-line no-unused-vars
import React from 'react';
import moment from 'moment';
// eslint-disable-next-line no-unused-vars
import { DatePicker } from 'antd';
const FORMATS = {
  day: 'YYYY-MM-DD',
  week: 'YYYY-wo',
  month: 'YYYY-MM',
  year: 'YYYY',
};
const STANTFORMAT = 'YYYY-MM-DD 00:00:00';
const ENDFORMAT = 'YYYY-MM-DD 23:59:59';
const DatePickerCustom = (props) => {
  const modes = props.picker ? (props.picker !== 'date' ? props.picker : 'day') : 'day';
  const selectedDate = props.value ? props.value : new Date();
  const onChange = (date, dateString) => {
    const dateTime = moment(date).format('YYYY-MM-DD');
    let startTime = null;
    let endTime = null;
    switch (modes) {
      case 'day':
        startTime = moment(dateTime);
        endTime = moment(dateTime);
        break;
      case 'week':
        startTime = moment(dateTime).startOf(modes);
        endTime = moment(dateTime).endOf(modes);
        break;
      case 'month':
        startTime = moment(dateTime).startOf(modes);
        endTime = moment(dateTime).endOf(modes);
        break;
      case 'year':
        startTime = moment(dateTime).startOf(modes);
        endTime = moment(dateTime).endOf(modes);
        break;
      default:
        startTime = moment(dateTime);
        endTime = moment(dateTime);
        break;
    }
    props.onChange(
      { startTime: startTime.format(STANTFORMAT), endTime: endTime.format(ENDFORMAT) },
      dateString,
    );
  };
  const customFormat = (value) => {
    let name = null;
    switch (modes) {
      case 'day':
        name = value.format(FORMATS[modes]);
        if (name === getTime(0, 'days', FORMATS[modes])) {
          name = '今天';
        } else if (name === getTime(1, 'days', FORMATS[modes])) {
          name = '昨天';
        }
        break;
      case 'week':
        // eslint-disable-next-line no-use-before-define
        name = getMonthWeek(value);
        // eslint-disable-next-line no-use-before-define
        if (value.format('YYYY-wo') === getTime(0, 'week', FORMATS[modes])) {
          name = '本周';
        } else if (value.format('YYYY-wo') === getTime(1, 'week', FORMATS[modes])) {
          name = '上周';
        }
        break;
      case 'month':
        name = value.format(FORMATS[modes]);
        if (name === getTime(0, 'month', FORMATS[modes])) {
          name = '本月';
        } else if (name === getTime(1, 'month', FORMATS[modes])) {
          name = '上月';
        }
        break;
      case 'year':
        name = value.format(FORMATS[modes]);
        if (name === getTime(0, 'year', FORMATS[modes])) {
          name = '今年';
        } else if (name === getTime(1, 'year', FORMATS[modes])) {
          name = '去年';
        }
        break;
      default:
        break;
    }
    return name;
  };
  const getTime = (num, type, format) => {
    return moment(new Date()).subtract(num, type).format(format);
  };
  const getMonthWeek = (value) => {
    const a = value.format('YYYY'),
      b = value.format('MM'),
      c = value.format('DD');
    let date = new Date(a, parseInt(b) - 1, c),
      w = date.getDay(),
      d = date.getDate();
    if (w == 0) {
      w = 7;
    }
    let config = {
      getMonth: moment(date).format('MM'),
      getYear: date.getFullYear(),
      getWeek: Math.ceil((d + 6 - w) / 7),
    };
    return '第' + config.getWeek + '周(' + config.getYear + '/' + config.getMonth + ')';
  };
  return (
    <DatePicker
      {...props}
      defaultValue={selectedDate ? moment(selectedDate) : ''}
      format={customFormat}
      onChange={onChange}
    />
  );
};

export default DatePickerCustom;
