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;

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

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import Instance from './instance.js';

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 Instance(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.fail) _this2.props.fail({ errMsg: '已包含其它时间段' });
      },
      onCross: function onCross(e) {
        if (_this2.props.fail) _this2.props.fail({ 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,
        fail = _props.fail,
        others = _objectWithoutProperties(_props, ['multiple', 'startTime', 'endTime', 'times', 'onChange', 'fail']);

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

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

  onChange: PropTypes.func, // onChange(times)
  fail: PropTypes.func
} : {};