"use strict";
Object.defineProperty(exports, "__esModule", {
  value: !0
}), exports.default = void 0;
var _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 _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 _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 n, i = !0,
    o = !1;
  return {
    s: function() {
      r = e[Symbol.iterator]()
    },
    n: function() {
      var e = r.next();
      return i = e.done, e
    },
    e: function(e) {
      o = !0, n = e
    },
    f: function() {
      try {
        i || null == r.return || r.return()
      } finally {
        if (o) throw n
      }
    }
  }
}

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

    function r(e) {
      var c;
      return _classCallCheck(this, r), _defineProperty(_assertThisInitialized(c = t.call(this, e)), "onClickDown", function(e) {
        var r = c.state.activeKey,
          r = "".concat(parseInt(r) + 1);
        e.preventDefault(), c.props.form.validateFields(function(e, t) {
          e || (t = (0, _validate.objOfStrPropTrim)(t), c.setState({
            itemInfo: t,
            activeKey: r
          }))
        })
      }), _defineProperty(_assertThisInitialized(c), "onClickUp", function() {
        var e = c.state.activeKey,
          e = "".concat(parseInt(e) - 1);
        c.setState({
          activeKey: e
        })
      }), _defineProperty(_assertThisInitialized(c), "onSaveDevice", function(e) {
        c.setState({
          waiting: !0
        });
        var t = c.props.form.getFieldValue("name"),
          r = c.props.form.getFieldValue("model"),
          a = c.props.form.getFieldValue("type"),
          n = c.props.form.getFieldValue("desc"),
          i = c.props.form.getFieldValue("viewPath"),
          o = c.props.form.getFieldValue("industryId");
        _extends(c.state.device, {
          name: t,
          model: r,
          type: a,
          viewPath: i,
          desc: n,
          visibility: 1,
          industryId: o
        }, e), _processor.AmosFetch.post(_urlConsts.OltSerUrl.addDevice, {
          data: c.state.device
        }).then(function(e) {
          if (e.ok) return e.text();
          throw new Error("从服务端获取数据失败！")
        }).then(function(e) {
          e = JSON.parse(e);
          "SUCCESS" === e.result ? 0 === e.dataList ? (c.setState({
            waiting: !1
          }), _amosFramework.message.danger("设备原型名称重复")) : (_amosFramework.message.success("保存成功"), c.setState({
            device: {},
            imgFile: {}
          }), c.props.updateAddView("2"), c.props.form.resetFields()) : (c.setState({
            waiting: !1
          }), _amosFramework.message.danger("保存失败"))
        })
      }), _defineProperty(_assertThisInitialized(c), "setImgFile", function(e) {
        c.setState({
          imgFile: e
        })
      }), _defineProperty(_assertThisInitialized(c), "autoCardCalculate", function(e) {
        var t = c.state.interfaceProList;
        void 0 !== e && (t = [].concat(_toConsumableArray(t), _toConsumableArray(e)), c.setState({
          interfaceProList: t
        })), c.autoProView()
      }), _defineProperty(_assertThisInitialized(c), "autoProView", function(e) {
        var e = void 0 === e ? c.state.interfaceProList : e,
          t = [];
        if (t.push(_react.default.createElement("div", {
            key: 0
          }, _react.default.createElement(_AddDeviceProperty.default, {
            repuestType: "addProperty",
            addDeviceProperty: c.addDeviceProperty,
            autoCardCalculate: c.autoCardCalculate
          }))), 0 < e.length) {
          var r = _createForOfIteratorHelper(e);
          try {
            for (r.s(); !(n = r.n()).done;) {
              var a = n.value,
                n = [];
              n.push(_react.default.createElement("p", null, "分  类：", (0, _DeviceLang.stringUtil)(_DeviceEnum.classifyEnum, parseInt(a.type)))), n.push(_react.default.createElement("p", null, "数据类型：", (0, _DeviceLang.stringUtil)(_DeviceEnum.dataTypeEnum, parseInt(a.dataType)))), t.push(_react.default.createElement(_CommonCell.default, {
                contentHtml: n,
                cardKey: a.key,
                value: a,
                deleteCellProperty: c.deleteCellProperty
              }))
            }
          } catch (e) {
            r.e(e)
          } finally {
            r.f()
          }
        }
        return t
      }), _defineProperty(_assertThisInitialized(c), "addDeviceProperty", function(e) {
        var t = c.state.device;
        t.equipmentPropertyParams = [].concat(_toConsumableArray(t.equipmentPropertyParams), [e]), c.setState({
          device: t
        })
      }), _defineProperty(_assertThisInitialized(c), "saveImg", function() {
        var e = new FileReader;
        e.addEventListener("load", function() {
          return c.upLoadImgToSave(e.result)
        });
        var t = c.state.imgFile;
        "{}" === JSON.stringify(t) ? c.onSaveDevice({
          binaryImg: null,
          imgName: "default.png"
        }) : e.readAsBinaryString(c.state.imgFile)
      }), _defineProperty(_assertThisInitialized(c), "addDeviceInterface", function(e) {
        var t = c.state.device;
        t.equipmentInterfaceParams = [].concat(_toConsumableArray(t.equipmentInterfaceParams), [e]), c.setState({
          device: t
        })
      }), _defineProperty(_assertThisInitialized(c), "addDeviceCapacity", function(e) {
        var t = c.state.device;
        t.equipmentCapacityParams = [].concat(_toConsumableArray(t.equipmentCapacityParams), [e]), c.setState({
          device: t
        })
      }), _defineProperty(_assertThisInitialized(c), "saveDeviceData", function() {
        c.saveImg()
      }), _defineProperty(_assertThisInitialized(c), "upLoadImgToSave", function(e) {
        e = {
          binaryImg: e,
          imgName: c.state.imgFile.name
        };
        c.onSaveDevice(e)
      }), _defineProperty(_assertThisInitialized(c), "deleteCellProperty", function(e) {
        var t = [],
          r = _createForOfIteratorHelper(c.state.interfaceProList);
        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()
        }
        c.setState({
          interfaceProList: t
        });
        var n = c.state.device,
          i = [],
          o = _createForOfIteratorHelper(n.equipmentPropertyParams);
        try {
          for (o.s(); !(l = o.n()).done;) {
            var l = l.value;
            l.key !== e && i.push(l)
          }
        } catch (e) {
          o.e(e)
        } finally {
          o.f()
        }
        n.equipmentPropertyParams = i, c.setState({
          device: n
        }), c.autoProView()
      }), _defineProperty(_assertThisInitialized(c), "deleteCellInterface", function(e) {
        var t = c.state.device,
          r = [],
          a = _createForOfIteratorHelper(t.equipmentInterfaceParams);
        try {
          for (a.s(); !(n = a.n()).done;) {
            var n = n.value;
            n.key !== e && r.push(n)
          }
        } catch (e) {
          a.e(e)
        } finally {
          a.f()
        }
        t.equipmentInterfaceParams = r, c.setState({
          device: t
        })
      }), _defineProperty(_assertThisInitialized(c), "deleteCellCapacity", function(e) {
        var t = c.state.device,
          r = [],
          a = _createForOfIteratorHelper(t.equipmentCapacityParams);
        try {
          for (a.s(); !(n = a.n()).done;) {
            var n = n.value;
            n.key !== e && r.push(n)
          }
        } catch (e) {
          a.e(e)
        } finally {
          a.f()
        }
        t.equipmentCapacityParams = r, c.setState({
          device: t
        })
      }), c.state = {
        activeKey: "1",
        interfaceProList: [],
        itemInfo: {},
        device: {
          equipmentPropertyParams: [],
          equipmentInterfaceParams: [],
          equipmentCapacityParams: []
        },
        imgFile: {},
        waiting: !1
      }, c
    }
    return _createClass(r, [{
      key: "render",
      value: function() {
        var e = this.state.device,
          t = this.props.equiptemplateIndustryOptions || [],
          r = this.props.form.getFieldProps,
          a = {
            labelCol: {
              span: 5
            },
            wrapperCol: {
              span: 18
            }
          },
          n = r("name", {
            rules: [{
              required: !0,
              min: 1,
              max: 20,
              message: "支持1-20位字符！"
            }, {
              validator: (0, _validate.noSpaceLengthValidate)(null, !0, "名称不能为空！")
            }]
          }),
          i = r("model", {
            rules: [{
              required: !0,
              message: "型号不能为空！"
            }, {
              validator: (0, _validate.noSpaceLengthValidate)(null, !0, "型号不能为空！")
            }]
          }),
          o = r("type", {
            rules: [{
              required: !0,
              message: "类别不能为空！"
            }]
          }),
          l = r("industryId", {
            rules: [{
              required: !0,
              message: "所属行业不能为空！"
            }]
          }),
          r = (r("viewPath", {
            rules: []
          }), r("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, _extends({
          hasFeedback: !0,
          label: "名称"
        }, a), _react.default.createElement(_amosAntd.Input, _extends({
          className: "amos-grid-form-input"
        }, n, {
          placeholder: "支持1-20位字符"
        }))), _react.default.createElement(FormItem, _extends({
          hasFeedback: !0,
          label: "型号"
        }, a), _react.default.createElement(_amosAntd.Input, _extends({
          className: "amos-grid-form-input"
        }, i, {
          placeholder: "请输入型号"
        }))), _react.default.createElement(FormItem, _extends({
          hasFeedback: !0,
          label: "类别"
        }, a), _react.default.createElement(_amosFramework.Select, _extends({
          style: {
            width: "100%",
            textAlign: "left",
            height: "32px"
          }
        }, o, {
          placeholder: "请选择类别"
        }), dataTypeOption)), _react.default.createElement(FormItem, _extends({
          hasFeedback: !0,
          label: "所属行业: "
        }, a), _react.default.createElement(_amosFramework.Select, _extends({
          style: {
            width: "100%",
            textAlign: "left",
            height: "32px"
          }
        }, l, {
          placeholder: "请选择所属行业"
        }), t.map(function(e) {
          return _react.default.createElement(Option, {
            key: "".concat(e.value, "Option"),
            value: e.value
          }, e.label)
        }))), _react.default.createElement(FormItem, _extends({
          hasFeedback: !0,
          label: "说明"
        }, a), _react.default.createElement(TextArea, _extends({
          className: "amos-grid-form-input text-area-put"
        }, r, {
          placeholder: "请输入说明"
        })))), _react.default.createElement(_amosAntd.Col, {
          span: 10,
          offset: 1
        }, _react.default.createElement(FormItem, _extends({
          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
        })))))
      }
    }]), r
  }();
AddDevice.displayName = "AddDevice", AddDevice.propTypes = {
  form: _propTypes.default.any,
  updateAddView: _propTypes.default.func,
  equiptemplateIndustryOptions: _propTypes.default.any
};
var _default = createForm(AddDevice);
exports.default = _default, module.exports = exports.default;
