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

var _form = _interopRequireDefault(require("../../../form"));

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

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

var _regulationSettingItem = _interopRequireDefault(require("../../../regulation-setting-item"));

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

var _rate = _interopRequireDefault(require("../../../rate"));

var _api = require("./api");

var _encourage = _interopRequireDefault(require("./encourage"));

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 precls = "evaluation-dialog";

var EvaluationDialog = function EvaluationDialog(props) {
  var visible = props.visible,
      closeDialog = props.closeDialog,
      toUserId = props.toUserId,
      feedId = props.feedId,
      isUpdated = props.isUpdated;

  var _useState = (0, _react.useState)(""),
      _useState2 = _slicedToArray(_useState, 2),
      remark = _useState2[0],
      setRemark = _useState2[1];

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

  var _useState5 = (0, _react.useState)(""),
      _useState6 = _slicedToArray(_useState5, 2),
      encourageState = _useState6[0],
      setEencourageState = _useState6[1];

  var inputRef = (0, _react.useRef)(document.createElement('input'));

  var _onChange = function onChange(e) {
    setRemark(e);
  };

  var changeRate = function changeRate(v) {
    setRatingScore(v);
    setEencourageState("");
  };

  var changeEncourage = function changeEncourage(v) {
    setEencourageState(v);
    setRatingScore(0);
  };

  var handleSubmit = function handleSubmit() {
    if (!encourageState && ratingScore === 0) {
      return _toast["default"].Warning("综合评分与奖章鼓励，任选其一");
    }

    if ((remark === null || remark === void 0 ? void 0 : remark.length) > 5000) return false;
    var evaluateType = "";
    var value = "";

    if (encourageState) {
      evaluateType = "2";
      value = encourageState;
    } else if (ratingScore !== 0) {
      evaluateType = "1";
      value = ratingScore * 2;
    }

    var params = {
      objId: feedId,
      objType: "8",
      toUserId: toUserId,
      remark: remark,
      evaluateInfo: [{
        evaluateType: evaluateType,
        value: value
      }]
    };
    (0, _api.setSummaryEvaluate)(params).then(function (res) {
      if (res.Code === 1) {
        var _res$Data;

        closeDialog({
          remark: remark,
          type: Number(evaluateType),
          value: value,
          user: res === null || res === void 0 ? void 0 : (_res$Data = res.Data) === null || _res$Data === void 0 ? void 0 : _res$Data.user
        });

        _toast["default"].Success((0, _getLocale.getLocale)("Mod_Successfuloperat"));
      }
    });
  };

  (0, _react.useEffect)(function () {
    if (!isUpdated && visible) {
      var params = {
        objectId: feedId,
        objectType: "8",
        toUserId: toUserId
      };
      (0, _api.getSummaryEvaluate)(params).then(function (res) {
        var _res$Data2, _res$Data3, _res$Data5;

        setRemark(res === null || res === void 0 ? void 0 : (_res$Data2 = res.Data) === null || _res$Data2 === void 0 ? void 0 : _res$Data2.remark);

        if ((res === null || res === void 0 ? void 0 : (_res$Data3 = res.Data) === null || _res$Data3 === void 0 ? void 0 : _res$Data3.type) === 1) {
          var _res$Data4;

          setRatingScore(Number(res === null || res === void 0 ? void 0 : (_res$Data4 = res.Data) === null || _res$Data4 === void 0 ? void 0 : _res$Data4.value) / 2);
        } else if ((res === null || res === void 0 ? void 0 : (_res$Data5 = res.Data) === null || _res$Data5 === void 0 ? void 0 : _res$Data5.type) === 2) {
          var _res$Data6;

          setEencourageState(res === null || res === void 0 ? void 0 : (_res$Data6 = res.Data) === null || _res$Data6 === void 0 ? void 0 : _res$Data6.value);
        }
      });
    }
  }, [visible]);
  var onCloseHandler = (0, _react.useCallback)(function () {
    closeDialog();
    setEencourageState("");
    setRatingScore(0);
  }, []);
  return /*#__PURE__*/_react["default"].createElement(_dialog["default"], {
    visible: visible,
    style: {
      width: 538
    },
    onCancel: onCloseHandler,
    onOk: handleSubmit,
    noFooterLine: true,
    destroyOnClose: true,
    noHeadLine: true,
    title: /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement("p", null, (0, _getLocale.getLocale)("Rep_NewS_Evaluation")), /*#__PURE__*/_react["default"].createElement("span", {
      className: "titleDesc"
    }, (0, _getLocale.getLocale)("Rep_NewS_ComhenRatin"))),
    onClose: onCloseHandler
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(precls, "-content")
  }, /*#__PURE__*/_react["default"].createElement(_form["default"], null, /*#__PURE__*/_react["default"].createElement(_regulationSettingItem["default"], {
    label: (0, _getLocale.getLocale)("Rep_NewS_OverallRating")
  }, /*#__PURE__*/_react["default"].createElement(_rate["default"], {
    count: 5,
    allowHalf: false,
    value: ratingScore,
    onChange: changeRate
  }), ratingScore !== 0 && /*#__PURE__*/_react["default"].createElement("span", null, ratingScore * 2, (0, _getLocale.getLocale)("Per_Set_Form_Quantifier_Points"))), /*#__PURE__*/_react["default"].createElement(_regulationSettingItem["default"], {
    label: (0, _getLocale.getLocale)("Rep_NewS_MedalEncour")
  }, /*#__PURE__*/_react["default"].createElement(_encourage["default"], {
    encourageState: encourageState,
    changeEncourage: changeEncourage
  })), /*#__PURE__*/_react["default"].createElement(_regulationSettingItem["default"], {
    className: "evaluation-text",
    label: (0, _getLocale.getLocale)("Per_Ass_New_Appr_detail_Com")
  }, /*#__PURE__*/_react["default"].createElement(_textarea["default"], {
    maxLine: 4,
    line: 4,
    maxLength: 5000,
    value: remark,
    onChange: function onChange(e) {
      return _onChange(e.target.value);
    },
    wrapperClass: "textarea",
    placeholder: (0, _getLocale.getLocale)("Rep_NewS_PleaEnteCom"),
    inputRef: inputRef
  })))));
};

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