"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"), _typeof = require("@babel/runtime/helpers/typeof");

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

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")), _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")), _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized")), _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits")), _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn")), _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf")), _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")), _react = _interopRequireWildcard(require("react")), _propTypes = _interopRequireDefault(require("prop-types")), _amosReactRouter = require("amos-react-router"), _amosFramework = require("amos-framework"), endConf = _interopRequireWildcard(require("amos-processor/lib/config/endconf")), _cacheUtils = require("./../../utils/cacheUtils"), _navApi = require("./../../services/navApi"), _securityService = require("./../../services/securityService"), _storageConsts = require("../../consts/storageConsts"), _amosTool = require("amos-tool"), _consts = _interopRequireDefault(require("amos-processor/lib/config/consts"));

function _getRequireWildcardCache(e) {
  if ("function" != typeof WeakMap) return null;
  var t = new WeakMap, r = new WeakMap;
  return (_getRequireWildcardCache = function(e) {
    return e ? r : t;
  })(e);
}

function _interopRequireWildcard(e, t) {
  if (!t && e && e.__esModule) return e;
  if (null === e || "object" !== _typeof(e) && "function" != typeof e) return {
    default: e
  };
  var r = _getRequireWildcardCache(t);
  if (r && r.has(e)) return r.get(e);
  var n = {}, a = Object.defineProperty && Object.getOwnPropertyDescriptor;
  for (var o in e) if ("default" !== o && Object.prototype.hasOwnProperty.call(e, o)) {
    var i = a ? Object.getOwnPropertyDescriptor(e, o) : null;
    i && (i.get || i.set) ? Object.defineProperty(n, o, i) : n[o] = e[o];
  }
  return n.default = e, r && r.set(e, n), n;
}

function _createSuper(e) {
  var t = _isNativeReflectConstruct();
  return function() {
    var r, n = (0, _getPrototypeOf2.default)(e);
    if (t) {
      var a = (0, _getPrototypeOf2.default)(this).constructor;
      r = Reflect.construct(n, arguments, a);
    } else r = n.apply(this, arguments);
    return (0, _possibleConstructorReturn2.default)(this, r);
  };
}

function _isNativeReflectConstruct() {
  if ("undefined" == typeof Reflect || !Reflect.construct) return !1;
  if (Reflect.construct.sham) return !1;
  if ("function" == typeof Proxy) return !0;
  try {
    return Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], (function() {}))), 
    !0;
  } catch (e) {
    return !1;
  }
}

