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 { BizSelect, BizCycleTime, IconFont } from '@zgfe/business-lib';
import { Form, Radio, Spin, Tooltip, AutoComplete } from 'antd';
import React, { useContext, useEffect, useRef, useState } from 'react';
import TaskContext from '../../modules/createJob/context';
import ZModulesCDP from '../../types';
import { timeUnitList } from '../../modules/dataFusion/components/fusionModal/utils';
import './styles/updateForm.less';
import _ from 'lodash';
var classPrefix = 'cdp-dscreator-update';
export var SyncType = function SyncType(props) {
  var columnList = props.columnList,
    dbType = props.dbType;
  var _useContext = useContext(TaskContext),
    mode = _useContext.mode;
  var _useState = useState(ZModulesCDP.DataSet.SyncType.global),
    _useState2 = _slicedToArray(_useState, 2),
    syncType = _useState2[0],
    setSyncType = _useState2[1];
  var _useState3 = useState(),
    _useState4 = _slicedToArray(_useState3, 2),
    column = _useState4[0],
    setColumn = _useState4[1];
  var _useState5 = useState([]),
    _useState6 = _slicedToArray(_useState5, 2),
    columns = _useState6[0],
    setColumns = _useState6[1];
  // const [selectDateFormat, setSelectDateFormat] = useState('yyyy-MM-dd');
  var _useState7 = useState('yyyy-MM-dd'),
    _useState8 = _slicedToArray(_useState7, 2),
    dateFormat = _useState8[0],
    setDateFormat = _useState8[1];
  var _useState9 = useState(false),
    _useState10 = _slicedToArray(_useState9, 2),
    onChangeFlag = _useState10[0],
    setonChangeFlag = _useState10[1];
  /**
   * 初始化表单
   */
  useEffect(function () {
    var _props$value, _props$value3;
    if ((_props$value = props.value) === null || _props$value === void 0 ? void 0 : _props$value.syncType) {
      var _props$value2;
      // if (mode === 'create') return;
      setSyncType((_props$value2 = props.value) === null || _props$value2 === void 0 ? void 0 : _props$value2.syncType);
    }
    if ((_props$value3 = props.value) === null || _props$value3 === void 0 ? void 0 : _props$value3.updateBy) {
      var _props$value4, _props$value5, _props$value5$updateB;
      setColumn((_props$value4 = props.value) === null || _props$value4 === void 0 ? void 0 : _props$value4.updateBy);
      setDateFormat((_props$value5 = props.value) === null || _props$value5 === void 0 ? void 0 : (_props$value5$updateB = _props$value5.updateBy) === null || _props$value5$updateB === void 0 ? void 0 : _props$value5$updateB.dateFormat);
    }
  }, []);
  useEffect(function () {
    if (dbType && /mongodb|hive|api/i.test(dbType)) {
      setSyncType(ZModulesCDP.DataSet.SyncType.global);
    }
  }, [dbType]);
  useEffect(function () {
    if (!onChangeFlag) {
      return;
    }
    var _temp = {};
    if ((column === null || column === void 0 ? void 0 : column.webType) === 'date') {
      _temp = {
        dateFormat: dateFormat
      };
    }
    if (props.onChange) props.onChange({
      syncType: syncType,
      updateBy: _objectSpread(_objectSpread({}, column), _temp)
    });
  }, [column, syncType, dateFormat, onChangeFlag]);
  useEffect(function () {
    setColumns(columnList.filter(function (item) {
      return ['number', 'date'].includes(item.webType);
    }));
  }, [columnList]);
  useEffect(function () {
    if (!column) return;
    var match = false;
    columns.forEach(function (item) {
      if (item.name === column.name) match = true;
    });
    if (!match && dbType !== 'excel') setColumn(undefined);
  }, [columns]);
  var _useState11 = useState(''),
    _useState12 = _slicedToArray(_useState11, 2),
    errorTip = _useState12[0],
    setErrorTip = _useState12[1];
  var onChangeFormat = function onChangeFormat(data) {
    var inputValue = _.trim(data);
    setonChangeFlag(true);
    setDateFormat(inputValue);
    if (!inputValue) {
      setErrorTip('请选择时间格式');
    } else {
      setErrorTip('');
    }
  };
  return /*#__PURE__*/React.createElement("div", {
    className: "".concat(classPrefix, "-sync")
  }, /*#__PURE__*/React.createElement(Radio.Group, {
    value: syncType,
    onChange: function onChange(e) {
      setonChangeFlag(true);
      setSyncType(e.target.value);
    }
  }, /*#__PURE__*/React.createElement(Radio, {
    value: ZModulesCDP.DataSet.SyncType.incre,
    disabled: /mongodb|hive|api/i.test(dbType || '')
  }, "\u589E\u91CF\u66F4\u65B0", /*#__PURE__*/React.createElement(Tooltip, {
    title: "\u589E\u91CF\u66F4\u65B0\u8868\u793A\u4EFB\u52A1\u53EA\u66F4\u65B0\u53D1\u751F\u53D8\u5316\u7684\u6570\u636E\uFF0C\u53EA\u80FD\u9009\u62E9\u65E5\u671F\u548C\u6570\u503C\u7C7B\u578B\u7684\u5B57\u6BB5\u3002"
  }, /*#__PURE__*/React.createElement(IconFont, {
    type: "tishi",
    style: {
      marginLeft: '4px',
      color: '#9aa1a9',
      verticalAlign: '-1px'
    }
  }))), /*#__PURE__*/React.createElement(Radio, {
    value: ZModulesCDP.DataSet.SyncType.global
  }, "\u5168\u91CF\u66F4\u65B0")), /*#__PURE__*/React.createElement("div", {
    className: "".concat(classPrefix, "-incre-box")
  }, syncType === ZModulesCDP.DataSet.SyncType.incre && /*#__PURE__*/React.createElement(BizSelect, {
    options: dbType === 'excel' ? [{
      name: 'createTime',
      label: '按文件创建日期'
    }, {
      name: 'updateTime',
      label: '按文件修改日期'
    }] : columns,
    keyField: "name",
    labelField: dbType === 'excel' ? 'label' : 'name',
    placeholder: "\u9009\u62E9\u5B57\u6BB5",
    enableSearch: true,
    value: column,
    disable: syncType !== ZModulesCDP.DataSet.SyncType.incre,
    customLabel: function customLabel(label, data) {
      return /*#__PURE__*/React.createElement(React.Fragment, null, label, data.isPrimaryKey ? '(主键)' : '');
    },
    onChange: function onChange(v) {
      setonChangeFlag(true);
      setColumn(v);
    }
  }), (column === null || column === void 0 ? void 0 : column.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: [{
      value: 'yyyyMMdd',
      label: 'yyyyMMdd'
    }, {
      value: 'yyyy-MM-dd',
      label: 'yyyy-MM-dd'
    }, {
      value: 'yyyy/MM/dd',
      label: 'yyyy/MM/dd'
    }],
    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))));
};
export var UpdateSetting = function UpdateSetting(props) {
  var _useState13 = useState({}),
    _useState14 = _slicedToArray(_useState13, 2),
    timeData = _useState14[0],
    setTimeData = _useState14[1];
  useEffect(function () {
    if (props.value) {
      setTimeData(props.value);
    } else {
      setTimeData({
        type: ZModulesCDP.DataSet.UpdateType.timer
      });
    }
  }, []);
  useEffect(function () {
    if (props.onChange) props.onChange(timeData);
  }, [timeData]);
  return /*#__PURE__*/React.createElement("div", {
    className: "".concat(classPrefix, "-update")
  }, /*#__PURE__*/React.createElement(Radio.Group, {
    value: timeData.type,
    onChange: function onChange(e) {
      setTimeData(_objectSpread(_objectSpread({}, timeData), {}, {
        type: e.target.value
      }));
    }
  }, /*#__PURE__*/React.createElement(Radio, {
    value: ZModulesCDP.DataSet.UpdateType.mumal
  }, "\u624B\u52A8"), /*#__PURE__*/React.createElement(Radio, {
    value: ZModulesCDP.DataSet.UpdateType.timer
  }, "\u5B9A\u65F6")), timeData.type === ZModulesCDP.DataSet.UpdateType.timer ? /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(BizCycleTime, {
    value: timeData.rule,
    size: "middle",
    multiple: true,
    showHour: true,
    format: "HH",
    minTime: "Now",
    options: timeUnitList,
    onChange: function onChange(val) {
      setTimeData(_objectSpread(_objectSpread({}, timeData), {}, {
        rule: {
          unit: val.unit,
          day: val.day,
          time: val.time
        }
      }));
    }
  })) : null);
};
var UpdateForm = function UpdateForm(props) {
  var _useContext2 = useContext(TaskContext),
    mode = _useContext2.mode;
  var columnList = props.columnList,
    task = props.task;
  var formRef = useRef();
  function onChange() {
    var _formRef$current;
    (_formRef$current = formRef.current) === null || _formRef$current === void 0 ? void 0 : _formRef$current.submit();
  }
  function onSubmit(data) {
    if (props.onChange) props.onChange(data);
  }
  var initialValues = {
    sync: {
      syncType: task === null || task === void 0 ? void 0 : task.syncType,
      updateBy: task === null || task === void 0 ? void 0 : task.updateBy
    },
    updateSetting: task === null || task === void 0 ? void 0 : task.updateSetting
  };
  if (mode === 'edit' && props.loading) {
    return /*#__PURE__*/React.createElement(Spin, null);
  }
  return /*#__PURE__*/React.createElement("div", {
    className: classPrefix
  }, /*#__PURE__*/React.createElement("div", null, "\u66F4\u65B0\u7B56\u7565"), /*#__PURE__*/React.createElement(Form, {
    colon: false,
    ref: formRef,
    onFinish: onSubmit,
    initialValues: mode === 'edit' ? initialValues : undefined,
    autoComplete: "off"
  }, /*#__PURE__*/React.createElement(Form.Item, {
    label: "\u66F4\u65B0\u65B9\u5F0F",
    name: "sync"
  }, /*#__PURE__*/React.createElement(SyncType, {
    columnList: columnList,
    onChange: onChange,
    dbType: props.dbType
  })), /*#__PURE__*/React.createElement(Form.Item, {
    label: "\u66F4\u65B0\u8BBE\u7F6E",
    name: "updateSetting"
  }, /*#__PURE__*/React.createElement(UpdateSetting, {
    onChange: onChange
  }))));
};
export default UpdateForm;