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 { BizSelect, IconFont } from '@zgfe/business-lib';
import React, { useEffect, useState } from 'react';
import './index.less';
import { AutoComplete, Tooltip } from 'antd';
import { dateFormatList } from '../utils';
var IncrementField = function IncrementField(props) {
  var _props$value, _props$tableColumns;
  var classPrefix = 'fusion-increment';
  var _useState = useState(props.value),
    _useState2 = _slicedToArray(_useState, 2),
    fieldValue = _useState2[0],
    setFieldValue = _useState2[1];
  var _useState3 = useState(((_props$value = props.value) === null || _props$value === void 0 ? void 0 : _props$value.format) || dateFormatList[0].value),
    _useState4 = _slicedToArray(_useState3, 2),
    dateFormat = _useState4[0],
    setDateFormat = _useState4[1];
  var _useState5 = useState(''),
    _useState6 = _slicedToArray(_useState5, 2),
    errorTip = _useState6[0],
    setErrorTip = _useState6[1];
  var _useState7 = useState(true),
    _useState8 = _slicedToArray(_useState7, 2),
    isFirst = _useState8[0],
    setIsFirst = _useState8[1];
  useEffect(function () {
    var _props$value2;
    if (isFirst) {
      setIsFirst(false);
      return;
    }
    setFieldValue(props.value);
    setDateFormat((_props$value2 = props.value) === null || _props$value2 === void 0 ? void 0 : _props$value2.format);
  }, [props.value]);
  // 增量字段改变
  var _onChange = function onChange(data) {
    setFieldValue(data);
    if ((data === null || data === void 0 ? void 0 : data.webType) === 'date') {
      setDateFormat(dateFormatList[0].value);
      setErrorTip('');
    }
    props.onChange && props.onChange({
      name: data === null || data === void 0 ? void 0 : data.name,
      webType: data === null || data === void 0 ? void 0 : data.webType,
      format: (data === null || data === void 0 ? void 0 : data.webType) === 'date' ? dateFormatList[0].value : undefined
    });
  };
  var onChangeFormat = function onChangeFormat(data) {
    var inputValue = data === null || data === void 0 ? void 0 : data.trim();
    setDateFormat(inputValue);
    props.onChange && props.onChange({
      name: fieldValue === null || fieldValue === void 0 ? void 0 : fieldValue.name,
      webType: fieldValue === null || fieldValue === void 0 ? void 0 : fieldValue.webType,
      format: inputValue
    });
    if (!inputValue) {
      setErrorTip('请选择时间格式');
    } else {
      setErrorTip('');
    }
  };
  return /*#__PURE__*/React.createElement("div", {
    className: classPrefix
  }, /*#__PURE__*/React.createElement(BizSelect, {
    value: {
      name: fieldValue === null || fieldValue === void 0 ? void 0 : fieldValue.name
    },
    options: ((_props$tableColumns = props.tableColumns) === null || _props$tableColumns === void 0 ? void 0 : _props$tableColumns.filter(function (item) {
      return ['number', 'date'].includes(item.webType);
    })) || [],
    enableSearch: true,
    labelField: "name",
    keyField: "name",
    className: "".concat(classPrefix, "-wd260"),
    onChange: function onChange(data) {
      return _onChange(data);
    }
  }), (fieldValue === null || fieldValue === void 0 ? void 0 : fieldValue.webType) === 'date' ? /*#__PURE__*/React.createElement("div", {
    className: "".concat(classPrefix, "-date")
  }, /*#__PURE__*/React.createElement(AutoComplete, {
    allowClear: true,
    maxLength: 20,
    popupClassName: "".concat(classPrefix, "-popup"),
    value: dateFormat,
    style: {
      width: 230
    },
    options: dateFormatList,
    placeholder: "\u8BF7\u8F93\u5165\u65E5\u671F\u683C\u5F0F",
    onChange: onChangeFormat
  }), /*#__PURE__*/React.createElement(Tooltip, {
    title: "\u201Cy\u201D\u4EE3\u8868\u201C\u5E74\u201D\uFF0C\u201CM\u201D\u4EE3\u8868\u201C\u6708\u201D\uFF0C\u201Cd\u201D\u4EE3\u8868\u201C\u5929\u201D\uFF0C\u4F8B\u5982\u683C\u5F0F\u201Cyyyy-MM-dd\u201D\u4EE3\u8868\u201C2023-01-01\u201D"
  }, /*#__PURE__*/React.createElement(IconFont, {
    className: "".concat(classPrefix, "-icon"),
    type: "tishi"
  })), errorTip && /*#__PURE__*/React.createElement("div", {
    className: "".concat(classPrefix, "-date-tip")
  }, errorTip)) : null);
};
export default IncrementField;