"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 _titaUi = require("tita-ui");

var _classnames = _interopRequireDefault(require("classnames"));

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

var _oProgressUpdate = _interopRequireDefault(require("../../../components/o-progress-update"));

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

var _okrProgressDetail = _interopRequireDefault(require("../../../../okr-progress-detail"));

var _title = _interopRequireDefault(require("../title"));

var _util = require("../../util");

var _dialogConfirm = _interopRequireWildcard(require("../../../../dialog-confirm"));

var _scheduleEmpty = _interopRequireDefault(require("../../img/schedule-empty.png"));

var _auth = require("../../../../../utils/auth");

var _context = require("../../../context");

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

var _i18nFrontTranslate = require("../../../../../utils/i18n-front-translate");

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

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

var OProgress = function OProgress(_ref) {
  var listData = _ref.listData,
      okrInfo = _ref.okrInfo,
      setUpdateProgress = _ref.setUpdateProgress,
      hasOtherProgress = _ref.hasOtherProgress,
      getOProgressData = _ref.getOProgressData;

  var _useContext = (0, _react.useContext)(_context.GeneralDataContext),
      generalPrincipalUser = _useContext.generalPrincipalUser;

  var _useContext2 = (0, _react.useContext)(_context.CommonContext),
      refreshAll = _useContext2.refreshAll;

  var _useState = (0, _react.useState)(listData.length > 1),
      _useState2 = _slicedToArray(_useState, 2),
      isMoreThanOneProgress = _useState2[0],
      setIsMoreThanOneProgress = _useState2[1];

  var _useState3 = (0, _react.useState)((0, _util.isPrincipal)(generalPrincipalUser)),
      _useState4 = _slicedToArray(_useState3, 2),
      isPrincipalUser = _useState4[0],
      setIsPrincipalUser = _useState4[1];

  var _useState5 = (0, _react.useState)((new _auth.OAuth({
    principalUser: generalPrincipalUser,
    createUserId: okrInfo.createUserId
  }).hasAuth() || okrInfo.isParticipant) && okrInfo.status !== 2),
      _useState6 = _slicedToArray(_useState5, 2),
      isCanEditProgress = _useState6[0],
      setIsCanEditProgress = _useState6[1];

  var _useState7 = (0, _react.useState)(false),
      _useState8 = _slicedToArray(_useState7, 2),
      isShowAll = _useState8[0],
      setIsShowAll = _useState8[1];

  var _useState9 = (0, _react.useState)(0),
      _useState10 = _slicedToArray(_useState9, 2),
      initHeight = _useState10[0],
      setInitHeight = _useState10[1];

  var _useState11 = (0, _react.useState)(false),
      _useState12 = _slicedToArray(_useState11, 2),
      editOProgressVisiable = _useState12[0],
      setEditOProgressVisiable = _useState12[1];

  var _useState13 = (0, _react.useState)(''),
      _useState14 = _slicedToArray(_useState13, 2),
      editOProgressContent = _useState14[0],
      setEditOProgressContent = _useState14[1];

  var _useState15 = (0, _react.useState)(''),
      _useState16 = _slicedToArray(_useState15, 2),
      editOProgressId = _useState16[0],
      setEditOProgressId = _useState16[1];

  var _useState17 = (0, _react.useState)(false),
      _useState18 = _slicedToArray(_useState17, 2),
      hasError = _useState18[0],
      setHasError = _useState18[1];

  var _useState19 = (0, _react.useState)(false),
      _useState20 = _slicedToArray(_useState19, 2),
      showRemindDialogConfirm = _useState20[0],
      setRemindDialogConfirm = _useState20[1];

  var _useState21 = (0, _react.useState)(listData),
      _useState22 = _slicedToArray(_useState21, 2),
      oProgressData = _useState22[0],
      setOProgressData = _useState22[1];

  var handleChangeShowAll = function handleChangeShowAll() {
    setIsShowAll(!isShowAll);
  };

  var handleEditProgress = function handleEditProgress(id, content) {
    setEditOProgressVisiable(true);
    setEditOProgressContent(content);
    setEditOProgressId(id);
  };

  var handleModalCancelClick = function handleModalCancelClick() {
    setEditOProgressVisiable(false);
  };

  var handleModalOkClick = function handleModalOkClick() {
    (0, _requestApis.updateProgressDesc)(editOProgressId, okrInfo.workId, editOProgressContent || '').then(function (resp) {
      if (resp.Code === 1) {
        _toast["default"].Success((0, _getLocale.getLocale)('Mod_Successfuloperat'), {
          canClose: false
        });

        setEditOProgressVisiable(false);
        refreshAll && refreshAll();
        getOProgressData();
      } else {
        _toast["default"].Error(resp.Message, {
          canClose: false
        });
      }
    })["catch"](function (e) {
      _toast["default"].Error('网络异常', {
        canClose: false
      });
    });
  };

  var handleTextareaChange = function handleTextareaChange(e) {
    var value = e.target.value;
    setEditOProgressContent(value);
    setHasError(value.trim().length > 500);
  };

  var renderContent = function renderContent(_ref2) {
    var onOpenModal = _ref2.onOpenModal;
    return /*#__PURE__*/_react["default"].createElement("span", {
      className: "o-progress__func-btn o-progress__func-btn--update",
      onClick: onOpenModal
    }, /*#__PURE__*/_react["default"].createElement("i", {
      className: "tu-icon-gengxin o-progress__func-btn-icon"
    }), (0, _getLocale.getLocale)('OKR_MyO_Prog_Butt_Update'));
  };

  var handleRemind = (0, _react.useCallback)(function () {
    var _BSGlobal$loginUserIn;

    // @ts-ignore
    (0, _requestApis.remindAction)({
      toUserId: (_BSGlobal$loginUserIn = BSGlobal.loginUserInfo) === null || _BSGlobal$loginUserIn === void 0 ? void 0 : _BSGlobal$loginUserIn.Id,
      work_id: okrInfo.workId,
      type: 2
    }).then(function (resp) {
      if (resp.Code === 1) {
        _toast["default"].Success((0, _getLocale.getLocale)('OKR_MyO_Prog_Remindersu'), {
          canClose: false
        });
      } else {
        _toast["default"].Error(resp.Message, {
          canClose: false
        });
      }
    })["catch"](function (e) {
      _toast["default"].Error('网络异常', {
        canClose: false
      });
    })["finally"](function () {
      setRemindDialogConfirm(false);
    });
  }, [generalPrincipalUser]);
  var handleRemindClick = (0, _react.useCallback)(function () {
    if (isPrincipalUser) {
      var _BSGlobal$loginUserIn2;

      // @ts-ignore 提醒上级查看
      (0, _requestApis.remindAction)({
        toUserId: (_BSGlobal$loginUserIn2 = BSGlobal.loginUserInfo) === null || _BSGlobal$loginUserIn2 === void 0 ? void 0 : _BSGlobal$loginUserIn2.Id,
        work_id: okrInfo.workId
      }).then(function (resp) {
        if (resp.Code === 1) {
          _toast["default"].Success((0, _getLocale.getLocale)('OKR_MyO_Prog_Remindersu'), {
            canClose: false
          });
        } else {
          _toast["default"].Error(resp.Message, {
            canClose: false
          });
        }
      })["catch"](function (e) {
        _toast["default"].Error('网络异常', {
          canClose: false
        });
      });
      return;
    }

    setRemindDialogConfirm(true);
  }, []);
  var handleCancelRemind = (0, _react.useCallback)(function () {
    setRemindDialogConfirm(false);
  }, []);
  var handleEnsureRemind = (0, _react.useCallback)(function () {
    handleRemind();
  }, []);
  (0, _react.useEffect)(function () {
    setIsPrincipalUser((0, _util.isPrincipal)(generalPrincipalUser));
    setIsCanEditProgress((new _auth.OAuth({
      principalUser: generalPrincipalUser,
      createUserId: okrInfo.createUserId
    }).hasAuth() || okrInfo.isParticipant) && okrInfo.status !== 2);
  }, [generalPrincipalUser]);
  (0, _react.useEffect)(function () {
    // @ts-ignore
    var dom = document.querySelector('.o-progress__contnet-wrapper').querySelector('.okr-progress-item-wrapper'); // @ts-ignore

    var height = dom ? dom.offsetHeight : 0; // 为了解决展开内容编辑过后收起时计算的高度是根据展开的算 16是第一个目标进展在展开和收起所以列表的时候的padding-bottom差

    var finalHeight = isShowAll ? height - 16 : height;
    setInitHeight(finalHeight);
  }, [listData, oProgressData]);
  (0, _react.useEffect)(function () {
    setOProgressData(listData);
  }, [listData]);
  (0, _react.useEffect)(function () {
    setIsMoreThanOneProgress(oProgressData.length > 1);
  }, [oProgressData]);
  var renderDialogRemind = (0, _react.useMemo)(function () {
    return /*#__PURE__*/_react["default"].createElement(_dialogConfirm["default"], {
      zIndex: 1200,
      visible: showRemindDialogConfirm,
      type: "warning"
    }, /*#__PURE__*/_react["default"].createElement("span", {
      type: "title"
    }, (0, _getLocale.getLocale)('OKR_DR_Pop_Areyousure')), /*#__PURE__*/_react["default"].createElement("span", {
      type: "content"
    }, (0, _getLocale.getLocale)('OKR_DR_Pop_ThesystemOKR')), /*#__PURE__*/_react["default"].createElement(_dialogConfirm.CancelButton, {
      type: "button",
      onClick: handleCancelRemind
    }, (0, _getLocale.getLocale)('Mod_Cancel')), /*#__PURE__*/_react["default"].createElement(_dialogConfirm.SureButton, {
      type: "button",
      onClick: handleEnsureRemind
    }, (0, _getLocale.getLocale)('OKR_MyO_Butt_Determine')));
  }, [showRemindDialogConfirm, handleCancelRemind, handleEnsureRemind]);
  var renderEmptyCmp = (0, _react.useMemo)(function () {
    var renderUpdateBtn = function renderUpdateBtn(_ref3) {
      var onOpenModal = _ref3.onOpenModal;
      return /*#__PURE__*/_react["default"].createElement("button", {
        className: "o-progress__empty-button ".concat(isPrincipalUser ? '' : 'o-progress__empty-button--not-principaluser'),
        onClick: onOpenModal
      }, (0, _getLocale.getLocale)('OKR_MyO_Prog_Butt_Update'));
    };

    return /*#__PURE__*/_react["default"].createElement("div", {
      className: (0, _classnames["default"])({
        'o-progress__empty-wrapper': true,
        'o-progress__empty-wrapper--not-principaluser': hasOtherProgress
      })
    }, /*#__PURE__*/_react["default"].createElement("img", {
      width: 128,
      src: _scheduleEmpty["default"]
    }), /*#__PURE__*/_react["default"].createElement("span", {
      className: "o-progress__empty-text"
    }, isPrincipalUser ? (0, _getLocale.getLocale)('OKR_MyO_KRP_tochecko') : (0, _getLocale.getLocale)('OKR_MyO_Pr_Text_Nooprogress')), (isPrincipalUser || okrInfo.isParticipant) && /*#__PURE__*/_react["default"].createElement(_oProgressUpdate["default"], {
      okrInfo: okrInfo,
      renderContent: renderUpdateBtn,
      setUpdateData: setUpdateProgress
    }), !isCanEditProgress && /*#__PURE__*/_react["default"].createElement("button", {
      className: "o-progress__empty-button",
      onClick: handleRemindClick
    }, (0, _getLocale.getLocale)('OKR_MyO_Pr_Butt_Reminder')), isCanEditProgress && !isPrincipalUser && !okrInfo.isParticipant && /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement("button", {
      className: "o-progress__empty-button",
      onClick: handleRemindClick
    }, (0, _getLocale.getLocale)('OKR_MyO_Pr_Butt_Reminder')), /*#__PURE__*/_react["default"].createElement(_oProgressUpdate["default"], {
      okrInfo: okrInfo,
      renderContent: renderUpdateBtn,
      setUpdateData: setUpdateProgress
    })));
  }, [hasOtherProgress]);
  var renderDefaultRemindCmp = (0, _react.useMemo)(function () {
    var text = isPrincipalUser ? (0, _auth.hasSuperior)() ? (0, _getLocale.getLocale)('OKR_MyO_Prog_Butt_Remind') : null : okrInfo.isParticipant ? '' : (0, _getLocale.getLocale)('OKR_MyO_Pr_Butt_Reminder');
    return /*#__PURE__*/_react["default"].createElement("span", {
      className: "o-progress__func-btn",
      onClick: handleRemindClick
    }, text && /*#__PURE__*/_react["default"].createElement("i", {
      className: "tu-icon-shoushi o-progress__func-btn-icon"
    }), text);
  }, [isPrincipalUser]);
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: "o-progress__wrapper"
  }, oProgressData.length === 0 ? renderEmptyCmp : /*#__PURE__*/_react["default"].createElement("div", {
    className: "o-progress__header"
  }, /*#__PURE__*/_react["default"].createElement(_title["default"], {
    title: (0, _getLocale.getLocale)('OKR_MyO_Prog_Objectiveprogress')
  }), /*#__PURE__*/_react["default"].createElement("div", {
    className: "o-progress__func-btns"
  }, okrInfo.status === 2 ? null : renderDefaultRemindCmp, isCanEditProgress ? /*#__PURE__*/_react["default"].createElement(_oProgressUpdate["default"], {
    okrInfo: okrInfo,
    renderContent: renderContent,
    setUpdateData: setUpdateProgress
  }) : null)), /*#__PURE__*/_react["default"].createElement("div", {
    className: "o-progress__contnet-wrapper ".concat(isMoreThanOneProgress && isShowAll ? '' : 'o-progress__content-wrapper--fold'),
    style: {
      height: isShowAll ? 'auto' : "".concat(initHeight, "px")
    }
  }, oProgressData && oProgressData.map(function (item, index) {
    var rightTip = '';

    if (okrInfo.status !== 2 && okrInfo.progress !== 100 && index === 0 && isCanEditProgress) {
      rightTip = (0, _util.getToNowTime)(item.time);
    }

    return /*#__PURE__*/_react["default"].createElement(_okrProgressDetail["default"], {
      key: item.id,
      riskLevel: item.riskLevel,
      title: (0, _openData.parseString)(item.title || ''),
      percent: item.percent,
      oldPercent: item.oldPercent,
      content: item.content,
      time: item.time,
      canEdit: isCanEditProgress,
      id: item.id,
      rightTip: rightTip,
      onEditProgress: handleEditProgress
    });
  })), isMoreThanOneProgress ? /*#__PURE__*/_react["default"].createElement("div", {
    className: "o-progress__shrink"
  }, /*#__PURE__*/_react["default"].createElement("span", {
    className: "o-progress__shrink-text",
    onClick: handleChangeShowAll
  }, isShowAll ? (0, _getLocale.getLocale)('OKR_MyO_Prog_Butt_Fold') : (0, _getLocale.getLocale)('OKR_MyO_Pr_Butt_Viewall'), /*#__PURE__*/_react["default"].createElement("i", {
    className: isShowAll ? 'tu-icon-arrow-up' : 'tu-icon-arrow-down'
  }))) : null, /*#__PURE__*/_react["default"].createElement(_titaUi.Modal, {
    className: "common-modal o-progress__modal",
    maskClosable: false,
    onCancel: handleModalCancelClick,
    onOk: handleModalOkClick,
    okDisabled: hasError,
    title: "\u7F16\u8F91\u76EE\u6807\u8FDB\u5C55",
    visible: editOProgressVisiable,
    width: 600
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "o-progress__textarea-wrapper ".concat(hasError ? 'o-progress__textarea-wrapper--error' : '')
  }, /*#__PURE__*/_react["default"].createElement("textarea", {
    placeholder: (0, _getLocale.getLocale)('OKR_MyO_Pop_Upd_Pleprogress'),
    className: "o-progress__textarea",
    value: editOProgressContent,
    onChange: handleTextareaChange
  })), hasError ? /*#__PURE__*/_react["default"].createElement("div", {
    className: "o-progress__error-text"
  }, (0, _i18nFrontTranslate.inputWordTrans)(editOProgressContent.trim().length, 500)) : null), renderDialogRemind);
};

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