"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")), _amosAntd = require("amos-antd"), _processor = require("../../utils/processor"), _amosTool = require("amos-tool"), _urlConsts = _interopRequireDefault(require("../../consts/urlConsts")), _amosFramework = require("amos-framework");

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

function _createSuper(e) {
  var t = _isNativeReflectConstruct();
  return function() {
    var a, r = (0, _getPrototypeOf2.default)(e);
    if (t) {
      var n = (0, _getPrototypeOf2.default)(this).constructor;
      a = Reflect.construct(r, arguments, n);
    } else a = r.apply(this, arguments);
    return (0, _possibleConstructorReturn2.default)(this, a);
  };
}

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;
  }
}

require("./common/index.scss");

var TabPane = _amosAntd.Tabs.TabPane, createForm = _amosAntd.Form.create(), FormItem = _amosAntd.Form.Item, Option = _amosAntd.Select.Option, monitorTypeArr = [ {
  title: "采集",
  value: 1
}, {
  title: "监听",
  value: 2
} ], periodArr = [ {
  title: "30秒/次",
  value: 30
}, {
  title: "60秒/次",
  value: 60
}, {
  title: "300秒/次",
  value: 300
}, {
  title: "900秒/次",
  value: 900
}, {
  title: "1800秒/次",
  value: 1800
} ], noSpaceLengthValidate = function(e) {
  return function(t, a, r) {
    a && (/^\S*$/.test(a) ? (0, _amosTool.trim)(a).length > e && r("支持1-".concat(e, "位非空格字符！")) : r("支持1-".concat(e, "位非空格字符！"))), 
    r();
  };
}, nameIsReplaceValidate = function(e) {
  return function(t, a, r) {
    if (a) if (a === e) r(); else {
      var n = "".concat(_urlConsts.default.monitorRuleNameIsReplace, "/").concat(a);
      _processor.AmosFetch.get(n).then((function(e) {
        if (e.ok) return e.json();
        throw new Error("从服务端获取数据失败！");
      })).then((function(e) {
        "SUCCESS" === e.result ? "ok" === e.message ? r() : r("任务名称重复，请重新输入！") : r("系统错误！");
      }));
    } else r();
  };
}, AddEditRule = function(e) {
  (0, _inherits2.default)(a, e);
  var t = _createSuper(a);
  function a(e) {
    var r;
    return (0, _classCallCheck2.default)(this, a), r = t.call(this, e), (0, _defineProperty2.default)((0, 
    _assertThisInitialized2.default)(r), "getMock", (function() {
      _processor.AmosFetch.get(_urlConsts.default.realDevice).then((function(e) {
        if (e.ok) return e.json();
        throw new Error("从服务端获取数据失败！");
      })).then((function(e) {
        "SUCCESS" === e.result && 0 !== e.dataList.length && r.makeMock(e.dataList), r.setState({
          dataList: []
        });
      }));
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(r), "makeMock", (function(e) {
      var t = [];
      e.map((function(e) {
        e.key = e.id, e.title = e.name, t.push(e);
      })), r.setState({
        dataList: e,
        mockData: t
      });
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(r), "handleSave", (function(e) {
      var t = r.state.targetKeys, a = r.props.monitorRuleInfo;
      a.name = e.name, a.monitorType = e.monitorType, "1" === e.monitorType ? a.period = e.period : a.period = 0, 
      a.remark = e.remark, t && 0 !== t.length ? (a.deviceIds = t, _processor.AmosFetch.post(_urlConsts.default.monitorRuleAddOrUpdate, {
        data: a
      }).then((function(e) {
        if (e.ok) return e.json();
        throw new Error("从服务端获取数据失败！");
      })).then((function(e) {
        "SUCCESS" === e.result ? (r.handleCancel(), _amosFramework.message.success("保存成功！")) : _amosFramework.message.danger("保存失败！");
      }))) : _amosAntd.Modal.info({
        title: "系统提示",
        content: "监测对象不能为空！"
      });
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(r), "handleCancel", (function() {
      r.props.form.resetFields(), r.setState({
        targetKeys: []
      }), r.props.cancel(), r.props.reload();
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(r), "handleSubmit", (function(e) {
      e.preventDefault(), r.props.form.validateFields((function(e, t) {
        e || r.handleSave(t);
      }));
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(r), "handleChange", (function(e) {
      r.setState({
        targetKeys: e
      });
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(r), "monitorTypeChange", (function(e) {
      r.props.typeChange(e);
    })), r.state = {
      mockData: [],
      targetKeys: []
    }, r;
  }
  return (0, _createClass2.default)(a, [ {
    key: "componentWillMount",
    value: function() {
      var e = this.props.monitorRuleInfo;
      this.setState({
        targetKeys: e.deviceIds
      });
    }
  }, {
    key: "componentDidMount",
    value: function() {
      this.getMock();
    }
  }, {
    key: "componentWillReceiveProps",
    value: function(e) {
      e.monitorRuleInfo && this.setState({
        targetKeys: e.monitorRuleInfo.deviceIds
      });
    }
  }, {
    key: "render",
    value: function() {
      var e = this.state, t = e.mockData, a = e.targetKeys, r = this.props, n = r.monitorRuleInfo, o = r.isCycle, l = n.name, i = n.monitorType, u = n.period, s = n.remark, c = this.props.form.getFieldProps, d = c("name", {
        initialValue: l,
        rules: [ {
          required: !0,
          message: "任务名称不能为空！"
        }, {
          validator: noSpaceLengthValidate(50)
        }, {
          validator: nameIsReplaceValidate(l)
        } ]
      }), p = c("monitorType", {
        initialValue: i ? "".concat(i) : "2",
        rules: [ {
          required: !0,
          message: "请选择监测方式！"
        } ]
      }), f = c("period", {
        initialValue: u ? "".concat(u) : "30",
        rules: []
      }), m = c("remark", {
        initialValue: s,
        rules: [ {
          validator: noSpaceLengthValidate(500)
        } ]
      }), _ = {
        labelCol: {
          span: 9
        },
        wrapperCol: {
          span: 8
        }
      };
      return _react.default.createElement("div", null, _react.default.createElement("div", {
        className: "amos-grid-from"
      }, _react.default.createElement(_amosAntd.Form, {
        inline: !0,
        onSubmit: this.handleSubmit
      }, _react.default.createElement(_amosAntd.Row, {
        style: {
          marginTop: "20px"
        }
      }, _react.default.createElement(_amosAntd.Col, {
        span: 24
      }, _react.default.createElement(FormItem, (0, _extends2.default)({
        hasFeedback: !0,
        label: "任务名称"
      }, _), _react.default.createElement(_amosAntd.Input, (0, _extends2.default)({
        className: "amos-grid-form-input"
      }, d, {
        placeholder: "请输入任务名称",
        style: {
          width: "250px"
        }
      }))))), _react.default.createElement(_amosAntd.Row, {
        style: {
          marginTop: "20px"
        }
      }, _react.default.createElement(_amosAntd.Col, {
        span: 24
      }, _react.default.createElement(FormItem, (0, _extends2.default)({
        hasFeedback: !0,
        label: "监测方式"
      }, _), _react.default.createElement(_amosAntd.Select, (0, _extends2.default)({
        style: {
          width: 250
        },
        onSelect: this.monitorTypeChange
      }, p, {
        placeholder: "请选择监测方式"
      }), monitorTypeArr.map((function(e) {
        return _react.default.createElement(Option, {
          key: "".concat(e.value),
          value: "".concat(e.value)
        }, e.title);
      })))))), o ? _react.default.createElement(_amosAntd.Row, {
        style: {
          marginTop: "20px"
        }
      }, _react.default.createElement(_amosAntd.Col, {
        span: 24
      }, _react.default.createElement(FormItem, (0, _extends2.default)({
        label: "任务周期"
      }, _), _react.default.createElement(_amosAntd.Select, (0, _extends2.default)({
        style: {
          width: 250
        }
      }, f, {
        placeholder: "请选择任务周期"
      }), periodArr.map((function(e) {
        return _react.default.createElement(Option, {
          key: "".concat(e.value),
          value: "".concat(e.value)
        }, e.title);
      })))))) : null, _react.default.createElement(_amosAntd.Row, {
        style: {
          marginTop: "20px"
        }
      }, _react.default.createElement(_amosAntd.Col, {
        span: 24
      }, _react.default.createElement(FormItem, (0, _extends2.default)({
        label: "描述"
      }, _, {
        placeholder: "请输入描述"
      }), _react.default.createElement(_amosAntd.Input, (0, _extends2.default)({
        className: "amos-grid-form-input",
        width: 250
      }, m))))))), _react.default.createElement("div", null, _react.default.createElement(_amosAntd.Tabs, {
        defaultActiveKey: "监测对象"
      }, _react.default.createElement(TabPane, {
        tab: _react.default.createElement("span", null, _react.default.createElement(_amosAntd.Icon, {
          type: "android"
        }), "监测对象"),
        key: "监测对象"
      }, _react.default.createElement("div", {
        style: {
          width: "100%",
          height: "100%",
          overflow: "auto"
        }
      }, _react.default.createElement(_amosAntd.Transfer, {
        dataSource: t,
        showSearch: !0,
        listStyle: {
          width: 250,
          height: 300,
          color: "#666"
        },
        operations: [ "添加", "移除" ],
        targetKeys: a,
        onChange: this.handleChange,
        render: function(e) {
          return "".concat(e.title);
        }
      }))))), _react.default.createElement(_amosAntd.Row, {
        style: {
          textAlign: "center",
          marginTop: "3rem"
        }
      }, _react.default.createElement(_amosAntd.Button, {
        type: "primary",
        onClick: this.handleSubmit
      }, "确定"), _react.default.createElement(_amosAntd.Button, {
        type: "primary",
        onClick: this.handleCancel,
        style: {
          marginLeft: "10px"
        }
      }, "取消")));
    }
  } ]), a;
}(_react.Component);

AddEditRule.displayName = "AddEditRule", AddEditRule.propTypes = {
  form: _propTypes.default.any,
  cancel: _propTypes.default.func,
  monitorRuleInfo: _propTypes.default.any,
  reload: _propTypes.default.func,
  flag: _propTypes.default.any,
  typeChange: _propTypes.default.func,
  isCycle: _propTypes.default.any
}, AddEditRule.defaultProps = {
  monitorRuleInfo: {
    monitorType: ""
  }
};

var _default = createForm(AddEditRule);

exports.default = _default;