var lsTool = _amosTool.Store.lsTool, AmosConfig = endConf.AmosConfig, isAutoOpen = AmosConfig.sysConf.isAutoOpenBussiness, FormItem = _amosFramework.Form.Item, Option = _amosFramework.Select.Option, RegionSelect = function(e) {
  (0, _inherits2.default)(r, e);
  var t = _createSuper(r);
  function r(e) {
    var n;
    return (0, _classCallCheck2.default)(this, r), n = t.call(this, e), (0, _defineProperty2.default)((0, 
    _assertThisInitialized2.default)(n), "onClick", (function() {
      var e = n.state.reginParams;
      lsTool.remove("selectedRole"), (0, _navApi.regionSelectAction)(e).then((function(t) {
        var r = n.props.location.state, a = (e.role || {}).sequenceNbr;
        lsTool.write("selectedRole", a), r ? (_amosReactRouter.browserHistory.push("/main"), 
        lsTool.write(_storageConsts.CONSTS.region_change, {
          refresh: {
            refresh: !0
          }
        })) : _amosReactRouter.browserHistory.push("/main");
      }));
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(n), "onSelectChange", (function(e, t) {
      var r = n.state.userInfo, a = r.companys, o = void 0 === a ? [] : a, i = r.companyDepartments, s = void 0 === i ? {} : i, l = r.orgRoles, u = void 0 === l ? {} : l, c = n.state.reginParams, f = n.state, m = f.departments, p = void 0 === m ? [] : m, d = f.roles, _ = void 0 === d ? [] : d;
      "company" === e && (c.company = o.find((function(e) {
        return e.sequenceNbr === t;
      })), p = s[t], _ = u[t]), "department" === e && (c.department = p.find((function(e) {
        return e.sequenceNbr === t;
      })), _ = u[t]), "role" === e && (c.role = _.find((function(e) {
        return e.sequenceNbr === t;
      }))), n.setState({
        reginParams: c,
        departments: p,
        roles: _
      });
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(n), "getCurrentUser", (function() {
      (0, _securityService.getCurrentUser)().then((function(e) {
        var t = n.state.reginParams, r = e.companys, a = void 0 === r ? [] : r, o = e.companyDepartments, i = void 0 === o ? {} : o, s = e.orgRoles, l = void 0 === s ? {} : s;
        if (lsTool.write(_consts.default.userId, e.userId), lsTool.write(_consts.default.company, JSON.stringify(a)), 
        lsTool.write(_consts.default.userName, e.userName), lsTool.write(_consts.default.nickName, e.realName), 
        1 === a.length) {
          var u = a[0], c = i[u.sequenceNbr] || [];
          1 === c.length && 1 === (l[u.sequenceNbr] || l[c[0].sequenceNbr] || []).length ? (t.company = u, 
          t.department = c[0], t.role = l[u.sequenceNbr] ? l[u.sequenceNbr][0] : l[c[0].sequenceNbr] ? l[c[0].sequenceNbr][0] : {}, 
          n.setState({
            reginParams: t
          }, (function() {
            return n.onClick();
          }))) : 0 === c.length && 1 === (l[u.sequenceNbr] || []).length ? (t.company = u, 
          t.department = {}, t.role = l[u.sequenceNbr][0], n.setState({
            reginParams: t
          }, (function() {
            return n.onClick();
          }))) : n.setState({
            userInfo: e
          });
        } else n.setState({
          userInfo: e
        });
      }));
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(n), "renderItem", (function(e) {
      var t = e.userId, r = e.companys, a = void 0 === r ? [] : r, o = n.state, i = o.departments, s = void 0 === i ? [] : i, l = o.roles, u = void 0 === l ? [] : l, c = s.filter((function(e) {
        return null != e && void 0 !== e;
      })), f = {
        wrapperCol: {
          xs: {
            span: 24
          },
          sm: {
            span: 24
          },
          className: "colspan"
        }
      };
      return _react.default.createElement("div", {
        key: t,
        className: "region-item"
      }, _react.default.createElement(_amosFramework.Form, {
        className: "region-form"
      }, _react.default.createElement(FormItem, f, _react.default.createElement(_amosFramework.Select, {
        className: "form-item",
        data: a,
        renderOption: function(e) {
          return _react.default.createElement(Option, {
            value: e.sequenceNbr
          }, e.companyName);
        },
        defaultOption: _react.default.createElement(Option, null, "请选择公司"),
        onChange: function(e, t) {
          return n.onSelectChange("company", e);
        }
      })), _react.default.createElement(FormItem, f, _react.default.createElement(_amosFramework.Select, {
        className: "form-item",
        data: c,
        renderOption: function(e) {
          return _react.default.createElement(Option, {
            value: e.sequenceNbr
          }, e.departmentName);
        },
        defaultOption: _react.default.createElement(Option, null, "请选择部门"),
        onChange: function(e, t) {
          return n.onSelectChange("department", e);
        }
      })), _react.default.createElement(FormItem, f, _react.default.createElement(_amosFramework.Select, {
        className: "form-item",
        data: u,
        renderOption: function(e) {
          return _react.default.createElement(Option, {
            value: e.sequenceNbr
          }, e.roleName);
        },
        defaultOption: _react.default.createElement(Option, null, "请选择角色"),
        onChange: function(e, t) {
          return n.onSelectChange("role", e);
        }
      })), _react.default.createElement(FormItem, f, _react.default.createElement("img", {
        src: "",
        alt: ""
      })), _react.default.createElement(FormItem, null, _react.default.createElement(_amosFramework.Button, {
        className: "regoin-button",
        onClick: n.onClick
      }, "确定"))));
    })), n.state = {
      userInfo: {},
      reginParams: {}
    }, n;
  }
  return (0, _createClass2.default)(r, [ {
    key: "componentDidMount",
    value: function() {
      this.getCurrentUser();
    }
  }, {
    key: "render",
    value: function() {
      var e = this.state.userInfo;
      return Object.keys(e).length > 0 ? _react.default.createElement("div", {
        className: "region-select"
      }, this.renderItem(e)) : null;
    }
  } ]), r;
}(_react.Component);

RegionSelect.displayName = "RegionSelect", RegionSelect.propTypes = {
  location: _propTypes.default.object
};

var _default = RegionSelect;

exports.default = _default;