'use strict';

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

var _VIEW$EDIT;

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 _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

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

var _StaticDisplay2 = _interopRequireDefault(_StaticDisplay);

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

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; } /**
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                * Created by baoyinghai on 11/20/16.
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                */

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

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

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

    _this.treeSearch = function () {
      var node = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      var value = arguments[1];

      if (node.value === value) {
        _this.area.push(node.label);
        return node.label;
      }
      if (!node.children) {
        return null;
      }
      if (node.label !== 'root') {
        _this.area.push(node.label);
      }
      var ret = node.children.some(function (children) {
        return _this.treeSearch(children, value);
      });
      if (!ret) {
        _this.area.pop();
      }
      return ret;
    };

    _this._changeList2String = function (value) {
      var ret = [];

      value.reduce(function (obj, code) {
        var tag = obj.filter(function (item) {
          return item.value === code;
        })[0];
        if (tag) {
          ret.push(tag.label);
          return tag.children;
        }
        return [];
      }, _this.props.options);
      var res = ret.join(',');
      if (_this.props.isIndustryCategory) {
        res = _this.props.options.filter(function (item) {
          return item.value === value[0];
        })[0].serialNo + ',' + res;
      }
      return res;
    };

    _this._changeString2List = function () {
      var value = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';

      if (value && value.forEach) {
        return value;
      }
      var ret = [];
      if (value) {
        var splitRes = value.split(',');
        if (_this.props.isIndustryCategory) {
          splitRes.shift();
        }
        splitRes.reduce(function (obj, code) {
          var tag = obj.filter(function (item) {
            return item.label === code;
          })[0];
          if (tag) {
            ret.push(tag.value);
            return tag.children;
          }
          return [];
        }, _this.props.options);
      }
      return ret;
    };

    _this._onChange = function (target) {
      var formValue = _this._changeList2String(target);
      if (_this.props.isArea) {
        _this.area = [];
        /* eslint-disable */
        _this.treeSearch({ label: 'root', value: '0', children: ___AREA }, target[0]);
        _this.props.formHandler(_this.props.fieldName.reduce(function (obj, item, index) {
          obj[item] = _this.area[index];return obj;
        }, {}));
      }
      _this.setState({ value: target });
      _this.props.onChange && _this.props.onChange(formValue);
    };

    _this.area = [];
    _this.state = {
      value: []
    };
    return _this;
  }

  _createClass(MyCascader, [{
    key: 'componentWillReceiveProps',
    value: function componentWillReceiveProps(next) {
      if (next.value !== this.state.value) {
        this.state.value = this._changeString2List(next.value);
      }
    }

    /* eslint-disable */

  }, {
    key: 'componentDidMount',
    value: function componentDidMount() {
      var _this2 = this;

      setTimeout(function () {
        return _this2.setState({ value: _this2._changeString2List(_this2.props.value) });
      });
    }
  }, {
    key: 'render',
    value: function render() {
      var bgStyle = { backgroundColor: '#F7F7F7' };
      return _react2.default.createElement(_mxa.Cascader, {
        options: this.props.options,
        placeholder: this.props.disabled ? '' : "请选择",
        disabled: this.props.disabled,
        value: this.state.value,
        onChange: this._onChange,
        style: this.props.disabled ? bgStyle : null
      });
    }
  }]);

  return MyCascader;
}(_react2.default.Component);

var compRender = function compRender(config) {
  var _config$record = config.record,
      record = _config$record === undefined ? {} : _config$record,
      props = config.props;

  var displayComponent = record.displayComponent || {};
  var options = displayComponent.dictionaryItems;
  var isArea = false;
  var isIndustryCategory = false;
  var fieldName = [];
  if (displayComponent.type === 'ADDRESS') {
    /* eslint-disable */
    options = ___ADDRESS;
    isArea = true;
    fieldName.push(displayComponent.areaName);
    fieldName.push(displayComponent.localName);
  } else if (displayComponent.type === 'INDUSTRYCATEGORY') {
    /* eslint-disable */
    options = ___INDUSTRYCATEGORY;
    isIndustryCategory = true;
  }
  return _react2.default.createElement(MyCascader, {
    formHandler: props.form && props.form.setFieldsValue,
    fieldName: fieldName,
    isArea: isArea,
    isIndustryCategory: isIndustryCategory,
    options: options,
    placeholder: '\u8BF7\u9009\u62E9',
    disabled: record.isReadonly || props.isModalInputMappingItem && props.isLeadInto && props.modalInputIsReadonly
  });
};

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