'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.renderFormComp = 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; }; /**
                                                                                                                                                                                                                                                                   * Created by baoyinghai on 11/17/16.
                                                                                                                                                                                                                                                                   *
                                                                                                                                                                                                                                                                   * 有数据字典的组件  服务器端返回的是由逗号隔开的字符串. 前端提交时为数组. 如果选项为空, 则返回数组或字符串的时候是null
                                                                                                                                                                                                                                                                   */
// add
// add
// add


var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _InputAnalyser = require('./analyser/InputAnalyser');

var _InputAnalyser2 = _interopRequireDefault(_InputAnalyser);

var _DatePickerAnalyser = require('./analyser/DatePickerAnalyser');

var _DatePickerAnalyser2 = _interopRequireDefault(_DatePickerAnalyser);

var _InputNumberAnalyser = require('./analyser/InputNumberAnalyser');

var _InputNumberAnalyser2 = _interopRequireDefault(_InputNumberAnalyser);

var _SelectAnalyser = require('./analyser/SelectAnalyser');

var _SelectAnalyser2 = _interopRequireDefault(_SelectAnalyser);

var _CascaderAnalyser = require('./analyser/CascaderAnalyser');

var _CascaderAnalyser2 = _interopRequireDefault(_CascaderAnalyser);

var _CheckboxAnalyser = require('./analyser/CheckboxAnalyser');

var _CheckboxAnalyser2 = _interopRequireDefault(_CheckboxAnalyser);

var _RadioAnalyser = require('./analyser/RadioAnalyser');

var _RadioAnalyser2 = _interopRequireDefault(_RadioAnalyser);

var _TimePickerAnalyser = require('./analyser/TimePickerAnalyser');

var _TimePickerAnalyser2 = _interopRequireDefault(_TimePickerAnalyser);

var _SwitchAnalyser = require('./analyser/SwitchAnalyser');

var _SwitchAnalyser2 = _interopRequireDefault(_SwitchAnalyser);

var _CheckboxGroupAnalyser = require('./analyser/CheckboxGroupAnalyser');

var _CheckboxGroupAnalyser2 = _interopRequireDefault(_CheckboxGroupAnalyser);

var _RadioGroupAnalyser = require('./analyser/RadioGroupAnalyser');

var _RadioGroupAnalyser2 = _interopRequireDefault(_RadioGroupAnalyser);

var _ModalInputAnalyser = require('./analyser/ModalInputAnalyser');

var _ModalInputAnalyser2 = _interopRequireDefault(_ModalInputAnalyser);

var _TextAreaAnalyser = require('./analyser/TextAreaAnalyser');

var _TextAreaAnalyser2 = _interopRequireDefault(_TextAreaAnalyser);

var _YearPickerAnalyser = require('./analyser/YearPickerAnalyser');

var _YearPickerAnalyser2 = _interopRequireDefault(_YearPickerAnalyser);

var _PasswordInputAnalyser = require('./analyser/PasswordInputAnalyser');

var _PasswordInputAnalyser2 = _interopRequireDefault(_PasswordInputAnalyser);

var _TransferAnalyser = require('./analyser/TransferAnalyser');

var _TransferAnalyser2 = _interopRequireDefault(_TransferAnalyser);

var _TextAnalyser = require('./analyser/TextAnalyser');

var _TextAnalyser2 = _interopRequireDefault(_TextAnalyser);

var _InnerListAnalyser = require('./analyser/InnerListAnalyser');

var _InnerListAnalyser2 = _interopRequireDefault(_InnerListAnalyser);

var _UploadAnalyser = require('./analyser/UploadAnalyser');

var _UploadAnalyser2 = _interopRequireDefault(_UploadAnalyser);

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

var _JSExecutor = require('../../../utils/JSExecutor');

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

var _formValidator2 = _interopRequireDefault(_formValidator);

var _constant = require('../constant');

var _compRefactHelper = require('./compRefactHelper');

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

var FormItem = _mxa.Form.Item;

var FormItemMap = {
  INPUT: _InputAnalyser2.default,
  DATEPICKER: _DatePickerAnalyser2.default,
  INPUTNUMBER: _InputNumberAnalyser2.default,
  SELECT: _SelectAnalyser2.default,
  CASCADER: _CascaderAnalyser2.default,
  CHECKBOX: _CheckboxAnalyser2.default,
  RADIO: _RadioAnalyser2.default,
  TIMEPICKER: _TimePickerAnalyser2.default,
  SWITCH: _SwitchAnalyser2.default,
  CHECKBOXGROUP: _CheckboxGroupAnalyser2.default,
  RADIOGROUP: _RadioGroupAnalyser2.default,
  MODALINPUT: _ModalInputAnalyser2.default,
  TEXTAREA: _TextAreaAnalyser2.default,
  YEARPICKER: _YearPickerAnalyser2.default,
  PASSWORDINPUT: _PasswordInputAnalyser2.default,
  TEXTLINK: _TextAnalyser2.default,
  TRANSFER: _TransferAnalyser2.default,
  INNERLIST: _InnerListAnalyser2.default,
  UPLOAD: _UploadAnalyser2.default,
  defaultAnalyser: _InputAnalyser2.default
};

// function isArray(o) {
//   return Object.prototype.toString.call(o) === '[object Array]';
// }


