"use strict";
Object.defineProperty(exports, "__esModule", {
  value: !0
}), exports.default = void 0;
var _react = _interopRequireWildcard(require("react")),
  _propTypes = _interopRequireDefault(require("prop-types")),
  _amosAntd = require("amos-antd"),
  _amosIcon = _interopRequireDefault(require("amos-icon")),
  _dt2react = _interopRequireDefault(require("dt2react")),
  _processor = require("../../../../utils/processor"),
  _urlConsts = _interopRequireDefault(require("../../../../consts/urlConsts")),
  _amosFramework = require("amos-framework"),
  _TreeHeader = _interopRequireDefault(require("./../header/TreeHeader")),
  _AddView = _interopRequireDefault(require("./AddView")),
  _ContextMenu = _interopRequireDefault(require("./ContextMenu")),
  _userConfig = require("./../../../../utils/userConfig");

function _interopRequireDefault(e) {
  return e && e.__esModule ? e : {
    default: e
  }
}

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

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

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

function _extends() {
  return (_extends = Object.assign || function(e) {
    for (var t = 1; t < arguments.length; t++) {
      var r, n = arguments[t];
      for (r in n) Object.prototype.hasOwnProperty.call(n, r) && (e[r] = n[r])
    }
    return e
  }).apply(this, arguments)
}

function _classCallCheck(e, t) {
  if (!(e instanceof t)) throw new TypeError("Cannot call a class as a function")
}

function _defineProperties(e, t) {
  for (var r = 0; r < t.length; r++) {
    var n = t[r];
    n.enumerable = n.enumerable || !1, n.configurable = !0, "value" in n && (n.writable = !0), Object.defineProperty(e, n.key, n)
  }
}

function _createClass(e, t, r) {
  return t && _defineProperties(e.prototype, t), r && _defineProperties(e, r), e
}

function _inherits(e, t) {
  if ("function" != typeof t && null !== t) throw new TypeError("Super expression must either be null or a function");
  e.prototype = Object.create(t && t.prototype, {
    constructor: {
      value: e,
      writable: !0,
      configurable: !0
    }
  }), t && _setPrototypeOf(e, t)
}

function _setPrototypeOf(e, t) {
  return (_setPrototypeOf = Object.setPrototypeOf || function(e, t) {
    return e.__proto__ = t, e
  })(e, t)
}

function _createSuper(r) {
  var n = _isNativeReflectConstruct();
  return function() {
    var e, t = _getPrototypeOf(r);
    return _possibleConstructorReturn(this, n ? (e = _getPrototypeOf(this).constructor, Reflect.construct(t, arguments, e)) : t.apply(this, arguments))
  }
}

function _possibleConstructorReturn(e, t) {
  return !t || "object" !== _typeof(t) && "function" != typeof t ? _assertThisInitialized(e) : t
}

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

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

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

