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 React, { useEffect, useState, useImperativeHandle } from 'react';
import useNode from '../useNode';
import { Checkbox, Table, Input, Form, Modal, Select } from 'antd';
import { IconFont } from '@zgfe/business-lib';
import './dataClean.less';
import ColumnSelect from '../../../../../components/columnSelect';
import _ from 'lodash';
import { getFieldNameError, getFieldInputErrorInfo } from '../utils';
var IDCard = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
  var node = _ref.node,
    nodeList = _ref.nodeList,
    editData = _ref.editData,
    setSubmit = _ref.setSubmit;
  var classPrefix = 'id-card';
  var nodeData = node.getData();
  var _useNode = useNode(node),
    columnList = _useNode.columnList;
  var _Form$useForm = Form.useForm(),
    _Form$useForm2 = _slicedToArray(_Form$useForm, 1),
    form = _Form$useForm2[0];
  var initData = [{
    checked: false,
    fieldType: '户籍地（省市区）',
    parserType: 'provinceMunicipality',
    name: '',
    alias: '',
    type: 'string',
    separator: ''
  }, {
    checked: false,
    fieldType: '户籍地（省）',
    parserType: 'province',
    name: '',
    alias: '',
    type: 'string'
  }, {
    checked: false,
    fieldType: '户籍地（市）',
    parserType: 'municipality',
    name: '',
    alias: '',
    type: 'string'
  }, {
    checked: false,
    fieldType: '户籍地（区/县）',
    parserType: 'county',
    name: '',
    alias: '',
    type: 'string'
  }, {
    checked: false,
    fieldType: '出生年月日',
    parserType: 'dateOfBirth',
    name: '',
    alias: '',
    type: 'string',
    separator: ''
  }, {
    checked: false,
    fieldType: '性别',
    parserType: 'sex',
    name: '',
    alias: '',
    type: 'string'
  }, {
    checked: false,
    fieldType: '年龄',
    parserType: 'age',
    name: '',
    alias: '',
    type: 'string'
  }];
  var getInitData = function getInitData() {
    var _initData = [];
    initData.map(function (item) {
      var json = item;
      _.cloneDeep(editData === null || editData === void 0 ? void 0 : editData.columns).map(function (_item) {
        if (item.parserType === _item.parserType) {
          json = _item;
        }
      });
      _initData.push(json);
    });
    return _initData;
  };
  var _useState = useState((editData === null || editData === void 0 ? void 0 : editData.columns) ? getInitData() : initData),
    _useState2 = _slicedToArray(_useState, 2),
    dataSource = _useState2[0],
    setDataSource = _useState2[1];
  var _useState3 = useState((editData === null || editData === void 0 ? void 0 : editData.columns) ? getInitData() : initData),
    _useState4 = _slicedToArray(_useState3, 2),
    dataSourceTemp = _useState4[0],
    setDataSourceTemp = _useState4[1];
  var _useState5 = useState(false),
    _useState6 = _slicedToArray(_useState5, 2),
    retain = _useState6[0],
    setRetain = _useState6[1];
  var _useState7 = useState(nodeData),
    _useState8 = _slicedToArray(_useState7, 2),
    formValue = _useState8[0],
    setFormValue = _useState8[1];
  var _useState9 = useState(false),
    _useState10 = _slicedToArray(_useState9, 2),
    show = _useState10[0],
    setShow = _useState10[1];
  var _useState11 = useState({}),
    _useState12 = _slicedToArray(_useState11, 2),
    selectColumns = _useState12[0],
    setSelectColumns = _useState12[1];
  var _useState13 = useState('1'),
    _useState14 = _slicedToArray(_useState13, 2),
    separatorType = _useState14[0],
    setSeparatorType = _useState14[1];
  useEffect(function () {
    // 错误信息判断逻辑处理
    var _list = getFieldNameError(nodeList, columnList, '', editData, dataSourceTemp, 'repeatList');
    dataSourceTemp.map(function (item) {
      // if (_list.indexOf(item.alias) !== -1) {
      //   item.aliasError = '字段名称已存在';
      // } else {
      //   item.aliasError = '';
      // }
      var _temp = getFieldInputErrorInfo(item.name);
      if (_list.indexOf(item.name) !== -1 || _temp) {
        if (_list.indexOf(item.name) !== -1) item.nameError = '字段名称已存在';
        if (_temp) item.nameError = _temp;
      } else {
        item.nameError = '';
      }
    });
    setDataSource(dataSourceTemp);
  }, [dataSourceTemp]);
  // 编辑
  useEffect(function () {
    if (Object.keys(editData).length > 0) {
      setRetain(editData === null || editData === void 0 ? void 0 : editData.retain);
      setSelectColumns(editData === null || editData === void 0 ? void 0 : editData.selectColumns);
    }
  }, []);
  useImperativeHandle(ref, function () {
    return {
      getData: function getData() {
        return {
          selectColumns: selectColumns,
          optField: selectColumns === null || selectColumns === void 0 ? void 0 : selectColumns.name,
          optFieldType: selectColumns === null || selectColumns === void 0 ? void 0 : selectColumns.type,
          webType: selectColumns === null || selectColumns === void 0 ? void 0 : selectColumns.webType,
          retain: retain,
          columns: dataSource.filter(function (item) {
            return item.checked;
          })
        };
      }
    };
  });
  var _useState15 = useState(false),
    _useState16 = _slicedToArray(_useState15, 2),
    selectAll = _useState16[0],
    setSelectAll = _useState16[1];
  var selectAllHandle = function selectAllHandle() {
    var _selectAll = !selectAll;
    setSelectAll(_selectAll);
    var _dataSource = [];
    dataSource.map(function (item) {
      item.checked = _selectAll;
      _dataSource.push(item);
    });
    setDataSource(_dataSource);
  };
  var checkboxChange = function checkboxChange(e, fieldType) {
    var _dataSource = [];
    dataSource.map(function (item) {
      if (item.fieldType === fieldType) {
        item.checked = !item.checked;
      }
      _dataSource.push(item);
    });
    setDataSource(_dataSource);
  };
  useEffect(function () {
    var _fieldNameSubmit = true;
    var _num = 0;
    dataSource.map(function (item) {
      if (item.checked) {
        _num++;
        if (item.name === '') _fieldNameSubmit = false;
      }
      if (item.nameError || item.aliasError) _fieldNameSubmit = false;
    });
    setSelectAll(_num === 7 ? true : false);
    if (Object.keys(selectColumns).length > 0 && _num >= 1 && _fieldNameSubmit) {
      setSubmit(true);
    } else {
      setSubmit(false);
    }
  }, [dataSource, selectColumns, retain]);
  var fieldChange = function fieldChange(value, text, fieldType) {
    var _dataSource = [];
    dataSource.map(function (item) {
      if (item.fieldType === fieldType) {
        item[text] = value;
      }
      _dataSource.push(item);
    });
    setDataSourceTemp(_dataSource);
  };
  var tableColumns = [{
    title: /*#__PURE__*/React.createElement(Checkbox, {
      checked: selectAll,
      onChange: selectAllHandle
    }),
    dataIndex: 'checked',
    render: function render(text, record) {
      return /*#__PURE__*/React.createElement(Checkbox, {
        checked: text,
        onChange: function onChange(e) {
          return checkboxChange(e.target.checked, record.fieldType);
        }
      });
    }
  }, {
    title: '字段类型',
    width: 224,
    dataIndex: 'fieldType'
  }, {
    title: /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement("span", {
      className: "requiredIcon"
    }, "*"), "\u5B57\u6BB5\u540D\u79F0"),
    width: 224,
    dataIndex: 'name',
    render: function render(text, record) {
      return /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(Input, {
        placeholder: "\u8BF7\u8F93\u5165",
        className: "".concat(record.nameError && 'error-hint'),
        value: text,
        maxLength: 200,
        showCount: true,
        onChange: function onChange(e) {
          return fieldChange(_.trim(e.currentTarget.value), 'name', record.fieldType);
        }
      }), record.nameError && /*#__PURE__*/React.createElement("div", {
        className: "error-hint-text"
      }, record.nameError));
    }
  }, {
    title: '注释',
    width: 224,
    dataIndex: 'alias',
    render: function render(text, record) {
      return /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(Input, {
        placeholder: "\u8BF7\u8F93\u5165",
        // className={`${record.aliasError && 'error-hint'}`}
        value: text,
        maxLength: 200,
        showCount: true,
        onChange: function onChange(e) {
          return fieldChange(_.trim(e.currentTarget.value), 'alias', record.fieldType);
        }
      }));
    }
  }, {
    title: '操作',
    dataIndex: 'set',
    render: function render(_, record) {
      if (record.parserType === 'provinceMunicipality' || record.parserType === 'dateOfBirth') {
        return /*#__PURE__*/React.createElement("div", {
          style: {
            color: '#165DFF',
            cursor: 'pointer',
            marginLeft: '12px'
          },
          onClick: function onClick() {
            var _temp = ['', ';', ',', '/', '-'];
            setSeparatorType(record.parserType === 'provinceMunicipality' ? '1' : '2');
            if (record.separator) {
              var _data = _temp.indexOf(record.separator) !== -1 ? {
                separator: record.separator
              } : {
                separator: 'custom',
                customSeparator: record.separator
              };
              setFormValue(_data);
              form.resetFields();
              form.setFieldsValue(_objectSpread({}, _data));
            } else {
              setFormValue({
                separator: ''
              });
              form.resetFields();
              form.setFieldsValue({
                separator: ''
              });
            }
            setShow(true);
          }
        }, "\u8BBE\u7F6E\u5206\u9694\u7B26");
      }
    }
  }];
  var separatorHandle = function separatorHandle() {
    form.validateFields().then(function () {
      var _dataSource = _.cloneDeep(dataSource);
      _dataSource[separatorType === '1' ? 0 : 4].separator = formValue.separator === 'custom' ? formValue.customSeparator : formValue.separator;
      setDataSource(_dataSource);
      setShow(false);
    });
  };
  var onValuesChange = function onValuesChange(_, allData) {
    setFormValue(_objectSpread({}, allData));
  };
  return /*#__PURE__*/React.createElement("div", {
    className: classPrefix
  }, /*#__PURE__*/React.createElement(Form.Item, {
    label: "\u9009\u62E9\u5B57\u6BB5",
    colon: false,
    labelAlign: "right"
  }, /*#__PURE__*/React.createElement(ColumnSelect, {
    options: columnList.filter(function (item) {
      return ['number', 'string'].includes(item.webType);
    }),
    value: selectColumns,
    onChange: setSelectColumns,
    multiple: false
  })), /*#__PURE__*/React.createElement("div", {
    className: "".concat(classPrefix, "-table-title")
  }, "\u63D0\u53D6\u5B57\u6BB5"), /*#__PURE__*/React.createElement(Table, {
    columns: tableColumns,
    dataSource: dataSource,
    // loading={loading}
    pagination: false,
    rowKey: "fieldType"
  }), /*#__PURE__*/React.createElement(Modal, {
    title: /*#__PURE__*/React.createElement("div", null, "\u8BBE\u7F6E", /*#__PURE__*/React.createElement(IconFont, {
      type: "qingchu",
      onClick: function onClick() {
        return setShow(false);
      }
    })),
    wrapClassName: "".concat(classPrefix, "-set-separator"),
    destroyOnClose: true,
    getContainer: false,
    open: show,
    okText: "\u786E\u5B9A",
    cancelText: "\u53D6\u6D88",
    onOk: separatorHandle,
    onCancel: function onCancel() {
      setShow(false);
    }
  }, /*#__PURE__*/React.createElement(Form, {
    form: form,
    onValuesChange: onValuesChange,
    initialValues: _objectSpread(_objectSpread({}, formValue), {}, {
      separator: (formValue === null || formValue === void 0 ? void 0 : formValue.separator) ? formValue === null || formValue === void 0 ? void 0 : formValue.separator : ''
    })
  }, /*#__PURE__*/React.createElement(Form.Item, {
    label: "\u5206\u9694\u7B26",
    name: "separator",
    colon: false
  }, /*#__PURE__*/React.createElement(Select, {
    suffixIcon: /*#__PURE__*/React.createElement(IconFont, {
      type: "xiangxia",
      style: {
        color: '#5F6085'
      }
    }),
    options: [{
      label: '无',
      value: ''
    }, {
      label: ';',
      value: ';'
    }, {
      label: ',',
      value: ','
    }, {
      label: '/',
      value: '/'
    }, {
      label: '-',
      value: '-'
    }, {
      label: '自定义',
      value: 'custom'
    }]
  })), formValue.separator === 'custom' && /*#__PURE__*/React.createElement(Form.Item, {
    label: "\u81EA\u5B9A\u4E49\u5206\u9694\u7B26",
    name: "customSeparator",
    colon: false,
    rules: [{
      required: true,
      message: '请输入自定义分隔符'
    }],
    getValueFromEvent: function getValueFromEvent(event) {
      return event.target.value.replace(/\s+/g, '');
    }
  }, /*#__PURE__*/React.createElement(Input, {
    placeholder: "\u8BF7\u8F93\u5165\u81EA\u5B9A\u4E49\u5206\u9694\u7B26",
    maxLength: 5,
    showCount: true
  })))));
});
export default IDCard;