"use strict";
Object.defineProperty(exports, "__esModule", {
  value: !0
}), exports.default = void 0;
var _react = _interopRequireWildcard(require("react")),
  _propTypes = _interopRequireDefault(require("prop-types")),
  _rayEventpool = require("ray-eventpool"),
  _urlConsts = require("../../../consts/urlConsts"),
  _processor = require("../../../utils/processor"),
  _dt2react = _interopRequireDefault(require("dt2react"));
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")),
  _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, a, n = {},
    i = Object.defineProperty && Object.getOwnPropertyDescriptor;
  for (r in e) Object.prototype.hasOwnProperty.call(e, r) && ((a = i ? Object.getOwnPropertyDescriptor(e, r) : null) && (a.get || a.set) ? Object.defineProperty(n, r, a) : n[r] = e[r]);
  return n.default = e, t && t.set(e, n), n
}

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 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 _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 _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 AddDevicePanel = function() {
  _inherits(r, _react.Component);
  var t = _createSuper(r);

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