"use strict";
Object.defineProperty(exports, "__esModule", {
  value: !0
}), exports.default = void 0;
var _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 _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, i = {},
    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(i, r, n) : i[r] = e[r]);
  return i.default = e, t && t.set(e, i), i
}

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 _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 attributeIndex = 1,
  conditionIndex = 2,
  resultIndex = 3,
  StatusTransformContent = function() {
    _inherits(r, _react.Component);
    var t = _createSuper(r);

    function r(e) {
      var c;
      return _classCallCheck(this, r), _defineProperty(_assertThisInitialized(c = t.call(this, e)), "onInitData", function(e) {
        var t = e.objId,
          e = e.linkId;
        t && (0, _services.queryRuleSubObj)(t).then(function(e) {
          c.setState({
            object: e || {}
          })
        }), (0, _services.queryStatusRuleAction)(e).then(function(e) {
          var t, r, i;
          e ? (t = e.pseudoCode, r = c.state, i = r.rules, r.current, r = r.currentRulesIndex, i = [], t.conditionList.map(function(e, n) {
            e.map(function(e, t) {
              var r = 0 === t ? "or" : "with";
              i.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 === n && 0 === t ? void 0 : r
              })
            })
          }), r = i.length - 1, c.setState({
            rules: i,
            current: resultIndex,
            currentRulesIndex: r,
            statusTrans: e
          })) : c.setState({
            current: 0,
            rules: [],
            currentRulesIndex: 0,
            active: "",
            logical: "",
            statusTrans: {}
          })
        }), t && (0, _services.queryAttributeAction)(t).then(function(e) {
          c.setState({
            attributeList: e || []
          })
        }), (0, _services.queryConditioneAction)().then(function(e) {
          c.setState({
            conditionList: e || []
          })
        })
      }), _defineProperty(_assertThisInitialized(c), "onAttributeClick", function(e) {
        var t = c.state,
          r = t.rules,
          n = t.current,
          i = t.logical,
          t = t.currentRulesIndex;
        0 === n ? ((n = {
          id: Date.now()
        }).attributeId = e.id, n.attribute = e.name, n.attributeName = e.displayName, n.bizClassName = e.bizclassName, n.fact = e.factId, n.propType = e.dataType, n.orWidth = 0 === t || i, r.push(n), c.setState({
          rules: r,
          current: attributeIndex,
          currentRulesIndex: r.length - 1
        })) : (r[t].attributeId = e.id, r[t].attribute = e.name, r[t].attributeName = e.displayName, r[t].bizClassName = e.bizclassName, r[t].fact = e.factId, r[t].propType = e.dataType, c.setState({
          rules: r
        }))
      }), _defineProperty(_assertThisInitialized(c), "onConditionClick", function(e) {
        var t = c.state,
          r = t.rules,
          n = t.current,
          t = t.currentRulesIndex;
        n <= 1 && c.setState({
          current: conditionIndex
        }), r[t].conditionId = e.name, r[t].conditionName = e.displayName, r[t].relationType = e.type, c.setState({
          rules: r
        })
      }), _defineProperty(_assertThisInitialized(c), "onResultChange", function(e) {
        var t = c.state,
          r = t.rules,
          n = t.current,
          t = t.currentRulesIndex;
        n <= conditionIndex && c.setState({
          current: resultIndex
        }), r[t].result = e.target.value, c.setState({
          rules: r
        })
      }), _defineProperty(_assertThisInitialized(c), "onLogicalChange", function(e) {
        var t = c.state.rules;
        c.setState({
          current: 0,
          logical: e,
          currentRulesIndex: t.length
        })
      }), _defineProperty(_assertThisInitialized(c), "onReset", function() {
        c.setState({
          current: 0,
          rules: [],
          currentRulesIndex: 0,
          logical: ""
        })
      }), _defineProperty(_assertThisInitialized(c), "onFinish", function() {
        var t, r, e = c.state,
          n = e.rules,
          i = e.current,
          o = e.isLoading,
          a = e.statusTrans,
          s = c.props,
          u = s.objId,
          e = s.targetStatus,
          s = s.linkId;
        0 < n.length && i < 2 ? _amosFramework.message.danger("信息填写未完整") : 0 !== n.length ? o || (t = [], 0 < n.length && (r = [], n.map(function(e) {
          "or" === e.orWidth && (t.push(r), r = []), r.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
          })
        }), 0 < r.length && t.push(r)), e = {
          elementId: s,
          groupId: a.groupId,
          id: a.id,
          oid: u,
          stateId: e.id,
          pseudoCode: JSON.stringify({
            conditionList: t
          })
        }, c.setState({
          isLoading: !0
        }), (a.id ? _services.updateStatusObjectAction : _services.addStatusObjectAction)(e).then(function(e) {
          c.onReset(), c.props.closeDesigner(), c.props.toggleReloadStage(!0), c.setState({
            isLoading: !1
          })
        }, function(e) {
          c.setState({
            isLoading: !1
          }), _amosFramework.message.danger(e || "保存失败")
        })) : _amosFramework.message.danger("必须填写条件")
      }), _defineProperty(_assertThisInitialized(c), "onRuleRemove", function(r, n) {
        var e = c.state.rules.filter(function(e, t) {
          return t < r || t >= r + n.length
        });
        0 < e.length && (e[0].orWidth = ""), c.setState({
          rules: e,
          current: 0 < e.length ? resultIndex : 0,
          currentRulesIndex: 0 < e.length ? e.length - 1 : 0
        })
      }), _defineProperty(_assertThisInitialized(c), "onRuleClick", function(e, t) {
        c.setState({
          currentRulesIndex: e
        })
      }), c.state = {
        current: 0,
        rules: [],
        currentRulesIndex: 0,
        attributeList: [],
        conditionList: [],
        object: {},
        active: "",
        logical: "",
        statusTrans: {}
      }, c
    }
    return _createClass(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,
          n = t.rules,
          i = t.object,
          o = t.conditionList,
          a = t.attributeList,
          s = t.currentRulesIndex,
          u = this.props,
          t = u.sourceStatus,
          u = u.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: n,
          object: i,
          sourceStatus: t.label,
          targetStatus: u.label
        })), _react.default.createElement(_StatusTransformTabs.default, {
          title: i.name,
          object: i,
          current: r,
          attributeList: a,
          conditionList: o,
          onAttributeClick: this.onAttributeClick,
          onConditionClick: this.onConditionClick,
          onResultChange: this.onResultChange,
          currentRules: n[s],
          onAndClick: function() {
            return e.onLogicalChange("with")
          },
          onOrClick: function() {
            return e.onLogicalChange("or")
          },
          onFinishClick: this.onFinish
        }))
      }
    }]), r
  }();
_defineProperty(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
}), _defineProperty(StatusTransformContent, "defaultProps", {
  sourceStatus: {},
  targetStatus: {}
}), StatusTransformContent.displayName = "StatusTransformContent";
var _default = StatusTransformContent;
exports.default = StatusTransformContent, module.exports = exports.default;
