"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 _tooltip = _interopRequireDefault(require("../tooltip"));

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

var _switch = _interopRequireDefault(require("../switch"));

var _textarea = _interopRequireDefault(require("./textarea"));

var _helper = require("./helper");

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

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

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

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 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 ProgressModal = function ProgressModal(props) {
  var _window, _window$BSGlobal, _window$BSGlobal$OkrA, _window$BSGlobal$OkrA2;

  var maxLength = props.maxLength,
      okrInfo = props.okrInfo,
      onClose = props.onClose,
      onOk = props.onOk,
      _props$title = props.title,
      title = _props$title === void 0 ? window.getLocale('OKR_MyO_Prog_Butt_Update') : _props$title,
      visible = props.visible,
      isKr = props.isKr;
  var configure = isKr ? (0, _helper.getKrSetting)() : (0, _helper.getOSetting)();
  var reg = /^[0-9]*[1-9][0-9]*$/;

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

  var _useState3 = (0, _react.useState)(false),
      _useState4 = _slicedToArray(_useState3, 2),
      errorRemark = _useState4[0],
      setErrorRemark = _useState4[1];

  var _useState5 = (0, _react.useState)(okrInfo.progress),
      _useState6 = _slicedToArray(_useState5, 2),
      progress = _useState6[0],
      setProgress = _useState6[1];

  var _useState7 = (0, _react.useState)(okrInfo.progressDescription || ''),
      _useState8 = _slicedToArray(_useState7, 2),
      progressDescription = _useState8[0],
      setProgressDescription = _useState8[1]; // @ts-ignore


  var _useState9 = (0, _react.useState)({}),
      _useState10 = _slicedToArray(_useState9, 2),
      recommandProgressObj = _useState10[0],
      setRecommandProgressObj = _useState10[1]; // 推荐值


  var _useState11 = (0, _react.useState)(isKr ? okrInfo.riskLevel : okrInfo.manualRiskLevel),
      _useState12 = _slicedToArray(_useState11, 2),
      selected = _useState12[0],
      setSelected = _useState12[1];

  var _useState13 = (0, _react.useState)((_window = window) === null || _window === void 0 ? void 0 : (_window$BSGlobal = _window.BSGlobal) === null || _window$BSGlobal === void 0 ? void 0 : (_window$BSGlobal$OkrA = _window$BSGlobal.OkrAdvancedSetting) === null || _window$BSGlobal$OkrA === void 0 ? void 0 : (_window$BSGlobal$OkrA2 = _window$BSGlobal$OkrA.KRSetting) === null || _window$BSGlobal$OkrA2 === void 0 ? void 0 : _window$BSGlobal$OkrA2.ShowKrProgress),
      _useState14 = _slicedToArray(_useState13, 1),
      ShowKrProgress = _useState14[0];

  var _useState15 = (0, _react.useState)(true),
      _useState16 = _slicedToArray(_useState15, 2),
      autoUpdate = _useState16[0],
      setUpdate = _useState16[1]; // 设置自动更新


  var _useState17 = (0, _react.useState)(0),
      _useState18 = _slicedToArray(_useState17, 2),
      updateWay = _useState18[0],
      setUpdateWay = _useState18[1]; // 自动更新方式 0: 关闭， 1: 根据关见成果， 2: 子目标


  var _useState19 = (0, _react.useState)(false),
      _useState20 = _slicedToArray(_useState19, 2),
      settingFolded = _useState20[0],
      setSettingFold = _useState20[1]; // 推荐


  var getRecommendProgress = /*#__PURE__*/function () {
    var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
      var getRecommandHandler, data, _data$Data, UpdateProgressWay, Progress;

      return regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              getRecommandHandler = isKr ? _requestApis.getKrRecommandProgress : _requestApis.getORecommandProgress;
              _context.next = 3;
              return getRecommandHandler(isKr ? okrInfo.mileStoneId : okrInfo.workId);

            case 3:
              data = _context.sent;

              if (data.Code === 1) {
                _data$Data = data.Data, UpdateProgressWay = _data$Data.UpdateProgressWay, Progress = _data$Data.Progress;
                setRecommandProgressObj(data.Data);
                setUpdate(UpdateProgressWay !== 0);
                setUpdateWay(UpdateProgressWay === 0 ? 1 : UpdateProgressWay);
                setProgress(Progress);
              }

            case 5:
            case "end":
              return _context.stop();
          }
        }
      }, _callee);
    }));

    return function getRecommendProgress() {
      return _ref.apply(this, arguments);
    };
  }();

  (0, _react.useEffect)(function () {
    if (visible) {
      getRecommendProgress();
    } else {
      setProgressDescription('');
    }
  }, [visible]);
  (0, _react.useEffect)(function () {
    if (!autoUpdate) return;
    var childOkrProgress = recommandProgressObj.childOkrProgress,
        childkrProgress = recommandProgressObj.childkrProgress,
        childEProgress = recommandProgressObj.childEProgress;

    if (updateWay === 1) {
      setProgress(isKr ? (0, _helper.transProgress)(childEProgress) : (0, _helper.transProgress)(childkrProgress));
    }

    if (updateWay === 2) {
      setProgress((0, _helper.transProgress)(childOkrProgress));
    }
  }, [updateWay, autoUpdate, recommandProgressObj]);

  var descriptionText = /*#__PURE__*/_react["default"].createElement(_style.Item, null, /*#__PURE__*/_react["default"].createElement("span", {
    className: "auto-update__item-label",
    style: {
      alignSelf: 'flex-start',
      marginTop: '10px'
    }
  }, (0, _getLocale.getLocale)('OKR_MyO_Pop_Upd_Progress')), /*#__PURE__*/_react["default"].createElement(_textarea["default"], {
    hasError: errorRemark,
    placeholder: (0, _getLocale.getLocale)('OKR_MyO_Pop_Upd_Pleprogress'),
    style: {
      flex: 1,
      height: '75px'
    },
    value: progressDescription,
    onChange: function onChange(value) {
      setProgressDescription(value);
      setErrorRemark(!!maxLength && value.trim().length > maxLength);
    }
  }), /*#__PURE__*/_react["default"].createElement(_style.ErrorTip, {
    className: errorRemark ? 'slideIn' : ''
  }, (0, _i18nFrontTranslate.inputWordTrans)(progressDescription.trim().length, maxLength))); // KR不顯示進展


  if (!ShowKrProgress && isKr) {
    descriptionText = /*#__PURE__*/_react["default"].createElement("span", null);
  }

  var onOkHandler = function onOkHandler() {
    onOk({
      progress: progress + (0, _getLocale.getLocale)('OKR_MyO_Pop_Upd_Percentage'),
      progressDescription: progressDescription,
      riskLevel: selected,
      updateProgressWay: autoUpdate ? updateWay : 0
    });
  };

  var onCancelHandler = function onCancelHandler() {
    return onClose();
  };

  var onProgressChangeHandler = function onProgressChangeHandler(e) {
    var value = e.target.value;
    setProgress(value);
    setErrorProgress(!reg.test(value));
  };

  return /*#__PURE__*/_react["default"].createElement(_titaUi.Modal, {
    className: "common-modal",
    maskClosable: false,
    onCancel: onCancelHandler,
    okText: (0, _getLocale.getLocale)('OKR_MyO_Butt_Determine'),
    cancelText: (0, _getLocale.getLocale)('Mod_Cancel'),
    onOk: onOkHandler,
    okDisabled: errorProgress || errorRemark,
    title: title,
    visible: visible,
    width: 600,
    zIndex: 2400
  }, /*#__PURE__*/_react["default"].createElement(_style.List, null, /*#__PURE__*/_react["default"].createElement(_style.Item, null, /*#__PURE__*/_react["default"].createElement("span", {
    className: "auto-update__item-label"
  }, (0, _getLocale.getLocale)('OKR_MyO_Pop_Upd_Progresss1')), /*#__PURE__*/_react["default"].createElement("div", {
    className: "auto-update__item-progress"
  }, /*#__PURE__*/_react["default"].createElement(_titaUi.Input, {
    type: "number",
    value: progress,
    disabled: !!autoUpdate,
    wrapperClass: (0, _classnames["default"])('auto-update__item-progress-input', {
      'auto-update__item-progress-input--disabled': autoUpdate,
      'auto-update__item-progress-input--error': errorProgress
    }),
    onChange: onProgressChangeHandler
  }), /*#__PURE__*/_react["default"].createElement("span", {
    className: (0, _classnames["default"])('auto-update__item-progress-input__suffix', {
      'auto-update__item-progress-input__suffix--disabled': autoUpdate
    })
  }, (0, _getLocale.getLocale)('OKR_MyO_Pop_Upd_Percentage'))), /*#__PURE__*/_react["default"].createElement("span", null, configure.getRecommandText(autoUpdate, updateWay, recommandProgressObj)), /*#__PURE__*/_react["default"].createElement(_style.ErrorTip, {
    className: "error-progress ".concat(errorProgress ? 'slideIn' : '')
  }, (0, _getLocale.getLocale)('OKR_MyO_KRP_Please0'))), /*#__PURE__*/_react["default"].createElement(_style.Item, null, /*#__PURE__*/_react["default"].createElement("span", {
    className: "auto-update__item-label"
  }, (0, _getLocale.getLocale)('Mod_States')), /*#__PURE__*/_react["default"].createElement(_style.OptionsWrapper, null, (isKr ? (0, _helper.getKrOptions)() : (0, _helper.getOptions)()).map(function (option) {
    return /*#__PURE__*/_react["default"].createElement(_style.Options, {
      key: option.id,
      onClick: function onClick() {
        return setSelected(option.id);
      }
    }, /*#__PURE__*/_react["default"].createElement(_style.Icon, {
      color: option.color,
      className: selected === option.id ? 'tu-icon-singlebox-avtive' : 'tu-icon-singlebox-have'
    }), /*#__PURE__*/_react["default"].createElement("p", null, option.text));
  }))), descriptionText, /*#__PURE__*/_react["default"].createElement("div", {
    className: "auto-update-setting"
  }, settingFolded && /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement("div", {
    className: "auto-update-setting__switch"
  }, /*#__PURE__*/_react["default"].createElement(_switch["default"], {
    label: configure.switchLabel,
    checked: autoUpdate,
    onChange: function onChange(data) {
      return setUpdate(data);
    }
  })), autoUpdate && /*#__PURE__*/_react["default"].createElement("div", null, configure.options.map(function (option) {
    return /*#__PURE__*/_react["default"].createElement("div", {
      onClick: function onClick() {
        return setUpdateWay(option.key);
      },
      className: "auto-update-setting__option"
    }, /*#__PURE__*/_react["default"].createElement("span", {
      className: (0, _classnames["default"])('auto-update-setting__radio', "auto-update-setting__radio".concat(updateWay === option.key ? '--selected' : ''))
    }), /*#__PURE__*/_react["default"].createElement("span", null, option.label), /*#__PURE__*/_react["default"].createElement(_tooltip["default"], {
      overlay: option.tooltip,
      className: "tttt"
    }, /*#__PURE__*/_react["default"].createElement("span", {
      className: (0, _classnames["default"])('tu-icon-question-mark', 'auto-update-setting__tips')
    })));
  }))), /*#__PURE__*/_react["default"].createElement("span", {
    className: (0, _classnames["default"])('auto-update-setting__fold', "auto-update-setting__fold".concat(settingFolded ? '--close' : '')),
    onClick: function onClick() {
      return setSettingFold(!settingFolded);
    }
  }, settingFolded ? (0, _getLocale.getLocale)('Mod_Fold') : configure.settingTitle))));
};

ProgressModal.defaultProps = {
  maxLength: 500
};
var _default = ProgressModal;
exports["default"] = _default;