"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports["default"] = void 0;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _upload = _interopRequireDefault(require("@alifd/next/lib/upload"));
var _dialog = _interopRequireDefault(require("@alifd/next/lib/dialog"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));
var _react = _interopRequireDefault(require("react"));
require("./index.scss");
var _api = require("../form-item/api");
var CommonImage = /*#__PURE__*/function (_React$Component) {
  (0, _inheritsLoose2["default"])(CommonImage, _React$Component);
  function CommonImage(props) {
    var _this;
    _this = _React$Component.call(this, props) || this;
    // 删除图片
    _this.onRemove = /*#__PURE__*/function () {
      var _ref = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(info, data) {
        var newData, currentData, formData;
        return _regenerator["default"].wrap(function _callee$(_context) {
          while (1) switch (_context.prev = _context.next) {
            case 0:
              newData = data;
              currentData = newData.filter(function (item) {
                return item.uid === info.uid;
              });
              formData = new FormData();
              formData.append('id', currentData[0].id);
              _context.next = 6;
              return (0, _api.delImage)(formData);
            case 6:
            case "end":
              return _context.stop();
          }
        }, _callee);
      }));
      return function (_x, _x2) {
        return _ref.apply(this, arguments);
      };
    }();
    // 图片上传
    _this.onImgChange = /*#__PURE__*/function () {
      var _ref2 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2(info) {
        var uids, newData, fileObject, fileUid, _this$props, fieldName, thumbnailQuality, thumbnailWidth, formData, newImg, id, fileUrl, fileName;
        return _regenerator["default"].wrap(function _callee2$(_context2) {
          while (1) switch (_context2.prev = _context2.next) {
            case 0:
              uids = info.map(function (item) {
                return item.uid;
              });
              newData = _this.state.imgData;
              if (!(uids.length > _this.state.imgData.length)) {
                _context2.next = 18;
                break;
              }
              fileObject = info[info.length - 1].originFileObj;
              fileUid = info[info.length - 1].uid;
              _this$props = _this.props, fieldName = _this$props.fieldName, thumbnailQuality = _this$props.thumbnailQuality, thumbnailWidth = _this$props.thumbnailWidth;
              formData = new FormData();
              formData.append('fileId', fieldName);
              formData.append('file', fileObject);
              formData.append('thumbnailQuality', thumbnailQuality);
              formData.append('thumbnailWidth', thumbnailWidth);
              _context2.next = 13;
              return (0, _api.saveImage)(formData);
            case 13:
              newImg = _context2.sent;
              id = newImg.id, fileUrl = newImg.fileUrl, fileName = newImg.fileName;
              newData.push({
                uid: fileUid,
                id: id,
                name: fileName,
                url: fileUrl,
                downloadURL: fileUrl,
                imgURL: fileUrl,
                state: "done",
                size: 100
              });
              _context2.next = 19;
              break;
            case 18:
              newData = newData.filter(function (item) {
                return uids.indexOf(item.uid) !== -1;
              });
            case 19:
              _this.setState({
                imgData: newData
              });
            case 20:
            case "end":
              return _context2.stop();
          }
        }, _callee2);
      }));
      return function (_x3) {
        return _ref2.apply(this, arguments);
      };
    }();
    _this.state = {
      childList: [],
      selectData: [],
      imgData: [] //图片
    };
    return _this;
  }

  // 预览图片
  var _proto = CommonImage.prototype;
  _proto.onPreview = function onPreview(info) {
    _dialog["default"].show({
      title: "图片预览",
      content: /*#__PURE__*/_react["default"].createElement("img", {
        src: info.imgURL,
        style: {
          width: 500,
          height: 500
        }
      }),
      footer: false
    });
  };
  _proto.render = function render() {
    var _this2 = this;
    return /*#__PURE__*/_react["default"].createElement(_upload["default"].Card, {
      shape: "card",
      useDataURL: true,
      autoUpload: false,
      accept: "image/png, image/jpg, image/jpeg, image/gif, image/bmp",
      style: {
        display: 'inline-block'
      },
      onPreview: this.onPreview,
      onRemove: function onRemove(e) {
        return _this2.onRemove(e, _this2.state.imgData);
      },
      onChange: this.onImgChange
    });
  };
  return CommonImage;
}(_react["default"].Component);
CommonImage.displayName = 'CommonImage';
CommonImage.displayName = 'CommonImage';
var _default = CommonImage;
exports["default"] = _default;