"use strict";

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

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

var _checkbox = _interopRequireDefault(require("../../../checkbox"));

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

var _openData = require("../../../../utils/open-data");

var _tools = require("../../../../utils/tools");

var _tooltipText = _interopRequireDefault(require("../../../tooltip/tooltip-text"));

var _helpers = require("../../../../utils/helpers");

var _scrollbar = _interopRequireDefault(require("../../../scrollbar"));

var _radio = _interopRequireDefault(require("../../radio"));

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

require("./index.css");

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

function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }

function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread 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 _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }

function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }

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 preCls = 'okr-import-task-work-tree';

var TaskWorkTree = function TaskWorkTree(_ref) {
  var data = _ref.data,
      selected = _ref.selected,
      multiple = _ref.multiple,
      setSelected = _ref.setSelected,
      pageSize = _ref.pageSize,
      _ref$isWork = _ref.isWork,
      isWork = _ref$isWork === void 0 ? false : _ref$isWork;
  var scrollStyle = {
    height: '100%',
    backgroundColor: 'transparent',
    width: '100%'
  };

  var handleName = function handleName(id) {
    if (isWork) {
      var _getLoginUserInfo;

      window.open("#work?work_id=".concat(id, "&to_user_id=").concat((_getLoginUserInfo = (0, _helpers.getLoginUserInfo)()) === null || _getLoginUserInfo === void 0 ? void 0 : _getLoginUserInfo.Id, "&status=1"));
    } else {
      window.Talent.app.vent.trigger('global-pull-screen', id, 'task');
    }
  };

  var handlerCheckAll = function handlerCheckAll(checked) {
    if (checked) {
      var ids = [];
      data.forEach(function (item) {
        var obj = {};
        obj['id'] = item.id;
        ids.push(obj);
      });
      setSelected(ids);
    } else {
      setSelected([]);
    }
  };

  var isSelectedAll = function isSelectedAll() {
    if (selected.length === pageSize) {
      return true;
    } else {
      return false;
    }
  };

  var onCheckHandler = function onCheckHandler(checked, id) {
    if (checked) {
      if (multiple) {
        setSelected([{
          id: id
        }].concat(_toConsumableArray(selected)));
      } else {
        setSelected([{
          id: id
        }]);
      }
    } else {
      if (multiple) {
        var newArr = selected.filter(function (item) {
          return item.id !== id;
        });
        setSelected(newArr);
      } else {
        setSelected([]);
      }
    }
  };

  var hasId = function hasId(id) {
    if (!id) return false;
    return !!selected.find(function (item) {
      return item.id === id;
    });
  };

  if (!(data !== null && data !== void 0 && data.length)) return /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(preCls, "__empty")
  }, (0, _getLocale.getLocale)('Pro_Butt_Nodata'));
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: preCls
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(preCls, "__header")
  }, multiple && /*#__PURE__*/_react["default"].createElement("span", {
    className: "".concat(preCls, "__checkbox")
  }, /*#__PURE__*/_react["default"].createElement(_checkbox["default"], {
    onChange: handlerCheckAll,
    name: 'all',
    checked: isSelectedAll()
  })), /*#__PURE__*/_react["default"].createElement("span", {
    className: "".concat(preCls, "__header-name")
  }, isWork ? (0, _getLocale.getLocale)('Feed_Set_Project') : (0, _getLocale.getLocale)('Feed_Tab_Title_Task')), /*#__PURE__*/_react["default"].createElement("span", {
    className: "".concat(preCls, "__header-status")
  }, (0, _getLocale.getLocale)('OKR_MyO_Pop_Upd_Status')), /*#__PURE__*/_react["default"].createElement("span", {
    className: "".concat(preCls, "__header-principer")
  }, (0, _getLocale.getLocale)('OKR_MyO_Pop_Owner')), /*#__PURE__*/_react["default"].createElement("span", {
    className: "".concat(preCls, "__header-time")
  }, (0, _getLocale.getLocale)('Mod_Startendtime'))), /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(preCls, "__content")
  }, /*#__PURE__*/_react["default"].createElement(_scrollbar["default"].Mask, {
    style: scrollStyle,
    maskHeight: 16
  }, data.map(function (item) {
    var _item$principalUser, _item$principalUser2, _item$principalUser3;

    return /*#__PURE__*/_react["default"].createElement("div", {
      className: "".concat(preCls, "__content-item"),
      key: item.id
    }, multiple && /*#__PURE__*/_react["default"].createElement("span", {
      className: "".concat(preCls, "__checkbox")
    }, /*#__PURE__*/_react["default"].createElement(_checkbox["default"], {
      onChange: onCheckHandler,
      name: item.id,
      checked: hasId(item.id)
    })), !multiple && /*#__PURE__*/_react["default"].createElement("span", {
      className: "".concat(preCls, "__checkbox")
    }, /*#__PURE__*/_react["default"].createElement(_radio["default"], {
      onChange: onCheckHandler,
      id: item.id,
      checked: hasId(item.id)
    })), /*#__PURE__*/_react["default"].createElement("div", {
      className: "".concat(preCls, "__content-item-name"),
      onClick: function onClick() {
        return handleName(item.taskId);
      }
    }, /*#__PURE__*/_react["default"].createElement(_tooltipText["default"], {
      text: (0, _tools.htmlDecodeByRegExp)(item.name),
      maxWidth: 332
    })), /*#__PURE__*/_react["default"].createElement("div", {
      className: "".concat(preCls, "__content-item-status")
    }, item.status), /*#__PURE__*/_react["default"].createElement("div", {
      className: "".concat(preCls, "__content-item-principer")
    }, /*#__PURE__*/_react["default"].createElement(_tooltipText["default"], {
      isPercent: true,
      text: /*#__PURE__*/_react["default"].createElement(_openData.OpenUserName, {
        name: (item === null || item === void 0 ? void 0 : (_item$principalUser = item.principalUser) === null || _item$principalUser === void 0 ? void 0 : _item$principalUser.name) || (item === null || item === void 0 ? void 0 : (_item$principalUser2 = item.principalUser) === null || _item$principalUser2 === void 0 ? void 0 : _item$principalUser2.Name),
        id: item === null || item === void 0 ? void 0 : (_item$principalUser3 = item.principalUser) === null || _item$principalUser3 === void 0 ? void 0 : _item$principalUser3.userId
      })
    })), /*#__PURE__*/_react["default"].createElement("div", {
      className: "".concat(preCls, "__content-item-time")
    }, (0, _helper.formatDateStr)(item.startDate), " -", ' ', (0, _helper.formatDateStr)(item.endDate)));
  }))));
};

exports.TaskWorkTree = TaskWorkTree;
var _default = TaskWorkTree;
exports["default"] = _default;