"use strict";

function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;
var _react = _interopRequireWildcard(require("react"));
var _vtxFormLayout = _interopRequireDefault(require("@vtx/components/lib/vtx-form-layout"));
var _vtxModal = _interopRequireDefault(require("@vtx/components/lib/vtx-modal"));
var _form = _interopRequireDefault(require("antd/lib/form"));
var _tabs = _interopRequireDefault(require("antd/lib/tabs"));
var _button = _interopRequireDefault(require("antd/lib/button"));
var _spin = _interopRequireDefault(require("antd/lib/spin"));
var _descriptions = _interopRequireDefault(require("antd/lib/descriptions"));
var _FormItem = _interopRequireDefault(require("../FormItem"));
require("./index.less");
var _MyEditCellTable = _interopRequireDefault(require("../MyEditCellTable"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _excluded = ["items", "column", "size", "formData", "disabled", "formStyle", "customContent", "modalLoading", "formLayout", "isShowModal", "cockpit"];
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
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; }
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], t.indexOf(o) >= 0 || {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (e.indexOf(n) >= 0) continue; t[n] = r[n]; } return t; }
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
var TabPane = _tabs["default"].TabPane;
var obj = {};
var RenderCard = function RenderCard(props) {
  var _props$items = props.items,
    items = _props$items === void 0 ? [] : _props$items,
    label = props.label,
    disabled = props.disabled,
    _props$formData = props.formData,
    formData = _props$formData === void 0 ? {} : _props$formData,
    parentForm = props.parentForm;
  return /*#__PURE__*/_react["default"].createElement(_vtxFormLayout["default"].Card, {
    title: label
  }, /*#__PURE__*/_react["default"].createElement(RenderFormItems, {
    items: items,
    disabled: disabled,
    parentForm: parentForm,
    formData: formData
  }));
};
var RenderTabs = function RenderTabs(props) {
  var _props$items2 = props.items,
    items = _props$items2 === void 0 ? [] : _props$items2,
    onTabsChange = props.onTabsChange,
    disabled = props.disabled,
    initialActiveKey = props.initialActiveKey,
    _props$formData2 = props.formData,
    formData = _props$formData2 === void 0 ? {} : _props$formData2,
    activeKey = props.activeKey,
    column = props.column,
    _props$parentForm = props.parentForm,
    parentForm = _props$parentForm === void 0 ? {} : _props$parentForm;
  return /*#__PURE__*/_react["default"].createElement(_tabs["default"], {
    defaultActiveKey: initialActiveKey || items[0].label || items[0].activeKey,
    activeKey: activeKey,
    className: "vtx-form-tabs",
    onChange: onTabsChange,
    tabBarExtraContent: props.operations
  }, (items === null || items === void 0 ? void 0 : items.length) > 0 && items.map(function (v) {
    return /*#__PURE__*/_react["default"].createElement(TabPane, {
      tab: v.label,
      key: v.label || v.activeKey,
      forceRender: v.forceRender
    }, /*#__PURE__*/_react["default"].createElement(_vtxFormLayout["default"], {
      card: true,
      cols: column,
      mode: disabled && 'view'
    }, /*#__PURE__*/_react["default"].createElement(RenderFormItems, {
      items: v.items,
      disabled: disabled,
      parentForm: parentForm,
      formData: formData
    })));
  }));
};
var RenderPane = function RenderPane(props) {
  var _props$items3 = props.items,
    items = _props$items3 === void 0 ? [] : _props$items3,
    label = props.label,
    disabled = props.disabled,
    _props$formData3 = props.formData,
    formData = _props$formData3 === void 0 ? {} : _props$formData3,
    parentForm = props.parentForm;
  return /*#__PURE__*/_react["default"].createElement(_vtxFormLayout["default"].Pane, {
    title: label
  }, /*#__PURE__*/_react["default"].createElement(RenderFormItems, {
    items: items,
    disabled: disabled,
    parentForm: parentForm,
    formData: formData
  }));
};
RenderPane.propTypes = {
  items: _propTypes["default"].array,
  label: _propTypes["default"].string,
  disabled: _propTypes["default"].bool,
  formData: _propTypes["default"].any,
  parentForm: _propTypes["default"].any
};
var RenderDescriptions = function RenderDescriptions(props) {
  var _props$items4 = props.items,
    items = _props$items4 === void 0 ? [] : _props$items4,
    _props$layout = props.layout,
    layout = _props$layout === void 0 ? 'horizontal' : _props$layout,
    label = props.label,
    _props$labelStyle = props.labelStyle,
    labelStyle = _props$labelStyle === void 0 ? {} : _props$labelStyle,
    _props$bordered = props.bordered,
    bordered = _props$bordered === void 0 ? true : _props$bordered,
    _props$size = props.size,
    size = _props$size === void 0 ? 'default' : _props$size,
    _props$column = props.column,
    column = _props$column === void 0 ? 2 : _props$column,
    _props$contentStyle = props.contentStyle,
    contentStyle = _props$contentStyle === void 0 ? {} : _props$contentStyle,
    _props$formData4 = props.formData,
    formData = _props$formData4 === void 0 ? {} : _props$formData4;
  var defaultCol = 24 / column; // 默认2列布局
  return /*#__PURE__*/_react["default"].createElement(_descriptions["default"], {
    title: label,
    layout: layout,
    labelStyle: labelStyle,
    bordered: bordered,
    size: size,
    contentStyle: contentStyle,
    column: {
      xs: 8,
      sm: 16,
      md: 24
    }
  }, items.map(function (item, index) {
    return /*#__PURE__*/_react["default"].createElement(_descriptions["default"].Item, {
      key: index,
      label: item.label,
      span: item.span || defaultCol
    }, formData["".concat(item.paramName)] || '--');
  }));
};
RenderDescriptions.propTypes = {
  items: _propTypes["default"].array,
  layout: _propTypes["default"].string,
  label: _propTypes["default"].string,
  labelStyle: _propTypes["default"].object,
  bordered: _propTypes["default"].bool,
  size: _propTypes["default"].string,
  column: _propTypes["default"].number,
  contentStyle: _propTypes["default"].object,
  formData: _propTypes["default"].object
};

