"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 _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
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 _data = require("./data");
var _api = require("../../utils/api");
var Row = _grid["default"].Row,
  Col = _grid["default"].Col;
// 自定义表单
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.renderColumns = function () {
      var _this$props = _this.props,
        children = _this$props.children,
        textToColumn = _this$props.textToColumn,
        __designMode = _this$props.__designMode,
        isPreview = _this$props.isPreview;
      return _react["default"].Children.map(children, function (child) {
        var props = child.props;
        var componentType = props.componentType,
          bizType = props.bizType,
          isHide = props.isHide,
          fieldName = props.fieldName;
        if (!componentType) {
          var list = _typeData.componentTypeList.filter(function (item) {
            return item.tplType.includes(bizType);
          });
          componentType = list.length ? list[0].value : 'Input';
        }
        if (isHide && __designMode !== 'design') {
          return null;
        }
        var defaultValue = _data.data[fieldName] ? _data.data[fieldName] : '';
        return /*#__PURE__*/_react["default"].createElement(Col, {
          span: 24 / textToColumn
        }, /*#__PURE__*/_react["default"].cloneElement(child, {
          componentType: componentType,
          comparison: '=',
          isPreview: isPreview,
          defaultValue: defaultValue
        }));
      });
    };
    // 处理数据
    _this.processData = function (v, fieldList) {
      var formGroups = [];
      var _loop = function _loop(i) {
        if (v[i] !== undefined) {
          var field = fieldList.filter(function (item) {
            return item.fieldName === i && !item.isAddTitle;
          })[0];
          if (field) {
            field.value = v[i];
          }
          formGroups.push(field);
        }
      };
      for (var i in v) {
        _loop(i);
      }
      formGroups = formGroups.filter(function (item) {
        return item;
      });
      var formData = formGroups.map(function (item) {
        return {
          fieldName: item.fieldName,
          value: item.value
        };
      });
      var obj = {};
      formData.map(function (item) {
        obj[item.fieldName] = item.value;
      });
      return obj;
    };
    // 表单新增
    _this.handleSubmit = /*#__PURE__*/function () {
      var _ref = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(v, e) {
        var mess, _this$props2, fieldList, pageInfo, obj;
        return _regenerator["default"].wrap(function _callee$(_context) {
          while (1) switch (_context.prev = _context.next) {
            case 0:
              console.log(v, e);
              if (!e) {
                _context.next = 5;
                break;
              }
              mess = Object.values(Object.values(e));
              _message["default"].error(mess[0].errors[0]);
              return _context.abrupt("return");
            case 5:
              _this$props2 = _this.props, fieldList = _this$props2.fieldList, pageInfo = _this$props2.pageInfo;
              obj = _this.processData(v, fieldList);
              obj._status = 'Insert';
              _context.next = 10;
              return (0, _api.saveStoreAdd)({
                pageCode: pageInfo.code,
                data: obj
              }).then(function () {
                _message["default"].success("新增成功");
              })["catch"](function () {
                _message["default"].error("新增失败");
              });
            case 10:
            case "end":
              return _context.stop();
          }
        }, _callee);
      }));
      return function (_x, _x2) {
        return _ref.apply(this, arguments);
      };
    }();
    // 表单编辑
    _this.handleEdit = /*#__PURE__*/function () {
      var _ref2 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2(v, e) {
        var mess, _this$props3, fieldList, pageInfo, obj;
        return _regenerator["default"].wrap(function _callee2$(_context2) {
          while (1) switch (_context2.prev = _context2.next) {
            case 0:
              console.log(v, e);
              if (!e) {
                _context2.next = 5;
                break;
              }
              mess = Object.values(Object.values(e));
              _message["default"].error(mess[0].errors[0]);
              return _context2.abrupt("return");
            case 5:
              _this$props3 = _this.props, fieldList = _this$props3.fieldList, pageInfo = _this$props3.pageInfo;
              obj = _this.processData(v, fieldList);
              obj._status = 'Update';
              _context2.next = 10;
              return (0, _api.saveStoreAdd)({
                pageCode: pageInfo.code,
                data: obj
              }).then(function () {
                _message["default"].success("编辑成功");
              })["catch"](function () {
                _message["default"].error("编辑失败");
              });
            case 10:
            case "end":
              return _context2.stop();
          }
        }, _callee2);
      }));
      return function (_x3, _x4) {
        return _ref2.apply(this, arguments);
      };
    }();
    // 重置
    _this.reset = function () {
      // events.emit('onFilter', []);
    };
    _this.state = {
      data: {}
    };
    return _this;
  }
  var _proto = CommonForm.prototype;
  _proto.render = function render() {
    var _this2 = this;
    var _this$props4 = this.props,
      style = _this$props4.style,
      isPreview = _this$props4.isPreview;
    var formItemLayoutItem = {
      wrapperCol: {
        offset: 12
      }
    };
    return /*#__PURE__*/_react["default"].createElement(_form["default"], {
      className: _variables.bizCssPrefix + '-commonForm',
      isPreview: isPreview,
      style: style
    }, /*#__PURE__*/_react["default"].createElement(Row, {
      align: "stretch",
      wrap: true
    }, this.renderColumns()), isPreview && /*#__PURE__*/_react["default"].createElement(Row, {
      align: "center"
    }, /*#__PURE__*/_react["default"].createElement(Col, {
      style: {
        textAlign: 'right'
      }
    }, /*#__PURE__*/_react["default"].createElement(_form["default"].Item, formItemLayoutItem, /*#__PURE__*/_react["default"].createElement(_form["default"].Submit, {
      type: "primary",
      validate: true,
      onClick: function onClick(v, e) {
        return _this2.handleSubmit(v, e);
      },
      style: {
        marginRight: 10
      }
    }, "\u65B0\u589E"), /*#__PURE__*/_react["default"].createElement(_form["default"].Submit, {
      type: "primary",
      validate: true,
      onClick: function onClick(v, e) {
        return _this2.handleEdit(v, e);
      },
      style: {
        marginRight: 10
      }
    }, "\u7F16\u8F91"), /*#__PURE__*/_react["default"].createElement(_form["default"].Reset, {
      onClick: function onClick() {
        return _this2.reset();
      }
    }, "\u91CD\u7F6E")))));
  };
  return CommonForm;
}(_react["default"].Component);
CommonForm.displyName = 'CommonForm';
var _default = CommonForm;
exports["default"] = _default;