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";

/**
 * yearPanel.jsx
 *
 * @Author: apple
 * @Date: 2017/10/25 19:27
 */
import React, { Component } from 'react';
import classNames from 'classnames';
import moment from 'moment';
import Button from '../../button';
/**
 * @ignore
 * */

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

  function YearPanel() {
    var _getPrototypeOf2;

    var _this;

    _classCallCheck(this, YearPanel);

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

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

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

        if (selectType === 'year') {
          onValueChange(value);
        } else {
          onPanelTypeChange('month');
        }

        onPanelDateChange(value);
      };
    });

    return _this;
  }

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

      var _this$props2 = this.props,
          value = _this$props2.value,
          panelDate = _this$props2.panelDate,
          disabledYear = _this$props2.disabledYear,
          prefixCls = _this$props2.prefixCls,
          rangeType = _this$props2.rangeType;
      var isRange = Array.isArray(value);
      var start = moment(panelDate).subtract(panelDate.year() % 10 + 1, 'year');
      return React.createElement("tbody", {
        className: "".concat(prefixCls, "-body")
      }, [0, 1, 2, 4].map(function (_, index) {
        return React.createElement("tr", {
          key: index
        }, [0, 1, 2].map(function (y, yearIndex) {
          var _classNames;

          var selected = false;
          var year = yearIndex + 3 * index;
          var item = moment(start).add(year, 'year');

          if (isRange && rangeType === 'start') {
            selected = value[0] && item.isSame(value[0], 'year');
          } else if (isRange && rangeType === 'end') {
            selected = value[1] && item.isSame(value[1], 'year');
          } else {
            selected = value && item.isSame(value, 'year');
          }

          var disabled = disabledYear && disabledYear(item);
          var classes = classNames("".concat(prefixCls, "-year-item"), (_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls, "-year-item-selected"), selected), _defineProperty(_classNames, "".concat(prefixCls, "-year-item-disabled"), disabled), _classNames));
          var cellClasses = classNames("".concat(prefixCls, "-year-cell"));
          return React.createElement("td", {
            key: year,
            className: cellClasses
          }, React.createElement(Button, {
            className: classes,
            onClick: _this2.handleSelect(item),
            disabled: disabled,
            shape: "none",
            type: year === 0 || year === 1 ? 'secondary' : null
          }, item.year()));
        }));
      }));
    }
  }]);

  return YearPanel;
}(Component);

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

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

export default YearPanel;