"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 _classnames = _interopRequireDefault(require("classnames"));

var _rectBtn = _interopRequireDefault(require("../button/rect-btn"));

var _commonShare = _interopRequireDefault(require("../common-share"));

var _card = _interopRequireDefault(require("./card"));

var _request = require("../../utils/request");

var _tools = require("../../utils/tools");

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 _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

function _objectDestructuringEmpty(obj) { if (obj == null) throw new TypeError("Cannot destructure undefined"); }

function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

var preCls = "weekly-report-card";

var getImageBase64ByUrl = function getImageBase64ByUrl(base64) {
  return (0, _request.rpost)("v1")("/okr/share/imageDownLoadDfs", {
    SharePicLink: base64
  }).then(function (res) {
    return res.Data;
  });
};

function WeeklyReportPreview(_ref) {
  var onShare = _ref.onShare,
      onClose = _ref.onClose,
      data = _ref.data;
  var cardRef = (0, _react.useRef)();
  return /*#__PURE__*/_react["default"].createElement("div", {
    style: {
      position: "relative"
    }
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])("".concat(preCls, "__close")),
    onClick: onClose
  }), /*#__PURE__*/_react["default"].createElement("div", {
    style: {
      position: "absolute",
      left: "-99999999px"
    }
  }, /*#__PURE__*/_react["default"].createElement(_card["default"], {
    data: data,
    ref: cardRef
  })), /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(preCls, "-wrapper")
  }, /*#__PURE__*/_react["default"].createElement(_card["default"], {
    data: data
  })), /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(preCls, "__button-container")
  }, /*#__PURE__*/_react["default"].createElement(_rectBtn["default"], {
    className: "".concat(preCls, "__download"),
    onClick: /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
      var _cardRef$current;

      var img, url, a;
      return regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              _context.next = 2;
              return cardRef === null || cardRef === void 0 ? void 0 : (_cardRef$current = cardRef.current) === null || _cardRef$current === void 0 ? void 0 : _cardRef$current.getImage();

            case 2:
              img = _context.sent;
              _context.next = 5;
              return getImageBase64ByUrl(img.split(",")[1]);

            case 5:
              url = _context.sent;
              a = document.createElement("a");
              a.href = url.obj;
              a.download = "okr.png";
              a.click();

            case 10:
            case "end":
              return _context.stop();
          }
        }
      }, _callee);
    }))
  }, (0, _getLocale.getLocale)("OKR_MyO_Popup_Download")), /*#__PURE__*/_react["default"].createElement(_rectBtn["default"], {
    className: "".concat(preCls, "__share"),
    onClick: /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() {
      var img;
      return regeneratorRuntime.wrap(function _callee2$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              _context2.next = 2;
              return cardRef.current.getImage();

            case 2:
              img = _context2.sent;
              onShare(img);

            case 4:
            case "end":
              return _context2.stop();
          }
        }
      }, _callee2);
    }))
  }, (0, _getLocale.getLocale)("Mod_ShareFeed"))));
}

function WeeklyReportModal(_ref4) {
  _objectDestructuringEmpty(_ref4);

  var _useState = (0, _react.useState)(!!(0, _tools.getQueryString)().redisKey),
      _useState2 = _slicedToArray(_useState, 2),
      visible = _useState2[0],
      setVisible = _useState2[1];

  var _useState3 = (0, _react.useState)((0, _tools.getQueryString)().redisKey || null),
      _useState4 = _slicedToArray(_useState3, 2),
      redisKey = _useState4[0],
      setRedisKey = _useState4[1];

  var _useState5 = (0, _react.useState)(),
      _useState6 = _slicedToArray(_useState5, 2),
      data = _useState6[0],
      setData = _useState6[1];

  (0, _react.useEffect)(function () {
    var _window, _window$Talent, _window2, _window2$Talent;

    (_window = window) === null || _window === void 0 ? void 0 : (_window$Talent = _window.Talent) === null || _window$Talent === void 0 ? void 0 : _window$Talent.app.vent.off("open-weekly-report-modal");
    (_window2 = window) === null || _window2 === void 0 ? void 0 : (_window2$Talent = _window2.Talent) === null || _window2$Talent === void 0 ? void 0 : _window2$Talent.app.vent.on("open-weekly-report-modal", function (redisKey) {
      if (visible) return;
      setRedisKey(redisKey);
      setVisible(true);
    });
    return function () {
      var _window3, _window3$Talent;

      return (_window3 = window) === null || _window3 === void 0 ? void 0 : (_window3$Talent = _window3.Talent) === null || _window3$Talent === void 0 ? void 0 : _window3$Talent.app.vent.off("open-weekly-report-modal");
    };
  }, [visible]);
  (0, _react.useEffect)(function () {
    if (!visible) return;
    var current = new Date();
    var dateTimeTicks = "".concat(current.getFullYear(), "/").concat(current.getMonth() + 1, "/").concat(current.getDate());
    var param = "redisKey=".concat(redisKey || "", "&dateTimeTicks=").concat(dateTimeTicks || "");

    if (data) {
      setData(null);
    }

    (0, _request.rget)("v1")("okr/wwsummary/get?".concat(param)).then(function (res) {
      if (res.Code == 1) {
        setData(res.Data);
      } else {
        setVisible(false);
      }
    });
  }, [redisKey, visible]);
  (0, _react.useEffect)(function () {
    if ((0, _tools.getQueryString)().redisKey) {
      location.replace(decodeURIComponent(location.href).replace(/&?redisKey=\S*(&|$)/g, ""));
    }

    if (visible) {
      (0, _request.rdelete)("v1")("Indicator/Clear?message_type=0&indicator_type=mWeekStatisticIndicator");
    }
  }, [visible]);
  if (!data) return null;
  return /*#__PURE__*/_react["default"].createElement(_commonShare["default"], {
    uid: new Date().getTime(),
    visible: visible,
    onClose: function onClose() {
      return setVisible(false);
    },
    defaultContent: "Show \u4E00\u4E0B\u6211\u8FD9\u5468\u7684\u52AA\u529B\uFF5E",
    shareComponent: function shareComponent(onShare) {
      return /*#__PURE__*/_react["default"].createElement(WeeklyReportPreview, {
        onShare: onShare,
        onClose: function onClose() {
          return setVisible(false);
        },
        data: data
      });
    }
  });
}

var _default = WeeklyReportModal;
exports["default"] = _default;