import _Form from "@alifd/next/es/form";
import _Radio from "@alifd/next/es/radio";
import _Checkbox from "@alifd/next/es/checkbox";
import _Select from "@alifd/next/es/select";
import _extends from "@babel/runtime/helpers/extends";
import _Switch from "@alifd/next/es/switch";
import _Rating from "@alifd/next/es/rating";
import _TimePicker from "@alifd/next/es/time-picker2";
import _NumberPicker from "@alifd/next/es/number-picker";
import _DatePicker from "@alifd/next/es/date-picker2";
import _Input from "@alifd/next/es/input";
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import _regeneratorRuntime from "@babel/runtime/regenerator";
//这是表单项组件
import React from 'react';
import "./index.scss";
import { data, selectList } from "./data";
import { config } from '@alilc/lowcode-engine';
import events from "../../utils/events";
var FormItem = /*#__PURE__*/function (_React$Component) {
  _inheritsLoose(FormItem, _React$Component);
  function FormItem(props) {
    var _this;
    _this = _React$Component.call(this, props) || this;
    // 事件通信
    _this.getData = /*#__PURE__*/function () {
      var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(commonVOs) {
        return _regeneratorRuntime.wrap(function _callee$(_context) {
          while (1) switch (_context.prev = _context.next) {
            case 0:
              // 清空数据
              _this.setState({
                selectData: ''
              });
              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 (config.has('dataSet')) {
        dataSet = 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
        }];
      }
      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[fieldName] ? data[fieldName] : defaultValue ? defaultValue : '';
      }
      if (componentType === 'CommonInput') {
        var _this$props3 = _this.props,
          InputLength = _this$props3.InputLength,
          hasLimitHint = _this$props3.hasLimitHint;
        return /*#__PURE__*/React.createElement(_Input, {
          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.createElement(_Input.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.createElement(_DatePicker, {
          onChange: function onChange(e) {
            return _this.onChange(e);
          },
          outputFormat: "YYYY-MM-DD",
          placeholder: placeholder,
          defaultValue: defaultValue
        });
      }
      if (componentType === 'NumberPicker') {
        return /*#__PURE__*/React.createElement(_NumberPicker, {
          onChange: function onChange(e) {
            return _this.onChange(e);
          },
          defaultValue: defaultValue
        });
      }
      if (componentType === 'TimePicker') {
        return /*#__PURE__*/React.createElement(_TimePicker, {
          onChange: function onChange(e) {
            return _this.onChange(e);
          },
          placeholder: placeholder,
          defaultValue: defaultValue
        });
      }
      if (componentType === 'Rating') {
        return /*#__PURE__*/React.createElement(_Rating, {
          onChange: function onChange(e) {
            return _this.onChange(e);
          },
          defaultValue: defaultValue
        });
      }
      if (componentType === 'Switch') {
        return /*#__PURE__*/React.createElement(_Switch, {
          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 _extends({}, item, {
            dictValue: item.dictValue,
            dictKey: item.dictKey,
            label: item.dictValue,
            value: item.dictKey
          });
        }) : selectList;
        isDeployment = isDeployment === undefined ? 'primary' : isDeployment;
        var mode = isMultiple ? 'multiple' : 'single';
        if (isDeployment === 'primary') {
          defaultValue = defaultValue === '' ? [] : defaultValue;
          return /*#__PURE__*/React.createElement(_Select, {
            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.createElement(_Checkbox.Group, {
              onChange: function onChange(e) {
                return _this.onChange(e);
              },
              defaultValue: defaultValue,
              disabled: isEdit,
              dataSource: childList
            });
          } else {
            return /*#__PURE__*/React.createElement(_Radio.Group, {
              onChange: function onChange(e) {
                return _this.onChange(e);
              },
              defaultValue: defaultValue,
              disabled: isEdit,
              dataSource: childList
            });
          }
        }
      }
      return /*#__PURE__*/React.createElement(_Input, {
        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 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
      var _this2 = this;
      return _regeneratorRuntime.wrap(function _callee2$(_context2) {
        while (1) switch (_context2.prev = _context2.next) {
          case 0:
            events.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.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.createElement(_Form.Item, _extends({
      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.Component);
FormItem.displayName = void 0;
export default FormItem;