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 _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
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 React, { useEffect, useState, useImperativeHandle } from 'react';
import useNode from './../useNode';
import { Modal, Table, Input, message } from 'antd';
import { IconFont, util } from '@zgfe/business-lib';
import './dataClean.less';
import ColumnSelect from '../../../../../components/columnSelect';
import _ from 'lodash';
import { getValueReplaceErrorInfo } from '../utils';
var ValueReplace = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
  var node = _ref.node,
    editData = _ref.editData,
    setSubmit = _ref.setSubmit;
  var classPrefix = 'value-replace';
  var _useNode = useNode(node),
    columnList = _useNode.columnList;
  var nodeData = node.getData();
  var initData = {
    name: null,
    originalValue: '',
    changeValue: ''
  };
  var _useState = useState((editData === null || editData === void 0 ? void 0 : editData.columns) ? _.cloneDeep(editData === null || editData === void 0 ? void 0 : editData.columns) : [_objectSpread(_objectSpread({}, initData), {}, {
      key: util.guid()
    })]),
    _useState2 = _slicedToArray(_useState, 2),
    columns = _useState2[0],
    setColumns = _useState2[1];
  var _useState3 = useState((editData === null || editData === void 0 ? void 0 : editData.columns) ? _.cloneDeep(editData === null || editData === void 0 ? void 0 : editData.columns) : [_objectSpread(_objectSpread({}, initData), {}, {
      key: util.guid()
    })]),
    _useState4 = _slicedToArray(_useState3, 2),
    dataSource = _useState4[0],
    setDataSource = _useState4[1];
  // 校验错误
  useEffect(function () {
    setDataSource(getValueReplaceErrorInfo(nodeData.list, columns, _.cloneDeep(editData)));
  }, [columns]);
  useEffect(function () {
    var submit = true;
    columns.map(function (item) {
      if (!item.name) submit = false;
      if (!item.originalValue) submit = false;
      if (!item.changeValue) submit = false;
      if (item.error) submit = false;
    });
    setSubmit(submit);
  }, [columns]);
  var _onChange = function onChange(type, record, value) {
    columns.map(function (item) {
      if (item.key === record.key) {
        item[type] = value;
        if (type === 'selectColumns') {
          // 已选择字段
          if (record.selectColumns) {
            item.type = record.selectColumns.type;
            item.name = record.selectColumns.name;
          } else {
            delete item.type;
            delete item.name;
          }
        }
      }
    });
    setColumns(_toConsumableArray(columns));
  };
  var tableColumns = [{
    title: /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement("span", {
      className: "requiredIcon"
    }, "*"), "\u5B57\u6BB5\u540D\u79F0"),
    width: 297,
    dataIndex: 'selectColumns',
    render: function render(text, record) {
      return /*#__PURE__*/React.createElement(ColumnSelect, {
        options: columnList.filter(function (item) {
          return ['number', 'string'].includes(item.webType);
        }),
        value: text,
        multiple: false,
        onChange: function onChange(e) {
          return _onChange('selectColumns', record, e);
        }
      });
    }
  }, {
    title: /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement("span", {
      className: "requiredIcon"
    }, "*"), "\u539F\u59CB\u503C"),
    width: 297,
    dataIndex: 'originalValue',
    render: function render(text, record) {
      var _record$selectColumns, _record$selectColumns2;
      return /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(Input, {
        className: "".concat(record.error && ((_record$selectColumns = record.selectColumns) === null || _record$selectColumns === void 0 ? void 0 : _record$selectColumns.name) && 'error-hint'),
        placeholder: "\u8BF7\u8F93\u5165",
        value: text,
        onChange: function onChange(e) {
          return _onChange('originalValue', record, _.trim(e.currentTarget.value));
        },
        maxLength: 100,
        showCount: true
      }), record.error && ((_record$selectColumns2 = record.selectColumns) === null || _record$selectColumns2 === void 0 ? void 0 : _record$selectColumns2.name) && /*#__PURE__*/React.createElement("div", {
        className: "error-hint-text"
      }, "\u3010", record.selectColumns.name, "\u3011\u5B58\u5728\u51B2\u7A81\u89C4\u5219"));
    }
  }, {
    title: /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement("span", {
      className: "requiredIcon"
    }, "*"), "\u53D8\u66F4\u503C"),
    width: 297,
    dataIndex: 'changeValue',
    render: function render(text, record) {
      return /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(Input, {
        placeholder: "\u8BF7\u8F93\u5165",
        value: text,
        onChange: function onChange(e) {
          return _onChange('changeValue', record, _.trim(e.currentTarget.value));
        },
        maxLength: 100,
        showCount: true
      }));
    }
  }, {
    title: '',
    dataIndex: 'set',
    render: function render(_, record) {
      return /*#__PURE__*/React.createElement(IconFont, {
        onClick: function onClick() {
          return deleteHandle(record.key);
        },
        type: "shanchu1"
      });
    }
  }];
  // 删除
  var deleteHandle = function deleteHandle(key) {
    if (columns.length <= 1) {
      message.error('请至少保留一条');
      return;
    }
    Modal.confirm({
      wrapClassName: 'cdp-modal-confirm',
      centered: true,
      title: '提示',
      content: '是否确认删除？',
      icon: /*#__PURE__*/React.createElement(IconFont, {
        type: "tishi1"
      }),
      okText: '确认',
      cancelText: '取消',
      maskClosable: false,
      onOk: function onOk() {
        var _dataSource = [];
        columns.map(function (item) {
          if (item.key !== key) {
            _dataSource.push(item);
          }
        });
        setColumns(_dataSource);
      }
    });
  };
  var addField = function addField() {
    if (columns.length >= 10) {
      message.error('最多可添加10条数据');
      return;
    }
    setColumns([].concat(_toConsumableArray(columns), [_objectSpread(_objectSpread({}, initData), {}, {
      key: util.guid()
    })]));
  };
  useImperativeHandle(ref, function () {
    return {
      getData: function getData() {
        return {
          columns: columns
        };
      }
    };
  });
  return /*#__PURE__*/React.createElement("div", {
    className: classPrefix
  }, /*#__PURE__*/React.createElement(Table, {
    className: "".concat(classPrefix, "-table"),
    columns: tableColumns,
    dataSource: dataSource,
    // loading={loading}
    pagination: false,
    rowKey: "key"
  }), /*#__PURE__*/React.createElement("div", {
    className: "".concat(classPrefix, "-add"),
    onClick: addField
  }, /*#__PURE__*/React.createElement(IconFont, {
    type: "zengjia"
  }), " \u6DFB\u52A0\u5B57\u6BB5"));
});
export default ValueReplace;