function _defineProperty(e, t, r) {
  return t in e ? Object.defineProperty(e, t, {
    value: r,
    enumerable: !0,
    configurable: !0,
    writable: !0
  }) : e[t] = r, e
}
var TreeNode = _amosAntd.Tree.TreeNode,
  loop = function n(e, a, o) {
    e.forEach(function(e, t, r) {
      return e.key === a ? o(e, t, r) : e.children ? n(e.children, a, o) : void 0
    })
  },
  rootNode = {
    title: "根目录",
    key: "0",
    parentKey: "-1",
    children: [],
    sourceId: 0
  },
  TopoTree = function() {
    _inherits(r, _react.Component);
    var t = _createSuper(r);

    function r(e) {
      var o;
      return _classCallCheck(this, r), _defineProperty(_assertThisInitialized(o = t.call(this, e)), "onExpand", function(e) {
        o.setState({
          expandedKeys: e,
          autoExpandParent: !1
        })
      }), _defineProperty(_assertThisInitialized(o), "onCheck", function(e) {
        console.log("onCheck", e), o.setState({
          checkedKeys: e
        })
      }), _defineProperty(_assertThisInitialized(o), "onSelect", function(e, t) {
        var r = (null === t.node.props.children ? t.node.props : t.node.props.dataRef).parentKey,
          n = (null === t.node.props.children ? t.node.props : t.node.props.dataRef).title,
          a = (null === t.node.props.children ? t.node.props : t.node.props.dataRef).sourceId,
          t = {
            parentKey: r,
            title: n,
            key: t.node.props.eventKey
          };
        void 0 !== a && _extends(t, {
          id: a
        }), o.setState({
          selectedKeys: e,
          currentNode: t
        }), (0, o.props.setCurrentSubviewId)(a, n, t, e, o.editTreeData)
      }), _defineProperty(_assertThisInitialized(o), "onArtificialSelect", function(e, t) {
        var r = t.parentKey,
          n = t.title,
          a = t.sourceId,
          t = {
            parentKey: r,
            title: n,
            key: t.key
          };
        void 0 !== a && _extends(t, {
          id: a
        }), o.setState({
          selectedKeys: e,
          currentNode: t
        }), (0, o.props.setCurrentSubviewId)(a, n, t, e, o.editTreeData)
      }), _defineProperty(_assertThisInitialized(o), "getOffset", function() {
        var e = _dt2react.default.getOffset(o.treeRoot) || {};
        o.setState({
          treeLocation: e
        })
      }), _defineProperty(_assertThisInitialized(o), "getTreeData", function() {
        (0, _userConfig.currentUser)().userId;
        o.setState({
          currentTreeData: [{
            sourceId: 0,
            key: "0",
            parentKey: "-1",
            title: "昆山宁华",
            children: [{
              sourceId: 1,
              key: "0-0",
              parentKey: "0",
              title: "昆山A区",
              value: "昆山A区"
            }, {
              sourceId: 2,
              key: "0-1",
              parentKey: "0",
              title: "昆山B区",
              children: [{
                sourceId: 3,
                key: "0-1-0",
                parentKey: "0-1",
                title: "一楼",
                value: "一楼"
              }, {
                sourceId: 4,
                key: "0-1-1",
                parentKey: "0-1",
                title: "二楼",
                value: "二楼"
              }],
              value: "昆山B区"
            }],
            value: "昆山宁华"
          }, {
            sourceId: 11,
            key: "1",
            parentKey: "-1",
            title: "信达可恩",
            children: [],
            value: "信达可恩"
          }, {
            sourceId: 11,
            key: "2",
            parentKey: "-1",
            title: "中安消防",
            children: [],
            value: "中安消防"
          }, {
            sourceId: 11,
            key: "3",
            parentKey: "-1",
            title: "鼎信消防",
            children: [],
            value: "鼎信消防"
          }, {
            sourceId: 11,
            key: "4",
            parentKey: "-1",
            title: "坚锐消防",
            children: [],
            value: "坚锐消防"
          }]
        })
      }), _defineProperty(_assertThisInitialized(o), "nodeRightClick", function(e) {
        o.refs.contextMenu.showMenu(e.event, e.node.props.eventKey)
      }), _defineProperty(_assertThisInitialized(o), "toggleView", function() {
        var e = 0 < arguments.length && void 0 !== arguments[0] && arguments[0];
        o.setState({
          viewVisable: e
        })
      }), _defineProperty(_assertThisInitialized(o), "addView", function() {
        1 === o.state.selectedKeys.length ? o.setState({
          viewVisable: !0
        }) : _amosFramework.message.danger("新选择父节点！")
      }), _defineProperty(_assertThisInitialized(o), "delView", function(e) {
        var t, r = o.state,
          n = r.currentNode,
          r = r.selectedKeys,
          a = n.parentKey;
        "-1" !== a ? (t = o.delTreeData, 1 === r.length ? _amosAntd.Modal.confirm({
          title: "批量删除",
          content: "是否删除所选信息？",
          okText: "确定",
          cancelText: "取消",
          onOk: function() {
            t(a, n.key)
          },
          onCancel: function() {}
        }) : _amosFramework.message.danger("新选择要删除的节点节点！")) : _amosAntd.Modal.info({
          title: "系统提示",
          content: "不能删除根节点",
          okText: "确定"
        })
      }), _defineProperty(_assertThisInitialized(o), "addTreeData", function(r) {
        var e = o.state,
          t = e.currentTreeData,
          n = e.expandedKeys,
          a = e.currentNode.key;
        loop(t, a, function(e) {
          e.children = e.children || [];
          var t = o.generateKey(e.children, a),
            e = {
              createTime: (new Date).getTime(),
              name: r,
              currentKey: t,
              parentKey: a,
              userId: (0, _userConfig.currentUser)().userId
            };
          o.saveSubgraphData(e), -1 === n.indexOf(t) && (n.push(a), o.setState({
            expandedKeys: n
          }))
        })
      }), _defineProperty(_assertThisInitialized(o), "editTreeData", function(r) {
        var e = o.state,
          t = e.currentTreeData,
          n = e.currentNode;
        "-1" !== n.parentKey ? (e = n.key, loop(t, e, function(e) {
          e.title = r;
          var t = (new Date).getTime(),
            e = {
              id: n.id,
              createTime: t,
              name: r,
              currentKey: e.key,
              parentKey: e.parentKey,
              userId: (0, _userConfig.currentUser)().userId
            };
          o.saveSubgraphData(e)
        })) : _amosAntd.Modal.info({
          title: "系统提示",
          content: "不能编辑根节点",
          okText: "确定"
        })
      }), _defineProperty(_assertThisInitialized(o), "saveSubgraphData", function(r) {
        _processor.AmosFetch.post(_urlConsts.default.addSubgraph, {
          data: r
        }).then(function(e) {
          if (e.ok) return e.text();
          throw new Error("从服务端获取数据失败！")
        }).then(function(e) {
          var t = o.props.setCurrentName,
            e = JSON.parse(e);
          "SUCCESS" === e.result ? (o.getTreeData(), t(r.name), _amosFramework.message.success("保存成功")) : _amosFramework.message.success("保存失败：".concat(e.message))
        })
      }), _defineProperty(_assertThisInitialized(o), "delTreeData", function(e, t) {
        "".concat(_urlConsts.default.delSubgraph, "/").concat(t);
        o.updateTreeData(e)
      }), _defineProperty(_assertThisInitialized(o), "updateTreeData", function(e) {
        (0, _userConfig.currentUser)().userId;
        o.setState({
          currentTreeData: [{
            sourceId: 0,
            key: "0",
            parentKey: "-1",
            title: "昆山宁华",
            children: [{
              sourceId: 1,
              key: "0-0",
              parentKey: "0",
              title: "昆山A区",
              value: "昆山A区"
            }, {
              sourceId: 2,
              key: "0-1",
              parentKey: "0",
              title: "昆山B区",
              children: [{
                sourceId: 3,
                key: "0-1-0",
                parentKey: "0-1",
                title: "一楼",
                value: "一楼"
              }, {
                sourceId: 4,
                key: "0-1-1",
                parentKey: "0-1",
                title: "二楼",
                value: "二楼"
              }],
              value: "昆山B区"
            }],
            value: "昆山宁华"
          }, {
            sourceId: 11,
            key: "1",
            parentKey: "-1",
            title: "信达可恩",
            children: [],
            value: "信达可恩"
          }, {
            sourceId: 11,
            key: "2",
            parentKey: "-1",
            title: "中安消防",
            children: [],
            value: "中安消防"
          }, {
            sourceId: 11,
            key: "3",
            parentKey: "-1",
            title: "鼎信消防",
            children: [],
            value: "鼎信消防"
          }, {
            sourceId: 11,
            key: "4",
            parentKey: "-1",
            title: "坚锐消防",
            children: [],
            value: "坚锐消防"
          }]
        })
      }), _defineProperty(_assertThisInitialized(o), "generateKey", function(e, t) {
        if (void 0 === e || 0 === e.length) return "".concat(t, "-0");
        var r = [];
        e.map(function(e) {
          e = e.key.split("-");
          r.push(parseInt(e[e.length - 1]))
        });
        e = Math.max.apply(null, r);
        return "".concat(t, "-").concat(e + 1)
      }), _defineProperty(_assertThisInitialized(o), "renderTreeNodes", function(e) {
        return e.map(function(e) {
          return e.children ? _react.default.createElement(TreeNode, {
            title: e.title,
            key: e.key,
            dataRef: e
          }, o.renderTreeNodes(e.children)) : _react.default.createElement(TreeNode, e)
        })
      }), _defineProperty(_assertThisInitialized(o), "renderTreeHeaderExtra", function() {
        return [_react.default.createElement("div", {
          key: "create-view",
          className: "tree-header-extra-item",
          onClick: o.addView,
          title: "创建视图"
        }, _react.default.createElement(_amosIcon.default, {
          icon: "luru"
        })), _react.default.createElement("div", {
          key: "del-view",
          className: "tree-header-extra-item",
          onClick: o.delView,
          title: "删除视图"
        }, _react.default.createElement(_amosIcon.default, {
          icon: "delete"
        }))]
      }), o.state = {
        expandedKeys: [],
        autoExpandParent: !0,
        checkedKeys: [],
        selectedKeys: [],
        treeLocation: {},
        currentTreeData: [],
        currentNode: {},
        viewVisable: !1
      }, o
    }
    return _createClass(r, [{
      key: "componentDidMount",
      value: function() {
        this.getOffset(), this.getTreeData()
      }
    }, {
      key: "render",
      value: function() {
        var t = this,
          e = this.state,
          r = e.treeLocation,
          n = e.viewVisable,
          a = e.selectedKeys,
          o = e.currentTreeData,
          i = e.expandedKeys,
          e = e.currentNode;
        return _react.default.createElement("div", {
          className: "topo-tree",
          ref: function(e) {
            return t.treeRoot = e
          }
        }, _react.default.createElement(_TreeHeader.default, {
          title: "物理拓扑图",
          extra: this.renderTreeHeaderExtra()
        }), _react.default.createElement("div", {
          className: "topo-tree-content"
        }, _react.default.createElement(_amosAntd.Tree, {
          showLine: !0,
          onExpand: this.onExpand,
          expandedKeys: i,
          autoExpandParent: this.state.autoExpandParent,
          onCheck: this.onCheck,
          checkedKeys: this.state.checkedKeys,
          onSelect: this.onSelect,
          selectedKeys: this.state.selectedKeys,
          onRightClick: this.nodeRightClick
        }, this.renderTreeNodes(o)), _react.default.createElement(_ContextMenu.default, {
          treeLocation: r,
          selectedKeys: a,
          addTreeData: this.addTreeData,
          onDelClick: this.delView,
          currentNode: e,
          editTreeData: this.editTreeData,
          ref: "contextMenu"
        })), _react.default.createElement(_AddView.default, {
          visible: n,
          selectedKeys: a,
          addTreeData: this.addTreeData,
          toggleView: this.toggleView
        }))
      }
    }]), r
  }();
TopoTree.displayName = "TopoTree", TopoTree.propTypes = {
  setCurrentSubviewId: _propTypes.default.func,
  setCurrentName: _propTypes.default.func
};
var _default = TopoTree;
exports.default = TopoTree, module.exports = exports.default;
