'use strict';

exports.__esModule = true;
exports.default = undefined;

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

var _class, _temp;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _instance = require('./instance.js');

var _instance2 = _interopRequireDefault(_instance);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var Timepart = (_temp = _class = function (_Component) {
  _inherits(Timepart, _Component);

  function Timepart(props) {
    _classCallCheck(this, Timepart);

    var _this = _possibleConstructorReturn(this, _Component.call(this, props));

    _this.update = function () {
      if (!_this.props.times) return;
      for (var i = 0, time; time = _this.props.times[i++];) {
        // eslint-disable-line
        if (time.className && time.startTime && time.endTime) {
          _this.instance.addProgress(time.startTime, time.endTime, time.className, time.data || null, time.cover || false);
        }
      }
      // onChange
      _this.onChange();
    };

    _this.onChange = function () {
      var times = _this.instance.getTimes();
      var part = _this.instance.container.querySelector('.timepart-part.active');
      if (part) {
        times.push({
          className: part.className.replace('timepart-part ', ''),
          startTime: part.startTime,
          endTime: part.endTime,
          data: part.getAttribute('data') || ''
        });
      }
      if (_this.props.onChange) _this.props.onChange(times);
    };

    return _this;
  }

  Timepart.prototype.componentDidUpdate = function componentDidUpdate(prevProps) {
    if (this.props.times && prevProps.times.length !== this.props.times.length) {
      this.update();
    }
  };

  Timepart.prototype.componentDidMount = function componentDidMount() {
    var _this2 = this;

    var clickCount = 0;
    var instance = new _instance2.default(this.$el, {
      startTime: this.props.startTime,
      endTime: this.props.endTime,
      onClickPart: function onClickPart(s) {
        if (s.target.classList.contains('active')) {
          s.target.classList.remove('active');
          clickCount = 0;
          // onChange
          _this2.onChange();
          return;
        }
        // 如果不允许多选,发现有已选中的先清除
        if (!_this2.props.multiple) {
          if (s.container.querySelectorAll('.progress-legend.active').length) {
            s.removeProgress('active');
            clickCount = 0;
            // onChange
            _this2.onChange();
            return;
          }
        }
        // 选中
        clickCount++;
        if (clickCount === 1) {
          // 如果点击了一次
          _this2.part1 = s.target;
          _this2.part1.classList.add('active');
          // onChange
          _this2.onChange();
        } else if (clickCount === 2) {
          // 如果点击了两次
          _this2.part1.classList.remove('active');
          _this2.part2 = s.target;
          var times = s.getTimesByParts(_this2.part1, _this2.part2);
          s.addProgress(times.startTime, times.endTime, 'active');
          clickCount = 0;
          // onChange
          _this2.onChange();
        }
      },
      onContain: function onContain(e) {
        clickCount = 0;
        if (_this2.props.onError) _this2.props.onError(e, { errMsg: '已包含其它时间段' });
      },
      onCross: function onCross(e) {
        if (_this2.props.onError) _this2.props.onError(e, { errMsg: '与其它时间段相交' });
      },
      onClickProgress: function onClickProgress(s) {
        if (s.target.classList.contains('active')) {
          // 根据data-id删除
          var id = s.target.getAttribute('data-id');
          [].slice.call(s.container.querySelectorAll('.timepart-progress[data-id="' + id + '"]')).forEach(function (el) {
            el.parentNode.removeChild(el);
          });
          // onChange
          _this2.onChange();
        }
      },
      onClickWhite: function onClickWhite(s) {}
    });
    this.instance = instance;
    this.update();
  };

  Timepart.prototype.render = function render() {
    var _this3 = this;

    var _props = this.props,
        multiple = _props.multiple,
        startTime = _props.startTime,
        endTime = _props.endTime,
        times = _props.times,
        onChange = _props.onChange,
        onError = _props.onError,
        others = _objectWithoutProperties(_props, ['multiple', 'startTime', 'endTime', 'times', 'onChange', 'onError']);

    return _react2.default.createElement('div', _extends({ ref: function ref(el) {
        _this3.$el = el;
      } }, others, { className: 'timepart' + (others.className ? ' ' + others.className : '') }));
  };

  return Timepart;
}(_react.Component), _class.defaultProps = {
  startTime: '07:00',
  endTime: '22:00'
}, _temp);
exports.default = Timepart;
Timepart.propTypes = process.env.NODE_ENV !== "production" ? {
  multiple: _propTypes2.default.bool, // 是否支持多选
  startTime: _propTypes2.default.string,
  endTime: _propTypes2.default.string,
  times: _propTypes2.default.array, // [{className: string, startTime: 'hh:ss', endTime: 'hh:ss', data: string, cover: bool}]

  onChange: _propTypes2.default.func, // onChange(times)
  onError: _propTypes2.default.func
} : {};
module.exports = exports['default'];