'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = undefined;

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _desc, _value, _class; /**
                            * Created by baoyinghai on 1/11/17.
                            */


var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _moment = require('moment');

var _moment2 = _interopRequireDefault(_moment);

var _coreDecorators = require('core-decorators');

var _components = require('../../../components');

var _mxa = require('../../../components/mxa');

var _SearchInput = require('../../../components/search/SearchInput');

var _SearchInput2 = _interopRequireDefault(_SearchInput);

var _filterBarContainer = require('./filterBarContainer');

var _filterBarContainer2 = _interopRequireDefault(_filterBarContainer);

var _EmptyUtils = require('../../utils/EmptyUtils');

var _dictCodes = require('../../constant/dictCodes');

var _dropMenu = require('./dropMenu');

var _dropMenu2 = _interopRequireDefault(_dropMenu);

var _IconHelper = require('../../utils/IconHelper');

var _SearchBar = require('../../../components/search/SearchBar');

var _SearchBar2 = _interopRequireDefault(_SearchBar);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) {
  var desc = {};
  Object['ke' + 'ys'](descriptor).forEach(function (key) {
    desc[key] = descriptor[key];
  });
  desc.enumerable = !!desc.enumerable;
  desc.configurable = !!desc.configurable;

  if ('value' in desc || desc.initializer) {
    desc.writable = true;
  }

  desc = decorators.slice().reverse().reduce(function (desc, decorator) {
    return decorator(target, property, desc) || desc;
  }, desc);

  if (context && desc.initializer !== void 0) {
    desc.value = desc.initializer ? desc.initializer.call(context) : void 0;
    desc.initializer = undefined;
  }

  if (desc.initializer === void 0) {
    Object['define' + 'Property'](target, property, desc);
    desc = null;
  }

  return desc;
}

