function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import { Button, Radio, Space, Upload, message } from 'antd';
import React, { useContext, useEffect, useState } from 'react';
import { ExcelLinkContext, classPrefix } from '.';
import apis from '../../constants/api';
import { responseErrorHandler } from '@zgfe/business-lib/es/utils/ajax';
var FileTypeSwitch = function FileTypeSwitch(_ref) {
  var value = _ref.value,
    onChange = _ref.onChange,
    onUpload = _ref.onUpload;
  var _useState = useState(value || 'local'),
    _useState2 = _slicedToArray(_useState, 2),
    type = _useState2[0],
    setType = _useState2[1];
  var _useContext = useContext(ExcelLinkContext),
    editLink = _useContext.editLink;
  useEffect(function () {
    if (onChange) onChange(type);
  }, [type]);
  var uploadData = editLink ? {
    id: editLink.id
  } : undefined;
  return /*#__PURE__*/React.createElement(Space, {
    direction: "vertical",
    size: 0,
    style: {
      verticalAlign: 'sub'
    }
  }, /*#__PURE__*/React.createElement(Radio.Group, {
    defaultValue: type,
    onChange: function onChange(event) {
      setType(event.target.value);
    }
  }, /*#__PURE__*/React.createElement(Radio, {
    value: "local"
  }, "\u672C\u5730"), /*#__PURE__*/React.createElement(Radio, {
    value: "ftp"
  }, "FTP")), type === 'local' ? /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
    className: "".concat(classPrefix, "-type-tip")
  }, /*#__PURE__*/React.createElement("div", null, "\u6587\u4EF6\u4E0A\u4F20\u9650\u5236\uFF1A ", /*#__PURE__*/React.createElement("br", null), "\u652F\u6301 xls\u3001xlsx\u3001csv \u683C\u5F0F\uFF1B", /*#__PURE__*/React.createElement("br", null), "\u5355\u6587\u4EF6\u6700\u5927\u652F\u6301100M\uFF0C\u82E5\u60A8\u7684\u6570\u636E\u6587\u4EF6\u8D85\u51FA\u9650\u5236\uFF0C\u5EFA\u8BAE\u62C6\u5206\u540E\u5206\u591A\u6B21\u8FDB\u884C\u4E0A\u4F20\uFF1B", /*#__PURE__*/React.createElement("br", null), "\u6587\u4EF6\u4E2D\u82E5\u5B58\u5728\u591ASheet\u9875\u6570\u636E\uFF0C\u5EFA\u8BAE\u62C6\u5206\u4E3A\u591A\u4E2A\u6587\u4EF6\u8FDB\u884C\u4E0A\u4F20\u3002", /*#__PURE__*/React.createElement("br", null))), /*#__PURE__*/React.createElement(Upload, {
    accept: ".xls,.xlsx,.csv",
    action: apis.uploadExcel,
    multiple: true,
    maxCount: 10,
    data: uploadData,
    showUploadList: false,
    beforeUpload: function beforeUpload(file, fileList) {
      var outLimit = file.size / 1024 / 1024 > 100; // 100MB
      if (outLimit) {
        message.error("\u4E0A\u4F20\u6587\u4EF6: ".concat(file.name, " \u5FC5\u987B\u5C0F\u4E8E100MB!"));
        return false;
      }
      return true;
    },
    onChange: function onChange(info) {
      if (info.file.status === 'done') {
        if (onUpload) {
          try {
            var res = responseErrorHandler(apis.uploadExcel, info.file.response);
            if (res.data) {
              message.success("\u4E0A\u4F20\u6210\u529F");
              onUpload(res.data[0]);
            }
          } catch (e) {
            message.error("\u4E0A\u4F20\u5931\u8D25");
          }
        }
      } else if (info.file.status === 'error') {
        message.error("\u4E0A\u4F20\u5931\u8D25");
      }
    },
    name: "files"
  }, /*#__PURE__*/React.createElement(Button, {
    type: "primary"
  }, "\u4E0A\u4F20\u6587\u4EF6"))) : null);
};
export default FileTypeSwitch;