import _Field from "@alifd/next/es/field";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import _Grid from "@alifd/next/es/grid";
import _Form from "@alifd/next/es/form";
import React from 'react';
import "./index.scss";
import { bizCssPrefix } from "../../variables";
import events from "../../utils/events";
import moment from 'moment';
var FormItem = _Form.Item;
var Row = _Grid.Row,
  Col = _Grid.Col;
//这是过滤器

//判断是不是时间函数
function isDate(date) {
  return date instanceof Date;
}
var CommonFilter = /*#__PURE__*/function (_React$Component) {
  _inheritsLoose(CommonFilter, _React$Component);
  function CommonFilter(_props) {
    var _this;
    _this = _React$Component.call(this, _props) || this;
    _this.formField = new _Field(_assertThisInitialized(_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 = moment(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);
      events.emit('onFilter', commonVOs);
    };
    _this.reset = function () {
      events.emit('onFilter', []);
    };
    _this.renderChildren_ = function () {
      var _this$props2 = _this.props,
        children = _this$props2.children,
        span = _this$props2.span,
        __designMode = _this$props2.__designMode;
      return React.Children.map(children, function (child) {
        var props = child.props;
        var isShow = props.isShow;
        // console.log(isShow)
        if (!isShow && __designMode !== 'design') {
          return null;
        }
        return /*#__PURE__*/React.createElement(Col, {
          span: span
        }, /*#__PURE__*/React.cloneElement(child, {
          formField: _this.formField
        }));
      });
    };
    return _this;
  }
  var _proto = CommonFilter.prototype;
  _proto.render = function render() {
    var _this2 = this;
    var style = this.props.style;
    var validate = this.formField.validate;
    return /*#__PURE__*/React.createElement(_Form, {
      className: bizCssPrefix + '-common-filter',
      style: style,
      field: this.formField
    }, /*#__PURE__*/React.createElement(Row, {
      align: "stretch",
      wrap: true
    }, this.renderChildren_()), /*#__PURE__*/React.createElement(Row, {
      align: "center"
    }, /*#__PURE__*/React.createElement(Col, {
      style: {
        textAlign: 'right'
      }
    }, /*#__PURE__*/React.createElement(FormItem, {
      wrapperCol: {
        offset: 12
      }
    }, /*#__PURE__*/React.createElement(_Form.Reset, {
      onClick: function onClick() {
        _this2.reset();
      },
      style: {
        marginRight: 10
      }
    }, "\u6E05\u7A7A"), /*#__PURE__*/React.createElement(_Form.Submit, {
      validate: true,
      type: "primary",
      onClick: function onClick(v, e) {
        validate(function (errors, values) {
          _this2.submit(values, errors);
        });
      },
      style: {
        marginRight: 10
      }
    }, "\u641C\u7D22")))));
  };
  return CommonFilter;
}(React.Component);
CommonFilter.displayName = 'CommonFilter';
export default CommonFilter;