"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 _radio = _interopRequireDefault(require("@alifd/next/lib/radio"));
var _checkbox = _interopRequireDefault(require("@alifd/next/lib/checkbox"));
var _select = _interopRequireDefault(require("@alifd/next/lib/select"));
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _switch = _interopRequireDefault(require("@alifd/next/lib/switch"));
var _rating = _interopRequireDefault(require("@alifd/next/lib/rating"));
var _timePicker = _interopRequireDefault(require("@alifd/next/lib/time-picker2"));
var _numberPicker = _interopRequireDefault(require("@alifd/next/lib/number-picker"));
var _datePicker = _interopRequireDefault(require("@alifd/next/lib/date-picker2"));
var _input = _interopRequireDefault(require("@alifd/next/lib/input"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));
var _react = _interopRequireDefault(require("react"));
require("./index.scss");
var _data = require("./data");
var _lowcodeEngine = require("@alilc/lowcode-engine");
var _events = _interopRequireDefault(require("../../utils/events"));
//这是表单项组件
var FormItem = /*#__PURE__*/function (_React$Component) {
  (0, _inheritsLoose2["default"])(FormItem, _React$Component);
  function FormItem(props) {
    var _this;
    _this = _React$Component.call(this, props) || this;
    // 事件通信
    _this.getData = /*#__PURE__*/function () {
      var _ref = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(commonVOs) {
        return _regenerator["default"].wrap(function _callee$(_context) {
          while (1) switch (_context.prev = _context.next) {
            case 0:
              // 清空数据
              _this.setState({
                selectData: ''
              });
              _lowcodeEngine.config.set('dataSet', []);
            case 2:
            case "end":
              return _context.stop();
          }
        }, _callee);
      }));
      return function (_x) {
        return _ref.apply(this, arguments);
      };
    }();
    _this.onChange = function (value) {
      var _this$props = _this.props,
        fieldName = _this$props.fieldName,
        isEmpty = _this$props.isEmpty,
        componentType = _this$props.componentType;
      var dataSet = [];
      _this.props.onChange && _this.props.onChange(value);
      if (componentType === 'CommonOption') {
        _this.setState({
          selectData: value
        });
      }
      if (_lowcodeEngine.config.has('dataSet')) {
        dataSet = _lowcodeEngine.config.get('dataSet') || [];
        dataSet = dataSet.filter(function (item) {
          return item.fieldName !== fieldName;
        });
        dataSet.push({
          fieldName: fieldName,
          isEmpty: isEmpty,
          value: Array.isArray(value) ? value.join(',') : value
        });
      } else {
        dataSet = [{
          fieldName: fieldName,
          isEmpty: isEmpty,
          value: Array.isArray(value) ? value.join(',') : value
        }];
      }
      _lowcodeEngine.config.set('dataSet', dataSet);
    };
    // 控件类型
    _this.renderColumns = function () {
      var _this$props2 = _this.props,
        componentType = _this$props2.componentType,
        placeholder = _this$props2.placeholder,
        fieldName = _this$props2.fieldName,
        isPreview = _this$props2.isPreview,
        defaultValue = _this$props2.defaultValue;
      if (!isPreview) {
        defaultValue = _data.data[fieldName] ? _data.data[fieldName] : defaultValue ? defaultValue : '';
      }
      if (componentType === 'CommonInput') {
        var _this$props3 = _this.props,
          InputLength = _this$props3.InputLength,
          hasLimitHint = _this$props3.hasLimitHint;
        return /*#__PURE__*/_react["default"].createElement(_input["default"], {
          onChange: function onChange(e) {
            return _this.onChange(e);
          },
          maxLength: InputLength,
          hasLimitHint: hasLimitHint,
          placeholder: placeholder,
          defaultValue: defaultValue
        });
      }
      if (componentType === 'CommonTextarea') {
        var _this$props4 = _this.props,
          row = _this$props4.row,
          _InputLength = _this$props4.InputLength,
          autoHeight = _this$props4.autoHeight,
          _hasLimitHint = _this$props4.hasLimitHint;
        return /*#__PURE__*/_react["default"].createElement(_input["default"].TextArea, {
          onChange: function onChange(e) {
            return _this.onChange(e);
          },
          rows: row,
          autoHeight: autoHeight,
          maxLength: _InputLength,
          hasLimitHint: _hasLimitHint,
          placeholder: placeholder,
          defaultValue: defaultValue
        });
      }
      if (componentType === 'DatePicker') {
        return /*#__PURE__*/_react["default"].createElement(_datePicker["default"], {
          onChange: function onChange(e) {
            return _this.onChange(e);
          },
          outputFormat: "YYYY-MM-DD",
          placeholder: placeholder,
          defaultValue: defaultValue
        });
      }
      if (componentType === 'NumberPicker') {
        return /*#__PURE__*/_react["default"].createElement(_numberPicker["default"], {
          onChange: function onChange(e) {
            return _this.onChange(e);
          },
          defaultValue: defaultValue
        });
      }
      if (componentType === 'TimePicker') {
        return /*#__PURE__*/_react["default"].createElement(_timePicker["default"], {
          onChange: function onChange(e) {
            return _this.onChange(e);
          },
          placeholder: placeholder,
          defaultValue: defaultValue
        });
      }
      if (componentType === 'Rating') {
        return /*#__PURE__*/_react["default"].createElement(_rating["default"], {
          onChange: function onChange(e) {
            return _this.onChange(e);
          },
          defaultValue: defaultValue
        });
      }
      if (componentType === 'Switch') {
        return /*#__PURE__*/_react["default"].createElement(_switch["default"], {
          onChange: function onChange(e) {
            return _this.onChange(e);
          },
          defaultChecked: defaultValue
        });
      }
      if (componentType === 'CommonOption') {
        var _this$props5 = _this.props,
          isMultiple = _this$props5.isMultiple,
          isDeployment = _this$props5.isDeployment,
          isEdit = _this$props5.isEdit,
          enumList = _this$props5.enumList;
        var childList = enumList ? enumList === null || enumList === void 0 ? void 0 : enumList.childList.map(function (item) {
          return (0, _extends2["default"])({}, item, {
            dictValue: item.dictValue,
            dictKey: item.dictKey,
            label: item.dictValue,
            value: item.dictKey
          });
        }) : _data.selectList;
        isDeployment = isDeployment === undefined ? 'primary' : isDeployment;
        var mode = isMultiple ? 'multiple' : 'single';
        if (isDeployment === 'primary') {
          defaultValue = defaultValue === '' ? [] : defaultValue;
          return /*#__PURE__*/_react["default"].createElement(_select["default"], {
            onChange: function onChange(e) {
              return _this.onChange(e);
            },
            defaultValue: defaultValue,
            hasClear: true,
            placeholder: placeholder,
            disabled: isEdit,
            mode: mode,
            dataSource: childList
          });
        } else {
          if (mode === 'multiple') {
            return /*#__PURE__*/_react["default"].createElement(_checkbox["default"].Group, {
              onChange: function onChange(e) {
                return _this.onChange(e);
              },
              defaultValue: defaultValue,
              disabled: isEdit,
              dataSource: childList
            });
          } else {
            return /*#__PURE__*/_react["default"].createElement(_radio["default"].Group, {
              onChange: function onChange(e) {
                return _this.onChange(e);
              },
              defaultValue: defaultValue,
              disabled: isEdit,
              dataSource: childList
            });
          }
        }
      }
      return /*#__PURE__*/_react["default"].createElement(_input["default"], {
        onChange: function onChange(e) {
          return _this.onChange(e);
        },
        placeholder: placeholder,
        defaultValue: defaultValue
      });
    };
    _this.state = {
      childList: [],
      selectData: []
    };
    return _this;
  }
  var _proto = FormItem.prototype;
  _proto.componentDidMount = /*#__PURE__*/function () {
    var _componentDidMount = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2() {
      var _this2 = this;
      return _regenerator["default"].wrap(function _callee2$(_context2) {
        while (1) switch (_context2.prev = _context2.next) {
          case 0:
            _events["default"].on('getData', function (commonVOs) {
              return _this2.getData(commonVOs);
            });
          case 1:
          case "end":
            return _context2.stop();
        }
      }, _callee2);
    }));
    function componentDidMount() {
      return _componentDidMount.apply(this, arguments);
    }
    return componentDidMount;
  }();
  _proto.componentWillUnmount = function componentWillUnmount() {
    var _this3 = this;
    _events["default"].off('getData', function () {
      return _this3.getData;
    });
  };
  _proto.render = function render() {
    var _this$props6 = this.props,
      displayName = _this$props6.displayName,
      fieldName = _this$props6.fieldName,
      style = _this$props6.style,
      isEdit = _this$props6.isEdit,
      isEmpty = _this$props6.isEmpty;
    // 表单项
    var formItemLayoutItem = {
      labelCol: {
        span: 8
      },
      wrapperCol: {
        span: 14
      }
    };
    return /*#__PURE__*/_react["default"].createElement(_form["default"].Item, (0, _extends2["default"])({
      style: style,
      colon: true,
      name: fieldName
    }, formItemLayoutItem, {
      requiredMessage: "\u8BF7\u8F93\u5165" + displayName,
      required: isEmpty,
      disabled: isEdit,
      labelAlign: "left",
      label: displayName
    }), this.renderColumns());
  };
  return FormItem;
}(_react["default"].Component);
FormItem.displayName = void 0;
var _default = FormItem;
exports["default"] = _default;