"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 _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")), _amosAntd = require("amos-antd"), _urlConsts = require("../../../../consts/urlConsts");

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

function _createSuper(e) {
  var t = _isNativeReflectConstruct();
  return function() {
    var r, a = (0, _getPrototypeOf2.default)(e);
    if (t) {
      var i = (0, _getPrototypeOf2.default)(this).constructor;
      r = Reflect.construct(a, arguments, i);
    } else r = a.apply(this, arguments);
    return (0, _possibleConstructorReturn2.default)(this, r);
  };
}

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 HEXSIZE = 1024, TopoUpLoadIcon = function(e) {
  (0, _inherits2.default)(r, e);
  var t = _createSuper(r);
  function r(e) {
    var a;
    return (0, _classCallCheck2.default)(this, r), a = t.call(this, e), (0, _defineProperty2.default)((0, 
    _assertThisInitialized2.default)(a), "getBase64", (function(e, t) {
      var r = new FileReader;
      r.addEventListener("load", (function() {
        return t(r.result);
      })), r.readAsDataURL(e);
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(a), "handleChange", (function(e) {
      a.setState({
        file: e.file
      }), "uploading" !== e.file.status ? "done" === e.file.status && (a.getBase64(e.file.originFileObj, (function(e) {
        return a.setState({
          imageUrl: e,
          loading: !1
        });
      })), a.props.setImgFile(e.file.originFileObj)) : a.setState({
        loading: !0
      });
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(a), "beforeUpload", (function(e) {
      var t = /image\/(gif|jpeg|png|jpg|bmp)$/.test(e.type);
      t || _amosAntd.Message.error("请选择符合格式的图片！（gif|jpeg|png|jpg|bmp）");
      var r = e.size / HEXSIZE < 500;
      return r || _amosAntd.Message.error("图片最大为 500Kb!"), t && r;
    })), a.state = {
      loading: !1,
      file: void 0,
      imageUrl: ""
    }, a;
  }
  return (0, _createClass2.default)(r, [ {
    key: "render",
    value: function() {
      var e = this.props, t = e.style, r = e.defaultImg, a = e.enable, i = _react.default.createElement("img", {
        src: require("./../../../../assets/iot/topo/upload.png"),
        alt: ""
      }), n = _react.default.createElement("div", {
        className: "upload-icon-panel"
      }, this.state.loading ? _react.default.createElement(_amosAntd.Icon, {
        type: "loading"
      }) : r ? _react.default.createElement("img", {
        src: r,
        alt: "",
        style: t
      }) : i), o = this.state.imageUrl, l = _urlConsts.OltSerUrl.addDevice;
      return _react.default.createElement(_amosAntd.Upload, {
        name: "avatar",
        listType: "picture-card",
        showUploadList: !1,
        action: l,
        beforeUpload: this.beforeUpload,
        onChange: this.handleChange,
        disabled: !a
      }, o ? _react.default.createElement("img", {
        src: o,
        alt: "",
        style: t
      }) : n);
    }
  } ]), r;
}(_react.Component);

TopoUpLoadIcon.displayName = "TopoUpLoadIcon", TopoUpLoadIcon.propTypes = {
  setImgFile: _propTypes.default.func,
  style: _propTypes.default.object,
  defaultImg: _propTypes.default.string,
  enable: _propTypes.default.bool
}, TopoUpLoadIcon.defaultProps = {
  style: {
    position: "relative",
    top: "-10px"
  },
  enable: !0
};

var _default = TopoUpLoadIcon;

exports.default = _default;