import _Form from "@alifd/next/es/form";
import _extends from "@babel/runtime/helpers/extends";
import _Message from "@alifd/next/es/message";
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import _Grid from "@alifd/next/es/grid";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import React from 'react';
var Row = _Grid.Row,
  Col = _Grid.Col;
import "./index.scss";
import { bizCssPrefix } from "../../variables";
import { componentTypeList } from "../../type/type-data";
import { saveStoreAdd } from "../../utils/api";
import events from "../../utils/events";
import { getJsonData } from "./api";
// 自定义表单
var CommonForm = /*#__PURE__*/function (_React$Component) {
  _inheritsLoose(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,
        isPreview = _this$props.isPreview,
        modeStyle = _this$props.modeStyle;
      return React.Children.map(children, function (child) {
        var props = child.props;
        var componentType = props.componentType,
          bizType = props.bizType,
          isShow = props.isShow;
        if (!componentType) {
          var list = 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 && modeStyle !== 'browse') {
          return null;
        }
        return /*#__PURE__*/React.createElement(Col, {
          span: 24 / textToColumn
        }, /*#__PURE__*/React.cloneElement(child, {
          componentType: componentType,
          comparison: '=',
          isPreview: isPreview
        }));
      });
    };
    // 处理数据
    _this.processData = /*#__PURE__*/function () {
      var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(mess) {
        var _newData$, _data;
        var obj, _this$props$pageInfo, code, appId, treeData, newData, fieldNames, data;
        return _regeneratorRuntime.wrap(function _callee$(_context) {
          while (1) switch (_context.prev = _context.next) {
            case 0:
              obj = {};
              _this$props$pageInfo = _this.props.pageInfo, code = _this$props$pageInfo.code, appId = _this$props$pageInfo.appId;
              _context.next = 4;
              return getJsonData(code);
            case 4:
              treeData = _context.sent;
              newData = treeData === null || treeData === void 0 ? void 0 : treeData.filter(function (item) {
                return item.appId === appId;
              });
              fieldNames = (_newData$ = newData[0]) === null || _newData$ === void 0 ? void 0 : _newData$.fieldList.map(function (item, index) {
                return item.fieldName;
              });
              data = mess === null || mess === void 0 ? void 0 : mess.map(function (item, index) {
                return {
                  fieldName: item.fieldName,
                  value: item.value
                };
              });
              data = (_data = data) === null || _data === void 0 ? void 0 : _data.filter(function (item) {
                return fieldNames.indexOf(item.fieldName) !== -1;
              });
              data.map(function (item) {
                obj[item.fieldName] = item.value;
              });
              return _context.abrupt("return", obj);
            case 11:
            case "end":
              return _context.stop();
          }
        }, _callee);
      }));
      return function (_x) {
        return _ref.apply(this, arguments);
      };
    }();
    // 数据处理
    _this.dataHandle = function (value) {
      var children = _this.props.children;
      var data = Object.assign(children);
      data = data.map(function (item) {
        return item.props;
      });
      var newData = data.map(function (item) {
        return {
          displayName: item.displayName,
          fieldName: item.fieldName,
          componentType: item.componentType,
          isEmpty: item.isEmpty,
          isCustom: item.isCustom ? item.isCustom : false,
          //自定义
          value: item.defaultValue ? item.defaultValue : ''
        };
      });
      for (var i = 0; i < newData.length; i++) {
        for (var j in value) {
          if (newData[i].fieldName === j) {
            if (newData[i].componentType === "CommonOption") {
              newData[i].value = Array.isArray(value[j]) ? value[j].join(',') : value[j];
            } else if (newData[i].componentType === "CommonImage" || newData[i].componentType === "CommonAttachment") {
              var uids = value[j].map(function (item) {
                return item.uid;
              });
              newData[i].value = Array.isArray(uids) ? uids.join(',') : uids;
            } else {
              newData[i].value = value[j];
            }
          }
        }
      }
      return newData;
    };
    // 表单新增
    _this.handleSubmit = /*#__PURE__*/function () {
      var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(value, e) {
        var data, i, pageInfo, obj;
        return _regeneratorRuntime.wrap(function _callee2$(_context2) {
          while (1) switch (_context2.prev = _context2.next) {
            case 0:
              data = _this.dataHandle(value);
              i = 0;
            case 2:
              if (!(i < data.length)) {
                _context2.next = 9;
                break;
              }
              if (!(data[i].isEmpty && (data[i].value === '' || data[i].value === undefined))) {
                _context2.next = 6;
                break;
              }
              _Message.error(data[i].displayName + "\u4E0D\u80FD\u4E3A\u7A7A\uFF01");
              return _context2.abrupt("return");
            case 6:
              i++;
              _context2.next = 2;
              break;
            case 9:
              pageInfo = _this.props.pageInfo;
              _context2.next = 12;
              return _this.processData(data);
            case 12:
              obj = _context2.sent;
              _context2.next = 15;
              return saveStoreAdd({
                pageCode: pageInfo.code,
                data: _extends({}, obj, {
                  _status: 'Insert'
                })
              }).then(function (res) {
                console.log(res);
                _Message.success("新增成功");
              })["catch"](function (err) {
                console.log(err);
                _Message.error("新增失败");
              });
            case 15:
            case "end":
              return _context2.stop();
          }
        }, _callee2);
      }));
      return function (_x2, _x3) {
        return _ref2.apply(this, arguments);
      };
    }();
    // 表单编辑
    _this.handleEdit = /*#__PURE__*/function () {
      var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(value, e) {
        var data, i, pageInfo, obj;
        return _regeneratorRuntime.wrap(function _callee3$(_context3) {
          while (1) switch (_context3.prev = _context3.next) {
            case 0:
              data = _this.dataHandle(value);
              i = 0;
            case 2:
              if (!(i < data.length)) {
                _context3.next = 9;
                break;
              }
              if (!(data[i].isEmpty && (data[i].value === '' || data[i].value === undefined))) {
                _context3.next = 6;
                break;
              }
              _Message.error(data[i].displayName + "\u4E0D\u80FD\u4E3A\u7A7A\uFF01");
              return _context3.abrupt("return");
            case 6:
              i++;
              _context3.next = 2;
              break;
            case 9:
              pageInfo = _this.props.pageInfo;
              _context3.next = 12;
              return _this.processData(data);
            case 12:
              obj = _context3.sent;
              _context3.next = 15;
              return saveStoreAdd({
                pageCode: pageInfo.code,
                data: _extends({}, obj, {
                  _status: 'Update'
                })
              }).then(function () {
                _Message.success("编辑成功");
              })["catch"](function () {
                _Message.error("编辑失败");
              });
            case 15:
            case "end":
              return _context3.stop();
          }
        }, _callee3);
      }));
      return function (_x4, _x5) {
        return _ref3.apply(this, arguments);
      };
    }();
    // 重置
    _this.reset = function () {
      events.emit('getData', []);
    };
    _this.state = {
      data: {},
      entityFields: []
    };
    return _this;
  }
  var _proto = CommonForm.prototype;
  _proto.render = function render() {
    var _this2 = this;
    var _this$props2 = this.props,
      style = _this$props2.style,
      isPreview = _this$props2.isPreview;
    var formItemLayoutItem = {
      wrapperCol: {
        offset: 12
      }
    };
    return /*#__PURE__*/React.createElement(_Form, {
      className: bizCssPrefix + '-commonForm',
      style: style
    }, /*#__PURE__*/React.createElement(Row, {
      align: "stretch",
      wrap: true
    }, this.renderColumns()), isPreview && /*#__PURE__*/React.createElement(Row, {
      align: "center"
    }, /*#__PURE__*/React.createElement(Col, {
      style: {
        textAlign: 'right'
      }
    }, /*#__PURE__*/React.createElement(_Form.Item, formItemLayoutItem, /*#__PURE__*/React.createElement(_Form.Submit, {
      type: "primary",
      validate: true,
      onClick: function onClick(v, e) {
        return _this2.handleSubmit(v, e);
      },
      style: {
        marginRight: 10
      }
    }, "\u65B0\u589E"), /*#__PURE__*/React.createElement(_Form.Submit, {
      type: "primary",
      validate: true,
      onClick: function onClick(v, e) {
        return _this2.handleEdit(v, e);
      },
      style: {
        marginRight: 10
      }
    }, "\u7F16\u8F91"), /*#__PURE__*/React.createElement(_Form.Reset, {
      onClick: function onClick() {
        return _this2.reset();
      }
    }, "\u91CD\u7F6E")))));
  };
  return CommonForm;
}(React.Component);
CommonForm.displyName = 'CommonForm';
CommonForm.displyName = 'CommonForm';
export default CommonForm;