"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"),
  _urlConsts = require("./../../../../consts/urlConsts"),
  _amosFramework = require("amos-framework"),
  _amosIcon = _interopRequireDefault(require("amos-icon")),
  _topoService = require("./../../../../services/topoService");

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, i, n = {},
    a = Object.defineProperty && Object.getOwnPropertyDescriptor;
  for (r in e) Object.prototype.hasOwnProperty.call(e, r) && ((i = a ? Object.getOwnPropertyDescriptor(e, r) : null) && (i.get || i.set) ? Object.defineProperty(n, r, i) : 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 _extends() {
  return (_extends = Object.assign || function(e) {
    for (var t = 1; t < arguments.length; t++) {
      var r, i = arguments[t];
      for (r in i) Object.prototype.hasOwnProperty.call(i, r) && (e[r] = i[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 i = t[r];
    i.enumerable = i.enumerable || !1, i.configurable = !0, "value" in i && (i.writable = !0), Object.defineProperty(e, i.key, i)
  }
}

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

    function i(e) {
      var r;
      return _classCallCheck(this, i), _defineProperty(_assertThisInitialized(r = t.call(this, e)), "getBase64", function(e, t) {
        var r = new FileReader;
        r.addEventListener("load", function() {
          return t(r.result)
        }), r.readAsDataURL(e)
      }), _defineProperty(_assertThisInitialized(r), "handleChange", function(e) {
        r.setState({
          file: e.file
        }), "uploading" !== e.file.status ? "done" === e.file.status && (r.getBase64(e.file.originFileObj, function(e) {
          return r.setState({
            imageUrl: e,
            loading: !1
          })
        }), r.imgFile = e.file.originFileObj) : r.setState({
          loading: !0
        })
      }), _defineProperty(_assertThisInitialized(r), "beforeUpload", function(e) {
        var t = /image\/(gif|jpeg|png|jpg|bmp)$/.test(e.type);
        t || _amosAntd.Message.error("请选择符合格式的图片！（gif|jpeg|png|jpg|bmp）");
        e = e.size / HEXSIZE < 500;
        return e || _amosAntd.Message.error("图片最大为 500Kb!"), t && e
      }), _defineProperty(_assertThisInitialized(r), "handleCancel", function() {
        return r.setState({
          previewVisible: !1
        })
      }), _defineProperty(_assertThisInitialized(r), "uploadSUbgraphPics", function() {
        r.setState({
          visible: !0
        })
      }), _defineProperty(_assertThisInitialized(r), "handCancel", function() {
        r.setState({
          visible: !1,
          loading: !1,
          file: void 0,
          imageUrl: void 0,
          previewVisible: !1
        }), r.imgFile = void 0
      }), _defineProperty(_assertThisInitialized(r), "saveSubGraphImgAction", function() {
        var e = new FileReader;
        e.addEventListener("load", function() {
          return r.upLoadImgToSave(e.result)
        });
        var t = r.imgFile;
        "{}" === JSON.stringify(t) ? r.saveSubGraphImg({
          binaryImg: null,
          imgName: "default.png"
        }) : void 0 !== t ? e.readAsBinaryString(r.imgFile) : _amosFramework.message.danger("请选择要替换的背景图片！")
      }), _defineProperty(_assertThisInitialized(r), "upLoadImgToSave", function(e) {
        e = {
          binaryImg: e,
          imgName: "".concat(r.imgFile.uid, "-").concat(r.imgFile.name)
        };
        r.saveSubGraphImg(e)
      }), _defineProperty(_assertThisInitialized(r), "saveSubGraphImg", function(t) {
        var e = r.props.subviewId;
        _extends(t, {
          id: e
        }), (0, _topoService.editSubviewImgAction)(t).then(function(e) {
          _amosFramework.message.success("操作成功"), r.props.chooseBackground(t.imgName), r.props.updateBkImg(t.imgName), r.handCancel()
        })
      }), r.state = {
        loading: !1,
        file: void 0,
        imageUrl: void 0,
        previewVisible: !1
      }, r
    }
    return _createClass(i, [{
      key: "componentWillMount",
      value: function() {}
    }, {
      key: "componentDidMount",
      value: function() {}
    }, {
      key: "componentWillReceiveProps",
      value: function() {}
    }, {
      key: "componentWillUnmount",
      value: function() {}
    }, {
      key: "render",
      value: function() {
        var e = this.props,
          t = e.style,
          r = e.imgName,
          i = _react.default.createElement("div", {
            className: "upload-icon-panel"
          }, this.state.loading ? _react.default.createElement(_amosIcon.default, {
            type: "loading"
          }) : null !== r ? _react.default.createElement("img", {
            src: "".concat(_urlConsts.OltSerUrl.fetchDeviceImg, "/graph/").concat(r),
            alt: "",
            style: t
          }) : _react.default.createElement("img", {
            src: require("./../../../../assets/iot/deviceManage/add_template.png"),
            alt: "",
            style: {
              paddingTop: "60px"
            }
          })),
          e = this.state.imageUrl,
          r = _urlConsts.OltSerUrl.addDevice;
        return _react.default.createElement("div", {
          className: "single-upload-img-root"
        }, _react.default.createElement("div", {
          className: "single-upload-img-icon",
          onClick: this.uploadSUbgraphPics,
          title: "上传背景图片"
        }, _react.default.createElement("img", {
          src: require("./../../../../assets/iot/topo/bk.png"),
          style: {
            width: "30px",
            height: "26px"
          },
          alt: ""
        })), _react.default.createElement(_amosAntd.Modal, {
          className: "background-modal",
          maskClosable: !1,
          title: "上传背景图片",
          closable: !0,
          visible: this.state.visible,
          footer: null,
          onCancel: this.handCancel,
          width: 367
        }, _react.default.createElement("div", {
          className: "single-upload-pics-content"
        }, _react.default.createElement(_amosAntd.Upload, {
          name: "avatar",
          listType: "picture-card",
          showUploadList: !1,
          action: r,
          beforeUpload: this.beforeUpload,
          onChange: this.handleChange
        }, e ? _react.default.createElement("img", {
          src: e,
          alt: "",
          style: t
        }) : i), _react.default.createElement("div", {
          className: "modal-operate-root"
        }, _react.default.createElement("div", {
          className: "modal-operate-clone",
          onClick: this.handCancel
        }, "取消"), _react.default.createElement("div", {
          className: "modal-operate-view",
          onClick: this.saveSubGraphImgAction
        }, "确定")))))
      }
    }]), i
  }();
UploadSinglePicModal.displayName = "UploadSinglePicModal", UploadSinglePicModal.propTypes = {
  setImgFile: _propTypes.default.func,
  style: _propTypes.default.object,
  defaultImg: _propTypes.default.string,
  subviewId: _propTypes.default.string,
  imgName: _propTypes.default.string,
  chooseBackground: _propTypes.default.func,
  updateBkImg: _propTypes.default.func
}, UploadSinglePicModal.defaultProps = {
  style: {
    position: "relative",
    top: "-10px"
  }
};
var _default = UploadSinglePicModal;
exports.default = UploadSinglePicModal, module.exports = exports.default;
