"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 _html2canvas = _interopRequireDefault(require("html2canvas"));

var _board = _interopRequireDefault(require("./board"));

var _helpers = require("../../utils/helpers");

var _darwAvatar = require("./common-painter/darwAvatar");

var _drawName = _interopRequireDefault(require("./common-painter/drawName"));

var _statistics = _interopRequireDefault(require("./statistics"));

var _bsGlobal = require("../../utils/bs-global");

require("./index.css");

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

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

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

var preCls = "weekly-report-card";

var getImageBase64ByUrl = function getImageBase64ByUrl(toUserId) {
  return (0, _request.rpost)("v1")("okr/share/getBase64Str", {
    toUserId: toUserId
  }).then(function (getBase64Str) {
    return getBase64Str.Data;
  });
};

function Card(_ref, ref) {
  var data = _ref.data;

  var _useState = (0, _react.useState)(),
      _useState2 = _slicedToArray(_useState, 2),
      avatarSrc = _useState2[0],
      setAvatarSrc = _useState2[1];

  var _useState3 = (0, _react.useState)(),
      _useState4 = _slicedToArray(_useState3, 2),
      nameSrc = _useState4[0],
      setNameSrc = _useState4[1];

  var loginUserInfo = (0, _helpers.getLoginUserInfo)();
  var cardRef = (0, _react.useRef)();
  (0, _react.useImperativeHandle)(ref, function () {
    return {
      getImage: function getImage() {
        return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
          return regeneratorRuntime.wrap(function _callee$(_context) {
            while (1) {
              switch (_context.prev = _context.next) {
                case 0:
                  return _context.abrupt("return", new Promise(function (res) {
                    var width = 375; // 获取dom 宽度

                    // 获取dom 宽度
                    var height = 710; // 获取dom 高度

                    // 获取dom 高度
                    var canvas = document.createElement("canvas"); // 创建一个canvas节点

                    // 创建一个canvas节点
                    var scale = 2; // 定义任意放大倍数 支持小数

                    // 定义任意放大倍数 支持小数
                    canvas.width = width * scale; // 定义canvas 宽度 * 缩放

                    // 定义canvas 宽度 * 缩放
                    canvas.height = height * scale; // 定义canvas高度 *缩放
                    // @ts-ignore

                    // 定义canvas高度 *缩放
                    // @ts-ignore
                    canvas.style.width = "".concat(width, "px"); // @ts-ignore

                    // @ts-ignore
                    canvas.style.height = "".concat(height, "px"); // @ts-ignore

                    // @ts-ignore
                    var opts = {
                      backgroundColor: null,
                      // 背景透明
                      scale: scale,
                      // 添加的scale 参数
                      canvas: canvas,
                      // 自定义 canvas
                      logging: false,
                      // 日志开关，便于查看html2canvas的内部执行流程
                      width: width,
                      // dom 原始宽度
                      height: height,
                      useCORS: true // 【重要】开启跨域配置

                    };
                    (0, _html2canvas["default"])(cardRef.current, opts).then(function (canvas) {
                      var imgUrl = canvas.toDataURL("image/png"); // 得到图片的base64编码数据

                      // 得到图片的base64编码数据
                      res(imgUrl);
                    });
                  }));

                case 1:
                case "end":
                  return _context.stop();
              }
            }
          }, _callee);
        }))();
      }
    };
  }, []);
  (0, _react.useEffect)(function () {
    if ((0, _bsGlobal.isWx)()) {
      window.WWOpenData && window.WWOpenData.enableCanvasSharing && window.WWOpenData.enableCanvasSharing();
    }

    if (loginUserInfo !== null && loginUserInfo !== void 0 && loginUserInfo.UserAvatar.HasAvatar) {
      getImageBase64ByUrl(loginUserInfo === null || loginUserInfo === void 0 ? void 0 : loginUserInfo.Id).then(function (data) {
        return (0, _darwAvatar.drawAvatar)({
          name: loginUserInfo === null || loginUserInfo === void 0 ? void 0 : loginUserInfo.Name,
          userId: loginUserInfo === null || loginUserInfo === void 0 ? void 0 : loginUserInfo.Id,
          color: loginUserInfo === null || loginUserInfo === void 0 ? void 0 : loginUserInfo.UserAvatar.Color
        }, "data:image/jpeg;base64,".concat(data.obj));
      }).then(setAvatarSrc);
    } else {
      (0, _darwAvatar.drawAvatar)({
        name: loginUserInfo === null || loginUserInfo === void 0 ? void 0 : loginUserInfo.Name,
        userId: loginUserInfo === null || loginUserInfo === void 0 ? void 0 : loginUserInfo.Id,
        color: loginUserInfo === null || loginUserInfo === void 0 ? void 0 : loginUserInfo.UserAvatar.Color
      }, "").then(setAvatarSrc);
    }

    (0, _drawName["default"])(loginUserInfo === null || loginUserInfo === void 0 ? void 0 : loginUserInfo.Name, loginUserInfo === null || loginUserInfo === void 0 ? void 0 : loginUserInfo.Id).then(setNameSrc);
    return function () {
      if ((0, _bsGlobal.isWx)()) {
        window.WWOpenData && window.WWOpenData.disableCanvasSharing && window.WWOpenData.disableCanvasSharing();
      }
    };
  }, []);
  var increase = Number(data.Increase);
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(preCls, "-content"),
    ref: cardRef
  }, /*#__PURE__*/_react["default"].createElement("img", {
    className: "".concat(preCls, "__title"),
    src: require("./img/title.png")
  }), /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(preCls, "__date-range")
  }, data.WeekCycle), /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(preCls, "__shadow")
  }), /*#__PURE__*/_react["default"].createElement("img", {
    className: "".concat(preCls, "__laba"),
    src: require("./img/laba.png")
  }), /*#__PURE__*/_react["default"].createElement("img", {
    className: "".concat(preCls, "__bofang"),
    src: require("./img/bofang.png")
  }), /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(preCls, "__avatar")
  }, !!avatarSrc && /*#__PURE__*/_react["default"].createElement("img", {
    src: avatarSrc
  })), /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(preCls, "__body")
  }, /*#__PURE__*/_react["default"].createElement("img", {
    className: "".concat(preCls, "__userName"),
    src: nameSrc,
    style: {
      opacity: nameSrc ? 1 : 0
    }
  }), /*#__PURE__*/_react["default"].createElement("img", {
    className: "".concat(preCls, "__quote"),
    src: require("./img/quote.png"),
    alt: ""
  }), /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(preCls, "__text")
  }, data.Quotations), /*#__PURE__*/_react["default"].createElement(_board["default"], {
    percent: Number(data.Complete)
  }), /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(preCls, "__comparison")
  }, "OKR \u5B8C\u6210\u5EA6", increase != 0 && !isNaN(increase) ? /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, " ", /*#__PURE__*/_react["default"].createElement("img", {
    className: "".concat(preCls, "__direction"),
    src: increase > 0 ? require("./img/up.png") : require("./img/down.png"),
    alt: ""
  }), /*#__PURE__*/_react["default"].createElement("span", {
    style: {
      color: increase > 0 ? "#4DC591" : "#F05E5E"
    }
  }, Math.abs(increase), "%")) : ""), /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(preCls, "__colleague")
  }, data.PromptLanguage), /*#__PURE__*/_react["default"].createElement(_statistics["default"], {
    data: data
  })), /*#__PURE__*/_react["default"].createElement("img", {
    className: "".concat(preCls, "__bottom"),
    src: require("./img/bottom.png")
  }));
}

var _default = /*#__PURE__*/(0, _react.forwardRef)(Card);

exports["default"] = _default;