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 _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import React from 'react';
import "./index.scss";
import { saveImage, delImage } from "../form-item/api";
var CommonAttachment = /*#__PURE__*/function (_React$Component) {
  _inheritsLoose(CommonAttachment, _React$Component);
  function CommonAttachment(props) {
    var _this;
    _this = _React$Component.call(this, props) || this;
    // 附件上传
    _this.onFileChange = /*#__PURE__*/function () {
      var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(info) {
        var uids, newData, fileObject, fileUid, fileType, imgList, _this$props, fieldName, thumbnailQuality, thumbnailWidth, formData, newImg, id, fileUrl, fileName;
        return _regeneratorRuntime.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, fieldName = _this$props.fieldName, thumbnailQuality = _this$props.thumbnailQuality, thumbnailWidth = _this$props.thumbnailWidth;
              formData = new FormData();
              formData.append('fileId', fieldName);
              formData.append('file', fileObject);
              // 图片参数
              if (imgList.indexOf(fileType) !== -1) {
                formData.append('thumbnailQuality', thumbnailQuality);
                formData.append('thumbnailWidth', thumbnailWidth);
              }
              _context.next = 14;
              return 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 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(info, data) {
        var newData, currentData, formData;
        return _regeneratorRuntime.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 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.createElement(_Upload, {
      style: {
        display: 'inline-block'
      },
      listType: "text",
      autoUpload: false,
      onChange: this.onFileChange,
      onRemove: function onRemove(e) {
        return _this2.onRemove(e, _this2.state.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")));
  };
  return CommonAttachment;
}(React.Component);
CommonAttachment.displyName = 'CommonAttachment';
CommonAttachment.displyName = 'CommonAttachment';
export default CommonAttachment;