"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")), _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray")), _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"), _amosFramework = require("amos-framework"), _amosTool = require("amos-tool"), _processor = require("../../../../utils/processor"), _urlConsts = require("../../../../consts/urlConsts"), _AddCapacityPropertyCard = _interopRequireDefault(require("./AddCapacityPropertyCard")), _DeviceEnum = require("./../../common/DeviceEnum"), _DeviceLang = require("./../../common/DeviceLang"), _TaskEnum = require("./../../../createEquipment/common/TaskEnum"), _validate = require("./../../../common/utils/validate");

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

function _createForOfIteratorHelper(e, t) {
  var r = "undefined" != typeof Symbol && e[Symbol.iterator] || e["@@iterator"];
  if (!r) {
    if (Array.isArray(e) || (r = _unsupportedIterableToArray(e)) || t && e && "number" == typeof e.length) {
      r && (e = r);
      var a = 0, o = function() {};
      return {
        s: o,
        n: function() {
          return a >= e.length ? {
            done: !0
          } : {
            done: !1,
            value: e[a++]
          };
        },
        e: function(e) {
          throw e;
        },
        f: o
      };
    }
    throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
  }
  var n, i = !0, l = !1;
  return {
    s: function() {
      r = r.call(e);
    },
    n: function() {
      var e = r.next();
      return i = e.done, e;
    },
    e: function(e) {
      l = !0, n = e;
    },
    f: function() {
      try {
        i || null == r.return || r.return();
      } finally {
        if (l) throw n;
      }
    }
  };
}

function _unsupportedIterableToArray(e, t) {
  if (e) {
    if ("string" == typeof e) return _arrayLikeToArray(e, t);
    var r = Object.prototype.toString.call(e).slice(8, -1);
    return "Object" === r && e.constructor && (r = e.constructor.name), "Map" === r || "Set" === r ? Array.from(e) : "Arguments" === r || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r) ? _arrayLikeToArray(e, t) : void 0;
  }
}

