"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 _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")), _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")), _amosAntd = require("amos-antd"), _dt2react = require("dt2react"), _amosTool = require("amos-tool"), _processor = require("../../../utils/processor"), _urlConsts = _interopRequireDefault(require("../../../consts/urlConsts")), _ContextMenu = _interopRequireDefault(require("./ContextMenu")), _UploadPicsModal = _interopRequireDefault(require("./uploadPicture/UploadPicsModal"));

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 o = {}, n = Object.defineProperty && Object.getOwnPropertyDescriptor;
  for (var a in e) if ("default" !== a && Object.prototype.hasOwnProperty.call(e, a)) {
    var i = n ? Object.getOwnPropertyDescriptor(e, a) : null;
    i && (i.get || i.set) ? Object.defineProperty(o, a, i) : o[a] = e[a];
  }
  return o.default = e, r && r.set(e, o), o;
}

function _createSuper(e) {
  var t = _isNativeReflectConstruct();
  return function() {
    var r, o = (0, _getPrototypeOf2.default)(e);
    if (t) {
      var n = (0, _getPrototypeOf2.default)(this).constructor;
      r = Reflect.construct(o, arguments, n);
    } else r = o.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;
  }
}

require("./topo.scss");

var lsTool = _amosTool.Store.lsTool, TreeNode = _amosAntd.Tree.TreeNode, loop = function e(t, r, o) {
  t.forEach((function(t, n, a) {
    return t.orgCode === r ? o(t, n, a) : t.children ? e(t.children, r, o) : void 0;
  }));
}, TopoTree = function(e) {
  (0, _inherits2.default)(r, e);
  var t = _createSuper(r);
  function r(e) {
    var o;
    return (0, _classCallCheck2.default)(this, r), o = t.call(this, e), (0, _defineProperty2.default)((0, 
    _assertThisInitialized2.default)(o), "onExpand", (function(e) {
      o.setState({
        expandedKeys: e,
        autoExpandParent: !1
      });
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(o), "onCheck", (function(e) {
      console.log("onCheck", e), o.setState({
        checkedKeys: e
      });
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(o), "onSelect", (function(e, t) {
      var r = t.node.props.orgCode ? t.node.props.orgCode : t.node.props.dataRef.orgCode, n = t.node.props.title ? t.node.props.title : t.node.props.dataRef.title, a = t.node.props.eventKey ? t.node.props.eventKey : t.node.props.dataRef.eventKey, i = {
        parentKey: r,
        title: n,
        key: t.node.props.eventKey
      };
      void 0 !== a && (0, _extends2.default)(i, {
        id: a
      }), o.setState({
        selectedKeys: e,
        currentNode: i
      }), (0, o.props.setCurrentSubviewId)(a, n, i, e, o.state.expandedKeys);
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(o), "onArtificialSelect", (function(e, t) {
      var r = t.title, n = t.sourceId, a = t.key, i = {
        parentKey: t.parentKey,
        title: r,
        key: a
      };
      void 0 !== n && (0, _extends2.default)(i, {
        id: n
      }), o.setState({
        selectedKeys: e,
        currentNode: i
      }), (0, o.props.setCurrentSubviewId)(n, r, i, e, o.state.expandedKeys);
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(o), "getOffset", (function() {
      var e = (0, _dt2react.getOffset)(o.treeRoot) || {};
      o.setState({
        treeLocation: e
      });
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(o), "getTreeData", (function() {
      var e = o.props.orgCode, t = o.props.orgName, r = o.props.expandedKeys;
      _processor.AmosFetch.get(_urlConsts.default.fetchSubgraphInfos).then((function(e) {
        if (e.ok) return e.json();
        throw new Error("从服务端获取数据失败！");
      })).then((function(n) {
        if ("SUCCESS" === n.result) {
          var a = n.dataList;
          if (a.length > 0) {
            var i = a[0].orgCode;
            null != o.props.orgCode && (i = o.props.orgCode);
            var s = [];
            s.push(i), loop(a, i, (function(r) {
              var n = r.orgCode, a = r.companyName;
              null != o.props.orgCode && (n = e, a = t);
              var i = {
                title: a,
                sourceId: n,
                key: n
              };
              o.onArtificialSelect(s, i);
            }));
          }
          r && r.length > 0 ? o.setState({
            currentTreeData: a,
            expandedKeys: o.props.expandedKeys,
            autoExpandParent: !1
          }) : o.setState({
            currentTreeData: a,
            expandedKeys: a[0].orgCode,
            autoExpandParent: !1
          });
        }
      }));
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(o), "nodeRightClick", (function(e) {
      o.contextMenuRef.showMenu(e.event, e.node.props.eventKey);
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(o), "renderTreeNodes", (function(e) {
      return e.map((function(e) {
        return e.children ? _react.default.createElement(TreeNode, {
          title: e.companyName,
          key: e.orgCode,
          dataRef: e
        }, o.renderTreeNodes(e.children)) : _react.default.createElement(TreeNode, (0, _extends2.default)({
          title: e.companyName,
          key: e.orgCode
        }, e));
      }));
    })), o.state = {
      expandedKeys: [],
      autoExpandParent: !0,
      checkedKeys: [],
      selectedKeys: [],
      treeLocation: {},
      currentTreeData: [],
      currentNode: {}
    }, o;
  }
  return (0, _createClass2.default)(r, [ {
    key: "componentDidMount",
    value: function() {
      this.getOffset(), this.getTreeData();
    }
  }, {
    key: "render",
    value: function() {
      var e = this, t = this.state, r = t.treeLocation, o = t.selectedKeys, n = t.currentTreeData, a = t.expandedKeys, i = t.currentNode, s = t.defaultSelectedKeys;
      return _react.default.createElement("div", {
        className: "topo-tree",
        ref: function(t) {
          return e.treeRoot = t;
        }
      }, _react.default.createElement("div", {
        className: "topo-tree-content"
      }, _react.default.createElement(_amosAntd.Tree, {
        showLine: !0,
        onExpand: this.onExpand,
        expandedKeys: a,
        defaultSelectedKeys: s,
        autoExpandParent: this.state.autoExpandParent,
        onCheck: this.onCheck,
        checkedKeys: this.state.checkedKeys,
        onSelect: this.onSelect,
        selectedKeys: this.state.selectedKeys
      }, this.renderTreeNodes(n)), _react.default.createElement(_ContextMenu.default, {
        treeLocation: r,
        selectedKeys: o,
        addTreeData: this.addTreeData,
        onDelClick: this.delView,
        currentNode: i,
        ref: function(t) {
          return e.contextMenuRef = t;
        }
      })));
    }
  } ]), r;
}(_react.Component);

TopoTree.displayName = "TopoTree", TopoTree.propTypes = {
  setCurrentSubviewId: _propTypes.default.func,
  setCurrentName: _propTypes.default.func,
  orgCode: _propTypes.default.any,
  orgName: _propTypes.default.any,
  expandedKeys: _propTypes.default.any
};

var _default = TopoTree;

exports.default = _default;