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

function ownKeys(e, t) {
  var r = Object.keys(e);
  if (Object.getOwnPropertySymbols) {
    var a = Object.getOwnPropertySymbols(e);
    t && (a = a.filter((function(t) {
      return Object.getOwnPropertyDescriptor(e, t).enumerable;
    }))), r.push.apply(r, a);
  }
  return r;
}

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

function _createSuper(e) {
  var t = _isNativeReflectConstruct();
  return function() {
    var r, a = (0, _getPrototypeOf2.default)(e);
    if (t) {
      var o = (0, _getPrototypeOf2.default)(this).constructor;
      r = Reflect.construct(a, arguments, o);
    } 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 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(e) {
  (0, _inherits2.default)(r, e);
  var t = _createSuper(r);
  function r(e) {
    var a;
    (0, _classCallCheck2.default)(this, r), a = t.call(this, e), (0, _defineProperty2.default)((0, 
    _assertThisInitialized2.default)(a), "onInput", (function(e, t) {
      var r = a.state.form;
      r[e] = t.target.value, a.setState({
        form: r,
        isEdited: !0
      });
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(a), "onSelectedChange", (function(e) {
      var t = a.state.form;
      t.emailSsl = !t.emailSsl, a.setState({
        form: t,
        isEdited: !0
      });
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(a), "getNodeConfig", (function(e) {
      (0, _ruleDesignService.fetchNodeConfigAction)(e.id).then((function(t) {
        var r = a.state.form;
        "outputEmail" === e.nodeType ? (r.itemName = null === t.configInfo ? e.label : t.configInfo.name, 
        r.emailAddress = null === t.configInfo ? "" : t.configInfo.toEmail, r.emailServer = null === t.configInfo ? "smtp.exmail.qq.com" : t.configInfo.server, 
        r.emailPort = null === t.configInfo ? "465" : t.configInfo.port, r.emailUser = null === t.configInfo ? "amospm@yeejoin.com" : t.configInfo.fromEmail, 
        r.emailPwd = null === t.configInfo ? "QJamos_123" : t.configInfo.fromPassword, r.emailSsl = null === t.configInfo || t.configInfo.isSSL) : "outputMqtt" === e.nodeType ? (r.mqttName = null === t.configInfo ? e.label : t.configInfo.name, 
        r.mqttIP = null === t.configInfo ? "" : t.configInfo.serverIp, r.mqttPort = null === t.configInfo ? "" : t.configInfo.port, 
        r.mqttTopic = null === t.configInfo ? "" : t.configInfo.theme, r.mqttUser = null === t.configInfo ? "" : t.configInfo.userName, 
        r.mqttPwd = null === t.configInfo ? "" : t.configInfo.password) : "outputTcp" === e.nodeType && (r.tcpName = null === t.configInfo ? e.label : t.configInfo.name, 
        r.tcpServer = null === t.configInfo ? "" : t.configInfo.serverIp, r.tcpPort = null === t.configInfo ? "" : t.configInfo.port), 
        a.setState({
          nodeConfig: t,
          form: r
        });
      }));
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(a), "getFormByType", (function() {
      var e = a.props.config, t = a.state, r = t.form, o = t.nodeConfig, n = [], l = {
        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: l
      }, _react.default.createElement(FormItem, (0, _extends2.default)({
        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, (0, _extends2.default)({
        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, (0, _extends2.default)({
        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, (0, _extends2.default)({
        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: {
          lineHeight: "31px",
          float: "left",
          "margin-left": "7px"
        }
      }, "使用安全连接(SSL)")), _react.default.createElement(FormItem, (0, _extends2.default)({
        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, (0, _extends2.default)({
        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: l
      }, _react.default.createElement(FormItem, (0, _extends2.default)({
        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, (0, _extends2.default)({
        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, (0, _extends2.default)({
        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, (0, _extends2.default)({
        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, (0, _extends2.default)({
        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, (0, _extends2.default)({
        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: l
      }, _react.default.createElement(FormItem, (0, _extends2.default)({
        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, (0, _extends2.default)({
        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, (0, _extends2.default)({
        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;
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(a), "checkPort", (function(e, t, r) {
      if (t && !(0, _rayValidate.isNum)(t)) r("请输入正确的端口号！"); else {
        var a = parseInt(t);
        a > 0 && a < maxPort ? r() : r("端口号范围（0-65535）！");
      }
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(a), "handleSubmit", (function(e) {
      e.preventDefault(), a.form.validate((function(e, t) {
        if (console.log("返回内容:", t, e), !e) return console.log("error submit!!"), !1;
        a.saveConfig();
      }));
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(a), "removeItem", (function(e, t) {
      t && (_rayEventpool.PubSub.publish(_stageConf.ELE_DEL_TOPIC.trigger, {
        eleId: t
      }), a.props.closeTriggerDesigner());
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(a), "saveConfig", (function() {
      var e, t, r = a.props.config, o = a.state, n = o.form, l = o.nodeConfig;
      "outputEmail" === r.nodeType ? (e = {
        nodeId: r.id,
        nodeType: r.nodeType,
        label: void 0 === n.itemName ? l.label : n.itemName
      }, t = {
        toEmail: void 0 === n.emailAddress ? l.configInfo.toEmail : n.emailAddress,
        server: void 0 === n.emailServer ? l.configInfo.server : n.emailServer,
        port: void 0 === n.emailPort ? l.configInfo.port : n.emailPort,
        fromEmail: void 0 === n.emailUser ? l.configInfo.fromEmail : n.emailUser,
        fromPassword: void 0 === n.emailPwd ? l.configInfo.fromPassword : n.emailPwd,
        isSSL: void 0 === n.emailSsl ? l.configInfo.isSSL : n.emailSsl
      }) : "outputMqtt" === r.nodeType ? (e = {
        nodeId: r.id,
        nodeType: r.nodeType,
        label: void 0 === n.mqttName ? l.label : n.mqttName
      }, t = {
        serverIp: void 0 === n.mqttIP ? l.configInfo.serverIp : n.mqttIP,
        port: void 0 === n.mqttPort ? l.configInfo.port : n.mqttPort,
        theme: void 0 === n.mqttTopic ? l.configInfo.theme : n.mqttTopic,
        userName: void 0 === n.mqttUser ? l.configInfo.userName : n.mqttUser,
        password: void 0 === n.mqttPwd ? l.configInfo.password : n.mqttPwd
      }) : "outputTcp" === r.nodeType && (e = {
        nodeId: r.id,
        nodeType: r.nodeType,
        label: void 0 === n.tcpName ? l.label : n.tcpName
      }, t = {
        serverIp: void 0 === n.tcpServer ? l.configInfo.serverIp : n.tcpServer,
        port: void 0 === n.tcpPort ? l.configInfo.port : n.tcpPort
      }), e = _objectSpread(_objectSpread({}, e), {}, {
        configInfo: t
      }), (0, _ruleDesignService.updateNode)(e).then((function(t) {
        _rayEventpool.PubSub.publish(_stageConf.ELE_UPDATE_TOPIC.trigger, {
          label: e.label,
          nodeId: r.id
        }), _amosFramework.message.success("保存成功");
      }));
    }));
    var o = a.props.config;
    return a.state = {
      form: {
        itemName: o.label,
        mqttName: o.label,
        tcpName: o.label,
        isEdited: !1
      }
    }, a;
  }
  return (0, _createClass2.default)(r, [ {
    key: "componentWillMount",
    value: function() {}
  }, {
    key: "componentDidMount",
    value: function() {
      var e = this;
      this.getNodeConfig(this.props.config), this.pubsub_update = _rayEventpool.PubSub.subscribe(_stageConf.ELE_UPDATE_TOPIC.eleUpdate, (function(t, r) {
        var a = r.label, o = r.nodeId, n = e.props.config;
        if (o === n.id) {
          var l = e.state.form;
          "outputEmail" === n.nodeType ? l.itemName = a : "outputMqtt" === n.nodeType ? l.mqttName = a : "outputTcp" === n.nodeType && (l.tcpName = a), 
          e.setState({
            form: l
          });
        }
      }));
    }
  }, {
    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, r = (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"
      }, r ? _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;
}(_react.Component);

OutputNodeConfig.displayName = "OutputNodeConfig", OutputNodeConfig.propTypes = {
  config: _propTypes.default.object,
  closeTriggerDesigner: _propTypes.default.func
}, OutputNodeConfig.defaultProps = {};

var _default = OutputNodeConfig;

exports.default = _default;