"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 _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray")), _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")), _amosAntd = require("amos-antd"), _amosFramework = require("amos-framework"), _propTypes = _interopRequireDefault(require("prop-types")), _processor = require("../../../utils/processor"), _urlConsts = require("../../../consts/urlConsts"), _amosTool = require("amos-tool"), _AddDeviceProperty = _interopRequireDefault(require("./deviceAddEdit/AddDeviceProperty")), _DeviceEnum = require("./../common/DeviceEnum"), _validate = require("./../../common/utils/validate"), _DeviceLang = require("./../common/DeviceLang"), _UpLoadImg = _interopRequireDefault(require("./../common/UpLoadImg")), _AddDeviceInterfaceCard = _interopRequireDefault(require("./deviceAddEdit/AddDeviceInterfaceCard")), _AddDeviceCapacityCard = _interopRequireDefault(require("./deviceAddEdit/AddDeviceCapacityCard")), _CommonCell = _interopRequireDefault(require("./../common/CommonCell")), _AddBtn = _interopRequireDefault(require("./../common/AddBtn"));

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

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

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

function _arrayLikeToArray(e, t) {
  (null == t || t > e.length) && (t = e.length);
  for (var a = 0, r = new Array(t); a < t; a++) r[a] = e[a];
  return r;
}

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;
  }
}

require("./add-device.scss"), require("./../common/common.scss");

