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 } from '@zgfe/business-lib';
import { CycleTimeUnit } from '@zgfe/business-lib/es/cycleTime';
import { Form, Input, Radio, Space, Spin } from 'antd';
import React, { useContext, useEffect, useRef, useState } from 'react';
import TaskContext from '../../modules/createJob/context';
import ZModulesCDP from '../../types';
import './styles/baseForm.less';
var classPrefix = 'cdp-dscreator-base';
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];
  /**
   * 初始化表单
   */
  useEffect(function () {
    var _props$value;
    if (mode === 'create') return;
    setSyncType((_props$value = props.value) === null || _props$value === void 0 ? void 0 : _props$value.syncType);
    if (dbType === 'excel') {
      var _props$value2;
      setColumn((_props$value2 = props.value) === null || _props$value2 === void 0 ? void 0 : _props$value2.updateBy);
    } else {
      columnList.forEach(function (item) {
        var _props$value3, _props$value3$updateB;
        if (item.name === ((_props$value3 = props.value) === null || _props$value3 === void 0 ? void 0 : (_props$value3$updateB = _props$value3.updateBy) === null || _props$value3$updateB === void 0 ? void 0 : _props$value3$updateB.name)) setColumn(item);
      });
    }
  }, []);
  useEffect(function () {
    if (dbType && /mongodb|hive|api/i.test(dbType)) {
      setSyncType(ZModulesCDP.DataSet.SyncType.global);
    }
  }, [dbType]);
  useEffect(function () {
    if (props.onChange) props.onChange({
      syncType: syncType,
      updateBy: column
    });
  }, [column, syncType]);
  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]);
  return /*#__PURE__*/React.createElement("div", {
    className: "".concat(classPrefix, "-sync")
  }, /*#__PURE__*/React.createElement(Radio.Group, {
    value: syncType,
    onChange: function onChange(e) {
      setSyncType(e.target.value);
    }
  }, /*#__PURE__*/React.createElement(Space, {
    direction: "vertical",
    size: 24
  }, /*#__PURE__*/React.createElement(Radio, {
    value: ZModulesCDP.DataSet.SyncType.incre,
    disabled: /mongodb|hive|api/i.test(dbType || '')
  }, "\u589E\u91CF\u66F4\u65B0"), /*#__PURE__*/React.createElement(Radio, {
    value: ZModulesCDP.DataSet.SyncType.global
  }, "\u5168\u91CF\u66F4\u65B0"))), /*#__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) {
      setColumn(v);
    }
  }));
};
export var UpdateSetting = function UpdateSetting(props) {
  var _props$value4;
  var _useState7 = useState(),
    _useState8 = _slicedToArray(_useState7, 2),
    type = _useState8[0],
    setType = _useState8[1];
  var _useState9 = useState((_props$value4 = props.value) === null || _props$value4 === void 0 ? void 0 : _props$value4.rule),
    _useState10 = _slicedToArray(_useState9, 2),
    rule = _useState10[0],
    setRule = _useState10[1];
  useEffect(function () {
    if (!props.value) {
      setType(ZModulesCDP.DataSet.UpdateType.timer);
      return;
    }
    setRule(props.value.rule);
    setType(props.value.type);
  }, []);
  useEffect(function () {
    var res = {
      type: type,
      rule: rule
    };
    if ((rule === null || rule === void 0 ? void 0 : rule.unit) === CycleTimeUnit.day) res.rule.time = '00:00:00';
    if (props.onChange) props.onChange(res);
  }, [type, rule]);
  return /*#__PURE__*/React.createElement("div", {
    className: "".concat(classPrefix, "-update")
  }, /*#__PURE__*/React.createElement(Radio.Group, {
    value: type,
    onChange: function onChange(e) {
      setType(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")), type === ZModulesCDP.DataSet.UpdateType.timer ? /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(BizCycleTime, {
    size: "middle",
    value: rule,
    multiple: true,
    onChange: function onChange(val) {
      setRule({
        unit: val.unit,
        day: val.day,
        time: val.time
      });
    }
  }), /*#__PURE__*/React.createElement("span", null, "00:00:00")) : null);
};
var BaseForm = function BaseForm(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 = {
    jobName: task === null || task === void 0 ? void 0 : task.jobName,
    jobDesc: task === null || task === void 0 ? void 0 : task.jobDesc,
    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(Form, {
    colon: false,
    className: classPrefix,
    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
  })), /*#__PURE__*/React.createElement(Form.Item, {
    label: "\u4EFB\u52A1\u540D\u79F0",
    name: "jobName"
  }, /*#__PURE__*/React.createElement(Input, {
    width: 574,
    placeholder: "\u8BF7\u8F93\u5165\u4EFB\u52A1\u540D\u79F0",
    onChange: onChange
  })), /*#__PURE__*/React.createElement(Form.Item, {
    label: "\u4EFB\u52A1\u63CF\u8FF0",
    name: "jobDesc",
    rules: []
  }, /*#__PURE__*/React.createElement(Input.TextArea, {
    rows: 3,
    placeholder: "\u8F93\u5165\u6587\u5B57\u63CF\u8FF0\u60A8\u8981\u521B\u5EFA\u7684\u4EFB\u52A1",
    onChange: onChange
  })));
};
export default BaseForm;