"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"] = _default;

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

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

var _toast = _interopRequireDefault(require("../toast"));

var _selectCycle = _interopRequireDefault(require("./select-cycle"));

var _requestApis = require("./request-apis");

var _getLocale = require("../../utils/getLocale");

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 _default(props) {
  var okrId = props.okrId,
      cycle = props.cycle,
      setCycle = props.setCycle,
      renderInnerContent = props.renderInnerContent,
      _props$canEdit = props.canEdit,
      canEdit = _props$canEdit === void 0 ? true : _props$canEdit,
      _props$getCycleRange = props.getCycleRange,
      getCycleRange = _props$getCycleRange === void 0 ? false : _props$getCycleRange,
      popupAlign = props.popupAlign;
  var cycleRef = (0, _react.useRef)(); // const { okrInfo } = useContext(CommonContext);

  (0, _react.useEffect)(function () {
    // 若果外部提供了配置，则不调用默认季度配置
    if (cycle && cycle.yqmNum != undefined) return;
    (0, _requestApis.getCycleSetting)().then(function (res) {
      var cycleData;

      if (res.isOpenQuarter) {
        cycleData = {
          cycleType: "2",
          annualNum: "".concat((0, _moment["default"])().get("year")),
          yqmNum: "".concat((0, _moment["default"])().quarter()),
          // 默认当前季度
          startDate: (0, _moment["default"])().startOf("quarter").format("YYYY/MM/DD"),
          endDate: (0, _moment["default"])().endOf("quarter").format("YYYY/MM/DD")
        };
      } else {
        cycleData = {
          cycleType: "3",
          annualNum: "".concat((0, _moment["default"])().get("year")),
          yqmNum: "".concat((0, _moment["default"])().get("month") + 1),
          // 默认当前季度
          startDate: (0, _moment["default"])().startOf("month").format("YYYY/MM/DD"),
          endDate: (0, _moment["default"])().endOf("month").format("YYYY/MM/DD")
        };
      }

      setCycle(cycleData);
    });
  }, []);

  var handleChangeCyecle = function handleChangeCyecle(data) {
    (0, _requestApis.updateOCyecle)(okrId, data).then(function (resp) {
      if (resp.Code === 1) {
        _toast["default"].Success((0, _getLocale.getLocale)('Mod_Successfuloperat'));

        setCycle(data);
      } else {
        _toast["default"].Error(resp.Message);
      }
    });
  };

  return /*#__PURE__*/_react["default"].createElement(_selectCycle["default"], {
    renderInnerContent: renderInnerContent,
    onChange: handleChangeCyecle,
    container: cycleRef.current && cycleRef.current,
    cycle: cycle,
    canEdit: canEdit,
    getCycleRange: getCycleRange,
    popupAlign: popupAlign
  });
}