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

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 _extends() {
  return (_extends = Object.assign || function(e) {
    for (var t = 1; t < arguments.length; t++) {
      var r, n = arguments[t];
      for (r in n) Object.prototype.hasOwnProperty.call(n, r) && (e[r] = n[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 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
}
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(n) {
    return function(e, t, r) {
      t && (!/^\S*$/.test(t) || (0, _amosTool.trim)(t).length > n) && r("支持1-".concat(n, "位非空格字符！")), r()
    }
  },
  nameIsReplaceValidate = function(n) {
    return function(e, t, r) {
      !t || t === n ? r() : (t = "".concat(_urlConsts.default.monitorRuleNameIsReplace, "/").concat(t), _processor.AmosFetch.get(t).then(function(e) {
        if (e.ok) return e.json();
        throw new Error("从服务端获取数据失败！")
      }).then(function(e) {
        "SUCCESS" === e.result ? "ok" === e.message ? r() : r("任务名称重复，请重新输入！") : r("系统错误！")
      }))
    }
  },
  AddEditRule = function() {
    _inherits(r, _react.Component);
    var t = _createSuper(r);

    function r(e) {
      var n;
      return _classCallCheck(this, r), _defineProperty(_assertThisInitialized(n = t.call(this, e)), "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 && n.makeMock(e.dataList), n.setState({
            dataList: []
          })
        })
      }), _defineProperty(_assertThisInitialized(n), "makeMock", function(e) {
        var t = [];
        e.map(function(e) {
          e.key = e.id, e.title = e.name, t.push(e)
        }), n.setState({
          dataList: e,
          mockData: t
        })
      }), _defineProperty(_assertThisInitialized(n), "handleSave", function(e) {
        var t = n.state.targetKeys,
          r = n.props.monitorRuleInfo;
        r.name = e.name, r.monitorType = e.monitorType, "1" === e.monitorType ? r.period = e.period : r.period = 0, r.remark = e.remark, t && 0 !== t.length ? (r.deviceIds = t, _processor.AmosFetch.post(_urlConsts.default.monitorRuleAddOrUpdate, {
          data: r
        }).then(function(e) {
          if (e.ok) return e.json();
          throw new Error("从服务端获取数据失败！")
        }).then(function(e) {
          "SUCCESS" === e.result ? (n.handleCancel(), _amosFramework.message.success("保存成功！")) : _amosFramework.message.danger("保存失败！")
        })) : _amosAntd.Modal.info({
          title: "系统提示",
          content: "监测对象不能为空！"
        })
      }), _defineProperty(_assertThisInitialized(n), "handleCancel", function() {
        n.props.form.resetFields(), n.setState({
          targetKeys: []
        }), n.props.cancel(), n.props.reload()
      }), _defineProperty(_assertThisInitialized(n), "handleSubmit", function(e) {
        e.preventDefault(), n.props.form.validateFields(function(e, t) {
          e || n.handleSave(t)
        })
      }), _defineProperty(_assertThisInitialized(n), "handleChange", function(e) {
        n.setState({
          targetKeys: e
        })
      }), _defineProperty(_assertThisInitialized(n), "monitorTypeChange", function(e) {
        n.props.typeChange(e)
      }), n.state = {
        mockData: [],
        targetKeys: []
      }, n
    }
    return _createClass(r, [{
      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,
          r = e.targetKeys,
          n = this.props,
          a = n.monitorRuleInfo,
          o = n.isCycle,
          i = a.name,
          l = a.monitorType,
          e = a.period,
          n = a.remark,
          a = this.props.form.getFieldProps,
          i = a("name", {
            initialValue: i,
            rules: [{
              required: !0,
              message: "任务名称不能为空！"
            }, {
              validator: noSpaceLengthValidate(50)
            }, {
              validator: nameIsReplaceValidate(i)
            }]
          }),
          l = a("monitorType", {
            initialValue: l ? "".concat(l) : "2",
            rules: [{
              required: !0,
              message: "请选择监测方式！"
            }]
          }),
          e = a("period", {
            initialValue: e ? "".concat(e) : "30",
            rules: []
          }),
          a = a("remark", {
            initialValue: n,
            rules: [{
              validator: noSpaceLengthValidate(500)
            }]
          }),
          n = {
            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, _extends({
          hasFeedback: !0,
          label: "任务名称"
        }, n), _react.default.createElement(_amosAntd.Input, _extends({
          className: "amos-grid-form-input"
        }, i, {
          placeholder: "请输入任务名称",
          style: {
            width: "250px"
          }
        }))))), _react.default.createElement(_amosAntd.Row, {
          style: {
            marginTop: "20px"
          }
        }, _react.default.createElement(_amosAntd.Col, {
          span: 24
        }, _react.default.createElement(FormItem, _extends({
          hasFeedback: !0,
          label: "监测方式"
        }, n), _react.default.createElement(_amosAntd.Select, _extends({
          style: {
            width: 250
          },
          onSelect: this.monitorTypeChange
        }, l, {
          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, _extends({
          label: "任务周期"
        }, n), _react.default.createElement(_amosAntd.Select, _extends({
          style: {
            width: 250
          }
        }, e, {
          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, _extends({
          label: "描述"
        }, n, {
          placeholder: "请输入描述"
        }), _react.default.createElement(_amosAntd.Input, _extends({
          className: "amos-grid-form-input",
          width: 250
        }, a))))))), _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: r,
          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"
          }
        }, "取消")))
      }
    }]), r
  }();
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, module.exports = exports.default;
