"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _react = _interopRequireWildcard(require("react"));

var _reactDom = require("react-dom");

var _moment = _interopRequireDefault(require("moment"));

var _dateRangePicker = _interopRequireDefault(require("../date-range-picker"));

var _popup = _interopRequireDefault(require("../popup"));

require("./index.css");

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

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _extends() { _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; }; return _extends.apply(this, arguments); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

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

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }

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

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

var trigger = {
  onClick: "click",
  onMouseDown: "mousedonwn",
  onMouseUp: "mouseup"
};

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

  var _super = _createSuper(RangePickerPop);

  function RangePickerPop(p) {
    var _this;

    _classCallCheck(this, RangePickerPop);

    _this = _super.call(this, p);

    _this.handleSelect = function (range) {
      _this.setState(range);

      _this.props.onSelect && _this.props.onSelect();
    };

    _this.getDate = function (format) {
      var _this$state = _this.state,
          startDate = _this$state.startDate,
          endDate = _this$state.endDate;

      if (format) {
        return {
          startDate: startDate.format(_this.props.format),
          endDate: endDate.format(_this.props.format)
        };
      }

      return {
        startDate: (0, _moment["default"])(startDate).toDate(),
        endDate: (0, _moment["default"])(endDate).toDate()
      };
    };

    _this.handleClick = function (e) {
      if (_this.props.triggerStop) e.stopPropagation();

      if (_this.props.controlled) {
        var _this$props$defaultRa, _this$props$defaultRa2;

        _this.setState({
          startDate: (0, _moment["default"])((_this$props$defaultRa = _this.props.defaultRange) === null || _this$props$defaultRa === void 0 ? void 0 : _this$props$defaultRa.startDate),
          endDate: (0, _moment["default"])((_this$props$defaultRa2 = _this.props.defaultRange) === null || _this$props$defaultRa2 === void 0 ? void 0 : _this$props$defaultRa2.endDate)
        });
      } // this.refs.pop.open();

    };

    _this.setRange = function (range) {
      _this.setState({
        startDate: range ? (0, _moment["default"])(range.startDate) : (0, _moment["default"])(),
        endDate: range ? (0, _moment["default"])(range.endDate) : (0, _moment["default"])()
      });
    };

    _this.handleConfirm = function () {
      var onConfirm = _this.props.onConfirm;
      var isValid = true;
      isValid = onConfirm && onConfirm({
        startDate: _this.state.startDate ? _this.state.startDate.format(_this.props.format) : "",
        endDate: _this.state.endDate ? _this.state.endDate.format(_this.props.format) : ""
      });
      if (isValid === false) return;

      _this.refs.pop.close();
    };

    _this.handleCancel = function () {
      _this.refs.pop.close();
    };

    _this.handleExtraClick = function () {
      var res = _this.props.onExtraBtnClick({
        startDate: _this.state.startDate.format(_this.props.format),
        endDate: _this.state.endDate.format(_this.props.format)
      });

      if (res !== false) {
        _this.refs.pop.close();
      }
    };

    _this.show = function (el) {
      // @ts-ignore
      _this.refs.pop.open();

      document.addEventListener(trigger[_this.props.triggerMethod], _this.hide);
    };

    _this.hide = function (e) {
      if (e && e.srcElement) {
        var pageContainer = document.getElementById("bs_layout_container");
        if (pageContainer && !pageContainer.contains(e.srcElement)) return;
        var container = (0, _reactDom.findDOMNode)(_this.container);
        if (container && container.contains(e.srcElement)) return;
      }

      _this.refs.pop.close();

      document.removeEventListener(trigger[_this.props.triggerMethod], _this.hide);
    };

    if (p.defaultRange) {
      _this.state = {
        startDate: p.defaultRange.startDate != null ? (0, _moment["default"])(p.defaultRange.startDate) : null,
        endDate: p.defaultRange.endDate != null ? (0, _moment["default"])(p.defaultRange.endDate) : null
      };
    } else {
      _this.state = {
        startDate: (0, _moment["default"])(),
        endDate: (0, _moment["default"])()
      };
    }

    return _this;
  }

  _createClass(RangePickerPop, [{
    key: "componentWillReceiveProps",
    value: function componentWillReceiveProps(nextProps) {
      if (nextProps.defaultRange) {
        this.setState({
          startDate: nextProps.defaultRange.startDate != null ? (0, _moment["default"])(nextProps.defaultRange.startDate) : null,
          endDate: nextProps.defaultRange.endDate != null ? (0, _moment["default"])(nextProps.defaultRange.endDate) : null
        });
      }
    }
  }, {
    key: "render",
    value: function render() {
      var _this2 = this;

      var _this$props = this.props,
          triggerMethod = _this$props.triggerMethod,
          extraBtnText = _this$props.extraBtnText,
          onExtraBtnClick = _this$props.onExtraBtnClick,
          container = _this$props.container,
          style = _this$props.style,
          requireRangeCorrect = _this$props.requireRangeCorrect,
          _this$props$popupPlac = _this$props.popupPlacement,
          popupPlacement = _this$props$popupPlac === void 0 ? "bottom" : _this$props$popupPlac;

      var obj = _defineProperty({}, triggerMethod, this.handleClick);

      var sure = _defineProperty({}, triggerMethod, this.handleConfirm);

      var cancle = _defineProperty({}, triggerMethod, this.handleCancel);

      var extra = _defineProperty({}, triggerMethod, this.handleExtraClick);

      var popup = /*#__PURE__*/_react["default"].createElement("div", {
        className: "tita-ranger-pop-inner",
        onMouseDown: function onMouseDown(e) {
          _this2.props.stopPropagation && _this2.props.stopPropagation(e);
        },
        ref: function ref(r) {
          return _this2.container = r;
        }
      }, /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement(_dateRangePicker["default"], {
        defaultRange: [this.state.startDate, this.state.endDate],
        onSelect: this.handleSelect,
        triggerMethod: triggerMethod,
        container: container,
        requireRangeCorrect: requireRangeCorrect
      })), /*#__PURE__*/_react["default"].createElement("div", {
        className: "tita-range-pop-action"
      }, onExtraBtnClick && /*#__PURE__*/_react["default"].createElement("a", _extends({
        className: "range-primry-btn"
      }, extra, {
        style: {
          "float": "left",
          marginLeft: "10px"
        }
      }), extraBtnText), /*#__PURE__*/_react["default"].createElement("a", _extends({
        className: "range-default-btn"
      }, cancle, {
        style: {
          marginRight: "10px"
        }
      }), getLocale("Mod_Cancel")), /*#__PURE__*/_react["default"].createElement("a", _extends({
        className: "range-primry-btn"
      }, sure), getLocale("OKR_MyO_Butt_Determine"))));

      return /*#__PURE__*/_react["default"].createElement("div", {
        style: style || {
          display: "inline-block"
        },
        className: this.props.className,
        onMouseDown: function onMouseDown(e) {
          return _this2.props.stopPropagation && _this2.props.stopPropagation(e);
        }
      }, /*#__PURE__*/_react["default"].createElement(_popup["default"], {
        ref: "pop",
        popup: popup,
        popupPlacement: popupPlacement
      }, /*#__PURE__*/_react["default"].createElement("div", _extends({
        ref: "trigger"
      }, obj), this.props.children)));
    }
  }]);

  return RangePickerPop;
}(_react.Component);

exports["default"] = RangePickerPop;
RangePickerPop.defaultProps = {
  format: "YYYY-MM-DD",
  triggerMethod: "onClick",
  extraBtnText: "左侧按钮",
  triggerStop: false,
  cover: false,
  requireRangeCorrect: true // 是否校验选择的范围

};