'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 _class, _temp, _initialiseProps; /**
                                      * Created by baoyinghai on 6/28/17.
                                      */


var _react = require('react');

var _react2 = _interopRequireDefault(_react);

require('./index.less');

var _lib = require('./lib');

var _lib2 = _interopRequireDefault(_lib);

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 _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }

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; }

// import { Input } from '../../../mxa';

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

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

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

    _initialiseProps.call(_this);

    var _this$props$dataSourc = _this.props.dataSource,
        dataSource = _this$props$dataSourc === undefined ? {} : _this$props$dataSourc;

    _this.state = _extends({}, dataSource);
    return _this;
  }

  _createClass(Form, [{
    key: 'componentWillReceiveProps',
    value: function componentWillReceiveProps(nextProps) {
      // TODO: filter
      var _nextProps$dataSource = nextProps.dataSource,
          dataSource = _nextProps$dataSource === undefined ? {} : _nextProps$dataSource;

      this.setState.apply(this, _toConsumableArray(dataSource));
    }
  }, {
    key: 'render',
    value: function render() {
      var _props$dataSource = this.props.dataSource,
          dataSource = _props$dataSource === undefined ? {} : _props$dataSource;

      return _react2.default.createElement(
        'div',
        null,
        this._renderFormItem(dataSource)
      );
    }
  }]);

  return Form;
}(_react2.default.Component), _initialiseProps = function _initialiseProps() {
  var _this2 = this;

  this._getComp = function (cType) {
    return cType ? _lib2.default[cType] : _lib2.default.INPUT;
  };

  this._renderFormItem = function (dataSource) {
    var d = _this2.state;
    var fieldDesc = _this2.props.fieldDesc;

    var Comp = _this2._getComp(fieldDesc.cType);
    return Object.keys(dataSource).map(function (key) {
      return _react2.default.createElement(
        'div',
        { className: 'form-item-wrapper' },
        _react2.default.createElement(
          'span',
          { className: 'form-item-required' },
          '*'
        ),
        _react2.default.createElement(
          'span',
          { className: 'form-item-label' },
          fieldDesc[key].desc || key
        ),
        _react2.default.createElement(Comp, { className: 'form-item-input', value: d[key], onChange: function onChange(e) {
            return _this2._onChange(e, key);
          }, onBlur: function onBlur(e) {
            return _this2._onBlur(e, key);
          } }),
        _react2.default.createElement(
          'span',
          { className: 'form-item-error' },
          'error'
        )
      );
    });
  };

  this._onChange = function (e, key) {
    // TODO: 做校验
    _this2.setState(_defineProperty({}, key, e.target.value));
  };

  this._onBlur = function (e, key) {
    var _props = _this2.props,
        dispatch = _props.dispatch,
        _getDataPath = _props._getDataPath;
    // TODO: 做校验

    dispatch && dispatch(_getDataPath(key), e.target.value);
  };
}, _temp);
exports.default = Form;