'use strict';

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

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; /**
                    * Created by baoyinghai on 5/31/17.
                    */


var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: 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; }

var RadioGroup = _mxa.Radio.Group;
var FormItem = _mxa.Form.Item;
var Option = _mxa.Select.Option;

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

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

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

    _this.state = {
      methodType: 1
    };

    _this.renderItem = function (keys, configFunc, oldValue) {
      var formItemLayout = {
        labelCol: 22,
        wrapperCol: 22
      };
      var getFieldDecorator = _this.props.form.getFieldDecorator;

      return keys.map(function (key) {
        return configFunc(key, oldValue);
      }).map(function (_ref) {
        var label = _ref.label,
            key = _ref.key,
            initData = _ref.initData,
            comp = _ref.comp;

        return _react2.default.createElement(
          _mxa.Col,
          { span: 22 },
          _react2.default.createElement(
            FormItem,
            _extends({
              key: key
            }, formItemLayout, {
              label: label
            }),
            getFieldDecorator(key, {
              rules: [{ required: true, message: '请输入列' + label + '的描述' }],
              initialValue: initData
            })(comp)
          )
        );
      });
    };

    _this.triggerAction = function () {
      var _this$props = _this.props,
          onClick = _this$props.onClick,
          submit = _this$props.submit,
          form = _this$props.form;

      form.validateFieldsAndScroll({ force: true }, function (err, value) {
        if (!err) {
          submit && submit(value);
          onClick && onClick();
        }
      });
    };

    _this.renderButtons = function () {
      return [_react2.default.createElement(
        'a',
        { key: 'cancel', onClick: _this.props.onClick, className: 'cancel-btn' },
        '\u53D6\u6D88'
      ), _react2.default.createElement(
        'div',
        { key: 'submit', onClick: _this.triggerAction, className: 'common-btn' },
        _react2.default.createElement(
          'a',
          null,
          '\u786E\u5B9A'
        )
      )];
    };

    _this.methodChange = function (event) {
      var value = event.target.value;
      _this.setState({
        methodType: value
      });
    };

    return _this;
  }

  _createClass(SettingForm, [{
    key: 'componentDidMount',
    value: function componentDidMount() {
      var createBottomButtons = this.props.createBottomButtons;

      createBottomButtons && createBottomButtons(this.renderButtons());
    }
  }, {
    key: 'render',
    value: function render() {
      var _this2 = this;

      var dataSource = this.props.dataSource;

      return _react2.default.createElement(
        _mxa.Form,
        { horizontal: true },
        _react2.default.createElement(
          'div',
          { className: 'form-box' },
          _react2.default.createElement(
            _mxa.Row,
            { className: 'cell', type: 'flex', justify: 'start' },
            _react2.default.createElement(
              'section',
              { className: 'detail-sec' },
              _react2.default.createElement(
                'span',
                { className: 'anchorTag' },
                '\u65B0\u589E\u65B9\u5F0F'
              )
            ),
            this.renderItem(['methodType'], function (key, oldValue) {
              return {
                key: key,
                label: '数据源设置',
                comp: _react2.default.createElement(
                  RadioGroup,
                  { onChange: _this2.methodChange },
                  _react2.default.createElement(
                    _mxa.Radio,
                    { value: 1 },
                    '\u73B0\u6709\u5361\u7247'
                  ),
                  _react2.default.createElement(
                    _mxa.Radio,
                    { value: 2 },
                    '\u6570\u636E\u6E90'
                  )
                ),
                initData: oldValue[key]
              };
            }, { methodType: 1 }),
            function () {
              if (_this2.state.methodType === 1) {
                return _react2.default.createElement(
                  'div',
                  { style: { width: '100%' } },
                  _react2.default.createElement(
                    'section',
                    { className: 'detail-sec' },
                    _react2.default.createElement(
                      'span',
                      { className: 'anchorTag' },
                      _this2.props.title
                    )
                  ),
                  _this2.renderItem(['panelDefinitionId'], function (key, oldValue) {
                    return {
                      key: key,
                      label: '卡片选择',
                      comp: _react2.default.createElement(
                        _mxa.Select,
                        null,
                        dataSource.map(function (f) {
                          return _react2.default.createElement(
                            Option,
                            { value: f.panelDefinitionId },
                            f.dataDesc
                          );
                        })
                      ),
                      initData: oldValue[key]
                    };
                  }, {})
                );
              } else {
                return _react2.default.createElement(
                  'div',
                  { style: { width: '100%' } },
                  _react2.default.createElement(
                    'section',
                    { className: 'detail-sec' },
                    _react2.default.createElement(
                      'span',
                      { className: 'anchorTag' },
                      _this2.props.title
                    )
                  ),
                  _this2.renderItem(['panelDefinitionId'], function (key, oldValue) {
                    return {
                      key: key,
                      label: '数据源设置',
                      comp: _react2.default.createElement(
                        _mxa.Select,
                        null,
                        [{ panelDefinitionId: -1, dataDesc: '新增', querySql: '' }].concat(_toConsumableArray(dataSource)).map(function (f) {
                          return _react2.default.createElement(
                            Option,
                            { value: f.panelDefinitionId },
                            f.dataDesc
                          );
                        })
                      ),
                      initData: oldValue[key]
                    };
                  }, { panelDefinitionId: -1 })
                );
              }
            }()
          )
        )
      );
    }
  }]);

  return SettingForm;
}(_react2.default.Component), _class.defaultProps = {
  getKeys: function getKeys(keys) {
    return keys;
  },
  setFormItems: function setFormItems(key) {
    var oldValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

    return {
      key: key,
      label: key,
      comp: _react2.default.createElement(_mxa.Input, null),
      initData: oldValue[key]
    };
  }
}, _temp);
exports.default = _mxa.Form.create()(SettingForm);