"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")), _amosFramework = require("amos-framework"), _StatusTextArea = _interopRequireDefault(require("./StatusTextArea")), _StatusTransformTabs = _interopRequireDefault(require("./StatusTransformTabs")), _services = require("./../../services");

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

function _createSuper(e) {
  var t = _isNativeReflectConstruct();
  return function() {
    var r, a = (0, _getPrototypeOf2.default)(e);
    if (t) {
      var n = (0, _getPrototypeOf2.default)(this).constructor;
      r = Reflect.construct(a, arguments, n);
    } else r = a.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 attributeIndex = 1, conditionIndex = 2, resultIndex = 3, StatusTransformContent = function(e) {
  (0, _inherits2.default)(r, e);
  var t = _createSuper(r);
  function r(e) {
    var a;
    return (0, _classCallCheck2.default)(this, r), a = t.call(this, e), (0, _defineProperty2.default)((0, 
    _assertThisInitialized2.default)(a), "onInitData", (function(e) {
      var t = e.objId, r = e.linkId;
      t && (0, _services.queryRuleSubObj)(t).then((function(e) {
        a.setState({
          object: e || {}
        });
      })), (0, _services.queryStatusRuleAction)(r).then((function(e) {
        if (e) {
          var t = e.pseudoCode, r = a.state, n = r.rules, i = r.current, u = r.currentRulesIndex;
          n = [], t.conditionList.map((function(e, t) {
            e.map((function(e, r) {
              var a = 0 === r ? "or" : "with";
              n.push({
                attributeId: e.propId,
                attribute: e.property,
                attributeName: e.propName,
                conditionId: e.relation,
                conditionName: e.relation,
                bizClassName: e.bizClassName,
                relationType: e.relationType,
                propType: e.propType,
                fact: e.fact,
                result: e.value,
                orWidth: 0 === t && 0 === r ? void 0 : a
              });
            }));
          })), u = n.length - 1, i = resultIndex, a.setState({
            rules: n,
            current: i,
            currentRulesIndex: u,
            statusTrans: e
          });
        } else a.setState({
          current: 0,
          rules: [],
          currentRulesIndex: 0,
          active: "",
          logical: "",
          statusTrans: {}
        });
      })), t && (0, _services.queryAttributeAction)(t).then((function(e) {
        a.setState({
          attributeList: e || []
        });
      })), (0, _services.queryConditioneAction)().then((function(e) {
        a.setState({
          conditionList: e || []
        });
      }));
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(a), "onAttributeClick", (function(e) {
      var t = a.state, r = t.rules, n = t.current, i = t.logical, u = t.currentRulesIndex;
      if (0 === n) {
        var s = {
          id: Date.now()
        };
        s.attributeId = e.id, s.attribute = e.name, s.attributeName = e.displayName, s.bizClassName = e.bizclassName, 
        s.fact = e.factId, s.propType = e.dataType, s.orWidth = 0 === u || i, r.push(s), 
        a.setState({
          rules: r,
          current: attributeIndex,
          currentRulesIndex: r.length - 1
        });
      } else r[u].attributeId = e.id, r[u].attribute = e.name, r[u].attributeName = e.displayName, 
      r[u].bizClassName = e.bizclassName, r[u].fact = e.factId, r[u].propType = e.dataType, 
      a.setState({
        rules: r
      });
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(a), "onConditionClick", (function(e) {
      var t = a.state, r = t.rules, n = t.current, i = t.currentRulesIndex;
      n <= 1 && a.setState({
        current: conditionIndex
      }), r[i].conditionId = e.name, r[i].conditionName = e.displayName, r[i].relationType = e.type, 
      a.setState({
        rules: r
      });
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(a), "onResultChange", (function(e) {
      var t = a.state, r = t.rules, n = t.current, i = t.currentRulesIndex;
      n <= conditionIndex && a.setState({
        current: resultIndex
      }), r[i].result = e.target.value, a.setState({
        rules: r
      });
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(a), "onLogicalChange", (function(e) {
      var t = a.state.rules;
      a.setState({
        current: 0,
        logical: e,
        currentRulesIndex: t.length
      });
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(a), "onReset", (function() {
      a.setState({
        current: 0,
        rules: [],
        currentRulesIndex: 0,
        logical: ""
      });
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(a), "onFinish", (function() {
      var e = a.state, t = e.rules, r = e.current, n = e.isLoading, i = e.statusTrans, u = a.props, s = u.objId, o = u.targetStatus, l = u.linkId;
      if (t.length > 0 && r < 2) _amosFramework.message.danger("信息填写未完整"); else if (0 !== t.length) {
        if (!n) {
          var c = [];
          if (t.length > 0) {
            var d = [];
            t.map((function(e) {
              "or" === e.orWidth && (c.push(d), d = []), d.push({
                fact: e.fact,
                propId: e.attributeId,
                property: e.attribute,
                propName: e.attributeName,
                relation: e.conditionId,
                value: e.result || "",
                bizClassName: e.bizClassName,
                relationType: e.relationType,
                propType: e.propType
              });
            })), d.length > 0 && c.push(d);
          }
          var p = {
            elementId: l,
            groupId: i.groupId,
            id: i.id,
            oid: s,
            stateId: o.id,
            pseudoCode: JSON.stringify({
              conditionList: c
            })
          };
          a.setState({
            isLoading: !0
          }), (i.id ? _services.updateStatusObjectAction : _services.addStatusObjectAction)(p).then((function(e) {
            a.onReset(), a.props.closeDesigner(), a.props.toggleReloadStage(!0), a.setState({
              isLoading: !1
            });
          }), (function(e) {
            a.setState({
              isLoading: !1
            }), _amosFramework.message.danger(e || "保存失败");
          }));
        }
      } else _amosFramework.message.danger("必须填写条件");
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(a), "onRuleRemove", (function(e, t) {
      var r = a.state.rules.filter((function(r, a) {
        return e > a || a >= e + t.length;
      }));
      r.length > 0 && (r[0].orWidth = ""), a.setState({
        rules: r,
        current: r.length > 0 ? resultIndex : 0,
        currentRulesIndex: r.length > 0 ? r.length - 1 : 0
      });
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(a), "onRuleClick", (function(e, t) {
      a.setState({
        currentRulesIndex: e
      });
    })), a.state = {
      current: 0,
      rules: [],
      currentRulesIndex: 0,
      attributeList: [],
      conditionList: [],
      object: {},
      active: "",
      logical: "",
      statusTrans: {}
    }, a;
  }
  return (0, _createClass2.default)(r, [ {
    key: "componentDidMount",
    value: function() {
      this.onInitData(this.props);
    }
  }, {
    key: "componentWillReceiveProps",
    value: function(e) {
      e.linkId !== this.props.linkId && this.onInitData(e);
    }
  }, {
    key: "render",
    value: function() {
      var e = this, t = this.state, r = t.current, a = t.rules, n = t.object, i = t.conditionList, u = t.attributeList, s = t.currentRulesIndex, o = this.props, l = o.sourceStatus, c = o.targetStatus;
      return _react.default.createElement("div", {
        className: "trigger-editor-content"
      }, _react.default.createElement("div", {
        className: "trigger-editor-title"
      }, "状态转换管理"), _react.default.createElement("div", {
        className: "trigger-editor-textarea"
      }, _react.default.createElement(_StatusTextArea.default, {
        currentRulesIndex: s,
        onRuleRemove: this.onRuleRemove,
        onRuleClick: this.onRuleClick,
        rules: a,
        object: n,
        sourceStatus: l.label,
        targetStatus: c.label
      })), _react.default.createElement(_StatusTransformTabs.default, {
        title: n.name,
        object: n,
        current: r,
        attributeList: u,
        conditionList: i,
        onAttributeClick: this.onAttributeClick,
        onConditionClick: this.onConditionClick,
        onResultChange: this.onResultChange,
        currentRules: a[s],
        onAndClick: function() {
          return e.onLogicalChange("with");
        },
        onOrClick: function() {
          return e.onLogicalChange("or");
        },
        onFinishClick: this.onFinish
      }));
    }
  } ]), r;
}(_react.Component);

(0, _defineProperty2.default)(StatusTransformContent, "propTypes", {
  objId: _propTypes.default.any,
  linkId: _propTypes.default.any,
  closeDesigner: _propTypes.default.func,
  toggleReloadStage: _propTypes.default.func,
  sourceStatus: _propTypes.default.object,
  targetStatus: _propTypes.default.object
}), (0, _defineProperty2.default)(StatusTransformContent, "defaultProps", {
  sourceStatus: {},
  targetStatus: {}
}), StatusTransformContent.displayName = "StatusTransformContent";

var _default = StatusTransformContent;

exports.default = _default;