"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 _button = _interopRequireDefault(require("@alifd/next/lib/button"));
var _box = _interopRequireDefault(require("@alifd/next/lib/box"));
var _icon = _interopRequireDefault(require("@alifd/next/lib/icon"));
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 CommonAttachment = /*#__PURE__*/function (_React$Component) {
  (0, _inheritsLoose2["default"])(CommonAttachment, _React$Component);
  function CommonAttachment(props) {
    var _this;
    _this = _React$Component.call(this, props) || this;
    // 附件上传
    _this.onFileChange = /*#__PURE__*/function () {
      var _ref = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(info) {
        var uids, newData, fileObject, fileUid, fileType, imgList, _this$props, fileId, thumbnailQuality, thumbnailWidth, formData, newImg, id, fileUrl, fileName;
        return _regenerator["default"].wrap(function _callee$(_context) {
          while (1) switch (_context.prev = _context.next) {
            case 0:
              uids = info.map(function (item) {
                return item.uid;
              });
              newData = _this.state.fileData;
              if (!(uids.length > _this.state.fileData.length)) {
                _context.next = 19;
                break;
              }
              fileObject = info[info.length - 1].originFileObj;
              fileUid = info[info.length - 1].uid;
              fileType = info[info.length - 1].type;
              imgList = ['image/png', 'image/jpg', 'image/jpeg', 'image/gif', 'image/bmp'];
              _this$props = _this.props, fileId = _this$props.fileId, thumbnailQuality = _this$props.thumbnailQuality, thumbnailWidth = _this$props.thumbnailWidth;
              formData = new FormData();
              formData.append('fileId', fileId);
              formData.append('file', fileObject);
              // 图片参数
              if (imgList.indexOf(fileType) !== -1) {
                formData.append('thumbnailQuality', thumbnailQuality);
                formData.append('thumbnailWidth', thumbnailWidth);
              }
              _context.next = 14;
              return (0, _api.saveImage)(formData);
            case 14:
              newImg = _context.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
              });
              _context.next = 20;
              break;
            case 19:
              newData = newData.filter(function (item) {
                return uids.indexOf(item.uid) !== -1;
              });
            case 20:
              _this.setState({
                fileData: newData
              });
            case 21:
            case "end":
              return _context.stop();
          }
        }, _callee);
      }));
      return function (_x) {
        return _ref.apply(this, arguments);
      };
    }();
    // 删除图片
    _this.onRemove = /*#__PURE__*/function () {
      var _ref2 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2(info, data) {
        var newData, currentData, formData;
        return _regenerator["default"].wrap(function _callee2$(_context2) {
          while (1) switch (_context2.prev = _context2.next) {
            case 0:
              newData = data;
              currentData = newData.filter(function (item) {
                return item.uid === info.uid;
              });
              formData = new FormData();
              formData.append('id', currentData[0].id);
              _context2.next = 6;
              return (0, _api.delImage)(formData);
            case 6:
            case "end":
              return _context2.stop();
          }
        }, _callee2);
      }));
      return function (_x2, _x3) {
        return _ref2.apply(this, arguments);
      };
    }();
    _this.state = {
      childList: [],
      selectData: [],
      fileData: [] //附件
    };
    return _this;
  }
  var _proto = CommonAttachment.prototype;
  _proto.render = function render() {
    var _this2 = this;
    return /*#__PURE__*/_react["default"].createElement(_upload["default"], {
      style: {
        display: 'inline-block'
      },
      listType: "text",
      autoUpload: false,
      onChange: this.onFileChange,
      onRemove: function onRemove(e) {
        return _this2.onRemove(e, _this2.state.fileData);
      }
    }, /*#__PURE__*/_react["default"].createElement(_button["default"], {
      style: {
        margin: "0 0 10px"
      }
    }, /*#__PURE__*/_react["default"].createElement(_icon["default"], {
      type: "upload"
    }), /*#__PURE__*/_react["default"].createElement(_box["default"], null, "\u4E0A\u4F20\u6587\u4EF6")));
  };
  return CommonAttachment;
}(_react["default"].Component);
CommonAttachment.displyName = 'CommonAttachment';
CommonAttachment.displyName = 'CommonAttachment';
var _default = CommonAttachment;
exports["default"] = _default;