'use strict';

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

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 _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _tooltip = require('../../../components/tooltip');

var _displayComp = require('../detail/displayComp');

var _MapUtils = require('../../utils/MapUtils');

var _DOMUtils = require('../../utils/DOMUtils');

var _formValidator = require('../detail/formValidator');

var _formValidator2 = _interopRequireDefault(_formValidator);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: 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; } /**
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                * Created by baoyinghai on 3/28/17.
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                */


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

  function EditableCell() {
    var _ref;

    var _temp, _this, _ret;

    _classCallCheck(this, EditableCell);

    for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = EditableCell.__proto__ || Object.getPrototypeOf(EditableCell)).call.apply(_ref, [this].concat(args))), _this), _this.state = {
      value: _this.props.value,
      editable: false
    }, _this.handleChange = function (e) {
      var value = (0, _MapUtils.getValueByKey)(e, e, 'target', 'value');
      _this.setState({ value: value });
    }, _this._formApiAdapter = function () {
      var getCurrentRecord = _this.props.getCurrentRecord;

      return {
        getFieldsValue: function getFieldsValue() {
          return getCurrentRecord();
        },
        getFieldValue: function getFieldValue(key) {
          return getCurrentRecord()[key];
        },
        validateFields: function validateFields() {
          return true;
        }
      };
    }, _this._executeValidate = function () {
      var rules = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
      var value = arguments[1];

      var errorMsg = [];
      rules.forEach(function (item) {
        if (item && item.required) {
          if (value === undefined || value === '' || value === null) {
            errorMsg.push(item.message);
          }
        } else if (item && item.validator && typeof item.validator === 'function') {
          item.validator({}, value, function (errors) {
            errorMsg = errorMsg.concat(errors);
          });
        }
      });
      if (errorMsg.length > 0) {
        console.error(errorMsg);
        /* eslint-disable */
        var editableCell = _this.refs.editableCell;
        (0, _DOMUtils.addClassName)(editableCell.firstChild, 'editable-cell-error');
        (0, _tooltip.addToolTips)(editableCell, errorMsg[0].message);
        return false;
      }
      return true;
    }, _this.check = function (e) {
      e.stopPropagation();
      var fieldContent = _this.props.fieldContent;

      var rules = (0, _formValidator2.default)(fieldContent, _this._formApiAdapter());
      if (_this._executeValidate(rules, _this.state.value) && _this.props.onChange) {
        if (_this.props.onChange(_this.state.value)) {
          _this.setState({ editable: false });
        }
      }
      console.log('fail');
    }, _this.edit = function (e) {
      e.stopPropagation();
      /* eslint-disable */
      _this.setState({ editable: true });
    }, _this._renderEditComp = function () {
      var fieldContent = _this.props.fieldContent;

      var compRenderFunc = (0, _displayComp.renderFormComp)(fieldContent);
      return _react2.default.cloneElement(compRenderFunc({ record: fieldContent, props: {} }), { onChange: _this.handleChange, value: _this.state.value });
    }, _temp), _possibleConstructorReturn(_this, _ret);
  }

  _createClass(EditableCell, [{
    key: 'render',
    value: function render() {
      var _state = this.state,
          value = _state.value,
          editable = _state.editable;
      var _props = this.props,
          fieldContent = _props.fieldContent,
          renderColumn = _props.renderColumn;

      return _react2.default.createElement(
        'div',
        { className: 'editable-cell', onClick: function onClick(e) {
            return editable && e.stopPropagation();
          } },
        editable ? _react2.default.createElement(
          'div',
          { ref: 'editableCell', className: 'editable-cell-input-wrapper' },
          this._renderEditComp(),
          _react2.default.createElement(_mxa.Icon, {
            type: 'check',
            className: 'editable-cell-icon-check',
            onClick: this.check
          })
        ) : _react2.default.createElement(
          'div',
          { className: 'editable-cell-text-wrapper' },
          renderColumn(value, fieldContent),
          _react2.default.createElement(_mxa.Icon, {
            type: 'edit',
            className: 'editable-cell-icon',
            onClick: this.edit
          })
        )
      );
    }
  }]);

  return EditableCell;
}(_react2.default.Component);

exports.default = EditableCell;