var createForm = _amosAntd.Form.create(), FormItem = _amosAntd.Form.Item, Option = _amosFramework.Select.Option, TextArea = _amosAntd.Input.TextArea, Panel = _amosAntd.Collapse.Panel, maxDescLength = 500, dataTypeOption = (0, 
_DeviceLang.enums2array)(_DeviceEnum.deviceCategoryEnum).map((function(e) {
  return _react.default.createElement(Option, {
    key: e.key,
    value: "".concat(e.key)
  }, e.label);
})), AddDevice = 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), "onClickDown", (function(e) {
      var t = r.state.activeKey;
      t = "".concat(parseInt(t) + 1), e.preventDefault(), r.props.form.validateFields((function(e, a) {
        e || (a = (0, _validate.objOfStrPropTrim)(a), r.setState({
          itemInfo: a,
          activeKey: t
        }));
      }));
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(r), "onClickUp", (function() {
      var e = r.state.activeKey;
      e = "".concat(parseInt(e) - 1), r.setState({
        activeKey: e
      });
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(r), "onSaveDevice", (function(e) {
      r.setState({
        waiting: !0
      });
      var t = r.props.form.getFieldValue("name"), a = r.props.form.getFieldValue("model"), i = r.props.form.getFieldValue("type"), n = r.props.form.getFieldValue("desc"), l = r.props.form.getFieldValue("viewPath"), o = r.props.form.getFieldValue("industryId");
      (0, _extends2.default)(r.state.device, {
        name: t,
        model: a,
        type: i,
        viewPath: l,
        desc: n,
        visibility: 1,
        industryId: o
      }, e), _processor.AmosFetch.post(_urlConsts.OltSerUrl.addDevice, {
        data: r.state.device
      }).then((function(e) {
        if (e.ok) return e.text();
        throw new Error("从服务端获取数据失败！");
      })).then((function(e) {
        var t = JSON.parse(e);
        "SUCCESS" === t.result ? 0 === t.dataList ? (r.setState({
          waiting: !1
        }), _amosFramework.message.danger("设备原型名称重复")) : (_amosFramework.message.success("保存成功"), 
        r.setState({
          device: {},
          imgFile: {}
        }), r.props.updateAddView("2"), r.props.form.resetFields()) : (r.setState({
          waiting: !1
        }), _amosFramework.message.danger("保存失败"));
      }));
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(r), "setImgFile", (function(e) {
      r.setState({
        imgFile: e
      });
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(r), "autoCardCalculate", (function(e) {
      var t = r.state.interfaceProList;
      void 0 !== e && (t = [].concat((0, _toConsumableArray2.default)(t), (0, _toConsumableArray2.default)(e)), 
      r.setState({
        interfaceProList: t
      })), r.autoProView();
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(r), "autoProView", (function(e) {
      var t = void 0 === e ? r.state.interfaceProList : e, a = [];
      if (a.push(_react.default.createElement("div", {
        key: 0
      }, _react.default.createElement(_AddDeviceProperty.default, {
        repuestType: "addProperty",
        addDeviceProperty: r.addDeviceProperty,
        autoCardCalculate: r.autoCardCalculate
      }))), t.length > 0) {
        var i, n = _createForOfIteratorHelper(t);
        try {
          for (n.s(); !(i = n.n()).done; ) {
            var l = i.value, o = [];
            o.push(_react.default.createElement("p", null, "分  类：", (0, _DeviceLang.stringUtil)(_DeviceEnum.classifyEnum, parseInt(l.type)))), 
            o.push(_react.default.createElement("p", null, "数据类型：", (0, _DeviceLang.stringUtil)(_DeviceEnum.dataTypeEnum, parseInt(l.dataType)))), 
            a.push(_react.default.createElement(_CommonCell.default, {
              contentHtml: o,
              cardKey: l.key,
              value: l,
              deleteCellProperty: r.deleteCellProperty
            }));
          }
        } catch (e) {
          n.e(e);
        } finally {
          n.f();
        }
      }
      return a;
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(r), "addDeviceProperty", (function(e) {
      var t = r.state.device;
      t.equipmentPropertyParams = [].concat((0, _toConsumableArray2.default)(t.equipmentPropertyParams), [ e ]), 
      r.setState({
        device: t
      });
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(r), "saveImg", (function() {
      var e = new FileReader;
      e.addEventListener("load", (function() {
        return r.upLoadImgToSave(e.result);
      }));
      var t = r.state.imgFile;
      if ("{}" === JSON.stringify(t)) {
        r.onSaveDevice({
          binaryImg: null,
          imgName: "default.png"
        });
      } else e.readAsBinaryString(r.state.imgFile);
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(r), "addDeviceInterface", (function(e) {
      var t = r.state.device;
      t.equipmentInterfaceParams = [].concat((0, _toConsumableArray2.default)(t.equipmentInterfaceParams), [ e ]), 
      r.setState({
        device: t
      });
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(r), "addDeviceCapacity", (function(e) {
      var t = r.state.device;
      t.equipmentCapacityParams = [].concat((0, _toConsumableArray2.default)(t.equipmentCapacityParams), [ e ]), 
      r.setState({
        device: t
      });
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(r), "saveDeviceData", (function() {
      r.saveImg();
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(r), "upLoadImgToSave", (function(e) {
      var t = {
        binaryImg: e,
        imgName: r.state.imgFile.name
      };
      r.onSaveDevice(t);
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(r), "deleteCellProperty", (function(e) {
      var t, a = [], i = _createForOfIteratorHelper(r.state.interfaceProList);
      try {
        for (i.s(); !(t = i.n()).done; ) {
          var n = t.value;
          n.key !== e && a.push(n);
        }
      } catch (e) {
        i.e(e);
      } finally {
        i.f();
      }
      r.setState({
        interfaceProList: a
      });
      var l, o = r.state.device, d = [], u = _createForOfIteratorHelper(o.equipmentPropertyParams);
      try {
        for (u.s(); !(l = u.n()).done; ) {
          var c = l.value;
          c.key !== e && d.push(c);
        }
      } catch (e) {
        u.e(e);
      } finally {
        u.f();
      }
      o.equipmentPropertyParams = d, r.setState({
        device: o
      }), r.autoProView();
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(r), "deleteCellInterface", (function(e) {
      var t, a = r.state.device, i = [], n = _createForOfIteratorHelper(a.equipmentInterfaceParams);
      try {
        for (n.s(); !(t = n.n()).done; ) {
          var l = t.value;
          l.key !== e && i.push(l);
        }
      } catch (e) {
        n.e(e);
      } finally {
        n.f();
      }
      a.equipmentInterfaceParams = i, r.setState({
        device: a
      });
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(r), "deleteCellCapacity", (function(e) {
      var t, a = r.state.device, i = [], n = _createForOfIteratorHelper(a.equipmentCapacityParams);
      try {
        for (n.s(); !(t = n.n()).done; ) {
          var l = t.value;
          l.key !== e && i.push(l);
        }
      } catch (e) {
        n.e(e);
      } finally {
        n.f();
      }
      a.equipmentCapacityParams = i, r.setState({
        device: a
      });
    })), r.state = {
      activeKey: "1",
      interfaceProList: [],
      itemInfo: {},
      device: {
        equipmentPropertyParams: [],
        equipmentInterfaceParams: [],
        equipmentCapacityParams: []
      },
      imgFile: {},
      waiting: !1
    }, r;
  }
  return (0, _createClass2.default)(a, [ {
    key: "render",
    value: function() {
      var e = this.state.device, t = this.props.equiptemplateIndustryOptions ? this.props.equiptemplateIndustryOptions : [], a = this.props.form.getFieldProps, r = {
        labelCol: {
          span: 5
        },
        wrapperCol: {
          span: 18
        }
      }, i = a("name", {
        rules: [ {
          required: !0,
          min: 1,
          max: 20,
          message: "支持1-20位字符！"
        }, {
          validator: (0, _validate.noSpaceLengthValidate)(null, !0, "名称不能为空！")
        } ]
      }), n = a("model", {
        rules: [ {
          required: !0,
          message: "型号不能为空！"
        }, {
          validator: (0, _validate.noSpaceLengthValidate)(null, !0, "型号不能为空！")
        } ]
      }), l = a("type", {
        rules: [ {
          required: !0,
          message: "类别不能为空！"
        } ]
      }), o = a("industryId", {
        rules: [ {
          required: !0,
          message: "所属行业不能为空！"
        } ]
      }), d = (a("viewPath", {
        rules: []
      }), a("desc", {
        rules: [ {
          validator: (0, _validate.noSpaceLengthValidate)(maxDescLength, !1, null)
        } ]
      }));
      return _react.default.createElement("div", {
        className: "add-device"
      }, _react.default.createElement(_amosAntd.Collapse, {
        activeKey: this.state.activeKey,
        accordion: !0
      }, _react.default.createElement(Panel, {
        className: "device-info",
        header: "设备信息",
        key: "1",
        showArrow: !1
      }, _react.default.createElement("div", {
        style: {
          textAlign: "center"
        }
      }, _react.default.createElement(_amosAntd.Row, null, _react.default.createElement(_amosAntd.Col, {
        span: 9,
        offset: 4
      }, _react.default.createElement(FormItem, (0, _extends2.default)({
        hasFeedback: !0,
        label: "名称"
      }, r), _react.default.createElement(_amosAntd.Input, (0, _extends2.default)({
        className: "amos-grid-form-input"
      }, i, {
        placeholder: "支持1-20位字符"
      }))), _react.default.createElement(FormItem, (0, _extends2.default)({
        hasFeedback: !0,
        label: "型号"
      }, r), _react.default.createElement(_amosAntd.Input, (0, _extends2.default)({
        className: "amos-grid-form-input"
      }, n, {
        placeholder: "请输入型号"
      }))), _react.default.createElement(FormItem, (0, _extends2.default)({
        hasFeedback: !0,
        label: "类别"
      }, r), _react.default.createElement(_amosFramework.Select, (0, _extends2.default)({
        style: {
          width: "100%",
          textAlign: "left",
          height: "32px"
        }
      }, l, {
        placeholder: "请选择类别"
      }), dataTypeOption)), _react.default.createElement(FormItem, (0, _extends2.default)({
        hasFeedback: !0,
        label: "所属行业: "
      }, r), _react.default.createElement(_amosFramework.Select, (0, _extends2.default)({
        style: {
          width: "100%",
          textAlign: "left",
          height: "32px"
        }
      }, o, {
        placeholder: "请选择所属行业"
      }), t.map((function(e) {
        return _react.default.createElement(Option, {
          key: "".concat(e.value, "Option"),
          value: e.value
        }, e.label);
      })))), _react.default.createElement(FormItem, (0, _extends2.default)({
        hasFeedback: !0,
        label: "说明"
      }, r), _react.default.createElement(TextArea, (0, _extends2.default)({
        className: "amos-grid-form-input text-area-put"
      }, d, {
        placeholder: "请输入说明"
      })))), _react.default.createElement(_amosAntd.Col, {
        span: 10,
        offset: 1
      }, _react.default.createElement(FormItem, (0, _extends2.default)({
        label: "设备示意图"
      }, {
        labelCol: {
          span: 4
        },
        wrapperCol: {
          span: 10
        }
      }), _react.default.createElement(_UpLoadImg.default, {
        setImgFile: this.setImgFile
      })))), _react.default.createElement(_amosAntd.Row, null, _react.default.createElement(_AddBtn.default, {
        text: "下一步",
        onClick: this.onClickDown
      })))), _react.default.createElement(Panel, {
        className: "device-property",
        header: "新建设备属性",
        key: "2",
        showArrow: !1
      }, _react.default.createElement(_amosAntd.Row, null, this.autoProView()), _react.default.createElement("div", {
        style: {
          height: "3rem"
        }
      }), _react.default.createElement(_amosAntd.Row, {
        type: "flex",
        justify: "center"
      }, _react.default.createElement(_AddBtn.default, {
        text: "上一步",
        onClick: this.onClickUp
      }), _react.default.createElement("div", {
        style: {
          width: "1.5rem"
        }
      }), _react.default.createElement(_AddBtn.default, {
        text: "下一步",
        onClick: this.onClickDown
      }))), _react.default.createElement(Panel, {
        className: "device-interface",
        header: "新建设备接口",
        key: "3",
        showArrow: !1
      }, _react.default.createElement(_amosAntd.Row, null, _react.default.createElement(_AddDeviceInterfaceCard.default, {
        repuestType: "addProperty",
        deleteCellInterface: this.deleteCellInterface,
        addDeviceInterface: this.addDeviceInterface
      })), _react.default.createElement("div", {
        style: {
          height: "3rem"
        }
      }), _react.default.createElement(_amosAntd.Row, {
        type: "flex",
        justify: "center"
      }, _react.default.createElement(_AddBtn.default, {
        text: "上一步",
        onClick: this.onClickUp
      }), _react.default.createElement("div", {
        style: {
          width: "1.5rem"
        }
      }), _react.default.createElement(_AddBtn.default, {
        text: "下一步",
        onClick: this.onClickDown
      }))), _react.default.createElement(Panel, {
        className: "device-capacity",
        header: "新建数据采集模型",
        key: "4",
        showArrow: !1
      }, _react.default.createElement(_amosAntd.Row, null, _react.default.createElement(_AddDeviceCapacityCard.default, {
        deviceInterfaces: e.equipmentInterfaceParams,
        repuestType: "addProperty",
        deleteCellCapacity: this.deleteCellCapacity,
        addDeviceCapacity: this.addDeviceCapacity
      })), _react.default.createElement("div", {
        style: {
          height: "3rem"
        }
      }), _react.default.createElement(_amosAntd.Row, {
        type: "flex",
        justify: "center"
      }, _react.default.createElement(_AddBtn.default, {
        text: "上一步",
        onClick: this.onClickUp
      }), _react.default.createElement("div", {
        style: {
          width: "1.5rem"
        }
      }), _react.default.createElement(_AddBtn.default, {
        text: "确定",
        onClick: this.saveDeviceData,
        disabled: this.state.waiting
      })))));
    }
  } ]), a;
}(_react.Component);

AddDevice.displayName = "AddDevice", AddDevice.propTypes = {
  form: _propTypes.default.any,
  updateAddView: _propTypes.default.func,
  equiptemplateIndustryOptions: _propTypes.default.any
};

var _default = createForm(AddDevice);

exports.default = _default;