"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"] = _default;

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

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

var _rcDialog = _interopRequireDefault(require("rc-dialog"));

var _tree = _interopRequireDefault(require("../tree"));

var _table = _interopRequireDefault(require("../table"));

var _userSelector = _interopRequireDefault(require("../../../components/user-selector"));

var _searchInput = _interopRequireDefault(require("../../../components/search-input"));

var _toast = _interopRequireDefault(require("../../../components/toast"));

var _groupInterface = require("../groupInterface");

var _empty = _interopRequireDefault(require("./empty"));

var _groupEmpty = _interopRequireDefault(require("./group-empty"));

var _index = _interopRequireDefault(require("./importGroupStaff/index"));

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

require("rc-dialog/assets/bootstrap.css");

require("rc-dialog/assets/index.css");

require("./index.css");

var _department = _interopRequireDefault(require("../images/department.svg"));

var _group = _interopRequireDefault(require("../images/group.svg"));

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 = "user-group-page";

function _default() {
  var _useState = (0, _react.useState)(""),
      _useState2 = _slicedToArray(_useState, 2),
      dialogTitle = _useState2[0],
      setDialogTitle = _useState2[1];

  var _useState3 = (0, _react.useState)(),
      _useState4 = _slicedToArray(_useState3, 2),
      users = _useState4[0],
      setUsers = _useState4[1];

  var _useState5 = (0, _react.useState)(),
      _useState6 = _slicedToArray(_useState5, 2),
      groups = _useState6[0],
      setGroups = _useState6[1];

  var _useState7 = (0, _react.useState)(),
      _useState8 = _slicedToArray(_useState7, 2),
      selectedGroupId = _useState8[0],
      setSelectedGroupId = _useState8[1];

  var _useState9 = (0, _react.useState)(1),
      _useState10 = _slicedToArray(_useState9, 2),
      pageNum = _useState10[0],
      setPageNum = _useState10[1];

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

  var _useState13 = (0, _react.useState)(""),
      _useState14 = _slicedToArray(_useState13, 2),
      searchKey = _useState14[0],
      setSearchKey = _useState14[1];

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

  var _useState17 = (0, _react.useState)(false),
      _useState18 = _slicedToArray(_useState17, 2),
      visible = _useState18[0],
      setVisible = _useState18[1];

  var _useState19 = (0, _react.useState)(false),
      _useState20 = _slicedToArray(_useState19, 2),
      importVisible = _useState20[0],
      setImportVisible = _useState20[1];

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

  var _useState23 = (0, _react.useState)([]),
      _useState24 = _slicedToArray(_useState23, 2),
      rowSelected = _useState24[0],
      setRowSelected = _useState24[1];

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

  var _useState27 = (0, _react.useState)(false),
      _useState28 = _slicedToArray(_useState27, 2),
      staffLoading = _useState28[0],
      setStaffLoading = _useState28[1];

  var _useState29 = (0, _react.useState)({
    departments: [],
    groups: []
  }),
      _useState30 = _slicedToArray(_useState29, 2),
      details = _useState30[0],
      setDetails = _useState30[1];

  var _useState31 = (0, _react.useState)([]),
      _useState32 = _slicedToArray(_useState31, 2),
      selectedKeys = _useState32[0],
      setSelectedKeys = _useState32[1]; // 获取人员数据


  var getPageData = function getPageData(id) {
    var groupId = id || selectedGroupId;
    if (!groupId && groups && groups.length) groupId = groups[0].id;
    if (!groupId) return null;
    setStaffLoading(true);
    (0, _groupInterface.searchGroupStaffList)(groupId, pageNum).then(function (users) {
      setUsers(users.Data);
      setTotal(users.total);
      setStaffLoading(false);
    });
  }; // 获取用户部门群组信息列表


  var getStaffDetail = function getStaffDetail(id) {
    var groupId = id || selectedGroupId;
    if (!groupId && groups && groups.length) groupId = groups[0].id;
    if (!groupId) return null;
    setLoading(true);
    (0, _groupInterface.searchUserDetail)(groupId).then(function (details) {
      var data = {
        departments: [],
        groups: []
      };
      data.departments = details.Data.departments;
      data.groups = details.Data.groups;
      setDetails(data);
      setLoading(false);
    });
  }; // 获取群组


  var getGroup = function getGroup(groupId) {
    setLoading(true);
    (0, _groupInterface.getGroupList)().then(function (groups) {
      if (groups.Data.length) {
        setGroups(groups.Data); // 通过传入的id定位到我们刚刚创建的群组

        if (groupId) {
          var data = groups.Data.filter(function (item) {
            return item.id == groupId;
          });
          setSelectedGroup(data[0]);
          setSelectedGroupId(groupId);
          getPageData(groupId);
          setSelectedKeys([groupId]);
        } else {
          setSelectedGroup(groups.Data[0]);
          setSelectedGroupId(groups.Data[0].id);
          getPageData(groups.Data[0].id); // 获取群组下的人员列表

          setSelectedKeys([groups.Data[0].id]);
        }
      } else {
        setGroups();
        setSelectedGroup();
        setSelectedGroupId();
        setUsers();
      }

      setLoading(false);
    });
  };

  (0, _react.useEffect)(function () {
    // 获取群组列表
    getGroup();
  }, []); // 请求表格数据：翻页、搜索条件

  (0, _react.useEffect)(function () {
    getPageData();
    getStaffDetail();
  }, [selectedGroupId, pageNum]);

  var onTreeSelectHandler = function onTreeSelectHandler(keys, data) {
    if (keys.length == 0) {
      keys = selectedKeys;
    }

    setSelectedKeys(keys);
    setSelectedGroupId(data.node.id);
    setSelectedGroup(data.node);
    setRowSelected([]);
    console.log(searchKey);

    if (searchKey != "") {
      setSearchKey("");
      getPageData();
    }
  };

  var onSearchKeyHandler = function onSearchKeyHandler(e) {
    setSearchKey(e.target.value);
  }; // 搜索按钮点击


  var onSearchHandler = function onSearchHandler(value) {
    (0, _groupInterface.searchGroupStaffList)(selectedGroupId, 1, value).then(function (users) {
      if (users.Data.length > 0) {
        setUsers(users.Data);
        setTotal(users.total);
      } else {
        _toast["default"].Error("查询无结果");
      }
    });
  }; // onImportModalHandler


  var onImportModalHandler = function onImportModalHandler() {
    setImportVisible(true);
  }; // 添加员工


  var onAddGroupHandler = function onAddGroupHandler(selectedUsers) {
    var users = selectedUsers.users,
        departments = selectedUsers.departments,
        groups = selectedUsers.groups;
    (0, _groupInterface.addGroupStaff)({
      groupid: selectedGroupId,
      selectUserids: users.map(function (it) {
        return it.userId;
      }),
      selectDepartmentIds: departments.map(function (it) {
        return it.id;
      }),
      selectGroupIds: groups.map(function (it) {
        return it.id;
      })
    }).then(function (res) {
      if (res.Code == 1) {
        setVisible(false);
        getPageData();
        getStaffDetail();
      }
    });
  }; // 移除员工


  var onRemoveHandler = function onRemoveHandler() {
    if (rowSelected.length > 0) {
      var selectIds = [];
      rowSelected.map(function (item, i) {
        selectIds.push(users[item].id);
      });
      (0, _groupInterface.removeGroupStaff)(selectIds, selectedGroupId).then(function (res) {
        if (res.Code == 1) {
          getPageData();

          _toast["default"].Success("移出成功");

          setRowSelected([]);
        } else {
          _toast["default"].Error(res.Message);
        }
      });
    } else {
      _toast["default"].Error("请至少选择一名用户");
    }
  };

  var onDialogCloseHandler = function onDialogCloseHandler() {
    setVisible(false);
  };

  var onAddStaffHandler = function onAddStaffHandler() {
    setVisible(true);
  };

  var fifterAddTitle = function fifterAddTitle(data) {
    if (data && data.length > 0) {
      data.map(function (item, i) {
        data[i].title = item.name;
        data[i].key = item.id;
      });
    }

    return data;
  };

  if (loading) return /*#__PURE__*/_react["default"].createElement("span", null);
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])(preCls)
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])("".concat(preCls, "__nav"))
  }, /*#__PURE__*/_react["default"].createElement(_tree["default"], {
    selectedKeys: selectedKeys,
    onSelect: onTreeSelectHandler,
    treeData: fifterAddTitle(groups),
    getGroup: getGroup,
    setSelectedGroupId: setSelectedGroupId
  })), /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])("".concat(preCls, "__body"))
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])("".concat(preCls, "__body-header"))
  }, /*#__PURE__*/_react["default"].createElement("span", {
    className: (0, _classnames["default"])("".concat(preCls, "__body-header-title"))
  }, selectedGroup && selectedGroup.name), /*#__PURE__*/_react["default"].createElement("span", {
    className: (0, _classnames["default"])("".concat(preCls, "__body-header-btns"))
  }, /*#__PURE__*/_react["default"].createElement("span", {
    className: (0, _classnames["default"])("".concat(preCls, "__btn ").concat(preCls, "__btn--gray")),
    onClick: onRemoveHandler
  }, "\u79FB\u51FA"), /*#__PURE__*/_react["default"].createElement("span", {
    className: (0, _classnames["default"])("".concat(preCls, "__btn ").concat(preCls, "__btn--gray")),
    onClick: onImportModalHandler
  }, "\u6279\u91CF\u5BFC\u5165"), /*#__PURE__*/_react["default"].createElement(_userSelector["default"], {
    multiple: true,
    sourceFrom: "fromback",
    selectNodes: {},
    onOk: onAddGroupHandler,
    position: "bottomRight",
    className: (0, _classnames["default"])("".concat(preCls, "__btn ").concat(preCls, "__btn--blue")),
    trigger: /*#__PURE__*/_react["default"].createElement("span", {
      className: (0, _classnames["default"])("".concat(preCls, "__btn ").concat(preCls, "__btn--blue"))
    }, "\u6DFB\u52A0\u6210\u5458")
  }))), /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])("".concat(preCls, "__body-content"))
  }, staffLoading ? /*#__PURE__*/_react["default"].createElement(_loading["default"], null) : !groups || !groups.length ? /*#__PURE__*/_react["default"].createElement(_groupEmpty["default"], {
    getGroup: getGroup
  }) : !users || !users.length ? /*#__PURE__*/_react["default"].createElement(_empty["default"], {
    selectedGroupId: selectedGroupId,
    getPageData: getPageData
  }) : /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])("".concat(preCls, "__body-content-header"))
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])("".concat(preCls, "__body-content-header-detail"))
  }, (details.departments.length > 0 || details.groups.length > 0) && /*#__PURE__*/_react["default"].createElement("span", {
    className: (0, _classnames["default"])("".concat(preCls, "__body-content-header-detail-item"))
  }, "\u6240\u542B\u90E8\u95E8/\u7FA4\u7EC4\uFF1A"), details.departments.map(function (item, key) {
    return /*#__PURE__*/_react["default"].createElement("span", {
      className: (0, _classnames["default"])("".concat(preCls, "__body-content-header-detail-item")),
      key: item.id
    }, /*#__PURE__*/_react["default"].createElement("img", {
      src: _department["default"]
    }), " ", /*#__PURE__*/_react["default"].createElement("span", null, item.name));
  }), details.groups.map(function (item, key) {
    return /*#__PURE__*/_react["default"].createElement("span", {
      className: (0, _classnames["default"])("".concat(preCls, "__body-content-header-detail-item")),
      key: item.id
    }, /*#__PURE__*/_react["default"].createElement("img", {
      src: _group["default"]
    }), " ", /*#__PURE__*/_react["default"].createElement("span", null, item.name));
  })), /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])("".concat(preCls, "__body-content-search"))
  }, /*#__PURE__*/_react["default"].createElement(_searchInput["default"], {
    placeholder: "\u59D3\u540D\u3001\u624B\u673A\u53F7\u3001\u90AE\u7BB1",
    onSearch: onSearchHandler,
    setSearchKey: setSearchKey
  }))), /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])("".concat(preCls, "__body-content-table"))
  }, !users || !users.length ? /*#__PURE__*/_react["default"].createElement(EmptyPage, {
    getPageData: getPageData
  }) : /*#__PURE__*/_react["default"].createElement(_table["default"], {
    tableData: users,
    total: total,
    pageNum: pageNum,
    rowSelected: rowSelected,
    setRowSelected: setRowSelected,
    setPageNum: setPageNum
  }))))), /*#__PURE__*/_react["default"].createElement(_index["default"], {
    visible: importVisible,
    setVisible: setImportVisible
  }));
}