"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 _message = _interopRequireDefault(require("@alifd/next/lib/message"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _react = _interopRequireWildcard(require("react"));
var _api = require("../form-item/api");
var _utils = require("../../utils");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
var CommonAttachment = function CommonAttachment(_ref) {
  var style = _ref.style,
    value = _ref.value,
    _ref$defaultValue = _ref.defaultValue,
    defaultValue = _ref$defaultValue === void 0 ? false : _ref$defaultValue,
    _ref$isDisabled = _ref.isDisabled,
    isDisabled = _ref$isDisabled === void 0 ? false : _ref$isDisabled,
    _ref$isPreview = _ref.isPreview,
    isPreview = _ref$isPreview === void 0 ? false : _ref$isPreview,
    onChange = _ref.onChange;
  var _useState = (0, _react.useState)([]),
    fileData = _useState[0],
    setFileData = _useState[1];
  var _useState2 = (0, _react.useState)(''),
    fileId = _useState2[0],
    setFileId = _useState2[1];

  //初始化fileId
  (0, _react.useEffect)(function () {
    var id = value;
    if (!value) {
      id = (0, _utils.getShortid)('file_');
    }
    setFileId(id);
  }, [value]);
  var init = (0, _react.useCallback)( /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee() {
    var list;
    return _regenerator["default"].wrap(function _callee$(_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          if (fileId) {
            _context.next = 2;
            break;
          }
          return _context.abrupt("return");
        case 2:
          _context.next = 4;
          return (0, _api.getFileDetail)(fileId);
        case 4:
          list = _context.sent;
          setFileData(list);
        case 6:
        case "end":
          return _context.stop();
      }
    }, _callee);
  })), [fileId]);
  (0, _react.useEffect)(function () {
    init();
  }, [init]);

  // 附件上传
  var onFileChange = /*#__PURE__*/function () {
    var _ref3 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2(info) {
      var _newData;
      var uids, fileTypes, newData, fileSize, fileObject, fileUid, fileType, formData, newImg, id, fileUrl, fileName, _newData2;
      return _regenerator["default"].wrap(function _callee2$(_context2) {
        while (1) switch (_context2.prev = _context2.next) {
          case 0:
            uids = info.map(function (item) {
              return item.uid;
            });
            fileTypes = ['image/png', 'image/jpg', 'image/jpeg', 'image/gif', 'image/bmp']; // console.log(fileData)
            newData = fileData || [];
            if (fileId) {
              _context2.next = 5;
              break;
            }
            return _context2.abrupt("return");
          case 5:
            if (!(uids.length > ((_newData = newData) === null || _newData === void 0 ? void 0 : _newData.length))) {
              _context2.next = 24;
              break;
            }
            fileSize = info[info.length - 1].size;
            if (!(fileSize > 25 * 1024 * 1024)) {
              _context2.next = 10;
              break;
            }
            _message["default"].error('上传文件不能超过25M');
            return _context2.abrupt("return");
          case 10:
            fileObject = info[info.length - 1].originFileObj;
            fileUid = info[info.length - 1].uid;
            fileType = info[info.length - 1].type;
            formData = new FormData();
            formData.append('fileId', fileId);
            formData.append('file', fileObject);
            //若是图片类型，需要加缩放比
            if (fileTypes.indexOf(fileType) !== -1) {
              formData.append('thumbnailQuality', '0.7');
              formData.append('thumbnailWidth', '80');
            }
            _context2.next = 19;
            return (0, _api.saveImage)(formData);
          case 19:
            newImg = _context2.sent;
            id = newImg.id, fileUrl = newImg.fileUrl, fileName = newImg.fileName;
            newData.push({
              uid: fileUid,
              fileTypes: fileType,
              fileId: fileId,
              id: id,
              name: fileName,
              url: fileUrl,
              downloadURL: fileUrl,
              imgURL: fileUrl,
              state: "done",
              size: 100
            });
            _context2.next = 25;
            break;
          case 24:
            newData = (_newData2 = newData) === null || _newData2 === void 0 ? void 0 : _newData2.filter(function (item) {
              return uids.indexOf(item.uid) !== -1;
            });
          case 25:
            onChange && onChange(fileId);
            setFileData(newData);
          case 27:
          case "end":
            return _context2.stop();
        }
      }, _callee2);
    }));
    return function onFileChange(_x) {
      return _ref3.apply(this, arguments);
    };
  }();

  // 删除图片
  var _onRemove = /*#__PURE__*/function () {
    var _ref4 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee3(info, data) {
      var currentData, newData, formData;
      return _regenerator["default"].wrap(function _callee3$(_context3) {
        while (1) switch (_context3.prev = _context3.next) {
          case 0:
            currentData = data.filter(function (item) {
              return item.uid === info.uid;
            }).filter(function (item) {
              return item.uid === info.uid;
            });
            newData = data.filter(function (item) {
              return item.uid !== info.uid;
            });
            formData = new FormData();
            formData.append('id', currentData[0].id);
            setFileData(newData);
            onChange && onChange(newData);
            _context3.next = 8;
            return (0, _api.delImage)(formData);
          case 8:
          case "end":
            return _context3.stop();
        }
      }, _callee3);
    }));
    return function onRemove(_x2, _x3) {
      return _ref4.apply(this, arguments);
    };
  }();
  return /*#__PURE__*/_react["default"].createElement(_upload["default"], {
    style: style,
    value: fileData,
    defaultValue: defaultValue,
    disabled: isDisabled,
    listType: "text",
    autoUpload: false,
    isPreview: isPreview,
    onChange: function onChange(e) {
      return onFileChange(e);
    },
    onRemove: function onRemove(e) {
      return _onRemove(e, 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")));
};
CommonAttachment.displayName = 'CommonAttachment';
var _default = CommonAttachment;
exports["default"] = _default;