"use strict";

exports.__esModule = true;
exports["default"] = void 0;

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

var _Popover = _interopRequireDefault(require("./../Popover"));

var _Alert = _interopRequireDefault(require("./../Alert"));

var _InputDate = _interopRequireDefault(require("./../InputDate"));

var _locale = _interopRequireDefault(require("./../locale"));

var _helper = _interopRequireDefault(require("./helper"));

var _excluded = ["show", "top", "left", "startDate", "endDate", "ranges", "rangeErrMsg", "onHide", "onChange", "maskAttribute"];

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 _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

var range = 90; // 日期快捷选择弹窗

var DatePopover = (0, _react.forwardRef)(function (_ref, ref) {
  var _ref2;

  var _ref$show = _ref.show,
      show = _ref$show === void 0 ? false : _ref$show,
      _ref$top = _ref.top,
      top = _ref$top === void 0 ? 88 : _ref$top,
      _ref$left = _ref.left,
      left = _ref$left === void 0 ? 10 : _ref$left,
      startDate = _ref.startDate,
      endDate = _ref.endDate,
      _ref$ranges = _ref.ranges,
      ranges = _ref$ranges === void 0 ? (_ref2 = {}, _ref2[(0, _locale["default"])('今天', 'datepopover_today')] = [new Date().format('YYYY-MM-DD'), new Date().format('YYYY-MM-DD')], _ref2[(0, _locale["default"])('昨天', 'datepopover_yesterday')] = [new Date().prevDate().format('YYYY-MM-DD'), new Date().prevDate().format('YYYY-MM-DD')], _ref2[(0, _locale["default"])('本月', 'datepopover_this_month')] = [new Date().firstMonthDate().format('YYYY-MM-DD'), new Date().format('YYYY-MM-DD')], _ref2[(0, _locale["default"])('上月', 'datepopover_last_month')] = [new Date().prevMonth().firstMonthDate().format('YYYY-MM-DD'), new Date().prevMonth().lastMonthDate().format('YYYY-MM-DD')], _ref2[(0, _locale["default"])('最近7天', 'datepopover_last_days', ['7'])] = [new Date().prevDate(7).format('YYYY-MM-DD'), new Date().format('YYYY-MM-DD')], _ref2[(0, _locale["default"])('最近30天', 'datepopover_last_days', ['30'])] = [new Date().prevDate(30).format('YYYY-MM-DD'), new Date().format('YYYY-MM-DD')], _ref2[(0, _locale["default"])('自定义时间', 'datepopover_custom_date')] = 90, _ref2) : _ref$ranges,
      rangeErrMsg = _ref.rangeErrMsg,
      onHide = _ref.onHide,
      onChange = _ref.onChange,
      maskAttribute = _ref.maskAttribute,
      others = _objectWithoutPropertiesLoose(_ref, _excluded);

  // 自定义日期数据
  var _useState = (0, _react.useState)(false),
      customDialog = _useState[0],
      setCustomDialog = _useState[1];

  var _useState2 = (0, _react.useState)(startDate),
      customStartDate = _useState2[0],
      setCustomStartDate = _useState2[1];

  var _useState3 = (0, _react.useState)(endDate),
      customEndDate = _useState3[0],
      setCustomEndDate = _useState3[1];

  (0, _react.useEffect)(function () {
    setCustomStartDate(startDate);
    setCustomEndDate(endDate);
  }, [startDate, endDate]); // 修改日期

  function handleChangeDate(e, startDate, endDate) {
    if (onChange) {
      onChange(e, _helper["default"].getDateName(startDate, endDate), {
        startDate: startDate,
        endDate: endDate
      });
    }

    if (onHide) onHide();
  }
  /**
   * 自定义时间
   */
  // 点击自定义取消


  function handleCustomCancel() {
    setCustomDialog(false);
    setCustomStartDate(startDate);
    setCustomEndDate(endDate);
  } // 点击自定义确定


  function handleCustomSubmit(e) {
    if (!_helper["default"].isRange(customStartDate, customEndDate, range, rangeErrMsg)) {
      return;
    }

    if (onChange) {
      onChange(e, _helper["default"].getDateName(customStartDate, customEndDate), {
        startDate: customStartDate,
        endDate: customEndDate
      });
    }

    setCustomDialog(false);
  } // 显示自定义弹框


  function showCustomDialog(currentRange) {
    // 设置区间不能超过?天
    range = currentRange;
    setCustomDialog(true);
    if (onHide) onHide();
  } // 修改自定义开始时间


  function handleCustomStartDate(e, value) {
    setCustomStartDate(value);
  } // 修改自定义结束时间


  function handleCustomEndDate(e, value) {
    setCustomEndDate(value);
  }

  if (!ranges || typeof ranges !== 'object') {
    return null;
  }

  return /*#__PURE__*/_react["default"].createElement(_react.Fragment, null, /*#__PURE__*/_react["default"].createElement(_Popover["default"], _extends({
    ref: ref,
    show: show
  }, others, {
    className: "datepopover " + (others.className || 'top-left'),
    style: Object.assign({
      left: left + "px",
      top: top + "px"
    }, others.style || {}),
    maskAttribute: Object.assign({}, maskAttribute || {}, {
      onClick: onHide
    })
  }), Object.entries(ranges).map(function (item) {
    var _React$createElement;

    var key = item[0];
    var range = item[1];

    if (typeof range === 'number') {
      return /*#__PURE__*/_react["default"].createElement("div", {
        className: "datepopover-option",
        key: key,
        onClick: function onClick() {
          return showCustomDialog(range);
        }
      }, (0, _locale["default"])('自定义时间', 'datepopover_custom_date'));
    }

    return /*#__PURE__*/_react["default"].createElement("div", (_React$createElement = {
      className: "border-b",
      key: key
    }, _React$createElement["className"] = "datepopover-option", _React$createElement.onClick = function onClick(e) {
      return handleChangeDate(e, range[0], range[1]);
    }, _React$createElement), key);
  })), /*#__PURE__*/_react["default"].createElement(_Alert["default"], {
    caption: (0, _locale["default"])('自定义时间', 'datepopover_custom_date'),
    show: customDialog,
    submitAttribute: {
      onClick: handleCustomSubmit
    },
    cancelAttribute: {
      onClick: handleCustomCancel
    }
  }, /*#__PURE__*/_react["default"].createElement(_InputDate["default"], {
    max: customEndDate,
    value: customStartDate || startDate,
    onChange: handleCustomStartDate,
    placeholder: (0, _locale["default"])('请选择'),
    className: "datepopover-dialog-inputbox",
    pickerProps: {
      maskAttribute: {
        style: {
          zIndex: '11'
        }
      }
    }
  }), /*#__PURE__*/_react["default"].createElement(_InputDate["default"], {
    min: customStartDate,
    value: customEndDate || endDate,
    onChange: handleCustomEndDate,
    placeholder: (0, _locale["default"])('请选择'),
    className: "datepopover-dialog-inputbox",
    pickerProps: {
      maskAttribute: {
        style: {
          zIndex: '11'
        }
      }
    }
  })));
});
var _default = DatePopover;
exports["default"] = _default;
module.exports = exports.default;