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

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, a = arguments[t];
      for (r in a) Object.prototype.hasOwnProperty.call(a, r) && (e[r] = a[r])
    }
    return e
  }).apply(this, arguments)
}

function _createForOfIteratorHelper(e, t) {
  var r;
  if ("undefined" == typeof Symbol || null == e[Symbol.iterator]) {
    if (Array.isArray(e) || (r = _unsupportedIterableToArray(e)) || t && e && "number" == typeof e.length) {
      r && (e = r);
      var a = 0,
        t = function() {};
      return {
        s: t,
        n: function() {
          return a >= e.length ? {
            done: !0
          } : {
            done: !1,
            value: e[a++]
          }
        },
        e: function(e) {
          throw e
        },
        f: t
      }
    }
    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 o, n = !0,
    i = !1;
  return {
    s: function() {
      r = e[Symbol.iterator]()
    },
    n: function() {
      var e = r.next();
      return n = e.done, e
    },
    e: function(e) {
      i = !0, o = e
    },
    f: function() {
      try {
        n || null == r.return || r.return()
      } finally {
        if (i) throw o
      }
    }
  }
}

function _toConsumableArray(e) {
  return _arrayWithoutHoles(e) || _iterableToArray(e) || _unsupportedIterableToArray(e) || _nonIterableSpread()
}

function _nonIterableSpread() {
  throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")
}

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 _iterableToArray(e) {
  if ("undefined" != typeof Symbol && Symbol.iterator in Object(e)) return Array.from(e)
}

function _arrayWithoutHoles(e) {
  if (Array.isArray(e)) return _arrayLikeToArray(e)
}

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

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 _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 a = t[r];
    a.enumerable = a.enumerable || !1, a.configurable = !0, "value" in a && (a.writable = !0), Object.defineProperty(e, a.key, a)
  }
}

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 a = _isNativeReflectConstruct();
  return function() {
    var e, t = _getPrototypeOf(r);
    return _possibleConstructorReturn(this, a ? (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 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() {
    _inherits(r, _react.Component);
    var t = _createSuper(r);

    function r(e) {
      var n;
      return _classCallCheck(this, r), _defineProperty(_assertThisInitialized(n = t.call(this, e)), "handleSubmit", function(e) {
        e.preventDefault();
        e = [];
        n.monitorDevice && e.push("period");
        e = [].concat(fieldNames, e);
        n.props.form.validateFields(e, {}, function(e, t) {
          e || n.handleSave(t)
        })
      }), _defineProperty(_assertThisInitialized(n), "handleSave", function(e) {
        var t = n.props,
          r = t.interfaceMap,
          a = t.listInfo,
          t = e.intfcIds;
        _amosTool.utils.isNil(t) || _amosTool.utils.isArray(t) || (e.intfcIds = [t]);
        t = (new Date).getTime(), e = _objectSpread(_objectSpread({}, e), {}, {
          capacityPropertyParams: n.state.capacityProperty,
          key: t
        });
        _amosTool.utils.isEmpty(e) || _amosTool.utils.isEmpty(e.intfcIds) || (o = e.intfcIds.map(function(e) {
          return r[parseInt(e.split("-")[0])]
        }));
        var t = [_objectSpread(_objectSpread({}, e), {}, {
            intfcDisplayName: o
          })],
          o = n.props.deviceId;
        _amosTool.utils.isNil(o) ? (n.props.addDeviceCapacity(e), n.props.autoCardCalculate(t), n.props.form.resetFields(), n.props.callBack()) : (e = a.id ? _objectSpread(_objectSpread({}, e), {}, {
          id: a.id
        }) : e, n.saveDeviceCapacity(e))
      }), _defineProperty(_assertThisInitialized(n), "handleCancel", function(e) {
        n.props.form.resetFields(), n.props.callBack()
      }), _defineProperty(_assertThisInitialized(n), "addCapacityProperty", function(e) {
        var t = n.state.capacityProperty,
          t = [].concat(_toConsumableArray(t), [e]);
        n.setState({
          capacityProperty: t
        })
      }), _defineProperty(_assertThisInitialized(n), "deleteCellCapacityProperty", function(e) {
        var t = [],
          r = _createForOfIteratorHelper(n.state.capacityProperty);
        try {
          for (r.s(); !(a = r.n()).done;) {
            var a = a.value;
            a.key !== e && t.push(a)
          }
        } catch (e) {
          r.e(e)
        } finally {
          r.f()
        }
        n.setState({
          capacityProperty: t
        })
      }), _defineProperty(_assertThisInitialized(n), "saveDeviceCapacity", function(e) {
        _extends(e, {
          equipmentId: n.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("操作失败！"), n.props.getDeviceInfoById(n.props.deviceId), n.props.form.resetFields(), n.props.callBack()
        })
      }), _defineProperty(_assertThisInitialized(n), "changeMonitorType", function(e) {
        n.monitorDevice = 1 === e
      }), n.state = {
        capacityProperty: []
      }, n
    }
    return _createClass(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,
          c = t.selectedInterfaceList,
          l = n.name,
          s = n.type,
          u = n.descr,
          p = n.monitorType,
          t = n.monitorPeriod,
          n = n.mongodb,
          i = i || "",
          l = e("name", {
            initialValue: l,
            rules: [{
              required: !0,
              min: 1,
              max: 20,
              message: "支持1-20位非空格字符！"
            }, {
              validator: (0, _validate.noSpaceLengthValidate)(20, !0, "名称不能为空！")
            }]
          }),
          s = e("type", {
            initialValue: "".concat(s),
            rules: [{
              required: !0,
              message: "分类不能为空！"
            }]
          }),
          c = e("intfcIds", {
            initialValue: c,
            rules: [{
              required: !0,
              message: "设备接口不能为空！"
            }]
          }),
          i = e("protocolId", {
            initialValue: "".concat(i),
            rules: [{
              required: !0,
              message: "通讯协议不能为空！"
            }]
          }),
          u = e("descr", {
            initialValue: u,
            rules: [{
              required: !1,
              message: "描述不能为空！"
            }]
          }),
          n = e("mongodb", {
            initialValue: n
          }),
          p = e("monitortype", {
            initialValue: p,
            rules: [{
              required: !0,
              message: "监测方式不能为空！"
            }]
          }),
          e = e("period", {
            initialValue: t,
            rules: [{
              required: !0,
              message: "采集周期不能为空！"
            }]
          }),
          t = [];
        void 0 === o && t.push(_react.default.createElement(FormItem, _extends({
          label: "属性"
        }, formItemLayout), _react.default.createElement(_AddCapacityPropertyCard.default, {
          repuestType: "addProperty",
          addCapacityProperty: this.addCapacityProperty,
          deleteCellCapacityProperty: this.deleteCellCapacityProperty
        })));
        o = (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, _extends({}, formItemLayout, {
          hasFeedback: !0,
          label: "名称: "
        }), _react.default.createElement(_amosAntd.Input, _extends({}, l, {
          placeholder: "名称（支持1-20位非空格字符）"
        }))), _react.default.createElement(FormItem, _extends({}, formItemLayout, {
          hasFeedback: !0,
          label: "分类: "
        }), _react.default.createElement(RadioGroup, _extends({
          placeholder: "分类"
        }, s), o)), _react.default.createElement(FormItem, _extends({}, formItemLayout, {
          hasFeedback: !0,
          label: "设备接口: "
        }), _react.default.createElement(_amosFramework.Select, _extends({
          style: {
            width: "100%",
            height: "32px"
          },
          placeholder: "设备接口"
        }, c), a)), _react.default.createElement(FormItem, _extends({}, formItemLayout, {
          hasFeedback: !0,
          label: "协议: "
        }), _react.default.createElement(_amosFramework.Select, _extends({
          style: {
            width: "100%",
            height: "32px"
          },
          placeholder: "协议"
        }, i), r)), _react.default.createElement(FormItem, _extends({}, formItemLayout, {
          hasFeedback: !0,
          label: "描述: "
        }), _react.default.createElement(_amosAntd.Input, u)), _react.default.createElement(FormItem, _extends({}, formItemLayout, {
          hasFeedback: !0,
          label: "监测方式: "
        }), _react.default.createElement(_amosFramework.Select, _extends({
          style: {
            width: "100%",
            height: "32px"
          },
          onSelect: this.changeMonitorType,
          placeholder: "监测方式"
        }, p), _TaskEnum.monitorTypeArr.map(function(e) {
          return _react.default.createElement(Option, {
            key: e.value,
            value: e.value
          }, e.title)
        }))), this.monitorDevice ? _react.default.createElement(FormItem, _extends({}, formItemLayout, {
          hasFeedback: !0,
          label: "采集周期: "
        }), _react.default.createElement(_amosFramework.Select, _extends({
          style: {
            width: "100%",
            height: "32px"
          },
          placeholder: "采集周期"
        }, e), _TaskEnum.periodArr.map(function(e) {
          return _react.default.createElement(Option, {
            key: e.value,
            value: e.value
          }, e.title)
        }))) : "", t, _react.default.createElement(FormItem, _extends({}, formItemLayout, {
          hasFeedback: !0,
          label: "MongoDB: "
        }), _react.default.createElement(_amosAntd.Input, n)), _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
  }();
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 = AddEditDeviceCapacity, module.exports = exports.default;
