'use strict';

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

var _VIEW$EDIT;

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 12/7/16.
                                                                                                                                                                                                                                                                   */


var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _StaticDisplay = require('./StaticDisplay');

var _StaticDisplay2 = _interopRequireDefault(_StaticDisplay);

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

var _Compose = require('../../../../utils/Compose');

var _Compose2 = _interopRequireDefault(_Compose);

var _AsyncDecorator = require('../../../../pageContainer/AsyncDecorator');

var _AsyncDecorator2 = _interopRequireDefault(_AsyncDecorator);

var _InitDecorator = require('../../../../pageContainer/InitDecorator');

var _InitDecorator2 = _interopRequireDefault(_InitDecorator);

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

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

var _ModalInputUtils = require('../../../../utils/ModalInputUtils');

var _StringUtils = require('../../../../utils/StringUtils');

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

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

var compRender = function compRender(config) {
  var record = config.record,
      props = config.props,
      changeInitValue = config.changeInitValue;

  var TempPage = (0, _Compose2.default)(_AsyncDecorator2.default, _InitDecorator2.default)();
  // 模板中调用this.props.callback可以执行回调
  var modalData = record && record.modalInput || {};
  // const mappingFields = record.modalInput.mappingFields;
  var mappingFields = (0, _ModalInputUtils.getMappings)(modalData, props);
  var mapper = void 0;
  if (mappingFields.length > 0) {
    mapper = function mapper(value) {
      return value[mappingFields[0].remoteFieldName];
    };
  }

  var resetValue = function resetValue(value, selectType) {
    console.log(value);
    var newValue = {};
    if (selectType) {
      mappingFields.map(function (item) {
        item.type = props.dataSource.fields.filter(function (item1, index1) {
          return item1.name === item.fieldName;
        })[0].type;
        return item;
      });
    }
    mappingFields && mappingFields.forEach(function (m) {
      if (m.type && m.type === 'String') {
        newValue[m.fieldName] = value[m.remoteFieldName] !== undefined ? value[m.remoteFieldName] + '' : null;
      } else {
        newValue[m.fieldName] = value[m.remoteFieldName] !== undefined ? value[m.remoteFieldName] : null;
      }
    });
    changeInitValue(value, newValue);
  };

  var getDescription = function getDescription(name) {
    var res = props.dataSource.fields && props.dataSource.fields.filter(function (f) {
      return f.name === name;
    });
    return res && res[0] && res[0].description;
  };

  var getParams = function getParams() {
    var formValues = {};
    if (props && props.form) {
      formValues = props.form.getFieldsValue();
    }
    var ret = {};
    var isError = record.modalInput && record.modalInput.bindParameters && record.modalInput.bindParameters.some(function (bindP) {
      if (bindP.name && bindP && !bindP.allowNull) {
        if (!(0, _EmptyUtils.isEmpty)(bindP.value)) {
          ret[bindP.name] = bindP.value;
        } else if (formValues[bindP.name]) {
          ret[bindP.name] = formValues[bindP.name];
        } else if (props.query && props.query[bindP.name]) {
          ret[bindP.name] = props.query[bindP.name];
        } else {
          var content = record.modalInput.promptDesc && record.modalInput.promptDesc;
          _mxa.Modal.error({
            title: '提示',
            content: content || '请先输入' + getDescription(bindP.name) + '!'
          });
          ret = false;
          return true;
        }
      }
      return false;
    });
    if (!isError) {
      var templateFields = (0, _StringUtils.getTemplateFields)(record.modalInput.actionLink);
      if (templateFields.length > 0) {
        templateFields.some(function (item) {
          if (!formValues[item]) {
            var content = record.modalInput.promptDesc && record.modalInput.promptDesc;
            _mxa.Modal.error({
              title: '提示',
              content: content || '请先输入' + getDescription(item) + '!'
            });
            ret = false;
            return true;
          }
          return false;
        });
      }
    }
    return ret;
  };

  return _react2.default.createElement(_components.ModalInput, _extends({}, props, {
    getParams: record.modalInput && record.modalInput.bindParameters && getParams,
    mapper: mapper, resetValue: resetValue,
    disabled: modalData.disabled || !record.isReadonly && props.isLeadInto,
    isReadonly: modalData.disabled || record.isReadonly,
    domainType: modalData.domainType || modalData.templateType || modalData.subDomain,
    domainLink: modalData.domainLink || modalData.actionLink,
    modalData: modalData,
    params: props.query,
    selectType: modalData.selectType,
    record: record
  }));
};

exports.default = (_VIEW$EDIT = {}, _defineProperty(_VIEW$EDIT, _constant.VIEW, _StaticDisplay2.default), _defineProperty(_VIEW$EDIT, _constant.EDIT, compRender), _VIEW$EDIT);