"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 _ReviewBody = _interopRequireDefault(require("./components/ReviewBody"));

var _Toast = _interopRequireDefault(require("./components/Toast"));

var _requestV = require("../../../utils/request-v1");

var _mblog = require("../../../utils/mblog");

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

var _style = require("./style");

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 ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

var ReviewPop = /*#__PURE__*/function (_PureComponent) {
  _inherits(ReviewPop, _PureComponent);

  var _super = _createSuper(ReviewPop);

  function ReviewPop(props) {
    var _this;

    _classCallCheck(this, ReviewPop);

    _this = _super.call(this, props);

    _this._handlerEditChange = function (data) {
      var isAllowSubmit = _this.state.isAllowSubmit;
      var isStandardMode = data.isStandardMode; // 允许提交的条件

      if (isStandardMode) {
        // 标准模式
        isAllowSubmit = (_this.characterCheck(data.gains, 5000) || _this.characterCheck(data.Drawback, 5000)) && data.gains.length <= 5000 && data.Drawback.length <= 5000;
      } else {
        // 高级模式
        // 是否填写问题与不足
        var isHasDrawBack = data.drawbackList.some(function (item) {
          return _this.characterCheck(item.drawback, 5000);
        }); // 问题与不足字数是否超出限制

        var isDrawbackBeyondLimit = data.drawbackList.some(function (item) {
          return item.cause.length > 5000 || item.measures.length > 5000 || item.drawback.length > 5000;
        });
        isAllowSubmit = (_this.characterCheck(data.gains, 5000) || isHasDrawBack) && data.gains.length <= 5000 && !isDrawbackBeyondLimit;
      }

      _this.setState({
        isAllowSubmit: isAllowSubmit
      });

      _this.reviewData = data;
    };

    _this.characterCheck = function (str, strLimit) {
      return str.length > 0 && str.length <= strLimit;
    };

    _this._handlerSubmit = function () {
      var EditType = _this.props.EditType;

      var data = _objectSpread({}, _this.reviewData);

      var drawbackId = data.drawbackList.length > 0 ? data.drawbackList[0].drawbackId : 0;
      data.objId = String(data.objId);
      data.gains = (0, _mblog.htmlEncodeByRegExp)((0, _mblog.htmlDecodeByRegExp)(data.gains));

      if (data.isStandardMode) {
        // 标准模式
        data.drawbackList = [{
          drawbackId: drawbackId,
          drawback: (0, _mblog.htmlEncodeByRegExp)((0, _mblog.htmlDecodeByRegExp)(data.Drawback)),
          cause: "",
          measures: ""
        }];
      } else {
        // 高级模式
        // 问题与不足中只填写了除了问题之外的属性
        var missDrawbackList = data.drawbackList.map(function (item, index) {
          return (item.cause.trim().length > 0 || item.measures.trim().length > 0) && item.drawback.trim().length <= 0 ? "\u201C\u95EE\u9898\u603B\u7ED3".concat(index + 1, "\u201D") : "";
        });
        var toastInfo = missDrawbackList.filter(function (item) {
          return item;
        }).join(" ");
        var toastTips = "\u8BF7\u5B8C\u5584".concat(toastInfo);

        if (toastInfo) {
          _this.setState({
            isMissDrawBack: true
          });

          _this.showToast("Submit", 2, toastTips, 3000);

          return;
        }

        _this.setState({
          isMissDrawBack: false
        }); // 提交时过滤掉未填写的问题


        var drawbackList = data.drawbackList.filter(function (item) {
          return item.drawback || item.cause || item.measures;
        });
        data.drawbackList = drawbackList.length === 0 ? [{
          drawbackId: drawbackId,
          drawback: "",
          cause: "",
          measures: ""
        }] : drawbackList;
        data.drawbackList.forEach(function (item) {
          item.drawback = (0, _mblog.htmlEncodeByRegExp)((0, _mblog.htmlDecodeByRegExp)(item.drawback));
          item.cause = (0, _mblog.htmlEncodeByRegExp)((0, _mblog.htmlDecodeByRegExp)(item.cause));
          item.measures = (0, _mblog.htmlEncodeByRegExp)((0, _mblog.htmlDecodeByRegExp)(item.measures));
        });
      }

      delete data.Drawback;
      delete data.reviewMebers;
      delete data.reviewParticipantsList;
      delete data.reviewDepartmentsList;
      delete data.taskList;
      var url = (0, _requestV.getApiUrl)("replay/".concat(EditType === 0 ? "add" : "update"));
      (0, _requestV.taker)(url, "POST", data).then(function (res) {
        if (res.Code == 1) {
          localStorage.removeItem(_this.localStorageKey);

          _this.showToast("Submit", 1, "".concat(EditType === 0 ? (0, _getLocale.getLocale)("Pro_detail_Submission") : (0, _getLocale.getLocale)("OKR_MyO_Butt_Update"), "\u6210\u529F"), 1500);
        } else {
          _this.showToast("Submit", 0, res.Message, 3000);
        }
      });
    };

    _this.showToast = function (submitType, callbackStatus, toastInfo) {
      var duration = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 2000;
      var toastIcon = {
        0: "tu-icon-cross",
        1: "tu-icon-check",
        2: "tu-icon-warning"
      };

      _this.setState({
        submitType: submitType,
        submitStatus: 2
      }, function () {
        _this.setState({
          isShowToast: true,
          toastIcon: toastIcon[callbackStatus],
          toastInfo: toastInfo
        }, function () {
          _this.timer = setTimeout(function () {
            _this.setState({
              isShowToast: false,
              submitStatus: callbackStatus === 2 ? 0 : callbackStatus
            });

            if (callbackStatus === 1) {
              _this.props.onSubmit("Submit");
            }
          }, duration);
        });
      });
    };

    _this.state = {
      isAllowSubmit: false,
      isShowToast: false,
      isMissDrawBack: false,
      toastIcon: "",
      toastInfo: "",
      submitType: "",
      submitStatus: 0
    };
    return _this;
  }

  _createClass(ReviewPop, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      this.localStorageKey = "ReviewData_".concat(this.props.ObjData.ObjId);
    }
    /**
     *可提交的条件：
     * 1.“价值与收获”与“问题与不足”必须至少填一项，否则确定按钮置灰；
     * 2.不满足提交要求时（未填写“价值与收获”或“问题与不足”；或者填写字数超过限制时；或者问题与不足中只填写了除了问题之外的属性时），“确定”按钮置灰；
     * 3.当用户填写了“根本原因”或“相应解决措施”时，而没有填写“问题与不足”时，点击“确定”按钮提交总结时，系统提示“请完善问题与不足x”；
     *
     * @memberof ReviewPop
     */

  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      clearTimeout(this.timer);
    }
  }, {
    key: "render",
    value: function render() {
      var _this2 = this;

      var _this$props = this.props,
          onSubmit = _this$props.onSubmit,
          ObjData = _this$props.ObjData,
          EditType = _this$props.EditType,
          className = _this$props.className,
          ReplayId = _this$props.ReplayId,
          createTask = _this$props.createTask,
          isOkrPanel = _this$props.isOkrPanel;
      var _this$state = this.state,
          isAllowSubmit = _this$state.isAllowSubmit,
          isShowToast = _this$state.isShowToast,
          isMissDrawBack = _this$state.isMissDrawBack,
          toastIcon = _this$state.toastIcon,
          toastInfo = _this$state.toastInfo,
          submitType = _this$state.submitType,
          submitStatus = _this$state.submitStatus;
      return /*#__PURE__*/_react["default"].createElement(_style.ReviewPopWrapper, {
        className: className
      }, /*#__PURE__*/_react["default"].createElement(_style.ReviewContainer, null, /*#__PURE__*/_react["default"].createElement(_style.ReviewHead, null, /*#__PURE__*/_react["default"].createElement("p", null, "".concat((0, _getLocale.getLocale)("OKR_MyO_KRP_Review")).concat(ObjData.ObjType == 65 ? "KR" : (0, _getLocale.getLocale)("Mod_Objective"))), /*#__PURE__*/_react["default"].createElement(_style.IconClose, {
        className: "tu-icon-close",
        onClick: function onClick() {
          return onSubmit();
        }
      })), /*#__PURE__*/_react["default"].createElement(_ReviewBody["default"], {
        ObjData: ObjData,
        EditType: EditType,
        ReplayId: ReplayId,
        isMissDrawBack: isMissDrawBack,
        createTask: createTask,
        isOkrPanel: isOkrPanel,
        onChange: function onChange(data) {
          return _this2._handlerEditChange(data);
        }
      }), /*#__PURE__*/_react["default"].createElement(_style.ReviewFoot, null, /*#__PURE__*/_react["default"].createElement(_style.BtnConfirm, {
        style: {
          visibility: EditType === 0 ? "visible" : "hidden"
        },
        onClick: function onClick() {
          return _this2.showToast("SaveDraft", 1, (0, _getLocale.getLocale)("Pro_page_Plan_Saved"));
        },
        disabled: submitType === "SaveDraft" && submitStatus === 2
      }, (0, _getLocale.getLocale)("OKR_MyO_KRP_Pop_SaveDraft")), /*#__PURE__*/_react["default"].createElement(_style.BtnGroup, null, /*#__PURE__*/_react["default"].createElement(_style.BtnCancle, {
        onClick: function onClick() {
          localStorage.removeItem(_this2.localStorageKey);
          onSubmit();
        }
      }, (0, _getLocale.getLocale)("Mod_Cancel")), /*#__PURE__*/_react["default"].createElement(_style.BtnConfirm, {
        disabled: !isAllowSubmit || submitType == "Submit" && submitStatus === 2,
        onClick: function onClick() {
          return _this2._handlerSubmit();
        }
      }, (0, _getLocale.getLocale)("OKR_MyO_Butt_Determine")))), /*#__PURE__*/_react["default"].createElement(_Toast["default"], {
        className: isShowToast ? "slideIn" : ""
      }, /*#__PURE__*/_react["default"].createElement("span", {
        className: toastIcon
      }), /*#__PURE__*/_react["default"].createElement("p", null, toastInfo))));
    }
  }]);

  return ReviewPop;
}(_react.PureComponent);

exports["default"] = ReviewPop;