"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 _classnames = _interopRequireDefault(require("classnames"));

var _titaUi = require("tita-ui");

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

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

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

var _i18nFrontTranslate = require("../../../../utils/i18n-front-translate");

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

var _default = /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) {
  var data = props.data,
      edit = props.edit,
      setIsDisable = props.setIsDisable;

  var _useState = (0, _react.useState)(function () {
    if (!edit) return "";
    return data.name;
  }),
      _useState2 = _slicedToArray(_useState, 2),
      groupName = _useState2[0],
      setGroupName = _useState2[1];

  var _useState3 = (0, _react.useState)(""),
      _useState4 = _slicedToArray(_useState3, 2),
      groupNameErrMsg = _useState4[0],
      setGroupNameErrMsg = _useState4[1];

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

  var _useState7 = (0, _react.useState)({
    departments: [],
    users: [],
    groups: []
  }),
      _useState8 = _slicedToArray(_useState7, 2),
      selectedUsers = _useState8[0],
      setSelectedUsers = _useState8[1]; // 外部调用


  (0, _react.useImperativeHandle)(ref, function () {
    return {
      getData: function getData() {
        return {
          groupName: groupName,
          scopeChecked: scopeChecked,
          selectedUsers: selectedUsers
        };
      }
    };
  });
  (0, _react.useEffect)(function () {
    if (!edit) return;
    (0, _groupInterface.getVisible)(data.id).then(function (res) {
      // Data 返回值为 NUll 标识可见范围是 全部人员
      if (res.Data == null) {
        setScopeChecked(0);
        return;
      }

      var _res$Data = res.Data,
          departments = _res$Data.departments,
          users = _res$Data.users,
          groups = _res$Data.groups;
      setSelectedUsers({
        departments: departments,
        users: users,
        groups: groups
      });
      setScopeChecked(1);
    });
  }, []);

  var onGroupNameChange = function onGroupNameChange(value) {
    setGroupName(value);

    if (setIsDisable) {
      if (value && value.length < 51) {
        setIsDisable(false);
      } else {
        setIsDisable(true);
      }
    }

    if (value.length > 50) {
      setGroupNameErrMsg((0, _i18nFrontTranslate.inputWordTrans)(value.length, 50));
    } else {
      setGroupNameErrMsg("");
    }
  };

  var onGroupNameBlur = function onGroupNameBlur() {
    if (groupName.length > 50) {
      setGroupNameErrMsg((0, _i18nFrontTranslate.inputWordTrans)(groupName.length, 50));
    } else {
      setGroupNameErrMsg("");
    }
  };

  var DepartmentNameProps = {
    required: true,
    // 是否是必填
    value: groupName,
    type: "text",
    // 类型：text,password等
    placeholder: "请输入群组名称",
    errMsg: groupNameErrMsg,
    onChange: onGroupNameChange,
    onBlur: onGroupNameBlur,
    id: "groupName"
  };

  var onUserSelectorOkHandler = function onUserSelectorOkHandler(selected) {
    setSelectedUsers(selected);
  };

  var onRadioClickHandler = function onRadioClickHandler(id) {
    setScopeChecked(id);
  };

  return /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])("".concat(preCls, "__form"))
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])("".concat(preCls, "__form-title"))
  }, "\u7FA4\u7EC4\u540D\u79F0", /*#__PURE__*/_react["default"].createElement("span", null, "*")), /*#__PURE__*/_react["default"].createElement(_titaUi.Textarea, {
    errorDisplayType: "float-bottom",
    placeholder: "\u8BF7\u8F93\u5165\u7FA4\u7EC4\u540D\u79F0",
    maxLength: 50,
    manualWrap: false,
    value: groupName,
    onChange: function onChange(e) {
      return onGroupNameChange(e.target.value);
    },
    wrapperClass: "create-template-input"
  }), /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])("".concat(preCls, "__form-subtitle"))
  }, "\u53EF\u4F7F\u7528\u4EBA"), /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])("".concat(preCls, "__form-field ").concat(preCls, "__form-field-scope"))
  }, /*#__PURE__*/_react["default"].createElement("span", null, /*#__PURE__*/_react["default"].createElement(_titaUi.Radio, {
    id: "total",
    checked: scopeChecked == 0,
    onChange: function onChange() {
      return onRadioClickHandler(0);
    }
  }), /*#__PURE__*/_react["default"].createElement("label", {
    htmlFor: "total",
    className: "text"
  }, (0, _getLocale.getLocale)("Backst_Visibletoall"))), /*#__PURE__*/_react["default"].createElement("span", null, /*#__PURE__*/_react["default"].createElement(_titaUi.Radio, {
    id: "scope",
    checked: scopeChecked == 1,
    onChange: function onChange() {
      return onRadioClickHandler(1);
    }
  }), /*#__PURE__*/_react["default"].createElement("label", {
    htmlFor: "scope",
    className: "text"
  }, (0, _getLocale.getLocale)("Per_Ass_New_Appr_detail_Design"))), scopeChecked == 1 && /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])("".concat(preCls, "__form-field-seletor"))
  }, /*#__PURE__*/_react["default"].createElement(_userSelector["default"], {
    multiple: true,
    selectNodes: selectedUsers,
    position: "center",
    onOk: onUserSelectorOkHandler,
    placeholder: "\u8BF7\u9009\u62E9\u6307\u5B9A\u4EBA\u5458",
    sourceFrom: "fromback"
  }))));
});

exports["default"] = _default;