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); }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
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 { BizDialog, BizGlobalDataContext, IconFont, ajax } from '@zgfe/business-lib';
import { Button, Input, Space, Table } from 'antd';
import React, { useContext, useEffect, useState } from 'react';
import apis from '../../constants/api';
import Request from 'umi-request';
import { ExcelLinkContext, classPrefix } from '.';
var Handle = function Handle(_ref) {
  var fileName = _ref.fileName,
    columnRow = _ref.columnRow,
    onDel = _ref.onDel;
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    showStructure = _useState2[0],
    setShowStructure = _useState2[1];
  var _useState3 = useState(false),
    _useState4 = _slicedToArray(_useState3, 2),
    loading = _useState4[0],
    setLoading = _useState4[1];
  var _useContext = useContext(BizGlobalDataContext),
    currentApp = _useContext.currentApp;
  var _useState5 = useState([]),
    _useState6 = _slicedToArray(_useState5, 2),
    dataSource = _useState6[0],
    setDataSource = _useState6[1];
  var _useState7 = useState(false),
    _useState8 = _slicedToArray(_useState7, 2),
    showDelTip = _useState8[0],
    setShowDelTip = _useState8[1];
  var _useContext2 = useContext(ExcelLinkContext),
    editLink = _useContext2.editLink,
    ftpInfo = _useContext2.ftpInfo,
    linkType = _useContext2.linkType;
  function onView() {
    setShowStructure(true);
    setLoading(true);
    setDataSource([]);
    ajax(apis.getExcelColumns, {
      method: 'post',
      data: {
        fileName: fileName,
        appId: currentApp === null || currentApp === void 0 ? void 0 : currentApp.appId,
        type: linkType,
        columnRow: columnRow,
        ftpInfo: ftpInfo,
        dataSourceId: editLink === null || editLink === void 0 ? void 0 : editLink.id
      }
    }).then(function (res) {
      if (!res) return;
      setDataSource(res.data.map(function (item, i) {
        return _objectSpread(_objectSpread({}, item), {}, {
          key: "".concat(item.name, "-").concat(i)
        });
      }));
    }).catch(function (err) {
      console.error('获取结构失败', err);
    }).finally(function () {
      setLoading(false);
    });
  }
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Space, null, /*#__PURE__*/React.createElement("a", {
    onClick: onView
  }, "\u67E5\u770B\u6570\u636E\u7ED3\u6784"), /*#__PURE__*/React.createElement("a", {
    type: "link",
    onClick: function onClick() {
      setShowDelTip(true);
    },
    className: "".concat(classPrefix, "-del-btn")
  }, "\u5220\u9664")), showDelTip && /*#__PURE__*/React.createElement(BizDialog, {
    title: /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(IconFont, {
      style: {
        color: '#FD9F41',
        marginRight: '8px',
        verticalAlign: 'top',
        fontSize: '22px'
      },
      type: "tishi1"
    }), "\u98CE\u9669\u63D0\u793A"),
    open: true,
    className: "".concat(classPrefix, "-modal"),
    onCancel: function onCancel() {
      setShowDelTip(false);
    },
    onOk: function onOk() {
      if (onDel) onDel(fileName);
      setShowDelTip(false);
    }
  }, "\u6587\u4EF6\u5220\u9664\u540E\u4E0D\u53EF\u6062\u590D\uFF0C\u662F\u5426\u786E\u8BA4\u5220\u9664\uFF1F"), showStructure && /*#__PURE__*/React.createElement(BizDialog, {
    title: "\u67E5\u770B\u6570\u636E\u7ED3\u6784\uFF08".concat(fileName, "\uFF09"),
    open: true,
    className: "".concat(classPrefix, "-modal"),
    footer: /*#__PURE__*/React.createElement(Button, {
      type: "primary",
      onClick: setShowStructure.bind(undefined, false)
    }, "\u786E\u5B9A")
  }, /*#__PURE__*/React.createElement(Table, {
    columns: [{
      title: '字段名称',
      dataIndex: 'name',
      key: 'name'
    }],
    rowKey: "key",
    dataSource: dataSource,
    loading: loading,
    pagination: false,
    scroll: {
      y: 330
    }
  })));
};
var FieldRow = function FieldRow(_ref2) {
  var defaultRow = _ref2.defaultRow,
    fileName = _ref2.fileName,
    onChange = _ref2.onChange;
  var _useState9 = useState(),
    _useState10 = _slicedToArray(_useState9, 2),
    timer = _useState10[0],
    setTimer = _useState10[1];
  var _useContext3 = useContext(BizGlobalDataContext),
    currentApp = _useContext3.currentApp;
  var _useState11 = useState(),
    _useState12 = _slicedToArray(_useState11, 2),
    setCancelToken = _useState12[1];
  var _useState13 = useState(defaultRow),
    _useState14 = _slicedToArray(_useState13, 2),
    row = _useState14[0],
    setRow = _useState14[1];
  var _useContext4 = useContext(ExcelLinkContext),
    editLink = _useContext4.editLink,
    ftpInfo = _useContext4.ftpInfo,
    linkType = _useContext4.linkType;
  useEffect(function () {
    // 更新请求延迟1s，防止频繁请求，如果上次请求还没返回，就取消上次请求
    clearTimeout(timer);
    setTimer(setTimeout(function () {
      setCancelToken(function (token) {
        if (token) {
          token.cancel();
        }
        var newToken = Request.CancelToken.source();
        ajax(apis.setColumnRow, {
          method: 'post',
          data: {
            appId: currentApp === null || currentApp === void 0 ? void 0 : currentApp.appId,
            fileName: fileName,
            type: linkType,
            columnRow: row,
            dataSourceId: editLink === null || editLink === void 0 ? void 0 : editLink.id,
            ftpInfo: ftpInfo
          },
          cancelToken: newToken.token
        }).then(function (res) {
          if (!res) return;
          if (onChange) onChange(row);
        }).catch(function (err) {
          console.error('设置失败', err);
        });
        return newToken;
      });
    }, 700));
  }, [row]);
  function _onChange(e) {
    e.persist();
    var value = e.target.value;
    // 输入内容不允许为空、小于1、含有小数点
    if (value === '' || value < 1 || value.toString().indexOf('.') !== -1) return;
    setRow(value);
  }
  return /*#__PURE__*/React.createElement(Input, {
    type: "number",
    value: row,
    step: 1,
    min: 1,
    onChange: _onChange
  });
};
var FileList = function FileList(_ref3) {
  var value = _ref3.value,
    onChange = _ref3.onChange,
    onRemove = _ref3.onRemove;
  var _useState15 = useState([]),
    _useState16 = _slicedToArray(_useState15, 2),
    dataSource = _useState16[0],
    setDataSource = _useState16[1];
  var _useState17 = useState(false),
    _useState18 = _slicedToArray(_useState17, 2),
    ready = _useState18[0],
    setReady = _useState18[1];
  var _useContext5 = useContext(ExcelLinkContext),
    fileList = _useContext5.fileList;
  useEffect(function () {
    setDataSource(value || []);
    setReady(true);
  }, []);
  useEffect(function () {
    if (!ready) return;
    setDataSource((fileList === null || fileList === void 0 ? void 0 : fileList.map(function (fileName) {
      return {
        fileName: fileName,
        columnRow: 1
      };
    })) || []);
  }, [fileList]);
  useEffect(function () {
    if (onChange) onChange(dataSource);
  }, [dataSource]);
  function onDel(fileName) {
    setDataSource(dataSource.filter(function (item) {
      return item.fileName !== fileName;
    }));
    if (onRemove) onRemove(fileName);
  }
  var columns = [{
    title: '文件名称',
    dataIndex: 'fileName',
    key: 'fileName',
    width: 256,
    textWrap: 'word-break',
    ellipsis: true
  }, {
    title: /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("span", {
      style: {
        color: '#FB5547',
        marginRight: '6px',
        verticalAlign: 'inherit'
      }
    }, "*"), "\u5B57\u6BB5\u6240\u5728\u884C"),
    dataIndex: 'columnRow',
    key: 'columnRow',
    width: 160,
    render: function render(text, _ref4) {
      var fileName = _ref4.fileName;
      return /*#__PURE__*/React.createElement(FieldRow, {
        defaultRow: Number(text),
        fileName: fileName,
        onChange: function onChange(columnRow) {
          setDataSource(dataSource.map(function (item) {
            if (item.fileName === fileName) {
              return _objectSpread(_objectSpread({}, item), {}, {
                columnRow: columnRow
              });
            }
            return item;
          }));
        }
      });
    }
  }, {
    title: '操作',
    dataIndex: 'action',
    key: 'action',
    width: 188,
    render: function render(_val, row) {
      return /*#__PURE__*/React.createElement(Handle, {
        fileName: row.fileName,
        onDel: onDel,
        columnRow: row.columnRow
      });
    }
  }];
  return /*#__PURE__*/React.createElement(Table, {
    columns: columns,
    rowKey: "fileName",
    dataSource: dataSource,
    pagination: false
  });
};
export default FileList;