function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _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(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _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(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
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;