"use strict";

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

require("maycur-antd/lib/list/style/css");

var _list = _interopRequireDefault(require("maycur-antd/lib/list"));

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

var _lodash = _interopRequireDefault(require("lodash"));

var _classnames = _interopRequireDefault(require("classnames"));

var _constants = _interopRequireDefault(require("../../utils/constants"));

var _utils = _interopRequireDefault(require("../../utils/utils"));

var _i18next = _interopRequireDefault(require("i18next"));

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (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 _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

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); return Constructor; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

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

var prefix = _utils["default"].prefixCls;
var DEPARTMENT_PICKER = _constants["default"].DEPARTMENT_PICKER;

var UserGroupList =
/*#__PURE__*/
function (_Component) {
  _inherits(UserGroupList, _Component);

  function UserGroupList(props) {
    var _this;

    _classCallCheck(this, UserGroupList);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(UserGroupList).call(this, props));
    _this.state = {
      sourceList: [],
      selectList: props.selectList || [],
      allSelected: false
    };
    _this.toggleAll = _this.toggleAll.bind(_assertThisInitialized(_this));
    _this.toggleItem = _this.toggleItem.bind(_assertThisInitialized(_this));
    _this.checkAllFlag = _this.checkAllFlag.bind(_assertThisInitialized(_this));
    _this.output = _this.output.bind(_assertThisInitialized(_this));
    return _this;
  }

  _createClass(UserGroupList, [{
    key: "toggleAll",
    value: function toggleAll() {
      var _this$state = this.state,
          sourceList = _this$state.sourceList,
          allSelected = _this$state.allSelected;
      this.setState({
        selectList: sourceList,
        allSelected: !allSelected
      });
      this.output(sourceList, false);
    }
  }, {
    key: "toggleItem",
    value: function toggleItem(item) {
      var selectList = this.state.selectList;

      var idx = _lodash["default"].findIndex(selectList, {
        code: item.code
      });

      if (idx !== -1) {
        selectList.splice(idx, 1);
      } else {
        selectList.push(item);
      }

      this.setState({
        selectList: selectList
      });
      this.output(selectList, true);
      setTimeout(this.checkAllFlag);
    }
  }, {
    key: "checkAllFlag",
    value: function checkAllFlag() {
      var _this$state2 = this.state,
          sourceList = _this$state2.sourceList,
          selectList = _this$state2.selectList;
      var targetList = sourceList;
      var allSelected = targetList.length && !_lodash["default"].find(targetList, function (dep) {
        return !_lodash["default"].find(selectList, {
          code: dep.code
        });
      });
      this.setState({
        allSelected: allSelected
      });
    }
  }, {
    key: "output",
    value: function output(selectList, itemSelected) {
      var _this$state3 = this.state,
          sourceList = _this$state3.sourceList,
          allSelected = _this$state3.allSelected;
      var onOutput = this.props.onOutput;
      var result = {
        added: selectList,
        removed: _lodash["default"].differenceWith(sourceList, selectList, function (o1, o2) {
          return o1.code === o2.code;
        }),
        allSelected: allSelected,
        itemSelected: itemSelected
      };

      if (typeof onOutput === "function") {
        onOutput(result);
      }
    }
  }, {
    key: "fetchUserGroup",
    value: function fetchUserGroup() {
      var _this2 = this;

      var fetchUserGroup = this.props.fetchUserGroup;
      fetchUserGroup({
        pageId: DEPARTMENT_PICKER.USER_GROUP_SOURCE_LIST
      }).then(function (data) {
        if (_utils["default"].apiOK(data)) {
          var _data$data = data.data,
              userGroups = _data$data.userGroups,
              userGroupCategorys = _data$data.userGroupCategorys;

          var sourceList = _lodash["default"].flatten(userGroups.concat(_lodash["default"].map(userGroupCategorys, function (ugt) {
            return ugt.userGroups;
          })));

          _lodash["default"].forEach(sourceList, function (item) {
            item.type = DEPARTMENT_PICKER.USER_GROUP;
          });

          _this2.setState({
            sourceList: sourceList
          }, function () {
            _this2.checkAllFlag();
          });
        }
      });
    }
  }, {
    key: "UNSAFE_componentWillReceiveProps",
    value: function UNSAFE_componentWillReceiveProps(nextProps) {
      var selectList = nextProps.selectList;
      this.setState({
        selectList: selectList
      });
      setTimeout(this.checkAllFlag);
    }
  }, {
    key: "componentDidMount",
    value: function componentDidMount() {
      this.fetchUserGroup();
    }
  }, {
    key: "render",
    value: function render() {
      var _this3 = this;

      var _this$props = this.props,
          loading = _this$props.loading,
          maxSelectLength = _this$props.maxSelectLength;
      var _this$state4 = this.state,
          sourceList = _this$state4.sourceList,
          selectList = _this$state4.selectList,
          allSelected = _this$state4.allSelected,
          className = _this$state4.className;
      var containerClass = (0, _classnames["default"])("".concat(prefix, "-user-group-list"), className);
      var allSelectedClass = (0, _classnames["default"])({
        selected: allSelected
      });
      return _react["default"].createElement("div", {
        className: containerClass
      }, maxSelectLength > 1 ? _react["default"].createElement("div", {
        className: 'opt-bar'
      }, _react["default"].createElement("div", {
        className: 'select-all-btn',
        onClick: this.toggleAll
      }, _react["default"].createElement("span", {
        className: allSelectedClass + ' fm fm-check'
      }), _react["default"].createElement("span", null, allSelected ? _i18next["default"].i18n.t('MkGlobal:selectInvert') : _i18next["default"].t('MkGlobal:selectAll')))) : null, _react["default"].createElement(_list["default"], {
        size: "small",
        loading: loading,
        dataSource: sourceList,
        renderItem: function renderItem(item) {
          var selected = _lodash["default"].findIndex(selectList, {
            code: item.code
          }) !== -1;
          var itemClass = (0, _classnames["default"])({
            selected: selected
          });
          return _react["default"].createElement(_list["default"].Item, {
            className: itemClass,
            key: item.code,
            onClick: function onClick() {
              _this3.toggleItem(item);
            }
          }, _react["default"].createElement("span", {
            className: 'fm fm-check'
          }), _react["default"].createElement("span", {
            className: 'item-name'
          }, _react["default"].createElement("span", {
            className: 'fm fm-user-group'
          }), item.name));
        }
      }));
    }
  }]);

  return UserGroupList;
}(_react.Component);

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