import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _possibleConstructorReturn from "@babel/runtime/helpers/esm/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/esm/getPrototypeOf";
import _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";

/**
 * datePanel.jsx
 *
 * @Author: apple
 * @Date: 2017/10/25 19:28
 */
import React, { Fragment, Component } from 'react';
import classNames from 'classnames';
import moment from 'moment';
import Button from '../../button';
var weeks = [0, 1, 2, 3, 4, 5];
var days = ['日', '一', '二', '三', '四', '五', '六'];
/**
 * @ignore
 * */

var DatePanel =
/*#__PURE__*/
function (_Component) {
  _inherits(DatePanel, _Component);

  function DatePanel() {
    var _getPrototypeOf2;

    var _this;

    _classCallCheck(this, DatePanel);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(DatePanel)).call.apply(_getPrototypeOf2, [this].concat(args)));

    _defineProperty(_assertThisInitialized(_this), "handleSelect", function (value) {
      return function (e) {
        e.stopPropagation();
        var _this$props = _this.props,
            onValueChange = _this$props.onValueChange,
            onPanelDateChange = _this$props.onPanelDateChange;
        onValueChange(value);
        onPanelDateChange(value);
      };
    });

    return _this;
  }

  _createClass(DatePanel, [{
    key: "render",
    value: function render() {
      var _this2 = this;

      var _this$props2 = this.props,
          panelDate = _this$props2.panelDate,
          value = _this$props2.value,
          prefixCls = _this$props2.prefixCls,
          disabledDate = _this$props2.disabledDate,
          onPanelTypeChange = _this$props2.onPanelTypeChange;
      var startOfMonth = moment(panelDate).date(1);
      var dateOfEnd = moment(panelDate).endOf('month').date();
      var dayOfStart = startOfMonth.day();
      var isRange = Array.isArray(value);
      return React.createElement(Fragment, null, React.createElement("thead", {
        className: "".concat(prefixCls, "-date-header")
      }, React.createElement("tr", null, days.map(function (day) {
        return React.createElement("th", {
          key: day
        }, day);
      }))), React.createElement("tbody", {
        className: "".concat(prefixCls, "-body")
      }, weeks.map(function (week, index) {
        return React.createElement("tr", {
          key: index
        }, days.map(function (day, dayIndex) {
          var _classNames;

          var dateIndex = week * 7 + dayIndex - dayOfStart;
          var item = moment(startOfMonth).add(dateIndex, 'day');
          var isToday = value && item.isSame(moment(), 'day');
          var isRangeStart = isRange && value[0] && item.isSame(value[0], 'day');
          var isRangeEnd = isRange && value[1] && item.isSame(value[1], 'day');
          var isSelectedSingleDay = !isRange && value && item.isSame(value, 'day');
          var inDifferentMonth = dateIndex < 0 || dateIndex > dateOfEnd - 1;
          var disabled = disabledDate && disabledDate(item);
          var classes = classNames("".concat(prefixCls, "-date-item"), (_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls, "-date-item-selected"), !inDifferentMonth && (isRangeStart || isRangeEnd || isSelectedSingleDay)), _defineProperty(_classNames, "".concat(prefixCls, "-date-item-start"), !inDifferentMonth && isRangeStart), _defineProperty(_classNames, "".concat(prefixCls, "-date-item-end"), !inDifferentMonth && isRangeEnd), _defineProperty(_classNames, "".concat(prefixCls, "-date-item-disabled"), disabled), _defineProperty(_classNames, "".concat(prefixCls, "-date-item-different-month"), inDifferentMonth), _defineProperty(_classNames, "".concat(prefixCls, "-date-item-today"), isToday), _classNames));
          var cellClasses = classNames("".concat(prefixCls, "-date-cell"), _defineProperty({}, "".concat(prefixCls, "-date-cell-in-range"), !inDifferentMonth && isRange && value.length === 2 && item.isBetween(value[0], value[1])));
          return React.createElement("td", {
            key: dayIndex,
            title: item.format('YYYY-MM-DD'),
            className: cellClasses
          }, React.createElement(Button, {
            className: classes,
            disabled: disabled,
            onClick: _this2.handleSelect(item),
            type: inDifferentMonth ? 'secondary' : null
          }, item.date()));
        }));
      })));
    }
  }]);

  return DatePanel;
}(Component);

_defineProperty(DatePanel, "propTypes", {});

_defineProperty(DatePanel, "defaultProps", {});

export default DatePanel;