function _arrayLikeToArray(e, t) {
  (null == t || t > e.length) && (t = e.length);
  for (var r = 0, a = new Array(t); r < t; r++) a[r] = e[r];
  return 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 Option = _amosFramework.Select.Option, RadioGroup = _amosAntd.Radio.Group, FormItem = _amosAntd.Form.Item, formItemLayout = {
  labelCol: {
    xs: {
      span: 24
    },
    sm: {
      span: 5
    }
  },
  wrapperCol: {
    xs: {
      span: 24
    },
    sm: {
      span: 16
    }
  }
}, fieldNames = [ "name", "type", "intfcIds", "protocolId", "descr", "monitortype", "mongodb" ], AddEditDeviceCapacity = function(e) {
  (0, _inherits2.default)(r, e);
  var t = _createSuper(r);
  function r(e) {
    var a;
    return (0, _classCallCheck2.default)(this, r), a = t.call(this, e), (0, _defineProperty2.default)((0, 
    _assertThisInitialized2.default)(a), "handleSubmit", (function(e) {
      e.preventDefault();
      var t = [];
      a.monitorDevice && t.push("period");
      var r = [].concat(fieldNames, t);
      a.props.form.validateFields(r, {}, (function(e, t) {
        e || a.handleSave(t);
      }));
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(a), "handleSave", (function(e) {
      var t = a.props, r = t.interfaceMap, o = t.listInfo, n = e.intfcIds;
      _amosTool.utils.isNil(n) || _amosTool.utils.isArray(n) || (e.intfcIds = [ n ]);
      var i, l = (new Date).getTime(), u = _objectSpread(_objectSpread({}, e), {}, {
        capacityPropertyParams: a.state.capacityProperty,
        key: l
      });
      _amosTool.utils.isEmpty(u) || _amosTool.utils.isEmpty(u.intfcIds) || (i = u.intfcIds.map((function(e) {
        return r[parseInt(e.split("-")[0])];
      })));
      var c = [ _objectSpread(_objectSpread({}, u), {}, {
        intfcDisplayName: i
      }) ], s = a.props.deviceId;
      if (_amosTool.utils.isNil(s)) a.props.addDeviceCapacity(u), a.props.autoCardCalculate(c), 
      a.props.form.resetFields(), a.props.callBack(); else {
        var d = o.id ? _objectSpread(_objectSpread({}, u), {}, {
          id: o.id
        }) : u;
        a.saveDeviceCapacity(d);
      }
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(a), "handleCancel", (function(e) {
      a.props.form.resetFields(), a.props.callBack();
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(a), "addCapacityProperty", (function(e) {
      var t = a.state.capacityProperty;
      t = [].concat((0, _toConsumableArray2.default)(t), [ e ]), a.setState({
        capacityProperty: t
      });
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(a), "deleteCellCapacityProperty", (function(e) {
      var t, r = [], o = _createForOfIteratorHelper(a.state.capacityProperty);
      try {
        for (o.s(); !(t = o.n()).done; ) {
          var n = t.value;
          n.key !== e && r.push(n);
        }
      } catch (e) {
        o.e(e);
      } finally {
        o.f();
      }
      a.setState({
        capacityProperty: r
      });
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(a), "saveDeviceCapacity", (function(e) {
      (0, _extends2.default)(e, {
        equipmentId: a.props.deviceId
      }), _processor.AmosFetch.post(_urlConsts.OltSerUrl.deviceCapacityAdd, {
        data: e
      }).then((function(e) {
        if (e.ok) return e.json();
        throw new Error("从服务端获取数据失败！");
      })).then((function(e) {
        "SUCCESS" === e.result ? _amosFramework.message.success("操作成功！") : _amosFramework.message.danger("操作失败！"), 
        a.props.getDeviceInfoById(a.props.deviceId), a.props.form.resetFields(), a.props.callBack();
      }));
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(a), "changeMonitorType", (function(e) {
      a.monitorDevice = 1 === e;
    })), a.state = {
      capacityProperty: []
    }, a;
  }
  return (0, _createClass2.default)(r, [ {
    key: "componentDidMount",
    value: function() {}
  }, {
    key: "render",
    value: function() {
      var e = this.props.form.getFieldProps, t = this.props, r = t.protocolOption, a = t.interfaceOption, o = t.deviceId, n = t.listInfo, i = t.protocolId, l = t.selectedInterfaceList, u = n.name, c = n.type, s = n.descr, d = n.monitorType, p = n.monitorPeriod, f = n.mongodb;
      i || (i = "");
      var m = e("name", {
        initialValue: u,
        rules: [ {
          required: !0,
          min: 1,
          max: 20,
          message: "支持1-20位非空格字符！"
        }, {
          validator: (0, _validate.noSpaceLengthValidate)(20, !0, "名称不能为空！")
        } ]
      }), y = e("type", {
        initialValue: "".concat(c),
        rules: [ {
          required: !0,
          message: "分类不能为空！"
        } ]
      }), _ = e("intfcIds", {
        initialValue: l,
        rules: [ {
          required: !0,
          message: "设备接口不能为空！"
        } ]
      }), h = e("protocolId", {
        initialValue: "".concat(i),
        rules: [ {
          required: !0,
          message: "通讯协议不能为空！"
        } ]
      }), b = e("descr", {
        initialValue: s,
        rules: [ {
          required: !1,
          message: "描述不能为空！"
        } ]
      }), v = e("mongodb", {
        initialValue: f
      }), I = e("monitortype", {
        initialValue: d,
        rules: [ {
          required: !0,
          message: "监测方式不能为空！"
        } ]
      }), C = e("period", {
        initialValue: p,
        rules: [ {
          required: !0,
          message: "采集周期不能为空！"
        } ]
      }), g = [];
      void 0 === o && g.push(_react.default.createElement(FormItem, (0, _extends2.default)({
        label: "属性"
      }, formItemLayout), _react.default.createElement(_AddCapacityPropertyCard.default, {
        repuestType: "addProperty",
        addCapacityProperty: this.addCapacityProperty,
        deleteCellCapacityProperty: this.deleteCellCapacityProperty
      })));
      var E = (0, _DeviceLang.enums2array)(_DeviceEnum.capacityClassifyEnum).map((function(e) {
        return _react.default.createElement(_amosAntd.Radio, {
          key: e.key,
          value: "".concat(e.key)
        }, e.label);
      }));
      return _react.default.createElement("div", {
        className: "device-capacity-add-modal"
      }, _react.default.createElement(_amosAntd.Form, {
        horizontal: !0,
        onSubmit: this.handleSubmit
      }, _react.default.createElement(FormItem, (0, _extends2.default)({}, formItemLayout, {
        hasFeedback: !0,
        label: "名称: "
      }), _react.default.createElement(_amosAntd.Input, (0, _extends2.default)({}, m, {
        placeholder: "名称（支持1-20位非空格字符）"
      }))), _react.default.createElement(FormItem, (0, _extends2.default)({}, formItemLayout, {
        hasFeedback: !0,
        label: "分类: "
      }), _react.default.createElement(RadioGroup, (0, _extends2.default)({
        placeholder: "分类"
      }, y), E)), _react.default.createElement(FormItem, (0, _extends2.default)({}, formItemLayout, {
        hasFeedback: !0,
        label: "设备接口: "
      }), _react.default.createElement(_amosFramework.Select, (0, _extends2.default)({
        style: {
          width: "100%",
          height: "32px"
        },
        placeholder: "设备接口"
      }, _), a)), _react.default.createElement(FormItem, (0, _extends2.default)({}, formItemLayout, {
        hasFeedback: !0,
        label: "协议: "
      }), _react.default.createElement(_amosFramework.Select, (0, _extends2.default)({
        style: {
          width: "100%",
          height: "32px"
        },
        placeholder: "协议"
      }, h), r)), _react.default.createElement(FormItem, (0, _extends2.default)({}, formItemLayout, {
        hasFeedback: !0,
        label: "描述: "
      }), _react.default.createElement(_amosAntd.Input, b)), _react.default.createElement(FormItem, (0, 
      _extends2.default)({}, formItemLayout, {
        hasFeedback: !0,
        label: "监测方式: "
      }), _react.default.createElement(_amosFramework.Select, (0, _extends2.default)({
        style: {
          width: "100%",
          height: "32px"
        },
        onSelect: this.changeMonitorType,
        placeholder: "监测方式"
      }, I), _TaskEnum.monitorTypeArr.map((function(e) {
        return _react.default.createElement(Option, {
          key: e.value,
          value: e.value
        }, e.title);
      })))), this.monitorDevice ? _react.default.createElement(FormItem, (0, _extends2.default)({}, formItemLayout, {
        hasFeedback: !0,
        label: "采集周期: "
      }), _react.default.createElement(_amosFramework.Select, (0, _extends2.default)({
        style: {
          width: "100%",
          height: "32px"
        },
        placeholder: "采集周期"
      }, C), _TaskEnum.periodArr.map((function(e) {
        return _react.default.createElement(Option, {
          key: e.value,
          value: e.value
        }, e.title);
      })))) : "", g, _react.default.createElement(FormItem, (0, _extends2.default)({}, formItemLayout, {
        hasFeedback: !0,
        label: "MongoDB: "
      }), _react.default.createElement(_amosAntd.Input, v)), _react.default.createElement(FormItem, {
        wrapperCol: {
          xs: {
            span: 24,
            offset: 0
          },
          sm: {
            span: 24,
            offset: 0
          }
        }
      }, _react.default.createElement(_amosAntd.Row, null, _react.default.createElement(_amosAntd.Col, {
        span: 3,
        offset: 14
      }, _react.default.createElement(_amosAntd.Button, {
        type: "primary",
        htmlType: "submit"
      }, "提交")), _react.default.createElement(_amosAntd.Col, {
        span: 3,
        offset: 1
      }, _react.default.createElement(_amosAntd.Button, {
        onClick: this.handleCancel
      }, "取消"))))));
    }
  } ]), r;
}(_react.Component);

AddEditDeviceCapacity.displayName = "AddEditDeviceCapacity", AddEditDeviceCapacity.propTypes = {
  form: _propTypes.default.any,
  callBack: _propTypes.default.func,
  addDeviceCapacity: _propTypes.default.func,
  autoCardCalculate: _propTypes.default.func,
  protocolOption: _propTypes.default.array,
  interfaceOption: _propTypes.default.array,
  interfaceMap: _propTypes.default.object,
  deviceId: _propTypes.default.number,
  getDeviceInfoById: _propTypes.default.func,
  listInfo: _propTypes.default.any,
  protocolId: _propTypes.default.number,
  selectedInterfaceList: _propTypes.default.array,
  capacityId: _propTypes.default.number,
  updateNewInfo: _propTypes.default.func
}, AddEditDeviceCapacity.defaultProps = {
  listInfo: {
    name: "",
    type: "1",
    descr: ""
  }
};

var _default = AddEditDeviceCapacity;

exports.default = _default;