"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = _default;

var _react = _interopRequireWildcard(require("react"));

var _rcUpload = _interopRequireDefault(require("rc-upload"));

var _rcDialog = _interopRequireDefault(require("rc-dialog"));

var _toast = _interopRequireDefault(require("../../../../components/toast"));

var _groupInterface = require("../../groupInterface");

var _getLocale = require("../../../../utils/getLocale");

var _excel = _interopRequireDefault(require("./img/excel.svg"));

require("rc-dialog/assets/bootstrap.css");

require("rc-dialog/assets/index.css");

require("./index.css");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

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; }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

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 = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

function _default(props) {
  var _window$BSGlobal$tena;

  var visible = props.visible,
      setVisible = props.setVisible,
      getGroup = props.getGroup;
  var type = (_window$BSGlobal$tena = window.BSGlobal.tenantInfo) === null || _window$BSGlobal$tena === void 0 ? void 0 : _window$BSGlobal$tena.Source;
  var sourceType = type == 861 ? 861 : type == 860 ? 860 : 0;

  var _useState = (0, _react.useState)([]),
      _useState2 = _slicedToArray(_useState, 2),
      fileList = _useState2[0],
      setFileList = _useState2[1];

  var _useState3 = (0, _react.useState)(""),
      _useState4 = _slicedToArray(_useState3, 2),
      filename = _useState4[0],
      setFilename = _useState4[1];

  var _useState5 = (0, _react.useState)((0, _getLocale.getLocale)("Pro_page_File_Butt_UploadFolder")),
      _useState6 = _slicedToArray(_useState5, 2),
      btnname = _useState6[0],
      setBtnname = _useState6[1];

  var onDialogCloseHandler = function onDialogCloseHandler() {
    setVisible(false);

    _toast["default"].Success("", {
      style: {
        display: "none"
      }
    });
  }; // const biggerTenant = [366123, 366434, 217208]
  // const size = biggerTenant.indexOf(window.BSGlobal.tenantInfo?.Id - 0) == -1 ? 50 : 100


  var beforeUpload = function beforeUpload(file) {
    // console.log('Source:'+type,'sourceType:'+sourceType)
    var fileType = file.name.slice(file.name.lastIndexOf(".") + 1);

    if (fileType === "xlsx" || fileType === "xls") {
      setFileList([file]);
      setFilename(file.name);
      setBtnname("重新上传");
    } else {
      _toast["default"].Error("暂不支持此文件类型，请上传excel文件格式");
    }

    return false;
  };

  var deleFileList = function deleFileList() {
    setFileList([]);
    setFilename("");
  };

  var onSubmitFileHandler = function onSubmitFileHandler() {
    if (fileList[0]) {
      var fileType = fileList[0].name.slice(fileList[0].name.lastIndexOf(".") + 1);

      if (fileType === "xlsx" || fileType === "xls") {
        var formData = new FormData(); // FormData 对象

        console.log(fileList[0]);
        formData.append("file", fileList[0]); // 获取平台source：0-tita，860-企业微信，861-钉钉
        // let sourceType = 0

        var _url = (0, _groupInterface.getApiUrl)("group/template/import?sourceType=".concat(sourceType));

        var xhr = new XMLHttpRequest(); // 创建xhr

        xhr.onreadystatechange = function () {
          if (xhr.readyState == 4 && xhr.status == 200) {
            var result = JSON.parse(xhr.responseText); // 上传成功接口返回数据

            if (result.Code === 1) {
              setBtnname((0, _getLocale.getLocale)("Pro_page_File_Butt_UploadFolder"));
              setFileList([]);
              setFilename("");
              setVisible(false);

              _toast["default"].Success(result.Message);
            }
          } else {}
        };

        xhr.open("POST", _url, true);
        xhr.send(formData); // 开始上传，发送form数据
      } else {
        _toast["default"].Error("暂不支持此文件类型，请上传excel文件格式");
      }
    } else {
      _toast["default"].Error("请至少上传一个文件");
    }
  }; // 下载模板链接
  // let type=window.BSGlobal.tenantInfo?.Source
  // let sourceType = (type==861 ? 861 : type==860 ? 860 : 0)
  // let sourceType = 861


  var url = (0, _groupInterface.getApiUrl)("group/template/get?sourceType=".concat(sourceType));
  var config = {
    beforeUpload: beforeUpload // 上传前的验证

  };
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: "user-group-page__import"
  }, /*#__PURE__*/_react["default"].createElement(_rcDialog["default"], {
    className: "user-group-page__dialog",
    destroyOnClose: true,
    animation: "slide-fade",
    maskAnimation: "fade",
    onClose: onDialogCloseHandler,
    style: {
      width: 455
    },
    visible: visible,
    maskClosable: false,
    title: /*#__PURE__*/_react["default"].createElement("div", null, "\u6279\u91CF\u5BFC\u5165"),
    footer: [/*#__PURE__*/_react["default"].createElement("button", {
      type: "button",
      className: "btn btn-default",
      key: "close",
      onClick: onDialogCloseHandler
    }, (0, _getLocale.getLocale)("Mod_Cancel")), /*#__PURE__*/_react["default"].createElement("button", {
      type: "button",
      className: "btn btn-primary",
      key: "save",
      onClick: onSubmitFileHandler
    }, (0, _getLocale.getLocale)("OKR_MyO_Butt_Determine"))]
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "user-group-page__import-download"
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "user-group-page__import-download-template"
  }, /*#__PURE__*/_react["default"].createElement("p", null, "1\u3001\u8BF7\u5148\u4E0B\u8F7D\u5BFC\u5165\u6A21\u677F\uFF0C\u6279\u91CF\u586B\u5199\u5458\u5DE5\u4FE1\u606F"), /*#__PURE__*/_react["default"].createElement("a", {
    href: url,
    target: "_blank",
    className: "user-group-page__import-download-template-button"
  }, "\u4E0B\u8F7D\u6A21\u677F")), /*#__PURE__*/_react["default"].createElement("div", {
    className: "user-group-page__import-download-template"
  }, /*#__PURE__*/_react["default"].createElement("p", null, "2\u3001\u4E0A\u4F20\u586B\u5199\u597D\u7684\u5458\u5DE5\u4FE1\u606F\u8868"), /*#__PURE__*/_react["default"].createElement("div", {
    className: "user-group-page__import-download-template-button"
  }, /*#__PURE__*/_react["default"].createElement(_rcUpload["default"], _extends({
    className: "import-uploader-input"
  }, config), btnname))), /*#__PURE__*/_react["default"].createElement("div", {
    className: "user-group-page__import-download-filename",
    style: filename ? {} : {
      display: "none"
    }
  }, /*#__PURE__*/_react["default"].createElement("img", {
    src: _excel["default"]
  }), /*#__PURE__*/_react["default"].createElement("div", {
    className: "user-group-page__import-download-filename-text"
  }, filename), /*#__PURE__*/_react["default"].createElement("span", {
    className: "tu-icon-del filename-del",
    onClick: deleFileList,
    style: {
      cursor: "pointer"
    }
  })))));
}