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

require("./index.css");

var _html2canvas = _interopRequireDefault(require("html2canvas"));

var _constant = require("./constant");

var _shower = _interopRequireDefault(require("./img/shower.png"));

var _light = _interopRequireDefault(require("./img/light.svg"));

var _target = _interopRequireDefault(require("./img/target.svg"));

var _createSuccessLeftQuotation = _interopRequireDefault(require("./img/create-success-left-quotation.svg"));

var _createSuccessRightQuotation = _interopRequireDefault(require("./img/create-success-right-quotation.svg"));

var _createSuccessMessage = _interopRequireDefault(require("./img/create-success-message.svg"));

var _qrCode = _interopRequireDefault(require("./img/qr-code.png"));

var _createSuccessHead = _interopRequireDefault(require("./img/create-success-head.png"));

var _createSuccessShadow = _interopRequireDefault(require("./img/create-success-shadow.svg"));

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

var _openData = require("../../utils/open-data");

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

var CreateSuccessCmp = function CreateSuccessCmp(_ref) {
  var createdSuccessInfo = _ref.createdSuccessInfo,
      isDownload = _ref.isDownload,
      onCloseModel = _ref.onCloseModel,
      onShowDetail = _ref.onShowDetail,
      tipIndex = _ref.tipIndex,
      downLoadRef = _ref.downLoadRef;

  var _useState = (0, _react.useState)(false),
      _useState2 = _slicedToArray(_useState, 2),
      dontShowValue = _useState2[0],
      setDontShowValue = _useState2[1];

  var _useState3 = (0, _react.useState)(null),
      _useState4 = _slicedToArray(_useState3, 2),
      okrName = _useState4[0],
      setOkrName = _useState4[1];

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

  var handleCloseModel = (0, _react.useCallback)(function () {
    onCloseModel && onCloseModel(dontShowValue);
  }, [dontShowValue, onCloseModel]);
  var handleShowDetail = (0, _react.useCallback)(function () {
    onShowDetail && onShowDetail(dontShowValue, createdSuccessInfo.workId);
  }, [dontShowValue, onCloseModel]);

  var handleInputChange = function handleInputChange(e) {
    e.persist();
    setDontShowValue(e.target.checked);
  };

  var handleDownloadPic = (0, _react.useCallback)(function () {
    var shareContent = downLoadRef.current; // 需要截图的包裹的（原生的）DOM 对象
    // @ts-ignore

    var width = shareContent.clientWidth; // 获取dom 宽度
    // @ts-ignore

    var height = shareContent.clientHeight; // 获取dom 高度

    var canvas = document.createElement('canvas'); // 创建一个canvas节点

    var scale = 2; // 定义任意放大倍数 支持小数

    canvas.width = width * scale; // 定义canvas 宽度 * 缩放

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

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

    canvas.style.height = "".concat(height, "px"); // @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"])(downLoadRef.current, opts).then(function (canvas) {
      var imgUrl = canvas.toDataURL('image/png'); // 得到图片的base64编码数据

      var image = new Image();
      image.setAttribute('crossOrigin', 'anonymous');

      image.onload = function () {
        var a = document.createElement('a'); // 生成一个a元素

        var event = new MouseEvent('click'); // 创建一个单击事件

        a.download = (0, _getLocale.getLocale)('OKR_MyO_Share'); // 设置图片名称

        a.href = imgUrl; // 将生成的URL设置为a.href属性

        a.dispatchEvent(event); // 触发a的单击事件
      };

      image.src = imgUrl;
    });
  }, []);
  (0, _react.useEffect)(function () {
    (0, _openData.parseOpenDataStringToImg)(createdSuccessInfo.workName, {
      fontSize: 14,
      fontWeight: '600',
      color: '141c28',
      lineHeight: 22
    }).then(setOkrName);
    Promise.all(createdSuccessInfo.Krs.map(function (kr) {
      return (0, _openData.parseOpenDataStringToImg)(kr.mileStoneName, {
        fontSize: 12,
        fontWeight: 'normal',
        color: '3f4755',
        lineHeight: 18
      });
    })).then(setKrNames);
  }, []); // console.log(createdSuccessInfo)

  return /*#__PURE__*/_react["default"].createElement("div", {
    className: "create-success-model"
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "create-success-model__card",
    ref: isDownload ? downLoadRef : null
  }, isDownload ? null : /*#__PURE__*/_react["default"].createElement("div", {
    className: "tu-icon-cross2 create-success-model__close",
    onClick: handleCloseModel
  }), /*#__PURE__*/_react["default"].createElement("div", {
    className: "create-success-model__top-block"
  }, /*#__PURE__*/_react["default"].createElement("img", {
    src: _light["default"],
    className: "create-success-model__light ".concat(isDownload ? 'create-success-model--animation-unset' : '')
  }), isDownload ? null : /*#__PURE__*/_react["default"].createElement("img", {
    src: _shower["default"],
    className: "create-success-model__shower ".concat(isDownload ? 'create-success-model--animation-unset' : '')
  }), /*#__PURE__*/_react["default"].createElement("img", {
    src: _createSuccessHead["default"],
    className: "create-success-model__create-head"
  }), isDownload ? null : /*#__PURE__*/_react["default"].createElement("img", {
    src: _createSuccessMessage["default"],
    className: "create-success-model__create-success-words"
  }), /*#__PURE__*/_react["default"].createElement("div", {
    className: "create-success-model__avatar"
  }, createdSuccessInfo.principalUser.avatar.hasAvatar ? /*#__PURE__*/_react["default"].createElement("img", {
    className: "create-success-model__img",
    src: "".concat(createdSuccessInfo.userAvatarBase64Url ? "data:image/png;base64,".concat(createdSuccessInfo.userAvatarBase64Url) : '')
  }) : /*#__PURE__*/_react["default"].createElement("div", {
    className: "create-success-model__text-avatar",
    style: {
      background: createdSuccessInfo.principalUser.avatar.color
    }
  }, createdSuccessInfo.principalUser.name[0]))), /*#__PURE__*/_react["default"].createElement("div", {
    className: "create-success-model__bottom-block"
  }, /*#__PURE__*/_react["default"].createElement("img", {
    src: _createSuccessLeftQuotation["default"],
    className: "create-success-model__left-quotation"
  }), /*#__PURE__*/_react["default"].createElement("div", {
    className: "create-success-model__tip"
  }, _constant.CreateSuccessTips[tipIndex]), /*#__PURE__*/_react["default"].createElement("div", {
    className: "create-success-model__right-quotation"
  }, /*#__PURE__*/_react["default"].createElement("img", {
    src: _createSuccessRightQuotation["default"]
  })), /*#__PURE__*/_react["default"].createElement("div", {
    className: "create-success-model__congratulation"
  }, (0, _getLocale.getLocale)('OKR_MyO_Pop_OKRscreated'), "\xA0\xA0", /*#__PURE__*/_react["default"].createElement("span", {
    className: "create-success-model__percent"
  }, createdSuccessInfo.countSurpassPercentage, "%"), "\xA0\xA0", (0, _getLocale.getLocale)('OKR_MyO_Pop_ofpeople')), /*#__PURE__*/_react["default"].createElement("div", {
    className: "create-success-model__okr-info ".concat(isDownload ? '' : 'create-success-model--fix-height')
  }, isDownload ? null : /*#__PURE__*/_react["default"].createElement("img", {
    className: "create-success-model__shadow-img",
    src: _createSuccessShadow["default"]
  }), /*#__PURE__*/_react["default"].createElement("div", {
    className: "create-success-model__o-name"
  }, /*#__PURE__*/_react["default"].createElement("img", {
    src: _target["default"],
    className: "create-success-model__o-name-icon"
  }), /*#__PURE__*/_react["default"].createElement("span", {
    className: "create-success-model__o-name-text"
  }, okrName)), krNames.map(function (kr, index) {
    return (
      /*#__PURE__*/
      // if(!isDownload && index > 2) {
      //   return null;
      // }
      _react["default"].createElement("div", {
        className: "create-success-model__kr-wrapper"
      }, /*#__PURE__*/_react["default"].createElement("span", {
        className: "create-success-model__dot"
      }), /*#__PURE__*/_react["default"].createElement("li", {
        className: "create-success-model__kr ".concat(isDownload ? 'create-success-model__kr--no-wrap' : '')
      }, kr))
    );
  })), isDownload ? /*#__PURE__*/_react["default"].createElement("div", {
    className: "create-success-model__qrcode ".concat(isDownload ? 'create-success-model--hidden-box-shadow' : '')
  }, /*#__PURE__*/_react["default"].createElement("img", {
    src: _qrCode["default"],
    width: "275"
  })) : null), isDownload ? null : /*#__PURE__*/_react["default"].createElement("div", {
    className: "create-success-model__func-btns-wrapper"
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "create-success-model__primary-btns"
  }, /*#__PURE__*/_react["default"].createElement("span", {
    className: "create-success-model__common-btn-style create-success-model__return-list",
    onClick: handleShowDetail
  }, (0, _getLocale.getLocale)('Mod_Checkdetails')), /*#__PURE__*/_react["default"].createElement("span", {
    className: "create-success-model__common-btn-style create-success-model__download-img",
    onClick: handleDownloadPic
  }, (0, _getLocale.getLocale)('OKR_MyO_Popup_Download'))), /*#__PURE__*/_react["default"].createElement("div", {
    className: "create-success-model__checkbox-wrapper"
  }, /*#__PURE__*/_react["default"].createElement("input", {
    id: "dontShowCreateSucess",
    checked: dontShowValue,
    type: "checkbox",
    onChange: handleInputChange,
    className: "create-success-model__checkbox"
  }), /*#__PURE__*/_react["default"].createElement("label", {
    htmlFor: "dontShowCreateSucess",
    className: "create-success-model__label"
  }, (0, _getLocale.getLocale)('OKR_MyO_Pop_Nomore'))))));
};

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