"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")), _rayEventpool = require("ray-eventpool"), _dt2react = require("dt2react"), _amosFramework = require("amos-framework"), _urlConsts = require("../../../consts/urlConsts"), _processor = require("../../../utils/processor");

require("./addDevicePanel.scss");

var _AddDevicePanelHead = _interopRequireDefault(require("./AddDevicePanelHead")), _AddDevicePanelBody = _interopRequireDefault(require("./AddDevicePanelBody")), _InterfacePropertyPanel = _interopRequireDefault(require("./propertyPanel/InterfacePropertyPanel")), _QuotaPropertyPanel = _interopRequireDefault(require("./propertyPanel/QuotaPropertyPanel")), _ActionConf = require("./../common/ActionConf"), _deviceService = require("./../../../services/deviceService"), _OnlineTest = _interopRequireDefault(require("./../onlineTestManger/OnlineTest"));

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

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

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

function _createSuper(e) {
  var t = _isNativeReflectConstruct();
  return function() {
    var a, r = (0, _getPrototypeOf2.default)(e);
    if (t) {
      var i = (0, _getPrototypeOf2.default)(this).constructor;
      a = Reflect.construct(r, arguments, i);
    } 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;
  }
}

var AddDevicePanel = 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), "getConfigPanelByType", (function() {
      var e = r.state, t = e.configType, a = e.currentId, i = e.intfc, n = e.capacity, u = e.onlinTestSwich, l = e.intfcList, o = e.quotaList, s = [];
      return 1 === t ? (r.openPanel(), s.push(_react.default.createElement(_InterfacePropertyPanel.default, (0, 
      _extends2.default)({
        key: "interface",
        updateInterfacePropertyCell: r.updateInterfacePropertyCell,
        id: a,
        intfc: i,
        getImgPath: r.getImgPath
      }, r.props)))) : 2 === t ? (r.openPanel(), s.push(_react.default.createElement(_QuotaPropertyPanel.default, (0, 
      _extends2.default)({
        key: "quota",
        updateQuotaPropertyCell: r.updateQuotaPropertyCell,
        updateQuotaTaskCell: r.updateQuotaTaskCell,
        updateInterfacePropertyCell: r.updateInterfacePropertyCell,
        updateCapacityThresholdInfo: r.updateCapacityThresholdInfo,
        id: a,
        capacity: n,
        device: r.props.device,
        getImgPath: r.getImgPath,
        taskParam: r.taskParam,
        eqpThreshold: r.eqpThreshold,
        quotaSelectedStatus: r.state.quotaSelectedStatus,
        resetStatus: r.resetStatus
      }, r.props)))) : 3 === t && (r.openPanel(), s.push(_react.default.createElement(_OnlineTest.default, {
        key: "test",
        onlinTestSwich: u,
        intfcList: l,
        quotaList: o,
        quotaSelected: r.quotaSelected,
        saveEquipmnet: r.saveEquipmnet,
        exprotButton: r.state.exprotButton
      }))), s;
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(r), "getImgPath", (function(e, t) {
      var a;
      return a = t.includes(e) ? "rotate-negative-ninety" : "rotate-ninety", _react.default.createElement("img", {
        src: require("./../../../assets/iot/deviceManage/arrow.png"),
        alt: "",
        className: a
      });
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(r), "openPanel", (function() {
      var e = r.mainContent;
      e && (0, _dt2react.addStyle)(e, "width", "calc(100% - 300px)");
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(r), "closePanel", (function() {
      var e = r.mainContent;
      e && (0, _dt2react.addStyle)(e, "width", "100%");
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(r), "updateInterfaceCellStatus", (function(e, t) {
      var a = r.interfaceCellStatus;
      a[e] = t, r.interfaceCellStatus = a;
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(r), "updateQuotaCellStatus", (function(e, t) {
      var a = r.quotaCellStatus;
      a[e] = t, r.quotaCellStatus = a;
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(r), "updateQuotaPropertyCell", (function(e, t) {
      var a = r.quotaSelected;
      a[e] = t, r.quotaSelected = a;
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(r), "updateInterfacePropertyCell", (function(e, t) {
      var a = r.interfacePropertys;
      a[e] = t, r.interfacePropertys = a;
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(r), "updateDevicePropertyCell", (function(e) {
      r.equipmentProperty = e;
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(r), "updateBasicInfo", (function(e) {
      r.basicInfo = e;
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(r), "updateQuotaTaskCell", (function(e, t) {
      var a = r.taskParam;
      a[e] = t, r.taskParam = a;
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(r), "updateCapacityThresholdInfo", (function(e, t) {
      var a = r.eqpThreshold;
      a[e] = t, r.eqpThreshold = a;
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(r), "setImgFile", (function(e) {
      r.imgFile = e;
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(r), "saveEquipmentAction", (function() {
      var e = r.basicInfo;
      if (void 0 !== e.name && "" !== e.name.trim()) {
        var t = new FileReader;
        t.addEventListener("load", (function() {
          return r.upLoadImgToSave(t.result);
        }));
        var a = r.imgFile;
        if ("{}" === JSON.stringify(a)) {
          r.saveEquipmnet({
            binaryImg: null,
            imgName: null
          });
        } else t.readAsBinaryString(r.imgFile);
      } else _amosFramework.message.danger("请输入设备名称");
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(r), "upLoadImgToSave", (function(e) {
      var t = {
        binaryImg: e,
        imgName: r.imgFile.name
      };
      r.saveEquipmnet(t);
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(r), "getEnableIntfcIds", (function() {
      var e = [], t = r.interfaceCellStatus;
      for (var a in r.interfaceCellStatus) t[a] && e.push(a);
      return e;
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(r), "getEnableCapacityfcIds", (function() {
      var e = [], t = r.quotaCellStatus;
      for (var a in r.quotaCellStatus) t[a] && e.push(a);
      return e;
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(r), "getEnableAttriSourceId", (function() {
      var e;
      return r.props.device.attributes.map((function(t) {
        var a = t.name;
        if (a && "sourceId" === a) return e = r.equipmentProperty["equip_".concat(t.id)];
      })), e;
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(r), "saveEquipmnet", (function(e, t) {
      console.table(r.interfaceCellStatus), console.table(r.quotaCellStatus), console.table(r.interfacePropertys), 
      console.table(r.quotaSelected), console.table(r.equipmentProperty), console.table(r.basicInfo), 
      console.table(r.eqpThreshold);
      var a = r.basicInfo, i = r.taskParam, n = r.eqpThreshold;
      (0, _extends2.default)(a, e);
      var u = r.props.equipment;
      u && u.id && (0, _extends2.default)(a, {
        id: u.id
      });
      var l = {
        propertyMap: _objectSpread(_objectSpread({
          sid: r.getEnableAttriSourceId(),
          selIntfcIds: r.getEnableIntfcIds(),
          selCpblIds: r.getEnableCapacityfcIds(),
          quotaSelected: r.quotaSelected
        }, r.interfacePropertys), r.equipmentProperty),
        basicInfo: a,
        taskParam: i,
        threshold: n
      };
      t ? r.todoTest(l, t) : (0, _deviceService.addEditEquipmentAction)(l).then((function(e) {
        if (null === e) _amosFramework.message.danger("设备名称重复"); else {
          _amosFramework.message.success("保存成功"), r.imgFile = {};
          var t = r.props;
          (0, t.updateMainView)({
            flag: "3",
            device: t.device,
            resourceFlag: t.resourceFlag,
            equipment: e,
            pageConfig: t.pageConfig
          });
        }
      }));
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(r), "todoTest", (function(e, t) {
      var a = "".concat(_urlConsts.OltSerUrl.ToDoDeviceTest, "/").concat(t);
      _processor.AmosFetch.post(a, {
        data: e
      }).then((function(e) {
        if (e.ok) return e.json();
        throw new Error("从服务端获取数据失败！");
      })).then((function(e) {
        "SUCCESS" === e.result ? console.log(e) : _amosFramework.message.info(e.message);
      }));
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(r), "fetchAllTemplatOrEqpQuotaProperty", (function(e, t) {
      (0, _deviceService.fetchAllTemplatOrEqpQuotaPropertyAction)(e, t).then((function(e) {
        if (null !== e) {
          var t = e.threshold, a = e.property, i = {};
          a.map((function(e) {
            var t = e.cpblId, a = i[t];
            void 0 === a && (a = []), a.push(e.id), i[t] = a;
          })), r.quotaSelected = i, t.map((function(e) {
            var t = "".concat(e.cpblId, "_").concat(e.cpblPropertyId), a = {
              upper: e.maxValue,
              lower: e.minValue
            }, i = r.eqpThreshold;
            i[t] = a, r.eqpThreshold = i;
          }));
        }
      }));
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(r), "resetStatus", (function() {
      r.setState({
        quotaSelectedStatus: void 0
      });
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(r), "changeRefreshCellStatus", (function() {
      var e = r.statusCount;
      e < 2 * r.statusSum - 1 ? (e += 1, r.statusCount = e) : (r.setState({
        refreshCellStatus: !r.state.refreshCellStatus
      }), r.statusCount = 0);
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(r), "setSumValue", (function(e) {
      r.statusSum = e;
    })), r.interfaceCellStatus = {}, r.quotaCellStatus = {}, r.interfacePropertys = {}, 
    r.quotaSelected = {}, r.equipmentProperty = {}, r.basicInfo = {}, r.taskParam = {}, 
    r.eqpThreshold = {}, r.imgFile = {}, r.statusCount = 0, r.statusSum = 0, r.state = {
      configType: void 0,
      currentId: void 0,
      quotaSelectedStatus: void 0,
      refreshCellStatus: !1,
      exprotButton: "none"
    }, r;
  }
  return (0, _createClass2.default)(a, [ {
    key: "componentWillMount",
    value: function() {}
  }, {
    key: "componentDidMount",
    value: function() {
      var e = this;
      this.pubsub_open = _rayEventpool.PubSub.subscribe(_ActionConf.CONFIG_PANEL_SHOW_TOPIC.configPanelShow, (function(t, a) {
        var r = a.type, i = a.id, n = a.intfc, u = a.capacity, l = a.onlinTestSwich, o = a.intfcList, s = a.quotaList, c = a.quotaSelected, d = a.status;
        if (3 === r) {
          var p = e.props, f = p.equipment, _ = p.device;
          e.fetchAllTemplatOrEqpQuotaProperty(_.id, f ? f.id : 0), e.setState({
            refreshCellStatus: !0
          });
        }
        e.setState({
          configType: r,
          currentId: i,
          intfc: n,
          capacity: u,
          onlinTestSwich: l,
          intfcList: o,
          quotaList: s,
          quotaSelected: c,
          quotaSelectedStatus: d
        });
      })), this.pubsub_close = _rayEventpool.PubSub.subscribe(_ActionConf.CONFIG_PANEL_HIDE_TOPIC.configPanelHide, (function(t, a) {
        a.type, a.id;
        e.closePanel();
      }));
      var t = this.props, a = t.equipment, r = t.device;
      this.fetchAllTemplatOrEqpQuotaProperty(r.id, a ? a.id : 0);
    }
  }, {
    key: "componentWillReceiveProps",
    value: function(e) {
      var t = this.props, a = t.equipment;
      (t.device.id !== e.device.id || a && e.equipment && a.id !== e.equipment.id) && this.fetchAllTemplatOrEqpQuotaProperty(e.device.id, e.equipment ? e.equipment.id : 0);
    }
  }, {
    key: "componentWillUnmount",
    value: function() {
      _rayEventpool.PubSub.unsubscribe(this.pubsub_open), _rayEventpool.PubSub.unsubscribe(this.pubsub_close);
    }
  }, {
    key: "render",
    value: function() {
      var e = this;
      return _react.default.createElement("div", {
        className: "add-device-panel-root"
      }, _react.default.createElement("div", {
        className: "add-device-panel",
        ref: function(t) {
          return e.mainContent = t;
        }
      }, _react.default.createElement("div", {
        className: "add-device-panel-top"
      }, _react.default.createElement(_AddDevicePanelHead.default, (0, _extends2.default)({
        saveEquipmentAction: this.saveEquipmentAction
      }, this.props))), _react.default.createElement("div", {
        className: "add-device-panel-body"
      }, _react.default.createElement(_AddDevicePanelBody.default, (0, _extends2.default)({
        updateInterfaceCellStatus: this.updateInterfaceCellStatus,
        updateQuotaCellStatus: this.updateQuotaCellStatus,
        updateDevicePropertyCell: this.updateDevicePropertyCell,
        updateBasicInfo: this.updateBasicInfo,
        setImgFile: this.setImgFile,
        interfaceStatus: this.interfaceCellStatus,
        quotaCellStatus: this.quotaCellStatus,
        quotaSelected: this.quotaSelected,
        updateQuotaTaskCell: this.updateQuotaTaskCell,
        refreshCellStatus: this.state.refreshCellStatus,
        changeRefreshCellStatus: this.changeRefreshCellStatus,
        saveEquipmnet: this.saveEquipmentAction,
        setSumValue: this.setSumValue
      }, this.props)))), _react.default.createElement("div", {
        className: "add-device-config-panel"
      }, this.getConfigPanelByType()));
    }
  } ]), a;
}(_react.Component);

AddDevicePanel.displayName = "AddDevicePanel", AddDevicePanel.propTypes = {
  updateMainView: _propTypes.default.func,
  device: _propTypes.default.object,
  resourceFlag: _propTypes.default.string,
  equipment: _propTypes.default.object,
  enable: _propTypes.default.bool,
  pageConfig: _propTypes.default.object
}, AddDevicePanel.defaultProps = {
  enable: !0
};

var _default = AddDevicePanel;

exports.default = _default;