"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 _searchForm = _interopRequireDefault(require("./search-form"));

var _okrTree = _interopRequireDefault(require("../components/okr-tree"));

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

var _util = require("./util");

var _context = require("../context");

var _paddingLayout = _interopRequireDefault(require("../components/padding-layout"));

var _loading = _interopRequireDefault(require("../loading"));

var _bsGlobal = require("../../../utils/bs-global");

var _conditionRender = _interopRequireDefault(require("../../condition-render"));

var _eListEmpty = _interopRequireDefault(require("./images/e-list-empty.png"));

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 = "okr-detail-e";

function Elist(props, ref) {
  var okrId = props.okrId,
      _props$hasSearch = props.hasSearch,
      hasSearch = _props$hasSearch === void 0 ? true : _props$hasSearch,
      _props$formatTree = props.formatTree,
      formatTree = _props$formatTree === void 0 ? _util.formatEListDataToSelectTree : _props$formatTree,
      _props$startDate = props.startDate,
      startDate = _props$startDate === void 0 ? '' : _props$startDate,
      _props$endDate = props.endDate,
      endDate = _props$endDate === void 0 ? '' : _props$endDate,
      _props$toLevel = props.toLevel,
      toLevel = _props$toLevel === void 0 ? 999 : _props$toLevel,
      _props$hasdivider = props.hasdivider,
      hasdivider = _props$hasdivider === void 0 ? false : _props$hasdivider,
      className = props.className,
      _props$IncludeNoTaskK = props.IncludeNoTaskKr,
      IncludeNoTaskKr = _props$IncludeNoTaskK === void 0 ? true : _props$IncludeNoTaskK,
      _props$hasLoading = props.hasLoading,
      hasLoading = _props$hasLoading === void 0 ? true : _props$hasLoading,
      _props$IsDateFilter = props.IsDateFilter,
      IsDateFilter = _props$IsDateFilter === void 0 ? false : _props$IsDateFilter,
      emptyPage = props.emptyPage,
      setIsEmpty = props.setIsEmpty,
      _props$layoutPadding = props.layoutPadding,
      layoutPadding = _props$layoutPadding === void 0 ? '0 32px 0 18px' : _props$layoutPadding;
  var loginUser = (0, _bsGlobal.getUserInfo)().Id;
  var hasCondition = hasSearch && localStorage.getItem("hasRememberCondition".concat(loginUser));

  var _useState = (0, _react.useState)([]),
      _useState2 = _slicedToArray(_useState, 2),
      krList = _useState2[0],
      setKrList = _useState2[1];

  var _useState3 = (0, _react.useState)(hasCondition ? JSON.parse(localStorage.getItem("okrDrawerECheckedStatus".concat(loginUser)) || '[]') : []),
      _useState4 = _slicedToArray(_useState3, 2),
      checkedStatus = _useState4[0],
      setCheckedStatus = _useState4[1];

  var _useState5 = (0, _react.useState)(hasCondition ? JSON.parse(localStorage.getItem("okrDrawerESelectUsers".concat(loginUser)) || '[]') : []),
      _useState6 = _slicedToArray(_useState5, 2),
      selectUsers = _useState6[0],
      setSelectUsers = _useState6[1];

  var _useState7 = (0, _react.useState)(hasCondition ? localStorage.getItem("okrDrawerESelectName".concat(loginUser)) : "orderBySystem"),
      _useState8 = _slicedToArray(_useState7, 2),
      selectName = _useState8[0],
      setSelectName = _useState8[1];

  var _useState9 = (0, _react.useState)(hasCondition ? localStorage.getItem("okrDrawerENoteOrder".concat(loginUser)) : null),
      _useState10 = _slicedToArray(_useState9, 2),
      noteOrder = _useState10[0],
      setNoteOrder = _useState10[1];

  var _useState11 = (0, _react.useState)(),
      _useState12 = _slicedToArray(_useState11, 2),
      searchKey = _useState12[0],
      setSearchKey = _useState12[1];

  var _useState13 = (0, _react.useState)([]),
      _useState14 = _slicedToArray(_useState13, 2),
      oEList = _useState14[0],
      setOEList = _useState14[1];

  var _useState15 = (0, _react.useState)([]),
      _useState16 = _slicedToArray(_useState15, 2),
      expandedKrEs = _useState16[0],
      setExpandedKrEs = _useState16[1];

  var _useState17 = (0, _react.useState)([]),
      _useState18 = _slicedToArray(_useState17, 2),
      expandedOEs = _useState18[0],
      setExpandedOEs = _useState18[1];

  var _useState19 = (0, _react.useState)(true),
      _useState20 = _slicedToArray(_useState19, 2),
      loading = _useState20[0],
      setLoading = _useState20[1];

  var _useState21 = (0, _react.useState)(0),
      _useState22 = _slicedToArray(_useState21, 2),
      totalDataNum = _useState22[0],
      setTotalDataNum = _useState22[1];

  var getKrEsData = function getKrEsData(formatTree) {
    setLoading(true);
    var formatEList = formatTree ? formatTree : _util.formatEListDataToSelectTree;
    (0, _requestApis.getKrsByCondition)({
      okrId: okrId,
      condition: (0, _util.formatCondition)({
        okrId: okrId,
        noteOrder: noteOrder,
        selectName: selectName,
        checkedStatus: checkedStatus,
        users: selectUsers,
        keyWord: searchKey,
        startDate: startDate,
        endDate: endDate,
        toLevel: toLevel,
        IncludeNoTaskKr: IncludeNoTaskKr,
        IsDateFilter: IsDateFilter
      })
    }).then(function (krs) {
      var formatKrs = formatEList(krs, okrId, (0, _util.getIfInitCondition)(selectName, checkedStatus, selectUsers, searchKey));
      var newexpandedKrEs = (formatKrs.krsEList || []).map(function (it) {
        return it.key;
      });
      var newexpandedOEs = (formatKrs.oEList[0] || []).map(function (it) {
        return it.key;
      });
      setExpandedKrEs(newexpandedKrEs);
      setExpandedOEs(newexpandedOEs);
      setKrList(formatKrs.krsEList);
      setOEList(formatKrs.oEList);
      setLoading(false);
    });
  };

  (0, _react.useImperativeHandle)(ref, function () {
    return {
      getKrEsData: getKrEsData
    };
  });
  (0, _react.useEffect)(function () {
    // 可以接收一个格式化方法
    getKrEsData(formatTree);
  }, [noteOrder, searchKey, selectName, checkedStatus, selectUsers, startDate, endDate, okrId]);
  (0, _react.useEffect)(function () {
    var oElistLength = oEList[0] && oEList[0].length || 0;
    var totalLength = krList.length + oElistLength;
    setTotalDataNum(totalLength);
    setIsEmpty && setIsEmpty(!totalLength);
  }, [krList, oEList]);
  return /*#__PURE__*/_react["default"].createElement(_context.EListContext.Provider, {
    value: {
      refreshEList: function refreshEList() {
        return getKrEsData(formatTree);
      }
    }
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(preCls, " ").concat(className)
  }, hasSearch && /*#__PURE__*/_react["default"].createElement(_paddingLayout["default"], null, /*#__PURE__*/_react["default"].createElement(_searchForm["default"], {
    checkedStatus: checkedStatus,
    setCheckedStatus: setCheckedStatus,
    selectName: selectName,
    setSelectName: setSelectName,
    noteOrder: noteOrder,
    setNoteOrder: setNoteOrder,
    setSearchKey: setSearchKey,
    selectUsers: selectUsers,
    setSelectUsers: setSelectUsers,
    okrId: okrId
  })), !loading || !hasLoading ? /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, /*#__PURE__*/_react["default"].createElement(_conditionRender["default"], {
    condition: totalDataNum !== 0
  }, krList.map(function (kr, index) {
    return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, /*#__PURE__*/_react["default"].createElement(_paddingLayout["default"], {
      padding: layoutPadding
    }, /*#__PURE__*/_react["default"].createElement(_okrTree["default"], {
      key: Math.random(),
      data: [kr],
      defaultExpandedKeys: expandedKrEs
    })), // kr左后一个节点 && 没有 OE 时，不显示横线
    !(index + 1 == krList.length && oEList.length == 0) && hasdivider && /*#__PURE__*/_react["default"].createElement("div", {
      className: "e__divider"
    }));
  }), !!oEList && !!oEList.length && oEList.map(function (kr) {
    return /*#__PURE__*/_react["default"].createElement(_paddingLayout["default"], {
      padding: layoutPadding
    }, /*#__PURE__*/_react["default"].createElement(_okrTree["default"], {
      key: Math.random(),
      data: kr,
      defaultExpandedKeys: expandedOEs
    }));
  })), /*#__PURE__*/_react["default"].createElement(_conditionRender["default"], {
    condition: totalDataNum === 0
  }, emptyPage ? emptyPage() : /*#__PURE__*/_react["default"].createElement("div", {
    className: "".concat(preCls, "__empty")
  }, /*#__PURE__*/_react["default"].createElement("img", {
    className: "".concat(preCls, "__empty-img"),
    src: _eListEmpty["default"]
  }), /*#__PURE__*/_react["default"].createElement("span", {
    className: "".concat(preCls, "__empty-text")
  }, "\u6CA1\u6709\u641C\u7D22\u5230\u7B26\u5408\u6761\u4EF6\u7684E-\u6267\u884C")))) : /*#__PURE__*/_react["default"].createElement(_loading["default"], {
    type: "panel"
  })));
} // @ts-ignore


Elist = /*#__PURE__*/(0, _react.forwardRef)(Elist);
var _default = Elist;
exports["default"] = _default;