"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 _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")), _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 _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 l in e) if ("default" !== l && Object.prototype.hasOwnProperty.call(e, l)) {
    var n = i ? Object.getOwnPropertyDescriptor(e, l) : null;
    n && (n.get || n.set) ? Object.defineProperty(r, l, n) : r[l] = e[l];
  }
  return r.default = e, a && a.set(e, r), 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;
  }
}

var HEXSIZE = 1024, UploadSinglePicModal = 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), "getBase64", (function(e, t) {
      var a = new FileReader;
      a.addEventListener("load", (function() {
        return t(a.result);
      })), a.readAsDataURL(e);
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(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
      });
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(r), "beforeUpload", (function(e) {
      var t = /image\/(gif|jpeg|png|jpg|bmp)$/.test(e.type);
      t || _amosAntd.Message.error("请选择符合格式的图片！（gif|jpeg|png|jpg|bmp）");
      var a = e.size / HEXSIZE < 500;
      return a || _amosAntd.Message.error("图片最大为 500Kb!"), t && a;
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(r), "handleCancel", (function() {
      return r.setState({
        previewVisible: !1
      });
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(r), "uploadSUbgraphPics", (function() {
      r.setState({
        visible: !0
      });
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(r), "handCancel", (function() {
      r.setState({
        visible: !1,
        loading: !1,
        file: void 0,
        imageUrl: void 0,
        previewVisible: !1
      }), r.imgFile = void 0;
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(r), "saveSubGraphImgAction", (function() {
      var e = new FileReader;
      e.addEventListener("load", (function() {
        return r.upLoadImgToSave(e.result);
      }));
      var t = r.imgFile;
      if ("{}" === JSON.stringify(t)) {
        r.saveSubGraphImg({
          binaryImg: null,
          imgName: "default.png"
        });
      } else void 0 !== t ? e.readAsBinaryString(r.imgFile) : _amosFramework.message.danger("请选择要替换的背景图片！");
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(r), "upLoadImgToSave", (function(e) {
      var t = {
        binaryImg: e,
        imgName: "".concat(r.imgFile.uid, "-").concat(r.imgFile.name)
      };
      r.saveSubGraphImg(t);
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(r), "saveSubGraphImg", (function(e) {
      var t = r.props.subviewId;
      (0, _extends2.default)(e, {
        id: t
      }), (0, _topoService.editSubviewImgAction)(e).then((function(t) {
        _amosFramework.message.success("操作成功"), r.props.chooseBackground(e.imgName), r.props.updateBkImg(e.imgName), 
        r.handCancel();
      }));
    })), r.state = {
      loading: !1,
      file: void 0,
      imageUrl: void 0,
      previewVisible: !1
    }, r;
  }
  return (0, _createClass2.default)(a, [ {
    key: "componentWillMount",
    value: function() {}
  }, {
    key: "componentDidMount",
    value: function() {}
  }, {
    key: "componentWillReceiveProps",
    value: function(e) {}
  }, {
    key: "componentWillUnmount",
    value: function() {}
  }, {
    key: "render",
    value: function() {
      var e = this.props, t = e.style, a = e.imgName, r = _react.default.createElement("div", {
        className: "upload-icon-panel"
      }, this.state.loading ? _react.default.createElement(_amosIcon.default, {
        type: "loading"
      }) : null !== a ? _react.default.createElement("img", {
        src: "".concat(_urlConsts.OltSerUrl.fetchDeviceImg, "/graph/").concat(a),
        alt: "",
        style: t
      }) : _react.default.createElement("img", {
        src: require("./../../../../assets/iot/deviceManage/add_template.png"),
        alt: "",
        style: {
          paddingTop: "60px"
        }
      })), i = this.state.imageUrl, l = _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: l,
        beforeUpload: this.beforeUpload,
        onChange: this.handleChange
      }, i ? _react.default.createElement("img", {
        src: i,
        alt: "",
        style: t
      }) : r), _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
      }, "确定")))));
    }
  } ]), a;
}(_react.Component);

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 = _default;