"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getEmptyBySource = exports.getColumsBySource = exports["default"] = void 0;

var _request = require("../../../../../utils/request");

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

var _drawerManager = _interopRequireDefault(require("../../../../../components/drawer-manager"));

var _columns = require("./columns");

var _periodSelector = _interopRequireDefault(require("../../../../../components/period-selector"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }

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

// 日期参数转换
var cycleFilterModels = function cycleFilterModels(cycleTime) {
  var res = [{
    CptFilterName: "cycle_type",
    CptFilterValue: cycleTime.cycleType === "-1" ? "0,1,2,3,4,5" : cycleTime.cycleType
  }];

  if (cycleTime.cycleType !== "-1") {
    res.push({
      CptFilterName: "annual_num",
      CptFilterValue: cycleTime.yearNum
    });
  }

  if (cycleTime.cycleType === "2" || cycleTime.cycleType === "3" || cycleTime.cycleType === "4" || cycleTime.cycleType === "5") {
    res.push({
      CptFilterName: "yqm_num",
      CptFilterValue: cycleTime.yqmNum
    });
  }

  return res;
}; // okr参数转换


var getComponentDataProp = {
  all: "allOkr",
  hadAlign: "MyParentOkrCount",
  hadNotAlign: "MyNoParentOkrCount",
  hadUpdate: "allMyProgressCount",
  hadNotUpdate: "allMyNoProgressCount",
  normalOkr: "MyNormalOkrCount",
  riskOkr: "MyRiskOkrCount",
  dalyOkr: "MyDlayOkrCount",
  endOkr: "MyFinishOkrCount"
}; // 周期转换

var cycleTimeDisplay = function cycleTimeDisplay(cycleType, annualNum, yqmNum) {
  return _periodSelector["default"].formatPeriod({
    annualNum: annualNum,
    cycleType: cycleType,
    yqmNum: yqmNum
  });
}; // ComponentId转换


var ComponentIdMap = {
  align: "Align-OKR-Target",
  update: "OKR-Progress-Display",
  target: "OKR-Target-Display"
};

var _default = function _default(source, params) {
  var type = params.type,
      cycleTime = params.cycleTime,
      pageNum = params.pageNum,
      pageSize = params.pageSize;
  return new Promise(function (resolve, reject) {
    var dataList = [];
    var dataTotal = 0;
    (0, _request.postForTableList)({
      ComponentId: ComponentIdMap[source],
      ComponentDataProp: getComponentDataProp[type],
      PageSize: pageSize,
      PageNum: pageNum,
      FilterModels: cycleFilterModels(cycleTime)
    }).then(function (res) {
      if (res.Code == 1) {
        var listData = [];

        if (res.Data.componentData) {
          var _iterator = _createForOfIteratorHelper(res.Data.componentData),
              _step;

          try {
            var _loop = function _loop() {
              var item = _step.value;
              listData.push({
                columnId: item.workId,
                okrType: "O",
                okrName: item.workName,
                cycleTime: cycleTimeDisplay(item.cycleType, item.annualNum, item.yqmnum),
                userInfo: {
                  name: item.principalUser.name,
                  avatarImg: item.principalUser.avatar.medium,
                  userId: item.principalUser.userId,
                  avatarColor: item.principalUser.avatar.color
                },
                riskLevel: item.manualRiskLevel,
                progress: item.progress,
                typeName: item.parentId > 0 || item.parentKrId > 0 ? (0, _getLocale.getLocale)("OKR_Db_Aligned") : (0, _getLocale.getLocale)("OKR_Db_Unaligned"),
                updateTime: item.lastUpdatedDate,
                status: item.status,
                createDate: item.CreateDate,
                behave: function behave() {
                  return _drawerManager["default"].open("okrDetail", {
                    okrId: item.workId
                  });
                }
              });

              if (item.Krs.length && source !== "align") {
                var _iterator2 = _createForOfIteratorHelper(item.Krs),
                    _step2;

                try {
                  var _loop2 = function _loop2() {
                    var item1 = _step2.value;
                    listData.push({
                      columnId: item1.mileStoneId,
                      okrType: "KR",
                      okrName: item1.mileStoneName,
                      cycleTime: "".concat(item1.startDate.slice(-5), "~").concat(item1.endDate.slice(-5)),
                      userInfo: {
                        name: item1.user.name,
                        avatarImg: item1.user.avatar.medium,
                        userId: item1.user.userId,
                        avatarColor: item1.user.avatar.color
                      },
                      riskLevel: item1.manualRiskLevel,
                      progress: item1.progress,
                      status: item1.status,
                      updateTime: item1.progressDescriptionDate,
                      createDate: item1.CreateDate,
                      behave: function behave() {
                        return _drawerManager["default"].open("krDetail", {
                          krId: item1.mileStoneId
                        });
                      }
                    });
                  };

                  for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
                    _loop2();
                  }
                } catch (err) {
                  _iterator2.e(err);
                } finally {
                  _iterator2.f();
                }
              }
            };

            for (_iterator.s(); !(_step = _iterator.n()).done;) {
              _loop();
            }
          } catch (err) {
            _iterator.e(err);
          } finally {
            _iterator.f();
          }
        }

        dataList = listData;
        dataTotal = res.Data.total;
        resolve({
          dataList: dataList,
          dataTotal: dataTotal
        });
      } else {
        reject("请求失败");
      }
    });
  });
};

exports["default"] = _default;

var getColumsBySource = function getColumsBySource(source) {
  switch (source) {
    case "align":
      return _columns.okrAlignColumns;
      break;

    case "update":
      return _columns.okrUpdateColumns;
      break;

    case "target":
      return _columns.myOkrColumns;
      break;
  }
};

exports.getColumsBySource = getColumsBySource;

var getEmptyBySource = function getEmptyBySource(source) {
  switch (source) {
    case "align":
      return {
        imageSrc: require("../../../image/align-list-empty.png"),
        link: "https://club.tita.com/18150.html",
        emptyText: "暂无对齐",
        linkText: "如何对齐"
      };
      break;

    case "update":
      return {
        imageSrc: require("../../../image/update-list-empty.png"),
        link: "https://club.tita.com/18179.html",
        emptyText: "暂无更新",
        linkText: "如何更新"
      };
      break;

    case "target":
      return {
        imageSrc: require("../../../image/myokr-list-empty.png"),
        link: "#okr%2Fmanage",
        emptyText: "\u300C".concat((0, _getLocale.getLocale)("Tasks_Tab_MyS_NoOKRilable"), "\u300D"),
        linkText: (0, _getLocale.getLocale)("OKR_MyO_Gotoview")
      };
      break;
  }
};

exports.getEmptyBySource = getEmptyBySource;