"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"),
  _amosAntd = require("amos-antd"),
  _rayValidate = require("ray-validate"),
  _rayEventpool = require("ray-eventpool"),
  _stageConf = require("./../ruleStatge/modelConf/stageConf"),
  _ruleDesignService = require("./../../../services/ruleDesignService");

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

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 ownKeys(t, e) {
  var r, o = Object.keys(t);
  return Object.getOwnPropertySymbols && (r = Object.getOwnPropertySymbols(t), e && (r = r.filter(function(e) {
    return Object.getOwnPropertyDescriptor(t, e).enumerable
  })), o.push.apply(o, r)), o
}

function _objectSpread(t) {
  for (var e = 1; e < arguments.length; e++) {
    var r = null != arguments[e] ? arguments[e] : {};
    e % 2 ? ownKeys(Object(r), !0).forEach(function(e) {
      _defineProperty(t, e, r[e])
    }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(t, Object.getOwnPropertyDescriptors(r)) : ownKeys(Object(r)).forEach(function(e) {
      Object.defineProperty(t, e, Object.getOwnPropertyDescriptor(r, e))
    })
  }
  return t
}

function _extends() {
  return (_extends = Object.assign || function(e) {
    for (var t = 1; t < arguments.length; t++) {
      var r, o = arguments[t];
      for (r in o) Object.prototype.hasOwnProperty.call(o, r) && (e[r] = o[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 o = t[r];
    o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, o.key, o)
  }
}

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 o = _isNativeReflectConstruct();
  return function() {
    var e, t = _getPrototypeOf(r);
    return _possibleConstructorReturn(this, o ? (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 maxPort = 65535,
  FormItem = _amosFramework.Form.Item,
  formItemLayout = {
    labelCol: {
      xs: {
        span: 24
      },
      sm: {
        span: 6,
        offset: 0
      },
      className: "colspanlab"
    },
    wrapperCol: {
      xs: {
        span: 24
      },
      sm: {
        span: 17,
        offset: 0
      },
      className: "colspan"
    }
  },
  OutputNodeConfig = function() {
    _inherits(r, _react.Component);
    var t = _createSuper(r);

    function r(e) {
      var a;
      _classCallCheck(this, r), _defineProperty(_assertThisInitialized(a = t.call(this, e)), "onInput", function(e, t) {
        var r = a.state.form;
        r[e] = t.target.value, a.setState({
          form: r,
          isEdited: !0
        })
      }), _defineProperty(_assertThisInitialized(a), "onSelectedChange", function(e) {
        var t = a.state.form;
        t.emailSsl = !t.emailSsl, a.setState({
          form: t,
          isEdited: !0
        })
      }), _defineProperty(_assertThisInitialized(a), "getNodeConfig", function(r) {
        (0, _ruleDesignService.fetchNodeConfigAction)(r.id).then(function(e) {
          var t = a.state.form;
          "outputEmail" === r.nodeType ? (t.itemName = null === e.configInfo ? r.label : e.configInfo.name, t.emailAddress = null === e.configInfo ? "" : e.configInfo.toEmail, t.emailServer = null === e.configInfo ? "smtp.exmail.qq.com" : e.configInfo.server, t.emailPort = null === e.configInfo ? "465" : e.configInfo.port, t.emailUser = null === e.configInfo ? "amospm@yeejoin.com" : e.configInfo.fromEmail, t.emailPwd = null === e.configInfo ? "QJamos_123" : e.configInfo.fromPassword, t.emailSsl = null === e.configInfo || e.configInfo.isSSL) : "outputMqtt" === r.nodeType ? (t.mqttName = null === e.configInfo ? r.label : e.configInfo.name, t.mqttIP = null === e.configInfo ? "" : e.configInfo.serverIp, t.mqttPort = null === e.configInfo ? "" : e.configInfo.port, t.mqttTopic = null === e.configInfo ? "" : e.configInfo.theme, t.mqttUser = null === e.configInfo ? "" : e.configInfo.userName, t.mqttPwd = null === e.configInfo ? "" : e.configInfo.password) : "outputTcp" === r.nodeType && (t.tcpName = null === e.configInfo ? r.label : e.configInfo.name, t.tcpServer = null === e.configInfo ? "" : e.configInfo.serverIp, t.tcpPort = null === e.configInfo ? "" : e.configInfo.port), a.setState({
            nodeConfig: e,
            form: t
          })
        })
      }), _defineProperty(_assertThisInitialized(a), "getFormByType", function() {
        var e = a.props.config,
          t = a.state,
          r = t.form,
          o = t.nodeConfig,
          n = [],
          t = {
            itemName: [{
              required: !0,
              message: "请输入活动名称"
            }, {
              min: 1,
              max: 150,
              message: "长度范围：0-150！"
            }],
            emailAddress: [{
              required: !0,
              message: "不能为空！"
            }, {
              type: "email",
              message: "非法的email地址！"
            }],
            emailServer: [{
              required: !0,
              message: "不能为空！"
            }],
            emailPort: [{
              required: !0,
              message: "不能为空！"
            }, {
              validator: a.checkPort
            }],
            emailUser: [{
              required: !0,
              message: "不能为空！"
            }, {
              type: "email",
              message: "非法的email地址！"
            }],
            emailPwd: [{
              required: !0,
              message: "不能为空！"
            }],
            mqttName: [{
              required: !0,
              message: "不能为空！"
            }],
            mqttIP: [{
              required: !0,
              message: "不能为空！"
            }],
            mqttPort: [{
              required: !0,
              message: "不能为空！"
            }, {
              validator: a.checkPort
            }],
            mqttTopic: [{
              required: !0,
              message: "不能为空！"
            }],
            mqttUser: [{
              required: !0,
              message: "不能为空！"
            }],
            mqttPwd: [{
              required: !0,
              message: "不能为空！"
            }],
            tcpName: [{
              required: !0,
              message: "不能为空！"
            }],
            tcpPort: [{
              required: !0,
              message: "不能为空！"
            }, {
              validator: a.checkPort
            }],
            tcpServer: [{
              required: !0,
              message: "不能为空！"
            }]
          };
        return o && ("outputEmail" === e.nodeType ? n.push(_react.default.createElement(_amosFramework.Form, {
          className: "config-form",
          ref: function(e) {
            return a.form = e
          },
          model: r,
          rules: t
        }, _react.default.createElement(FormItem, _extends({
          label: _react.default.createElement("span", null, "节点名称"),
          field: "itemName"
        }, formItemLayout), _react.default.createElement(_amosFramework.Input, {
          placeholder: "节点名称",
          value: r.itemName,
          style: {
            width: "100%"
          },
          onChange: function(e) {
            return a.onInput("itemName", e)
          }
        })), _react.default.createElement(FormItem, _extends({
          label: "接收邮箱",
          field: "emailAddress"
        }, formItemLayout), _react.default.createElement(_amosFramework.Input, {
          placeholder: "接收邮箱",
          value: r.emailAddress,
          style: {
            width: "100%"
          },
          onChange: function(e) {
            return a.onInput("emailAddress", e)
          }
        })), _react.default.createElement(FormItem, _extends({
          label: "服务器",
          field: "emailServer"
        }, formItemLayout), _react.default.createElement(_amosFramework.Input, {
          placeholder: "服务器",
          value: r.emailServer,
          style: {
            width: "100%"
          },
          onChange: function(e) {
            return a.onInput("emailServer", e)
          }
        })), _react.default.createElement(FormItem, _extends({
          label: "端口",
          field: "emailPort"
        }, formItemLayout), _react.default.createElement(_amosFramework.Input, {
          placeholder: "端口",
          value: r.emailPort,
          style: {
            width: "46%",
            float: "left"
          },
          onChange: function(e) {
            return a.onInput("emailPort", e)
          }
        }), _react.default.createElement(_amosAntd.Checkbox, {
          value: "0",
          checked: r.emailSsl,
          onChange: function(e) {
            return a.onSelectedChange(e)
          },
          style: {
            "line-height": "31px",
            float: "left",
            "margin-left": "7px"
          }
        }, "使用安全连接(SSL)")), _react.default.createElement(FormItem, _extends({
          label: "发送邮箱",
          field: "emailUser"
        }, formItemLayout), _react.default.createElement(_amosFramework.Input, {
          placeholder: "发送邮箱",
          value: r.emailUser,
          style: {
            width: "100%"
          },
          onChange: function(e) {
            return a.onInput("emailUser", e)
          }
        })), _react.default.createElement(FormItem, _extends({
          label: "密码",
          field: "emailPwd"
        }, formItemLayout), _react.default.createElement(_amosFramework.Input, {
          placeholder: "密码",
          value: r.emailPwd,
          style: {
            width: "100%"
          },
          onChange: function(e) {
            return a.onInput("emailPwd", e)
          },
          type: "password"
        })))) : "outputMqtt" === e.nodeType ? n.push(_react.default.createElement(_amosFramework.Form, {
          className: "config-form",
          ref: function(e) {
            return a.form = e
          },
          model: r,
          rules: t
        }, _react.default.createElement(FormItem, _extends({
          label: _react.default.createElement("span", null, "节点名称"),
          field: "mqttName"
        }, formItemLayout), _react.default.createElement(_amosFramework.Input, {
          placeholder: "节点名称",
          value: r.mqttName,
          style: {
            width: "100%"
          },
          onChange: function(e) {
            return a.onInput("mqttName", e)
          }
        })), _react.default.createElement(FormItem, _extends({
          label: _react.default.createElement("span", null, "Broker"),
          field: "mqttIP"
        }, formItemLayout), _react.default.createElement(_amosFramework.Input, {
          placeholder: "mqtt服务所在服务器IP",
          value: r.mqttIP,
          style: {
            width: "100%"
          },
          onChange: function(e) {
            return a.onInput("mqttIP", e)
          }
        })), _react.default.createElement(FormItem, _extends({
          label: _react.default.createElement("span", null, "端口"),
          field: "mqttPort"
        }, formItemLayout), _react.default.createElement(_amosFramework.Input, {
          placeholder: "mqtt服务端口号",
          value: r.mqttPort,
          style: {
            width: "100%"
          },
          onChange: function(e) {
            return a.onInput("mqttPort", e)
          }
        })), _react.default.createElement(FormItem, _extends({
          label: _react.default.createElement("span", null, "主题"),
          field: "mqttTopic"
        }, formItemLayout), _react.default.createElement(_amosFramework.Input, {
          placeholder: "mqtt主题",
          value: r.mqttTopic,
          style: {
            width: "100%"
          },
          onChange: function(e) {
            return a.onInput("mqttTopic", e)
          }
        })), _react.default.createElement(FormItem, _extends({
          label: "用户名",
          field: "mqttUser"
        }, formItemLayout), _react.default.createElement(_amosFramework.Input, {
          placeholder: "用户名",
          value: r.mqttUser,
          style: {
            width: "100%"
          },
          onChange: function(e) {
            return a.onInput("mqttUser", e)
          }
        })), _react.default.createElement(FormItem, _extends({
          label: "密码",
          field: "mqttPwd"
        }, formItemLayout), _react.default.createElement(_amosFramework.Input, {
          placeholder: "密码",
          value: r.mqttPwd,
          style: {
            width: "100%"
          },
          onChange: function(e) {
            return a.onInput("mqttPwd", e)
          },
          type: "password"
        })))) : "outputTcp" === e.nodeType && n.push(_react.default.createElement(_amosFramework.Form, {
          className: "config-form",
          ref: function(e) {
            return a.form = e
          },
          model: r,
          rules: t
        }, _react.default.createElement(FormItem, _extends({
          label: _react.default.createElement("span", null, "节点名称"),
          field: "tcpName"
        }, formItemLayout), _react.default.createElement(_amosFramework.Input, {
          placeholder: "节点名称",
          value: r.tcpName,
          style: {
            width: "100%"
          },
          onChange: function(e) {
            return a.onInput("tcpName", e)
          }
        })), _react.default.createElement(FormItem, _extends({
          label: _react.default.createElement("span", null, "主机"),
          field: "tcpServer"
        }, formItemLayout), _react.default.createElement(_amosFramework.Input, {
          placeholder: "tcp服务所在服务器IP",
          value: r.tcpServer,
          style: {
            width: "100%"
          },
          onChange: function(e) {
            return a.onInput("tcpServer", e)
          }
        })), _react.default.createElement(FormItem, _extends({
          label: _react.default.createElement("span", null, "端口"),
          field: "tcpPort"
        }, formItemLayout), _react.default.createElement(_amosFramework.Input, {
          placeholder: "tcp服务端口号",
          value: r.tcpPort,
          style: {
            width: "100%"
          },
          onChange: function(e) {
            return a.onInput("tcpPort", e)
          }
        }))))), n
      }), _defineProperty(_assertThisInitialized(a), "checkPort", function(e, t, r) {
        t && !(0, _rayValidate.isNum)(t) ? r("请输入正确的端口号！") : 0 < (t = parseInt(t)) && t < maxPort ? r() : r("端口号范围（0-65535）！")
      }), _defineProperty(_assertThisInitialized(a), "handleSubmit", function(e) {
        e.preventDefault(), a.form.validate(function(e, t) {
          return console.log("返回内容:", t, e), e ? void a.saveConfig() : (console.log("error submit!!"), !1)
        })
      }), _defineProperty(_assertThisInitialized(a), "removeItem", function(e, t) {
        t && (_rayEventpool.PubSub.publish(_stageConf.ELE_DEL_TOPIC.trigger, {
          eleId: t
        }), a.props.closeTriggerDesigner())
      }), _defineProperty(_assertThisInitialized(a), "saveConfig", function() {
        var t, e, r = a.props.config,
          o = a.state,
          n = o.form,
          o = o.nodeConfig;
        "outputEmail" === r.nodeType ? (t = {
          nodeId: r.id,
          nodeType: r.nodeType,
          label: void 0 === n.itemName ? o.label : n.itemName
        }, e = {
          toEmail: void 0 === n.emailAddress ? o.configInfo.toEmail : n.emailAddress,
          server: void 0 === n.emailServer ? o.configInfo.server : n.emailServer,
          port: void 0 === n.emailPort ? o.configInfo.port : n.emailPort,
          fromEmail: void 0 === n.emailUser ? o.configInfo.fromEmail : n.emailUser,
          fromPassword: void 0 === n.emailPwd ? o.configInfo.fromPassword : n.emailPwd,
          isSSL: void 0 === n.emailSsl ? o.configInfo.isSSL : n.emailSsl
        }) : "outputMqtt" === r.nodeType ? (t = {
          nodeId: r.id,
          nodeType: r.nodeType,
          label: void 0 === n.mqttName ? o.label : n.mqttName
        }, e = {
          serverIp: void 0 === n.mqttIP ? o.configInfo.serverIp : n.mqttIP,
          port: void 0 === n.mqttPort ? o.configInfo.port : n.mqttPort,
          theme: void 0 === n.mqttTopic ? o.configInfo.theme : n.mqttTopic,
          userName: void 0 === n.mqttUser ? o.configInfo.userName : n.mqttUser,
          password: void 0 === n.mqttPwd ? o.configInfo.password : n.mqttPwd
        }) : "outputTcp" === r.nodeType && (t = {
          nodeId: r.id,
          nodeType: r.nodeType,
          label: void 0 === n.tcpName ? o.label : n.tcpName
        }, e = {
          serverIp: void 0 === n.tcpServer ? o.configInfo.serverIp : n.tcpServer,
          port: void 0 === n.tcpPort ? o.configInfo.port : n.tcpPort
        }), t = _objectSpread(_objectSpread({}, t), {}, {
          configInfo: e
        }), (0, _ruleDesignService.updateNode)(t).then(function(e) {
          _rayEventpool.PubSub.publish(_stageConf.ELE_UPDATE_TOPIC.trigger, {
            label: t.label,
            nodeId: r.id
          }), _amosFramework.message.success("保存成功")
        })
      });
      e = a.props.config;
      return a.state = {
        form: {
          itemName: e.label,
          mqttName: e.label,
          tcpName: e.label,
          isEdited: !1
        }
      }, a
    }
    return _createClass(r, [{
      key: "componentWillMount",
      value: function() {}
    }, {
      key: "componentDidMount",
      value: function() {
        var n = this;
        this.getNodeConfig(this.props.config), this.pubsub_update = _rayEventpool.PubSub.subscribe(_stageConf.ELE_UPDATE_TOPIC.eleUpdate, function(e, t) {
          var r = t.label,
            o = t.nodeId,
            t = n.props.config;
          o === t.id && (o = n.state.form, "outputEmail" === t.nodeType ? o.itemName = r : "outputMqtt" === t.nodeType ? o.mqttName = r : "outputTcp" === t.nodeType && (o.tcpName = r), n.setState({
            form: o
          }))
        })
      }
    }, {
      key: "componentWillReceiveProps",
      value: function(e) {
        this.props.config.id !== e.config.id && this.getNodeConfig(e.config)
      }
    }, {
      key: "componentWillUnmount",
      value: function() {
        _rayEventpool.PubSub.unsubscribe(this.pubsub_update)
      }
    }, {
      key: "render",
      value: function() {
        var e = this.props,
          t = e.closeTriggerDesigner,
          e = (e.config, this.state.isEdited);
        return _react.default.createElement("div", {
          className: "output-config-root"
        }, _react.default.createElement("div", {
          className: "head-root"
        }, _react.default.createElement("div", {
          className: "head-one"
        }, "节点编辑"), _react.default.createElement("div", {
          className: "head-two"
        }, e ? _react.default.createElement("div", {
          className: "head-two-four",
          onClick: this.handleSubmit
        }, "保存") : "", _react.default.createElement("div", {
          className: "head-two-three",
          onClick: t
        }, "取消"))), _react.default.createElement("div", {
          className: "content-panel"
        }, this.getFormByType()))
      }
    }]), r
  }();
OutputNodeConfig.displayName = "OutputNodeConfig", OutputNodeConfig.propTypes = {
  config: _propTypes.default.object,
  closeTriggerDesigner: _propTypes.default.func
}, OutputNodeConfig.defaultProps = {};
var _default = OutputNodeConfig;
exports.default = OutputNodeConfig, module.exports = exports.default;
