"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.getOperation = exports["default"] = void 0;

var _react = _interopRequireWildcard(require("react"));

var _moment = _interopRequireDefault(require("moment"));

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

var _mockData = require("./mockData");

var _OperationRecordItem = _interopRequireDefault(require("./OperationRecordItem"));

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

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

var _request = require("@titaui/request");

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 _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 getOperation = function getOperation(data) {
  return (0, _request.rpost)("v1")("summaryTemplate/operations", data);
};

exports.getOperation = getOperation;

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

  var _super = _createSuper(OperationRecord);

  function OperationRecord(props) {
    var _this;

    _classCallCheck(this, OperationRecord);

    _this = _super.call(this, props);

    _this.getFeedInfo = function (feedId, commentCount) {
      var url = (0, _requestV.getApiUrl2)("user/feed/GetById?feed_id=".concat(feedId, "&comment_count=").concat(commentCount));
      (0, _requestV.taker)(url, "GET").then(function (res) {
        if (res.Code === 1) {
          var data = res.Data.feedOperations;

          var newOperationTrackList = _this.formatCommentsDate(data.comments ? data.comments : []);

          _this.setState({
            operationTrackList: newOperationTrackList,
            operationTrackTotal: data.total
          });
        }
      })["catch"](function (err) {
        console.log("OperationRecord -> err", err);

        if (_this.props.mockData) {
          var newOperationTrackList = _this.formatCommentsDate(_mockData.OperationData);

          _this.setState({
            operationTrackList: newOperationTrackList,
            operationTrackTotal: (_mockData.OperationData === null || _mockData.OperationData === void 0 ? void 0 : _mockData.OperationData.length) || 0
          });
        }
      });
    };

    _this.getOperationInfo = function (feedId, commentCount) {
      var params = {
        TemplateFeedId: feedId,
        commentCount: commentCount
      };
      getOperation(params).then(function (res) {
        if (res.Code === 1) {
          var data = res.Data;
          console.log('data', data);

          var newOperationTrackList = _this.formatCommentsDate(data.comments ? data.comments : []);

          _this.setState({
            operationTrackList: newOperationTrackList,
            operationTrackTotal: data.total
          });
        }
      });
    };

    _this.formatCommentsDate = function (comments) {
      var thisYear = (0, _moment["default"])().year(); // 获取当前年份

      var new_data = [];
      var new_year = null;
      var new_year_data = [];
      var tag_month = null;

      for (var i = 0; i < comments.length; i++) {
        var item = comments[i];
        var mCreatDate = (0, _moment["default"])(item.createDate, "YYYY/MM/DD HH:mm");
        var f_year = mCreatDate.format("YYYY");
        var f_month = mCreatDate.format("MM/DD");
        var f_time = mCreatDate.format("HH:mm");
        item.f_year = i == 0 && Number(f_year) == thisYear ? f_year : mCreatDate.format("YYYY/MM/DD");
        item.f_time = f_time; // 如果和上一条是数据是同一条，不显示月、日

        if (f_year !== new_year) {
          tag_month = f_month;
          item.f_month = f_month;
          new_year = f_year;
          new_year_data = [];
          new_year_data.push(item);
          new_data.push({
            year: new_year,
            data: new_year_data,
            thisYear: Number(f_year) == thisYear
          });
        } else {
          if (tag_month == f_month) {
            item.f_month = null;
          } else {
            tag_month = f_month;
            item.f_month = f_month;
          }

          new_year_data.push(item);
        }
      }

      return new_data;
    };

    _this.handlerSwitchShowRecord = function () {
      var _this$props = _this.props,
          feedId = _this$props.feedId,
          commentCount = _this$props.commentCount,
          isSummaryTemplate = _this$props.isSummaryTemplate; // 默认展示条数

      var operationTrackTotal = _this.state.operationTrackTotal; // 记录总数

      var isShowAllOperationRecord = _this.state.isShowAllOperationRecord;
      isShowAllOperationRecord = !isShowAllOperationRecord; // isShowAllOperationRecord ? this.getFeedInfo(feedId, operationTrackTotal) : this.getFeedInfo(feedId, commentCount);
      // isShowAllOperationRecord ? this.getOperationInfo(feedId, operationTrackTotal) : this.getOperationInfo(feedId, commentCount);

      if (isSummaryTemplate) {
        isShowAllOperationRecord ? _this.getOperationInfo(feedId, operationTrackTotal) : _this.getOperationInfo(feedId, commentCount);
      } else {
        isShowAllOperationRecord ? _this.getFeedInfo(feedId, operationTrackTotal) : _this.getFeedInfo(feedId, commentCount);
      }

      _this.setState({
        isShowAllOperationRecord: isShowAllOperationRecord
      });
    };

    _this.state = {
      operationTrackList: [],
      operationTrackTotal: 0,
      isShowAllOperationRecord: false
    };
    return _this;
  }

  _createClass(OperationRecord, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var _this$props2 = this.props,
          feedId = _this$props2.feedId,
          commentCount = _this$props2.commentCount,
          isSummaryTemplate = _this$props2.isSummaryTemplate;

      if (feedId) {
        // this.getFeedInfo(feedId, commentCount);
        // this.getOperationInfo(feedId, commentCount);
        if (isSummaryTemplate) {
          this.getOperationInfo(feedId, commentCount);
        } else {
          this.getFeedInfo(feedId, commentCount);
        }
      }
    }
  }, {
    key: "componentDidUpdate",
    value: function componentDidUpdate(prevProps, prevState) {
      if (prevProps.feedId != this.props.feedId) {
        var _this$props3 = this.props,
            feedId = _this$props3.feedId,
            commentCount = _this$props3.commentCount,
            isSummaryTemplate = _this$props3.isSummaryTemplate;

        if (isSummaryTemplate) {
          this.getOperationInfo(feedId, commentCount);
        } else {
          this.getFeedInfo(feedId, commentCount);
        }
      }
    }
    /**
    *获取feed信息
    * @feed_id
    * @comment_count 返回的回复数
    * @memberof Cooperation
    */

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

      var className = this.props.className;
      var _this$state = this.state,
          isShowAllOperationRecord = _this$state.isShowAllOperationRecord,
          operationTrackList = _this$state.operationTrackList,
          operationTrackTotal = _this$state.operationTrackTotal;
      return /*#__PURE__*/_react["default"].createElement(_style.OperationRecordWrapper, {
        className: className
      }, /*#__PURE__*/_react["default"].createElement(_style.OperationRecordTotal, null, /*#__PURE__*/_react["default"].createElement("p", {
        className: isShowAllOperationRecord ? "tu-icon-angles-up" : "tu-icon-angles-down",
        onMouseDown: function onMouseDown() {
          return _this2.handlerSwitchShowRecord();
        }
      }, (0, _getLocale.getLocale)("OKR_MyO_KRP_Alloperations"), "\xA0", /*#__PURE__*/_react["default"].createElement("span", null, operationTrackTotal))), /*#__PURE__*/_react["default"].createElement(_style.OperationRecordList, null, /*#__PURE__*/_react["default"].createElement("dl", null, operationTrackList.length > 0 && operationTrackList.map(function (element, index) {
        return /*#__PURE__*/_react["default"].createElement(_style.Dd, {
          className: ["tu-font-a2", operationTrackList.length === 1 && element.thisYear ? "one_tkfb" : ""].join(" "),
          key: index
        }, element.year && /*#__PURE__*/_react["default"].createElement(_style.OperationDateYearWrapper, {
          className: "operation__date__container"
        }, /*#__PURE__*/_react["default"].createElement(_style.OperationDateYear, null, /*#__PURE__*/_react["default"].createElement("span", null, element.year, "\u5E74")), /*#__PURE__*/_react["default"].createElement(_style.OperationDateYearDot, null), /*#__PURE__*/_react["default"].createElement(_style.OperationDateYearLine, null)), element.data.map(function (item, index) {
          return /*#__PURE__*/_react["default"].createElement(_OperationRecordItem["default"], {
            operationTrack: item,
            key: item.commentId
          });
        }));
      }))));
    }
  }]);

  return OperationRecord;
}(_react.PureComponent);

exports["default"] = OperationRecord;