import _Upload from "@alifd/next/es/upload";
import _Button from "@alifd/next/es/button";
import _Box from "@alifd/next/es/box";
import _Icon from "@alifd/next/es/icon";
import _Message from "@alifd/next/es/message";
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import React, { useCallback, useEffect, useState } from 'react';
import { saveImage, delImage, getFileDetail } from "../form-item/api";
import { getShortid } from "../../utils";
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 = useState([]),
    fileData = _useState[0],
    setFileData = _useState[1];
  var _useState2 = useState(''),
    fileId = _useState2[0],
    setFileId = _useState2[1];

  //初始化fileId
  useEffect(function () {
    var id = value;
    if (!value) {
      id = getShortid('file_');
    }
    setFileId(id);
  }, [value]);
  var init = useCallback( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
    var list;
    return _regeneratorRuntime.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 getFileDetail(fileId);
        case 4:
          list = _context.sent;
          setFileData(list);
        case 6:
        case "end":
          return _context.stop();
      }
    }, _callee);
  })), [fileId]);
  useEffect(function () {
    init();
  }, [init]);

  // 附件上传
  var onFileChange = /*#__PURE__*/function () {
    var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(info) {
      var _newData;
      var uids, fileTypes, newData, fileSize, fileObject, fileUid, fileType, formData, newImg, id, fileUrl, fileName, _newData2;
      return _regeneratorRuntime.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.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 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 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(info, data) {
      var currentData, newData, formData;
      return _regeneratorRuntime.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 delImage(formData);
          case 8:
          case "end":
            return _context3.stop();
        }
      }, _callee3);
    }));
    return function onRemove(_x2, _x3) {
      return _ref4.apply(this, arguments);
    };
  }();
  return /*#__PURE__*/React.createElement(_Upload, {
    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.createElement(_Button, {
    style: {
      margin: "0 0 10px"
    }
  }, /*#__PURE__*/React.createElement(_Icon, {
    type: "upload"
  }), /*#__PURE__*/React.createElement(_Box, null, "\u4E0A\u4F20\u6587\u4EF6")));
};
CommonAttachment.displayName = 'CommonAttachment';
export default CommonAttachment;