"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports["default"] = void 0;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _form = _interopRequireDefault(require("@alifd/next/lib/form"));
var _message = _interopRequireDefault(require("@alifd/next/lib/message"));
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _field = _interopRequireDefault(require("@alifd/next/lib/field"));
var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));
var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));
var _grid = _interopRequireDefault(require("@alifd/next/lib/grid"));
var _react = _interopRequireDefault(require("react"));
require("./index.scss");
var _variables = require("../../variables");
var _typeData = require("../../type/type-data");
var _api = require("../../utils/api");
var _lowcodeEngine = require("@alilc/lowcode-engine");
var Row = _grid["default"].Row,
  Col = _grid["default"].Col;
// import events from '../../utils/events';
// import { getJsonData } from './api';
// 自定义表单
var CommonForm = /*#__PURE__*/function (_React$Component) {
  (0, _inheritsLoose2["default"])(CommonForm, _React$Component);
  function CommonForm(_props) {
    var _this;
    _this = _React$Component.call(this, _props) || this;
    _this.field = new _field["default"]((0, _assertThisInitialized2["default"])(_this));
    //获取详情数据
    _this.onGetDetail = function (detailData) {
      // console.log(detailData);
      _this.field.setValues(detailData);
    };
    // 渲染列表
    _this.renderColumns = function () {
      var _this$props = _this.props,
        children = _this$props.children,
        textToColumn = _this$props.textToColumn,
        mode = _this$props.mode;
      // console.log(children)
      return _react["default"].Children.map(children, function (child) {
        var props = child.props;
        var componentType = props.componentType,
          bizType = props.bizType,
          isShow = props.isShow;
        if (!componentType) {
          var list = _typeData.componentTypeList.filter(function (item) {
            var _item$bizType;
            return item === null || item === void 0 ? void 0 : (_item$bizType = item.bizType) === null || _item$bizType === void 0 ? void 0 : _item$bizType.includes(bizType);
          });
          componentType = list.length ? list[0].value : 'CommonInput';
        }
        // 隐藏不渲染
        if (!isShow) {
          return null;
        }
        return /*#__PURE__*/_react["default"].createElement(Col, {
          span: 24 / textToColumn
        }, /*#__PURE__*/_react["default"].cloneElement(child, {
          componentType: componentType,
          mode: mode
        }));
      });
    };
    //保存
    _this.submit = function () {
      var _this$props2 = _this.props,
        pageInfo = _this$props2.pageInfo,
        mode = _this$props2.mode;
      return new Promise(function (resolve, reject) {
        _this.field.validate( /*#__PURE__*/function () {
          var _ref = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(error, values) {
            var key;
            return _regenerator["default"].wrap(function _callee$(_context) {
              while (1) switch (_context.prev = _context.next) {
                case 0:
                  if (!error) {
                    _context.next = 2;
                    break;
                  }
                  return _context.abrupt("return");
                case 2:
                  for (key in values) {
                    if (values[key] instanceof Array) {
                      values[key] = values[key].join(',');
                    }
                  }
                  //新增
                  if (!(mode == 'add')) {
                    _context.next = 6;
                    break;
                  }
                  _context.next = 6;
                  return (0, _api.saveStoreAdd)({
                    pageCode: pageInfo.code,
                    data: (0, _extends2["default"])({}, values, {
                      _status: 'Insert'
                    })
                  }).then(function (res) {
                    _message["default"].success('新增成功');
                    resolve();
                  })["catch"](function (err) {
                    _message["default"].error(err.msg);
                    reject();
                  });
                case 6:
                  if (!(mode == 'edit')) {
                    _context.next = 9;
                    break;
                  }
                  _context.next = 9;
                  return (0, _api.saveStoreAdd)({
                    pageCode: pageInfo.code,
                    data: (0, _extends2["default"])({}, values, {
                      _status: 'Update'
                    })
                  }).then(function () {
                    _message["default"].success('编辑成功');
                    resolve();
                  })["catch"](function (err) {
                    _message["default"].error(err.msg);
                    reject();
                  });
                case 9:
                case "end":
                  return _context.stop();
              }
            }, _callee);
          }));
          return function (_x, _x2) {
            return _ref.apply(this, arguments);
          };
        }());
      });
    };
    //表单新增/编辑
    _this.save = /*#__PURE__*/function () {
      var _ref2 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2(info) {
        return _regenerator["default"].wrap(function _callee2$(_context2) {
          while (1) switch (_context2.prev = _context2.next) {
            case 0:
              _context2.next = 2;
              return _this.submit();
            case 2:
              _lowcodeEngine.event.emit('cancel');
            case 3:
            case "end":
              return _context2.stop();
          }
        }, _callee2);
      }));
      return function (_x3) {
        return _ref2.apply(this, arguments);
      };
    }();
    //表单保存并新增
    _this.saveAndAdd = /*#__PURE__*/function () {
      var _ref3 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee3(info) {
        return _regenerator["default"].wrap(function _callee3$(_context3) {
          while (1) switch (_context3.prev = _context3.next) {
            case 0:
              _context3.next = 2;
              return _this.submit();
            case 2:
              _this.field.reset();
            case 3:
            case "end":
              return _context3.stop();
          }
        }, _callee3);
      }));
      return function (_x4) {
        return _ref3.apply(this, arguments);
      };
    }();
    // 重置
    _this.reset = function () {
      // events.emit('getData', []);
    };
    _this.state = {
      data: {},
      entityFields: []
    };
    return _this;
  }
  var _proto = CommonForm.prototype;
  _proto.componentDidMount = function componentDidMount() {
    var _this2 = this;
    var __designMode = this.props.__designMode;
    if (__designMode !== 'design') {
      _lowcodeEngine.event === null || _lowcodeEngine.event === void 0 ? void 0 : _lowcodeEngine.event.on('common:save', function (pageInfo) {
        return _this2.save(pageInfo);
      });
      _lowcodeEngine.event === null || _lowcodeEngine.event === void 0 ? void 0 : _lowcodeEngine.event.on('common:saveAndAdd', function (pageInfo) {
        return _this2.saveAndAdd(pageInfo);
      });
      _lowcodeEngine.event === null || _lowcodeEngine.event === void 0 ? void 0 : _lowcodeEngine.event.on('common:onGetDetail', function (detailData) {
        return _this2.onGetDetail(detailData);
      });
    }
  };
  _proto.componentWillUnmount = function componentWillUnmount() {
    var _this3 = this;
    var __designMode = this.props.__designMode;
    if (__designMode !== 'design') {
      _lowcodeEngine.event === null || _lowcodeEngine.event === void 0 ? void 0 : _lowcodeEngine.event.off('common:save', this.save);
      _lowcodeEngine.event === null || _lowcodeEngine.event === void 0 ? void 0 : _lowcodeEngine.event.off('common:saveAndAdd', function () {
        return _this3.saveAndAdd;
      });
      _lowcodeEngine.event === null || _lowcodeEngine.event === void 0 ? void 0 : _lowcodeEngine.event.off('common:onGetDetail', function () {
        return _this3.onGetDetail;
      });
    }
  };
  _proto.render = function render() {
    var _this$props3 = this.props,
      style = _this$props3.style,
      mode = _this$props3.mode;
    var formItemLayoutItem = {
      wrapperCol: {
        offset: 12
      }
    };
    return /*#__PURE__*/_react["default"].createElement(_form["default"], {
      className: _variables.bizCssPrefix + '-commonForm',
      style: style,
      field: this.field
    }, /*#__PURE__*/_react["default"].createElement(Row, {
      align: "stretch",
      wrap: true
    }, this.renderColumns()));
  };
  return CommonForm;
}(_react["default"].Component);
CommonForm.displyName = 'CommonForm';
CommonForm.displyName = 'CommonForm';
var _default = CommonForm;
exports["default"] = _default;