"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports["default"] = void 0;
var _button = _interopRequireDefault(require("@alifd/next/lib/button"));
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 _form = _interopRequireDefault(require("@alifd/next/lib/form"));
var _react = _interopRequireDefault(require("react"));
require("./index.scss");
var _variables = require("../../variables");
var _lowcodeEngine = require("@alilc/lowcode-engine");
var _moment = _interopRequireDefault(require("moment"));
// import events from '../../utils/events';

var FormItem = _form["default"].Item;
var Row = _grid["default"].Row,
  Col = _grid["default"].Col;
//这是过滤器

//判断是不是时间函数
function isDate(date) {
  return date instanceof Date;
}
var CommonFilter = /*#__PURE__*/function (_React$Component) {
  (0, _inheritsLoose2["default"])(CommonFilter, _React$Component);
  function CommonFilter(_props) {
    var _this;
    _this = _React$Component.call(this, _props) || this;
    _this.formField = new _field["default"]((0, _assertThisInitialized2["default"])(_this));
    _this.submit = function (v, e) {
      console.log(v);
      if (e) return;
      var _this$props = _this.props,
        fieldList = _this$props.fieldList,
        children = _this$props.children;
      var commonVOs = [];
      var _loop = function _loop(i) {
        var vd = v[i];
        if (vd !== undefined && vd !== null) {
          var value = '';
          if (Array.isArray(vd)) {
            value = vd.join(',');
          } else if (vd.$d && isDate(vd.$d)) {
            value = (0, _moment["default"])(vd.$d).format('YYYY-MM-DD');
          } else {
            value = vd;
          }
          var field = fieldList.find(function (item) {
            return item.fieldName === i;
          });
          var child = children.find(function (item) {
            return item.key === field.id;
          });
          var props = child === null || child === void 0 ? void 0 : child.props;
          var comparison = props === null || props === void 0 ? void 0 : props.comparison;
          if (!comparison) {
            comparison = (props === null || props === void 0 ? void 0 : props.bizType) == 'text' ? 'like' : '=';
          }
          commonVOs.push({
            key: i,
            oper: comparison,
            value1: value
          });
        }
      };
      for (var i in v) {
        _loop(i);
      }
      // console.log(commonVOs)
      _lowcodeEngine.event === null || _lowcodeEngine.event === void 0 ? void 0 : _lowcodeEngine.event.emit('onFilter', commonVOs);
    };
    _this.reset = function () {
      _lowcodeEngine.event === null || _lowcodeEngine.event === void 0 ? void 0 : _lowcodeEngine.event.emit('onFilter', []);
    };
    _this.renderChildren_ = function () {
      var _this$props2 = _this.props,
        children = _this$props2.children,
        span = _this$props2.span,
        __designMode = _this$props2.__designMode;
      return _react["default"].Children.map(children, function (child) {
        var props = child.props;
        var isShow = props.isShow;
        // console.log(isShow)
        if (!isShow) {
          return null;
        }
        return /*#__PURE__*/_react["default"].createElement(Col, {
          span: span
        }, /*#__PURE__*/_react["default"].cloneElement(child, {
          formField: _this.formField
        }));
      });
    };
    _this.state = {
      isOpen: false
    };
    return _this;
  }
  var _proto = CommonFilter.prototype;
  _proto.componentDidMount = function componentDidMount() {
    var __designMode = this.props.__designMode;
    if (__designMode == 'design') {
      this.setState({
        isOpen: true
      });
    }
  };
  _proto.render = function render() {
    var _this2 = this;
    var style = this.props.style;
    var validate = this.formField.validate;
    var isOpen = this.state.isOpen;
    return /*#__PURE__*/_react["default"].createElement(_form["default"], {
      className: _variables.bizCssPrefix + '-common-filter',
      style: style,
      field: this.formField
    }, /*#__PURE__*/_react["default"].createElement(Row, {
      align: "stretch",
      wrap: true,
      className: !isOpen ? 'field-row' : ''
    }, this.renderChildren_()), /*#__PURE__*/_react["default"].createElement(Row, {
      align: "center"
    }, /*#__PURE__*/_react["default"].createElement(Col, {
      style: {
        textAlign: 'right'
      }
    }, /*#__PURE__*/_react["default"].createElement(FormItem, {
      wrapperCol: {
        offset: 12
      }
    }, /*#__PURE__*/_react["default"].createElement(_form["default"].Reset, {
      onClick: function onClick() {
        _this2.reset();
      },
      style: {
        marginRight: 10
      }
    }, "\u6E05\u7A7A"), /*#__PURE__*/_react["default"].createElement(_form["default"].Submit, {
      validate: true,
      type: "primary",
      onClick: function onClick(v, e) {
        validate(function (errors, values) {
          _this2.submit(values, errors);
        });
      },
      style: {
        marginRight: 10
      }
    }, "\u641C\u7D22"), /*#__PURE__*/_react["default"].createElement(_button["default"], {
      onClick: function onClick() {
        _this2.setState({
          isOpen: !isOpen
        });
      },
      text: true,
      className: "btn"
    }, isOpen ? '收起' : '展开')))));
  };
  return CommonFilter;
}(_react["default"].Component);
CommonFilter.displayName = 'CommonFilter';
var _default = CommonFilter;
exports["default"] = _default;