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, useRef } from 'react';
import useNode from './../useNode';
import { Form, Table, Input, Drawer, Button, Modal, Select, Tooltip } from 'antd';
import { IconFont, util } from '@zgfe/business-lib';
import './dataClean.less';
import ConfigLoading from './../configLoading';
import ValueReplace from './valueReplace';
import IDCard from './idCard';
import DateToAge from './dateToAge';
import _ from 'lodash';
var DataCleanConfig = function DataCleanConfig(_ref) {
  var node = _ref.node,
    nodeList = _ref.nodeList;
  var classPrefix = 'dataClean-config';
  var _useNode = useNode(node),
    loading = _useNode.loading;
  var nodeData = node.getData();
  var _useState = useState(nodeData.label),
    _useState2 = _slicedToArray(_useState, 2),
    actName = _useState2[0],
    setActName = _useState2[1];
  var _useState3 = useState(false),
    _useState4 = _slicedToArray(_useState3, 2),
    open = _useState4[0],
    setOpen = _useState4[1];
  var valueReplaceRef = useRef(null);
  var IDCardRef = useRef(null);
  var DateToAgeRef = useRef(null);
  var _useState5 = useState(false),
    _useState6 = _slicedToArray(_useState5, 2),
    submit = _useState6[0],
    setSubmit = _useState6[1];
  var ruleTypeInit = {
    valueReplace: '值替换',
    idCardParser: '身份证号提取',
    ageBirthdayConvert: '出生日期转年龄'
  };
  var _useState7 = useState('valueReplace'),
    _useState8 = _slicedToArray(_useState7, 2),
    ruleType = _useState8[0],
    setRuleType = _useState8[1];
  var _useState9 = useState('值替换'),
    _useState10 = _slicedToArray(_useState9, 2),
    ruleName = _useState10[0],
    setRuleName = _useState10[1];
  var _useState11 = useState(nodeData.list ? nodeData.list : []),
    _useState12 = _slicedToArray(_useState11, 2),
    list = _useState12[0],
    setList = _useState12[1];
  var _useState13 = useState({}),
    _useState14 = _slicedToArray(_useState13, 2),
    editData = _useState14[0],
    setEditData = _useState14[1];
  useEffect(function () {
    node.updateData({
      status: actName && list.length > 0 ? 'success' : 'failed',
      list: list,
      label: actName
    });
  }, [list, actName]);
  var columns = [{
    title: '规则名称',
    dataIndex: 'ruleName'
  }, {
    title: '包含字段',
    dataIndex: 'field'
  }, {
    title: '操作',
    dataIndex: 'set',
    render: function render(_, record) {
      return /*#__PURE__*/React.createElement("div", {
        className: "".concat(classPrefix, "-table-set")
      }, /*#__PURE__*/React.createElement("div", {
        onClick: function onClick() {
          return editHandle(record);
        }
      }, "\u7F16\u8F91"), /*#__PURE__*/React.createElement("div", {
        onClick: function onClick() {
          return deleteHandle(record.key);
        }
      }, "\u5220\u9664"));
    }
  }];
  // 保存
  var onSave = function onSave() {
    var json = {
      valueReplace: valueReplaceRef,
      idCardParser: IDCardRef,
      ageBirthdayConvert: DateToAgeRef
    };
    if (Object.keys(editData).length <= 0) {
      var _json$ruleType, _json$ruleType$curren;
      add((_json$ruleType = json[ruleType]) === null || _json$ruleType === void 0 ? void 0 : (_json$ruleType$curren = _json$ruleType.current) === null || _json$ruleType$curren === void 0 ? void 0 : _json$ruleType$curren.getData());
    } else {
      var _json$ruleType2, _json$ruleType2$curre;
      edit((_json$ruleType2 = json[ruleType]) === null || _json$ruleType2 === void 0 ? void 0 : (_json$ruleType2$curre = _json$ruleType2.current) === null || _json$ruleType2$curre === void 0 ? void 0 : _json$ruleType2$curre.getData());
    }
    setOpen(false);
  };
  // 新增
  var add = function add(data) {
    var _data = _objectSpread(_objectSpread({}, data), {}, {
      ruleType: ruleType,
      ruleName: ruleName,
      key: util.guid(),
      field: ruleType === 'valueReplace' ? getField(data) : data.selectColumns[0].name
    });
    setList([_objectSpread({}, _data)].concat(_toConsumableArray(list)));
  };
  // 编辑
  var edit = function edit(data) {
    var _dataSource = [];
    list.map(function (item) {
      if (item.key === (editData === null || editData === void 0 ? void 0 : editData.key)) {
        item = _objectSpread(_objectSpread({}, item), {}, {
          ruleName: ruleName
        }, data);
      }
      _dataSource.push(item);
    });
    setList(_dataSource);
  };
  var getField = function getField(data) {
    var _data$columns;
    var _field = [];
    data === null || data === void 0 ? void 0 : (_data$columns = data.columns) === null || _data$columns === void 0 ? void 0 : _data$columns.map(function (item) {
      if (_field.indexOf(item.name) === -1) {
        _field.push(item.name);
      }
    });
    return _field.join(',');
  };
  var deleteHandle = function deleteHandle(key) {
    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 _list = [];
        list.map(function (item) {
          if (item.key !== key) {
            _list.push(item);
          }
        });
        setList(_list);
      }
    });
  };
  var editHandle = function editHandle(record) {
    setRuleName(record.ruleName);
    setEditData(record);
    setRuleType(record.ruleType);
    setOpen(true);
  };
  // 添加规则弹窗
  var addRuleDrawer = function addRuleDrawer() {
    setRuleName('值替换');
    setEditData({});
    setRuleType('valueReplace');
    setOpen(true);
  };
  if (loading) {
    return /*#__PURE__*/React.createElement(ConfigLoading, null);
  }
  return /*#__PURE__*/React.createElement("div", {
    className: classPrefix,
    id: classPrefix
  }, /*#__PURE__*/React.createElement(Form, {
    wrapperCol: {
      span: 12
    }
  }, /*#__PURE__*/React.createElement(Form.Item, {
    label: "\u52A8\u4F5C\u540D\u79F0",
    colon: false,
    className: "action-name-box ".concat(actName === '' && 'error')
  }, /*#__PURE__*/React.createElement(Input, {
    value: actName,
    maxLength: 50,
    showCount: true,
    onChange: function onChange(e) {
      return setActName(_.trim(e.currentTarget.value));
    }
  }), actName === '' && /*#__PURE__*/React.createElement("div", {
    className: "action-name-box-error"
  }, "\u8BF7\u8F93\u5165\u52A8\u4F5C\u540D\u79F0"))), /*#__PURE__*/React.createElement("div", {
    className: "".concat(classPrefix, "-ruleList")
  }, /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement("div", null, "\u6E05\u6D17\u89C4\u5219"), /*#__PURE__*/React.createElement(IconFont, {
    type: "chuangjian",
    onClick: addRuleDrawer
  }), /*#__PURE__*/React.createElement("div", {
    onClick: addRuleDrawer
  }, "\u65B0\u589E")), /*#__PURE__*/React.createElement(Table, {
    className: "".concat(classPrefix, "-table"),
    columns: columns,
    dataSource: list,
    // loading={loading}
    rowKey: "key"
  }), /*#__PURE__*/React.createElement(Drawer, {
    className: "".concat(classPrefix, "-drawer"),
    title: /*#__PURE__*/React.createElement("div", null, "\u65B0\u5EFA\u89C4\u5219 ", /*#__PURE__*/React.createElement(IconFont, {
      type: "qingchu"
    })),
    placement: "right",
    destroyOnClose: true,
    onClose: function onClose() {
      return setOpen(false);
    },
    open: open
  }, /*#__PURE__*/React.createElement(Form.Item, {
    label: /*#__PURE__*/React.createElement("div", null, "\u6E05\u6D17\u89C4\u5219"),
    colon: false,
    labelCol: {
      span: ruleType === 'ageBirthdayConvert' ? 3 : 2
    },
    wrapperCol: {
      span: 18
    },
    labelAlign: "right"
  }, /*#__PURE__*/React.createElement(Select, {
    suffixIcon: /*#__PURE__*/React.createElement(IconFont, {
      type: "xiangxia",
      style: {
        color: '#5F6085'
      }
    }),
    bordered: false,
    disabled: Object.keys(editData).length <= 0 ? false : true,
    options: [{
      label: '值替换',
      value: 'valueReplace'
    }, {
      label: '身份证号提取',
      value: 'idCardParser'
    }, {
      label: '出生日期转年龄',
      value: 'ageBirthdayConvert'
    }],
    value: ruleType,
    onChange: function onChange(e) {
      setRuleName(ruleTypeInit[e]);
      setRuleType(e);
    }
  }), /*#__PURE__*/React.createElement(Tooltip, {
    title: function title() {
      switch (ruleType) {
        case 'valueReplace':
          return '用于表内字段值的替换处理，例如：表A的性别字段，原始值为0、1、2，支持替换为未知、男、女；';
        case 'idCardParser':
          return '支持将表内身份证号字段提取姓名、户籍地、性别、出生年月日、年龄数据；';
        case 'ageBirthdayConvert':
          return '支持将出生日期字段转换为年龄字段；';
      }
    }
  }, /*#__PURE__*/React.createElement(IconFont, {
    type: "tishi"
  }))), /*#__PURE__*/React.createElement(Form.Item, {
    label: "\u89C4\u5219\u540D\u79F0",
    colon: false,
    labelCol: {
      span: ruleType === 'ageBirthdayConvert' ? 3 : 2
    },
    wrapperCol: {
      span: 18
    },
    labelAlign: "right"
  }, /*#__PURE__*/React.createElement(Input, {
    placeholder: "\u8BF7\u8F93\u5165\u89C4\u5219\u540D\u79F0",
    maxLength: 50,
    showCount: true,
    value: ruleName,
    onChange: function onChange(e) {
      return setRuleName(_.trim(e.currentTarget.value));
    }
  })), ruleType === 'valueReplace' && /*#__PURE__*/React.createElement(ValueReplace, {
    node: node,
    ref: valueReplaceRef,
    editData: editData,
    setSubmit: setSubmit
  }), ruleType === 'idCardParser' && /*#__PURE__*/React.createElement(IDCard, {
    node: node,
    nodeList: nodeList,
    ref: IDCardRef,
    editData: editData,
    setSubmit: setSubmit
  }), ruleType === 'ageBirthdayConvert' && /*#__PURE__*/React.createElement(DateToAge, {
    node: node,
    nodeList: nodeList,
    ref: DateToAgeRef,
    editData: editData,
    setSubmit: setSubmit
  }), /*#__PURE__*/React.createElement("div", {
    className: "".concat(classPrefix, "-drawer-submit")
  }, /*#__PURE__*/React.createElement(Button, {
    type: "default",
    onClick: function onClick() {
      return setOpen(false);
    }
  }, "\u53D6\u6D88"), /*#__PURE__*/React.createElement(Button, {
    type: "primary",
    disabled: !submit,
    onClick: onSave
  }, "\u4FDD\u5B58")))));
};
export default DataCleanConfig;