function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _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(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
import { Form, Input, Radio, Spin } from 'antd';
import React, { useEffect, useRef, useState } from 'react';
import { SyncType, UpdateSetting } from '../../../../components/job/updateForm';
import apis from '../../../../constants/api';
import ZModulesCDP from '../../../../types';
import CdpFlowTypes from '../../../../types/flow';
import request from '../../../../utils/ajax';
import Request from 'umi-request';
import '../styles/global.less';
var FlowGlobalConfig = function FlowGlobalConfig(props) {
  var _props$value, _props$value2, _props$value3, _props$value4, _props$value5, _props$value6, _props$value7;
  var formRef = useRef(null);
  var _useState = useState([]),
    _useState2 = _slicedToArray(_useState, 2),
    columnList = _useState2[0],
    setColumnList = _useState2[1];
  var _useState3 = useState(true),
    _useState4 = _slicedToArray(_useState3, 2),
    loading = _useState4[0],
    setLoading = _useState4[1];
  useEffect(function () {
    var output = getOutputNode();
    if (!(output === null || output === void 0 ? void 0 : output.length)) {
      setLoading(false);
      setColumnList([]);
      return;
    }
    var _Request$CancelToken$ = Request.CancelToken.source(),
      token = _Request$CancelToken$.token,
      cancel = _Request$CancelToken$.cancel;
    queryColumn(output[0].id, token);
    return function () {
      cancel();
    };
  }, []);
  function getOutputNode() {
    var _props$flowData;
    return (_props$flowData = props.flowData) === null || _props$flowData === void 0 ? void 0 : _props$flowData.nodeList.filter(function (node) {
      return node.shape === CdpFlowTypes.Shape.output;
    });
  }
  function queryColumn(nodeId, token) {
    var _props$flowData2, _props$flowData3;
    setLoading(true);
    request(apis.queryOutputColumns, {
      method: 'post',
      token: token,
      data: {
        nodeId: nodeId,
        nodeList: (_props$flowData2 = props.flowData) === null || _props$flowData2 === void 0 ? void 0 : _props$flowData2.nodeList,
        lineList: (_props$flowData3 = props.flowData) === null || _props$flowData3 === void 0 ? void 0 : _props$flowData3.lineList
      }
    }).then(function (res) {
      if (!res || token.reason) return;
      setColumnList(res.data);
      setLoading(false);
    }).catch(function (err) {
      setLoading(false);
      console.error('获取输出字段列表失败', err);
    });
  }
  function onSubmit(_ref) {
    var name = _ref.name,
      desc = _ref.desc,
      level = _ref.level,
      updateSetting = _ref.updateSetting,
      sync = _ref.sync;
    var syncType = sync.syncType,
      updateBy = sync.updateBy,
      dateFormat = sync.dateFormat;
    var res = {
      name: name,
      desc: desc,
      level: level,
      updateSetting: updateSetting,
      syncType: syncType,
      updateBy: updateBy,
      dateFormat: dateFormat
    };
    if (props.onChange) props.onChange(res);
  }
  if (loading) {
    return /*#__PURE__*/React.createElement("div", {
      className: "flow-global-config"
    }, /*#__PURE__*/React.createElement(Spin, {
      className: "flow-global-config-loading"
    }));
  }
  return /*#__PURE__*/React.createElement("div", {
    className: "flow-global-config"
  }, /*#__PURE__*/React.createElement(Form, {
    colon: false,
    ref: formRef,
    onFinish: onSubmit,
    autoComplete: "off",
    initialValues: {
      name: (_props$value = props.value) === null || _props$value === void 0 ? void 0 : _props$value.name,
      desc: (_props$value2 = props.value) === null || _props$value2 === void 0 ? void 0 : _props$value2.desc,
      level: ((_props$value3 = props.value) === null || _props$value3 === void 0 ? void 0 : _props$value3.level) || CdpFlowTypes.JobLevel.middle,
      sync: {
        syncType: ((_props$value4 = props.value) === null || _props$value4 === void 0 ? void 0 : _props$value4.syncType) || ZModulesCDP.DataSet.SyncType.global,
        updateBy: ((_props$value5 = props.value) === null || _props$value5 === void 0 ? void 0 : _props$value5.updateBy) || undefined,
        dateFormat: ((_props$value6 = props.value) === null || _props$value6 === void 0 ? void 0 : _props$value6.dateFormat) || undefined
      },
      updateSetting: (_props$value7 = props.value) === null || _props$value7 === void 0 ? void 0 : _props$value7.updateSetting
    },
    onValuesChange: function onValuesChange(e) {
      var _formRef$current;
      (_formRef$current = formRef.current) === null || _formRef$current === void 0 ? void 0 : _formRef$current.submit();
    }
  }, /*#__PURE__*/React.createElement(Form.Item, {
    label: "\u4EFB\u52A1\u540D\u79F0",
    name: "name"
  }, /*#__PURE__*/React.createElement(Input, {
    maxLength: 50,
    showCount: true
  })), /*#__PURE__*/React.createElement(Form.Item, {
    label: "\u4EFB\u52A1\u63CF\u8FF0",
    name: "desc"
  }, /*#__PURE__*/React.createElement(Input.TextArea, {
    rows: 5,
    maxLength: 100,
    showCount: true
  })), /*#__PURE__*/React.createElement(Form.Item, {
    label: "\u4F18\u5148\u7EA7",
    name: "level",
    style: {
      marginBottom: '18px'
    }
  }, /*#__PURE__*/React.createElement(Radio.Group, null, /*#__PURE__*/React.createElement(Radio, {
    value: CdpFlowTypes.JobLevel.lower
  }, "\u4F4E"), /*#__PURE__*/React.createElement(Radio, {
    value: CdpFlowTypes.JobLevel.middle
  }, "\u4E2D"), /*#__PURE__*/React.createElement(Radio, {
    value: CdpFlowTypes.JobLevel.high
  }, "\u9AD8"))), /*#__PURE__*/React.createElement(Form.Item, {
    label: "\u66F4\u65B0\u65B9\u5F0F",
    name: "sync"
  }, /*#__PURE__*/React.createElement(SyncType, {
    columnList: columnList
  })), /*#__PURE__*/React.createElement(Form.Item, {
    label: "\u66F4\u65B0\u8BBE\u7F6E",
    name: "updateSetting"
  }, /*#__PURE__*/React.createElement(UpdateSetting, null))));
};
export default FlowGlobalConfig;