var getBindParameter = function getBindParameter(props, record) {
  var defaultValue = record.defaultValue,
      initValueSource = record.initValueSource;

  if (defaultValue) {
    return defaultValue;
  }
  var params = {};
  if (props && props.params) {
    params = props.params;
  }
  var cache = _extends({}, params, (0, _MapUtils.getValueByKey)(props, {}, 'query'));
  if (initValueSource && initValueSource.bindParameter) {
    if (initValueSource.bindParameter.value) {
      return initValueSource.bindParameter.value;
    } else if (cache[initValueSource.bindParameter.name]) {
      return cache[initValueSource.bindParameter.name];
    }
  }
  return cache[record.name];
};

/* eslint-disable */
var transFromtoDate = function transFromtoDate(data, compRender, record) {
  if (data && record.type === 'Long') {
    return data - 0;
  }
  //  if (compRender === FormItemMap.DATEPICKER || compRender === FormItemMap.TIMEPICKER) {
  //    return data && moment(new Date(data));
  //  }
  return data;
};

// 表单项
// hasFeedback={model === 'edit'}
var formAnalyser = function formAnalyser(compRender, model, props) {
  return function (formItemLayout, record, getFieldDecorator, detailResult, changeInitValue, replaceMain) {
    // TODO: if result is true or false
    var initData = detailResult && detailResult[record.name] || getBindParameter(props, record);
    var compRenderData = { record: record, props: props, changeInitValue: changeInitValue, replaceMain: replaceMain };
    var visibleStyle = record.isVisible ? {} : { display: 'none' };
    var analyserName = (0, _MapUtils.getValueByKey)(record, 'default', 'displayComponent', 'componentType');
    var formItemJSX = _react2.default.createElement(
      FormItem,
      _extends({
        style: visibleStyle,
        key: record.description
      }, formItemLayout, {
        label: record.description
      }),
      renderCompItem(getFieldDecorator, record, props, compRender, model, initData, compRenderData)
    );
    if (analyserName === 'UPLOAD') {
      return _react2.default.createElement(
        'div',
        null,
        formItemJSX,
        _react2.default.createElement(
          'span',
          null,
          '\u63D0\u793A\u4FE1\u606F:',
          _react2.default.createElement('br', null),
          '1\u3001\u6587\u4EF6\u5927\u5C0F\u4E0D\u5F97\u8D85\u8FC710MB',
          _react2.default.createElement('br', null),
          '2\u3001\u6587\u4EF6\u540D\u4E0D\u80FD\u5305\u542B\u7F8E\u5143\u7B26\u53F7\u3001\u9017\u53F7\u3001\u659C\u6760\u3001\u53CD\u659C\u6760',
          _react2.default.createElement('br', null),
          '3\u3001\u6587\u4EF6\u7C7B\u578B\u4EC5\u652F\u6301\u9759\u6001\u56FE\u7247\u3001office(doc\u3001docx\u3001xls\u3001xlsx\u3001ppt\u3001pptx)\u6587\u6863\u3001pdf\u6587\u6863\u548C\u666E\u901A\u6587\u672C\u6587\u6863\u3002',
          _react2.default.createElement('br', null),
          '4\u3001\u6587\u4EF6\u540D\u957F\u5EA6\u4E0D\u80FD\u8D85\u8FC7100\u4E2A\u5B57\u7B26\u3002',
          _react2.default.createElement('br', null)
        )
      );
    }
    return formItemJSX;
  };
};

var renderCompItem = function renderCompItem(getFieldDecorator, record, props, compRender, model, initData, compRenderData) {
  if (record.isReadonly && record.displayComponent.componentType === 'INPUT' && props.dataSource.detailResult && props.dataSource.detailResult[record.name]) {
    return _react2.default.createElement(
      _mxa.Tooltip,
      { title: props.dataSource.detailResult[record.name], placement: 'topLeft' },
      getFieldDecorator(record.name, {
        rules: props.createRules && props.createRules(record, props.form) || (0, _formValidator2.default)(record, props.form, props.isModalInputMappingItem),
        initialValue: transFromtoDate(initData, compRender, record) })(compRender[model](compRenderData))
    );
  }
  return _react2.default.createElement(
    'div',
    null,
    getFieldDecorator(record.name, {
      rules: props.createRules && props.createRules(record, props.form) || (0, _formValidator2.default)(record, props.form, props.isModalInputMappingItem),
      initialValue: transFromtoDate(initData, compRender, record) })(compRender[model](compRenderData))
  );
};

var chooseAnalyser = function chooseAnalyser(record, props, changeDataSourceVisable, changeInitValue) {
  var analyserName = (0, _MapUtils.getValueByKey)(record, 'default', 'displayComponent', 'componentType');
  // console.log(analyserName);
  if (record.relatedFunc) {
    return (0, _compRefactHelper.createComp)(FormItemMap[analyserName], changeDataSourceVisable, (0, _JSExecutor.createFunc)(record.relatedFunc), changeInitValue, props.form && props.form.getFieldValue);
  }
  var ret = props.renderAnalyser && props.renderAnalyser(analyserName, record, changeDataSourceVisable);
  return ret || FormItemMap[analyserName] || FormItemMap.defaultAnalyser;
};

var renderFuc = function renderFuc(formItemLayout, record, getFieldDecorator, detailResult, props, changeDataSourceVisable, changeInitValue, replaceMain) {
  return formAnalyser(chooseAnalyser(record, props, changeDataSourceVisable, changeInitValue), props.model, props)(formItemLayout, record, getFieldDecorator, detailResult, changeInitValue, replaceMain);
};

var renderFormComp = exports.renderFormComp = function renderFormComp(fieldContent) {
  var analyserName = (0, _MapUtils.getValueByKey)(fieldContent, 'default', 'displayComponent', 'componentType');
  return FormItemMap[analyserName][_constant.EDIT];
};

exports.default = renderFuc;