'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 _desc, _value, _class, _VIEW$EDIT; /**
                                        * Created by geweimin on 11/17/16.
                                        */


var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _qs = require('qs');

var _qs2 = _interopRequireDefault(_qs);

var _coreDecorators = require('core-decorators');

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

var _StaticDisplay2 = _interopRequireDefault(_StaticDisplay);

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

var _ModalWrapper = require('../../../../pageContainer/ModalWrapper');

var _windowUtils = require('../../../../utils/windowUtils');

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

var _routes = require('../../../../entrance/routes');

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

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

function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) {
  var desc = {};
  Object['ke' + 'ys'](descriptor).forEach(function (key) {
    desc[key] = descriptor[key];
  });
  desc.enumerable = !!desc.enumerable;
  desc.configurable = !!desc.configurable;

  if ('value' in desc || desc.initializer) {
    desc.writable = true;
  }

  desc = decorators.slice().reverse().reduce(function (desc, decorator) {
    return decorator(target, property, desc) || desc;
  }, desc);

  if (context && desc.initializer !== void 0) {
    desc.value = desc.initializer ? desc.initializer.call(context) : void 0;
    desc.initializer = undefined;
  }

  if (desc.initializer === void 0) {
    Object['define' + 'Property'](target, property, desc);
    desc = null;
  }

  return desc;
}

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

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

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

    _this.state = {};
    return _this;
  }

  _createClass(LinkText, [{
    key: '_onClick',
    value: function _onClick() {
      var _this2 = this;

      var buttonLink = this.props.buttonLink;
      var detailResult = this.props.detailResult;
      var jumpProps = {
        buttonCode: this.props.buttonCode
      };
      if (buttonLink.simpleLink) {
        (function () {
          var bindParameter = buttonLink.bindParameters;
          var params = {};
          bindParameter.forEach(function (item, index) {
            if (buttonLink.key && index === 0) {
              params[buttonLink.key] = detailResult[item.name];
            }
            params[item.name] = detailResult[item.name];
          });
          _this2.jumpTab(buttonLink.actionLink, params, buttonLink.domainType, jumpProps);
        })();
      } else {
        (function () {
          var linkItems = buttonLink.actionLinks;
          var linkItem = null;
          linkItems.forEach(function (item, index) {
            if (item.name === detailResult[buttonLink.parameter.name]) {
              linkItem = item;
            }
          });
          if (linkItem) {
            (function () {
              var params = {};
              linkItem.bindParameters.forEach(function (item, index) {
                if (index === 0) {
                  params[linkItem.key] = detailResult[item.name];
                } else {
                  params[item.name] = detailResult[item.name];
                }
              });
              _this2.jumpTab(linkItem.link, params, linkItem.domainType, jumpProps);
            })();
          }
        })();
      }
    }
  }, {
    key: 'jumpTab',
    value: function jumpTab(actionLink, params, domainType, jumpProps) {
      (0, _windowUtils.jumpToNewTab)('/' + _routes.CONTAINER_PRE + (0, _StringUtils.convertTemplate)(actionLink, params, this.props.actionLinkMappings) + '?p=' + btoa(_qs2.default.stringify(_extends({}, params, { readonly: true, domainType: domainType, s: '1' }))), jumpProps);
    }
  }, {
    key: 'render',
    value: function render() {
      var disabled = this.props.disabled;

      return _react2.default.createElement(
        _mxa.Row,
        null,
        _react2.default.createElement(
          _mxa.Col,
          { span: 18 },
          _react2.default.createElement(_mxa.Input, { type: 'primary', disabled: disabled, value: this.props.value })
        ),
        _react2.default.createElement(
          _mxa.Col,
          { offset: 1, span: 5, style: { display: 'flex', flexDirection: 'row-reverse' } },
          _react2.default.createElement(
            _mxa.Button,
            { onClick: this._onClick },
            '\u67E5\u770B\u8BE6\u60C5'
          )
        )
      );
    }
  }]);

  return LinkText;
}(_react2.default.Component), (_applyDecoratedDescriptor(_class.prototype, '_onClick', [_coreDecorators.autobind], Object.getOwnPropertyDescriptor(_class.prototype, '_onClick'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, 'jumpTab', [_coreDecorators.autobind], Object.getOwnPropertyDescriptor(_class.prototype, 'jumpTab'), _class.prototype)), _class);


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

  var buttonLink = record.buttonLink;
  var detailResult = props.dataSource.detailResult;
  var fieldCode = props.dataSource.fieldCode;
  return _react2.default.createElement(LinkText, { buttonCode: fieldCode, buttonLink: buttonLink, detailResult: detailResult, disabled: record.isReadonly || props.isModalInputMappingItem && props.isLeadInto && props.modalInputIsReadonly });
};

// class StaticDisplay extends React.Component {
//   render() {
//     const value = this.props.value;
//     const format = this.props.format ? this.props.format : '0,0.0000';
//     return (
//       <span>{value && numeral(value).format(format)}</span>
//     );
//   }
// }

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