// 渲染表单项
var RenderFormItems = function RenderFormItems(props) {
  var _props$items5 = props.items,
    items = _props$items5 === void 0 ? [] : _props$items5,
    disabled = props.disabled,
    _props$formData5 = props.formData,
    formData = _props$formData5 === void 0 ? {} : _props$formData5,
    column = props.column,
    parentForm = props.parentForm;
  return items.map(function (item) {
    var type = item.type,
      weights = item.weights,
      onChange = item.onChange;
    if (type === 'tabs') {
      return /*#__PURE__*/_react["default"].createElement(RenderTabs, _extends({
        key: item.label || new Date() + Math.random(),
        parentForm: parentForm
      }, item, {
        column: column,
        onTabsChange: onChange,
        disabled: disabled,
        formData: formData
      }));
    } else if (type === 'card') {
      return /*#__PURE__*/_react["default"].createElement(RenderCard, _extends({
        key: item.label
      }, item, {
        disabled: disabled,
        parentForm: parentForm,
        formData: formData
      }));
    } else if (type === 'pane') {
      return /*#__PURE__*/_react["default"].createElement(RenderPane, _extends({
        key: item.label
      }, item, {
        disabled: disabled,
        parentForm: parentForm,
        formData: formData
      }));
    } else if (type === 'descriptions') {
      return /*#__PURE__*/_react["default"].createElement(RenderDescriptions, _extends({
        key: item.label
      }, item, {
        disabled: disabled,
        parentForm: parentForm,
        formData: formData
      }));
    } else if (type === 'blank' && !item.isFormItem) {
      return item.content;
    } else if (type === 'editCellTable') {
      return /*#__PURE__*/_react["default"].createElement(_MyEditCellTable["default"], {
        key: item.label,
        ref: function ref(_ref) {
          obj["fmyeditT_".concat(item.paramName)] = _ref;
        },
        hideMake: item.hideMake || true,
        rowKey: item.rowKey || 'id',
        scroll: item.scroll || {},
        columns: item.columns //中间表格头部
        ,
        loading: item.loading,
        pagination: false,
        tableData: item.dataSource,
        tableItems: item.tableItems
      });
    }
    return /*#__PURE__*/_react["default"].createElement(_FormItem["default"], _extends({
      formWrap: "Vtx",
      key: item.label,
      weights: weights,
      parentForm: parentForm
    }, item, {
      disabled: disabled,
      formData: formData
    }));
  });
};
var classnames = function classnames(name) {
  if (name) {
    return "ol-map-FormModal-".concat(name);
  }
  return "ol-map-FormModal";
};
var FormModal = /*#__PURE__*/(0, _react.forwardRef)(function (_ref2, ref) {
  var items = _ref2.items,
    _ref2$column = _ref2.column,
    column = _ref2$column === void 0 ? 2 : _ref2$column,
    _ref2$size = _ref2.size,
    size = _ref2$size === void 0 ? 'large' : _ref2$size,
    _ref2$formData = _ref2.formData,
    formData = _ref2$formData === void 0 ? {} : _ref2$formData,
    disabled = _ref2.disabled,
    _ref2$formStyle = _ref2.formStyle,
    formStyle = _ref2$formStyle === void 0 ? {} : _ref2$formStyle,
    customContent = _ref2.customContent,
    _ref2$modalLoading = _ref2.modalLoading,
    modalLoading = _ref2$modalLoading === void 0 ? false : _ref2$modalLoading,
    _ref2$formLayout = _ref2.formLayout,
    formLayout = _ref2$formLayout === void 0 ? {} : _ref2$formLayout,
    _ref2$isShowModal = _ref2.isShowModal,
    isShowModal = _ref2$isShowModal === void 0 ? true : _ref2$isShowModal,
    _ref2$cockpit = _ref2.cockpit,
    cockpit = _ref2$cockpit === void 0 ? false : _ref2$cockpit,
    modalProps = _objectWithoutProperties(_ref2, _excluded);
  var _Form$useForm = _form["default"].useForm(),
    _Form$useForm2 = _slicedToArray(_Form$useForm, 1),
    form = _Form$useForm2[0];
  (0, _react.useEffect)(function () {
    if (isShowModal && modalProps.footerShow) {
      // resetRefreshTicket();
    }
    if (!modalProps.visible && form) {
      form.resetFields();
    }
  }, [modalProps.visible]);
  (0, _react.useEffect)(function () {
    form.setFieldsValue(formData);
  }, [JSON.stringify(formData)]);
  (0, _react.useImperativeHandle)(ref, function () {
    return {
      form: form,
      obj: obj
    };
  });

  // 渲染底部
  function renderFooter() {
    var footerShow = modalProps.footerShow,
      _modalProps$modalBtnL = modalProps.modalBtnLoading,
      modalBtnLoading = _modalProps$modalBtnL === void 0 ? false : _modalProps$modalBtnL;
    if (footerShow) {
      // eslint-disable-next-line no-inner-declarations
      var onModalSave = function onModalSave(type) {
        var methods = {
          save: onConfirm,
          ts: onTS,
          submit: onSubmit
        };
        form.validateFields().then(function (values) {
          methods[type] && methods[type](values);
        })["catch"](function (error) {
          var _error$errorFields;
          form.scrollToField(error === null || error === void 0 || (_error$errorFields = error.errorFields) === null || _error$errorFields === void 0 || (_error$errorFields = _error$errorFields[0]) === null || _error$errorFields === void 0 ? void 0 : _error$errorFields.name);
        });
      }; // 默认按钮
      var TSBtn = modalProps.TSBtn,
        _modalProps$TSTitle = modalProps.TSTitle,
        TSTitle = _modalProps$TSTitle === void 0 ? '暂存' : _modalProps$TSTitle,
        onTS = modalProps.onTS,
        returnBtn = modalProps.returnBtn,
        _modalProps$returnTit = modalProps.returnTitle,
        returnTitle = _modalProps$returnTit === void 0 ? '退回' : _modalProps$returnTit,
        onReturnBack = modalProps.onReturnBack,
        _modalProps$saveBtn = modalProps.saveBtn,
        saveBtn = _modalProps$saveBtn === void 0 ? true : _modalProps$saveBtn,
        _modalProps$saveTitle = modalProps.saveTitle,
        saveTitle = _modalProps$saveTitle === void 0 ? '保存' : _modalProps$saveTitle,
        onConfirm = modalProps.onConfirm,
        submitBtn = modalProps.submitBtn,
        _modalProps$submitTit = modalProps.submitTitle,
        submitTitle = _modalProps$submitTit === void 0 ? '提交' : _modalProps$submitTit,
        onSubmit = modalProps.onSubmit,
        _modalProps$cancelBtn = modalProps.cancelBtn,
        cancelBtn = _modalProps$cancelBtn === void 0 ? true : _modalProps$cancelBtn,
        _modalProps$cancelTit = modalProps.cancelTitle,
        cancelTitle = _modalProps$cancelTit === void 0 ? '取消' : _modalProps$cancelTit,
        onCancel = modalProps.onCancel,
        _modalProps$buttonLis = modalProps.buttonList,
        buttonList = _modalProps$buttonLis === void 0 ? [] : _modalProps$buttonLis;
      var defaultBtnList = [].concat(_toConsumableArray(cancelBtn ? [{
        title: cancelTitle,
        onClick: onCancel,
        className: classnames('cancelBtn')
      }] : []), _toConsumableArray(returnBtn ? [{
        title: returnTitle,
        onClick: onReturnBack,
        className: classnames('returnBtn')
      }] : []), _toConsumableArray(TSBtn ? [{
        title: TSTitle,
        onClick: function onClick() {
          return onModalSave('ts');
        },
        className: classnames('tsBtn'),
        type: 'primary'
      }] : []), _toConsumableArray(saveBtn ? [{
        title: saveTitle,
        onClick: function onClick() {
          return onModalSave('save');
        },
        className: classnames('saveBtn'),
        type: 'primary'
      }] : []), _toConsumableArray(submitBtn ? [{
        title: submitTitle,
        onClick: function onClick() {
          return onModalSave('submit');
        },
        className: classnames('submitBtn'),
        type: 'primary'
      }] : []), _toConsumableArray(buttonList));
      return /*#__PURE__*/_react["default"].createElement("div", {
        className: classnames('showCenter')
      }, defaultBtnList.map(function (item, index) {
        return /*#__PURE__*/_react["default"].createElement(_button["default"], {
          key: index,
          loading: modalBtnLoading,
          className: item.className,
          onClick: item.onClick,
          type: item.type
        }, item.title);
      }));
    } else {
      var _modalProps$cancelTit2 = modalProps.cancelTitle,
        _cancelTitle = _modalProps$cancelTit2 === void 0 ? '取消' : _modalProps$cancelTit2,
        _onCancel = modalProps.onCancel;
      var _defaultBtnList = [{
        title: _cancelTitle,
        onClick: _onCancel,
        className: classnames('cancelBtn')
      } // 取消
      ];
      return /*#__PURE__*/_react["default"].createElement("div", {
        className: classnames('showCenter')
      }, _defaultBtnList.map(function (item, index) {
        return /*#__PURE__*/_react["default"].createElement(_button["default"], {
          key: index,
          loading: modalBtnLoading,
          className: item.className,
          onClick: item.onClick,
          type: item.type
        }, item.title);
      }));
    }
  }
  function renderChildren() {
    return /*#__PURE__*/_react["default"].createElement("div", {
      className: "".concat(classnames('formLayout'), " ").concat(disabled && classnames('arrow')),
      style: {
        padding: isShowModal ? 0 : 10
      }
    }, /*#__PURE__*/_react["default"].createElement(_spin["default"], {
      spinning: modalLoading
    }, /*#__PURE__*/_react["default"].createElement(_form["default"], _extends({
      ref: ref,
      form: form,
      layout: disabled ? 'inline' : 'vertical',
      style: customContent ? formStyle : {}
    }, formLayout), /*#__PURE__*/_react["default"].createElement(_vtxFormLayout["default"], {
      card: true,
      cols: column,
      mode: disabled && 'view'
    }, /*#__PURE__*/_react["default"].createElement(RenderFormItems, {
      items: items,
      parentForm: form,
      column: column,
      disabled: disabled,
      formData: formData
    }))), customContent && customContent(formData)));
  }
  return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, isShowModal ? /*#__PURE__*/_react["default"].createElement(_vtxModal["default"], _extends({
    width: _vtxModal["default"].size[size],
    onOk: function onOk() {
      return form.submit();
    },
    forceRender: true,
    footer: renderFooter()
  }, modalProps, {
    className: cockpit ? "".concat(classnames('cockpitModal'), " ").concat(modalProps.className) : modalProps.className
  }), renderChildren()) : renderChildren());
});
FormModal.propTypes = {
  items: _propTypes["default"].array,
  column: _propTypes["default"].number,
  size: _propTypes["default"].string,
  viewLoading: _propTypes["default"].bool,
  formData: _propTypes["default"].object,
  confirm: _propTypes["default"].func,
  mode: _propTypes["default"].any,
  disabled: _propTypes["default"].bool,
  formStyle: _propTypes["default"].object,
  formLayout: _propTypes["default"].object,
  customContent: _propTypes["default"].any,
  modalLoading: _propTypes["default"].bool,
  isShowModal: _propTypes["default"].bool,
  cockpit: _propTypes["default"].bool
};
var _default = exports["default"] = FormModal;
//# sourceMappingURL=index.js.map
