import "choerodon-ui/lib/button/style";
import _Button from "choerodon-ui/lib/button";
import "choerodon-ui/lib/date-picker/style";
import _DatePicker from "choerodon-ui/lib/date-picker";
import React, { Component } from 'react';
import { injectIntl, FormattedMessage } from 'react-intl';
import { withRouter } from 'react-router-dom';
import { Choerodon } from '@choerodon/boot';
import moment from 'moment';
import "./TimePicker.less";
var RangePicker = _DatePicker.RangePicker;
var ButtonGroup = _Button.Group;

function TimePicker(props) {
  var startTime = props.startTime,
      endTime = props.endTime,
      store = props.store,
      func = props.func,
      type = props.type,
      _onChange = props.onChange,
      unlimit = props.unlimit;

  var handleClick = function handleClick(val) {
    store.setEndTime(moment());
    store.setStartDate(null);
    store.setEndDate(null);

    switch (val) {
      case 'today':
        store.setStartTime(moment());
        _onChange && _onChange('today');
        func();
        break;

      case 'seven':
        store.setStartTime(moment().subtract(6, 'days'));
        _onChange && _onChange('seven');
        func();
        break;

      case 'thirty':
        store.setStartTime(moment().subtract(29, 'days'));
        _onChange && _onChange('thirty');
        func();
        break;

      default:
        store.setStartTime(moment().subtract(6, 'days'));
        func();
        break;
    }
  };

  var disabledDate = function disabledDate(current) {
    return current && current > moment().endOf('day');
  };

  return /*#__PURE__*/React.createElement("div", {
    className: "c7n-report-date-wrap"
  }, /*#__PURE__*/React.createElement("div", {
    className: "c7n-report-time-btn"
  }, /*#__PURE__*/React.createElement(ButtonGroup, null, /*#__PURE__*/React.createElement(_Button, {
    style: {
      backgroundColor: type === 'today' ? 'rgba(0,0,0,.08)' : ''
    },
    funcType: "flat",
    onClick: handleClick.bind(this, 'today')
  }, /*#__PURE__*/React.createElement(FormattedMessage, {
    id: "report.data.today"
  })), /*#__PURE__*/React.createElement(_Button, {
    style: {
      backgroundColor: type === 'seven' ? 'rgba(0,0,0,.08)' : ''
    },
    funcType: "flat",
    onClick: handleClick.bind(this, 'seven')
  }, /*#__PURE__*/React.createElement(FormattedMessage, {
    id: "report.data.seven"
  })), /*#__PURE__*/React.createElement(_Button, {
    style: {
      backgroundColor: type === 'thirty' ? 'rgba(0,0,0,.08)' : ''
    },
    funcType: "flat",
    onClick: handleClick.bind(this, 'thirty')
  }, /*#__PURE__*/React.createElement(FormattedMessage, {
    id: "report.data.thirty"
  })))), /*#__PURE__*/React.createElement("div", {
    className: "c7n-report-time-pick"
  }, /*#__PURE__*/React.createElement(RangePicker, {
    disabledDate: disabledDate,
    value: [startTime, endTime],
    allowClear: false,
    onChange: function onChange(date, dateString) {
      if (moment(dateString[1]).format() > moment(dateString[0]).add(29, 'days').format() && !unlimit) {
        Choerodon.prompt('报表暂支持最多查看30天，已自动截取开始日期后30天。');
        store.setStartTime(moment(dateString[0]));
        store.setEndTime(moment(dateString[0]).add(29, 'days'));
        store.setStartDate(moment(dateString[0]));
        store.setEndDate(moment(dateString[0]).add(29, 'days'));
      } else {
        store.setStartTime(moment(dateString[0]));
        store.setEndTime(moment(dateString[1]));
        store.setStartDate(moment(dateString[0]));
        store.setEndDate(moment(dateString[1]));
      }

      _onChange && _onChange('');
      func();
    }
  })));
}

export default withRouter(injectIntl(TimePicker));