var ToolBar = (_class = function (_React$Component) {
  _inherits(ToolBar, _React$Component);

  function ToolBar(props) {
    _classCallCheck(this, ToolBar);

    var _this = _possibleConstructorReturn(this, (ToolBar.__proto__ || Object.getPrototypeOf(ToolBar)).call(this, props));

    _this._deleteNull = function (ary) {
      return ary.filter(function (item) {
        return !(0, _EmptyUtils.isEmpty)(item.value);
      });
    };

    _this._onChange = function (value) {
      var isExist = _this.state.requestFilterFields && _this.state.requestFilterFields.some(function (item, index) {
        if (item.fieldName === value.fieldName) {
          _this.state.requestFilterFields[index] = value;
          return true;
        }
        return false;
      });
      if (!isExist) {
        _this.state.requestFilterFields.push(value);
      }

      _this.setState({ requestFilterFields: _this._deleteNull(_this.state.requestFilterFields) }, function () {
        return console.log(_this.state.requestFilterFields);
      });
    };

    _this._onSearch = function () {
      _this.setState({ advancedSearch: false }, function () {
        return _this.props.onSearch && _this.props.onSearch(_this.state.requestFilterFields, true);
      });
    };

    _this._clear = function () {
      _this.setState({ requestFilterFields: [] });
    };

    _this._getDefaultValue = function (code) {
      var tag = _this.state.requestFilterFields.filter(function (item) {
        return item.fieldName === code;
      });
      return tag[0] && tag[0].value;
    };

    _this._closeMenu = function () {
      if (_this.state.showOtherBtns) {
        _this.setState({ showOtherBtns: false });
      }
      console.log('b', _this.state.showOtherBtns);
    };

    _this._removeFilterItem = function (id) {
      _this.setState({
        requestFilterFields: _this.state.requestFilterFields.filter(function (item) {
          return item.fieldName !== id;
        })
      }, function () {
        return _this.props.onSearch && _this.props.onSearch(_this.state.requestFilterFields, true);
      });
    };

    _this._getValueByType = function (item) {
      var renderHelper = _this.props.filters.filter(function (f) {
        return f.name === item.fieldName;
      })[0];
      if (renderHelper.type === 'DATEPICKER') {
        var left = item.value[0] && (0, _moment2.default)(item.value[0]);
        var right = item.value[1] && (0, _moment2.default)(item.value[1]);
        return [left, right].join(', ');
      } else if (renderHelper.type === 'INPUT' && renderHelper.operatorType === 'BETWEEN') {
        return item.value;
      } else if (renderHelper.type === 'SELECT') {
        var tag = renderHelper.extra.filter(function (i) {
          return i.code === item.value;
        });
        return tag[0] && tag[0].value;
      }
      return item.value;
    };

    _this._searchFunc = function (value) {
      var filters = _this.props.filters || [];
      var v = filters.map(function (item) {
        return _defineProperty({}, item.name, value);
      });
      _this.props.onSearch(v, false);
    };

    _this._renderTags = function () {
      if (_this.state.advancedSearch) return null;
      var filterObj = _this.props.filters.reduce(function (obj, filter) {
        return _extends({}, obj, _defineProperty({}, filter.name, filter.displayValue));
      }, {});
      return _this.state.requestFilterFields.map(function (item) {
        // TODO: 数据字典
        var value = _this._getValueByType(item);
        return _react2.default.createElement(
          _mxa.Tag,
          { id: item.fieldName, callback: _this._removeFilterItem },
          filterObj[item.fieldName] + ':' + value
        );
      });
    };

    _this._renderSimpleOperates = function () {
      var btns = _this.props.renderTopButtons();
      return btns && _react2.default.createElement(
        'ul',
        null,
        btns.map(function (btn, index) {
          var buttonDescriptionSupper = btn.props.buttonDescription;
          if (_this.props.selectRows) {
            buttonDescriptionSupper = btn.props.buttonDescription + '(' + _this.props.selectRows + ')';
          }
          return _react2.default.createElement(
            'li',
            { key: index },
            btn
          );
        })
      );
    };

    _this._renderSimpleOperation = function () {
      /* eslint-disable */
      return _react2.default.createElement(
        'div',
        { className: _this.props.prefixCls + '-grid-simple-operate' },
        _this._renderSimpleOperates(),
        _this._renderRightPart()
      );
    };

    _this._renderRightPart = function () {
      return _react2.default.createElement(
        'div',
        { className: 'more-button' },
        _this.props.filters.length > 0 && _react2.default.createElement(_components.SearchSummary, { dataSource: _this.props.filters, onSearch: _this.props.onSearch }),
        _react2.default.createElement(
          'div',
          null,
          (0, _IconHelper.iconWrapper)('icon-raxicongengduo63')
        )
      );
    };

    _this._renderModalBar = function () {
      return _react2.default.createElement(
        'div',
        null,
        _this._renderSimpleOperation(true)
      );
    };

    _this.state = {
      advancedSearch: false,
      requestFilterFields: [],
      isFocus: false,
      showBtns: true,
      showOtherBtns: false,
      toolBarType: _this.props.toolBarType
    };
    return _this;
  }

  _createClass(ToolBar, [{
    key: 'renderAdvancedDetail',
    value: function renderAdvancedDetail() {
      var _this2 = this;

      var prefixCls = 'mx-search';
      return _react2.default.createElement(
        _filterBarContainer2.default,
        _extends({}, this.props, { advancedSearch: !!this.state.advancedSearch && !this.state.toolBarType, switchHandler: this.updateAdvancedSearch, querySelector: 'scroll-bar-container' }),
        _react2.default.createElement(
          'div',
          null,
          _react2.default.createElement(
            'div',
            { className: prefixCls + '-advanced-search-form' },
            this.props.filters && this.props.filters.map(function (item, index) {
              return _react2.default.createElement(
                'div',
                { key: index },
                _react2.default.createElement(_SearchInput2.default, {
                  key: item.name,
                  type: item.type,
                  operatorType: item.operatorType,
                  code: item.name,
                  title: item.displayValue,
                  extra: item.extra,
                  onChange: _this2._onChange,
                  value: _this2._getDefaultValue(item.name)
                })
              );
            })
          )
        )
      );
    }
  }, {
    key: 'updateAdvancedSearch',
    value: function updateAdvancedSearch() {
      this.setState({
        advancedSearch: !this.state.advancedSearch
      });
    }
  }, {
    key: '_switchOtherBtns',
    value: function _switchOtherBtns(e) {
      this.setState({
        showOtherBtns: !this.state.showOtherBtns
      });
    }
  }, {
    key: '_moreBtnBlur',
    value: function _moreBtnBlur(e) {
      if (this.state.showOtherBtns === true) {
        this.setState({
          showOtherBtns: false
        });
      }
    }
  }, {
    key: '_renderFilterButtons',
    value: function _renderFilterButtons() {
      if (this.state.advancedSearch) {
        var prefixCls = 'mx-search';
        return _react2.default.createElement(
          'div',
          { className: prefixCls + '-advanced-button-bar' },
          _react2.default.createElement(
            'a',
            {
              className: prefixCls + '-advanced-button-clear',
              onClick: this._clear
            },
            '\u6E05\u7A7A'
          ),
          _react2.default.createElement(
            'a',
            {
              className: prefixCls + '-advanced-button',
              onClick: this._onSearch
            },
            '\u68C0\u7D22'
          )
        );
      }
      var btns = this.props.renderTopButtons(function (item) {
        return item.relatedData === _dictCodes.BUTTON_BINDPARAMETERTYPE.NONE || item.relatedData === _dictCodes.BUTTON_BINDPARAMETERTYPE.ALL;
      });
      if (btns && btns.length > 0) {
        return _react2.default.createElement(
          'div',
          { className: 'create-entity-toolbar' },
          _react2.default.createElement(
            'div',
            { className: 'create-entity-btn dinline-block lfloat', onClick: this._moreBtnBlur },
            btns[0]
          ),
          _react2.default.createElement(
            'a',
            {
              className: 'create-entity-more dinline-block lfloat' + (this.state.showOtherBtns ? ' create-entity-more-down' : ''),
              onClick: this._switchOtherBtns
            },
            _react2.default.createElement('i', { className: 'dblock' })
          )
        );
      }
      return null;
    }
  }, {
    key: '_renderFilter',
    value: function _renderFilter() {
      var _this3 = this;

      var prefixCls = this.props.prefixCls;

      var toolbarClass = prefixCls + '-toolbar';
      return _react2.default.createElement(
        'div',
        null,
        _react2.default.createElement(
          'div',
          { className: toolbarClass },
          _react2.default.createElement(
            'div',
            { className: 'advanced-search-advanced' },
            _react2.default.createElement(_components.AdvancedSearch, { dataSource: this.props.filters, advancedSearch: this.state.advancedSearch, requestFilterFields: this.state.requestFilterFields, onSearch: function onSearch(value) {
                return _this3.props.onSearch(value, true);
              }, updateAdvancedSearch: this.updateAdvancedSearch }),
            _react2.default.createElement(
              'div',
              { className: 'tag-list' },
              this._renderTags()
            )
          ),
          _react2.default.createElement(
            'div',
            { className: 'toolbar-buttons grid_tool' },
            this._renderFilterButtons()
          ),
          _react2.default.createElement(_SearchBar2.default, { searchFunc: this._searchFunc })
        )
      );
    }
  }, {
    key: '_renderOperates',
    value: function _renderOperates(isAll) {
      var _this4 = this;

      var btns = this.props.renderTopButtons(isAll ? null : function (item) {
        return item.relatedData !== _dictCodes.BUTTON_BINDPARAMETERTYPE.NONE && item.relatedData !== _dictCodes.BUTTON_BINDPARAMETERTYPE.NONE;
      });
      return btns && _react2.default.createElement(
        'ul',
        null,
        btns.map(function (btn, index) {
          if (btn.props.showSelectRowsNum) {
            if (_this4.props.selectRows) {
              btn.props.buttonDescriptionSupper = btn.props.buttonDescription + '(' + _this4.props.selectRows + ')';
            }
          }
          return _react2.default.createElement(
            'li',
            { key: index },
            btn
          );
        })
      );
    }
  }, {
    key: '_switchToolBar',
    value: function _switchToolBar() {
      if (this.state.toolBarType) {
        this.props.closeCallback && this.props.closeCallback();
      }
      this.setState({
        toolBarType: !this.state.toolBarType
      });
    }
  }, {
    key: '_renderOperation',
    value: function _renderOperation() {
      /* eslint-disable */
      return _react2.default.createElement(
        'div',
        { className: this.props.prefixCls + '-grid-operate' },
        _react2.default.createElement('a', { className: 'close', onClick: this._switchToolBar }),
        this._renderOperates()
      );
    }
  }, {
    key: 'componentWillReceiveProps',
    value: function componentWillReceiveProps(nextProps) {
      if (nextProps.toolBarType !== this.state.toolBarType) {
        this.setState({
          toolBarType: !this.state.toolBarType
        });
      }
    }
  }, {
    key: 'render',
    value: function render() {
      var top = '45px';
      if (this.props.isInfo) {
        top = '56px';
      } else if (this.props.isModal) {
        top = '45px';
      } else if (this.props.isProcess) {
        top = '45px';
      }
      if (this.props.isModal) {
        return _react2.default.createElement(
          'div',
          { className: 'scroll-bar-container' },
          this._renderModalBar()
        );
      }
      return _react2.default.createElement(
        'div',
        { className: 'scroll-bar-container' },
        _react2.default.createElement(
          'div',
          { className: 'scroll-bar', style: { overflow: 'hidden', height: '60px' } },
          _react2.default.createElement(
            'div',
            { style: { marginTop: this.state.toolBarType ? '-60px' : '0px' } },
            this._renderFilter(),
            this._renderOperation()
          )
        ),
        this.renderAdvancedDetail(),
        _react2.default.createElement(_dropMenu2.default, {
          querySelector: 'scroll-bar-container',
          showOtherBtns: this.state.showOtherBtns,
          style: { display: this.state.showOtherBtns ? '' : 'none', top: top },
          renderTopButtons: this.props.renderTopButtons(function (item) {
            return item.relatedData === _dictCodes.BUTTON_BINDPARAMETERTYPE.NONE;
          }),
          moreBtnBlur: this._moreBtnBlur
        })
      );
    }
  }]);

  return ToolBar;
}(_react2.default.Component), (_applyDecoratedDescriptor(_class.prototype, 'renderAdvancedDetail', [_coreDecorators.autobind], Object.getOwnPropertyDescriptor(_class.prototype, 'renderAdvancedDetail'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, 'updateAdvancedSearch', [_coreDecorators.autobind], Object.getOwnPropertyDescriptor(_class.prototype, 'updateAdvancedSearch'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, '_switchOtherBtns', [_coreDecorators.autobind], Object.getOwnPropertyDescriptor(_class.prototype, '_switchOtherBtns'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, '_moreBtnBlur', [_coreDecorators.autobind], Object.getOwnPropertyDescriptor(_class.prototype, '_moreBtnBlur'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, '_renderFilterButtons', [_coreDecorators.autobind], Object.getOwnPropertyDescriptor(_class.prototype, '_renderFilterButtons'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, '_renderFilter', [_coreDecorators.autobind], Object.getOwnPropertyDescriptor(_class.prototype, '_renderFilter'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, '_renderOperates', [_coreDecorators.autobind], Object.getOwnPropertyDescriptor(_class.prototype, '_renderOperates'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, '_switchToolBar', [_coreDecorators.autobind], Object.getOwnPropertyDescriptor(_class.prototype, '_switchToolBar'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, '_renderOperation', [_coreDecorators.autobind], Object.getOwnPropertyDescriptor(_class.prototype, '_renderOperation'), _class.prototype)), _class);
exports.default = ToolBar;