'use strict';

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

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


var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

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

var _clickSpaceCallback = require('../../../components/clickSpaceCallback');

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

var _SessionStorage = require('../../service/SessionStorage');

var _MenuHelper = require('../../utils/MenuHelper');

var _globalNav = require('../../../components/navigator/globalNav');

var _IconHelper = require('../../utils/IconHelper');

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

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 coreLayout = (_class = function (_React$Component) {
  _inherits(coreLayout, _React$Component);

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

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

    _this._renderQuickEnter = function (menu, desc, menuCode) {
      if (!menu) return _react2.default.createElement(
        'span',
        { className: 'crumb-item' },
        _react2.default.createElement(
          'span',
          { style: { paddingLeft: '4px', paddingRight: '5px' } },
          '/'
        ),
        desc
      );
      var Comp = (0, _clickSpaceCallback.clickSpaceWrapper)(_globalNav.HNav, { querySelector: 'ref-class-name-' + menuCode });
      return _react2.default.createElement(Comp, {
        switchNavBar: _this._switchNavBar,
        updateEnterMenuId: _this.props.actions.updateEnterMenuId,
        navBarStatus: _this.state.navBarStatus,
        menu: menu,
        desc: desc,
        push: _this.props.actions.push,
        quickMenu: _this.props.quickMenu,
        updateQuickMenu: _this.props.actions.updateQuickMenu
      });
    };

    _this._goHome = function () {
      _this.props.actions.push({ pathname: '/' });
    };

    _this._logout = function () {
      /* eslint-disable */
      if (__logoutHandler) {
        __logoutHandler(function () {
          return console.log('logout success!');
        }, function (error) {
          Modal.error({
            title: '提示',
            content: error
          });
        });
      } else {
        console.error('there is no func logoutHandler!');
      }
    };

    _this._renderPageHeader = function () {
      /* eslint-disable */
      return _react2.default.createElement(
        'div',
        { className: 'page-header' },
        _react2.default.createElement(_components.GlobalNav, {
          switchNavBar: _this._switchNavBar,
          updateEnterMenuId: _this.props.actions.updateEnterMenuId,
          navBarStatus: _this.state.navBarStatus,
          menu: _this.props.menu,
          push: _this.props.actions.push,
          quickMenu: _this.props.quickMenu,
          updateQuickMenu: _this.props.actions.updateQuickMenu
        }),
        _react2.default.createElement(
          'div',
          { className: 'xsy_oa_detail_crumbs' },
          _react2.default.createElement(
            'ul',
            { className: 'xsy_oa_detail_path' },
            _react2.default.createElement(
              'li',
              null,
              _this._renderCrumbs()
            )
          )
        ),
        _react2.default.createElement(
          'div',
          { className: 'right-tool-bar' },
          _react2.default.createElement(
            'div',
            { className: 't-wrapper' },
            _react2.default.createElement(
              'div',
              null,
              _react2.default.createElement(
                'span',
                { className: 'userpic' },
                _this.props.userName && _this.props.userName[0]
              ),
              _react2.default.createElement(
                'span',
                null,
                _this.props.userName
              )
            )
          ),
          _react2.default.createElement(
            'div',
            { className: 't-wrapper' },
            _react2.default.createElement(
              'span',
              null,
              (0, _IconHelper.iconWrapper)('icon-icon'),
              '\u6D88\u606F\u4E2D\u5FC3'
            ),
            _react2.default.createElement(
              'i',
              { className: 't-badge' },
              '12'
            )
          ),
          _react2.default.createElement(
            'div',
            { className: 't-wrapper' },
            _react2.default.createElement(
              'span',
              null,
              (0, _IconHelper.iconWrapper)('icon-shezhi'),
              '\u8BBE\u7F6E'
            )
          ),
          _react2.default.createElement(
            'div',
            { className: 't-wrapper', onClick: _this._logout },
            _react2.default.createElement(
              'span',
              null,
              (0, _IconHelper.iconWrapper)('icon-tuichu1'),
              '\u9000\u51FA'
            )
          )
        )
      );
    };

    _this._getNavBarWidth = function () {
      return _this.state.navBarStatus ? 220 : 60;
    };

    var defaultNavBarStatus = (0, _MapUtils.getValueByKey)(_this.props.routing, null, 'locationBeforeTransitions', 'pathname') === '/';
    _this.state = {
      navBarStatus: defaultNavBarStatus
    };
    return _this;
  }

  _createClass(coreLayout, [{
    key: '_switchNavBar',
    value: function _switchNavBar() {
      var flag = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;

      var navBarStatus = this.state.navBarStatus;
      if (flag !== null) {
        navBarStatus = !flag;
        if (!navBarStatus === this.state.navBarStatus) return;
      }
      this.setState({
        navBarStatus: !navBarStatus
      }, function () {
        setTimeout(function () {
          return window.dispatchEvent(new Event('resize', { bubbles: true, cancelable: false }));
        }, 500);
      });
    }
  }, {
    key: '_renderCrumbs',
    value: function _renderCrumbs() {
      var _this2 = this;

      var homeIconStyle = { fontSize: '20px', color: '#00bcf2', cursor: 'pointer', position: 'relative', top: '-9px' };
      var defaultCrumb = _react2.default.createElement(
        'span',
        { className: 'crumb-item' },
        (0, _IconHelper.iconWrapper)('icon-home7', null, null, { onClick: this._goHome }, homeIconStyle)
      );
      if ((0, _MapUtils.getValueByKey)(this.props.routing, null, 'locationBeforeTransitions', 'pathname') === '/') {
        (0, _SessionStorage.saveEnterMenuId)();
        (0, _SessionStorage.saveProcessMenuCode)();
        return defaultCrumb;
      }
      var code = (0, _SessionStorage.getRecMenuId)().enterMenuId;
      if (code) {
        var _getMenuItemAndPathBy = (0, _MenuHelper.getMenuItemAndPathByFunc)(function (item) {
          return item.menuCode === code;
        }, this.props.menu),
            _getMenuItemAndPathBy2 = _getMenuItemAndPathBy.indexPath,
            indexPath = _getMenuItemAndPathBy2 === undefined ? [] : _getMenuItemAndPathBy2;

        var _indexPath$reduce = indexPath.reduce(function (mObj, p) {
          return { menu: mObj.menu[p].subMenus, desc: [].concat(_toConsumableArray(mObj.desc), [_this2._renderQuickEnter(mObj.menu[p].subMenus, mObj.menu[p].menuValue, mObj.menu[p].menuCode)]) };
        }, { menu: this.props.menu, desc: [defaultCrumb] }),
            _indexPath$reduce$des = _indexPath$reduce.desc,
            desc = _indexPath$reduce$des === undefined ? [defaultCrumb] : _indexPath$reduce$des;

        return desc;
      }
      return defaultCrumb;
    }
  }, {
    key: '_goBack',
    value: function _goBack() {
      this.props.actions.goBack();
    }
  }, {
    key: 'render',
    value: function render() {
      var _this3 = this;

      return _react2.default.createElement(
        'div',
        { className: 'body-wrapper wrapper remove-block-space' },
        _react2.default.createElement(
          'div',
          { style: { width: this._getNavBarWidth() + 'px', display: 'block' }, className: 'nav-menu' },
          _react2.default.createElement(_components.Nav, { updateQuickMenu: this.props.actions.updateQuickMenu, push: this.props.actions.push, updateEnterMenuId: this.props.actions.updateEnterMenuId, status: this.state.navBarStatus, switchMenu: function switchMenu() {
              return _this3._switchNavBar();
            }, splat: this.props.params && this.props.params.splat })
        ),
        _react2.default.createElement(
          'div',
          { className: 'main-content', style: { left: this._getNavBarWidth() + 'px' } },
          _react2.default.createElement(
            'div',
            { className: 'main-content-row' },
            this._renderPageHeader(),
            _react2.default.createElement(
              'div',
              { className: 'wrapper-panel', style: { padding: '10px 10px 0px 10px', backgroundColor: 'transparent' } },
              _react2.default.cloneElement(this.props.children, { query: this.props.query, goBack: this._goBack, switchNavBar: this._switchNavBar })
            )
          )
        )
      );
    }
  }]);

  return coreLayout;
}(_react2.default.Component), (_applyDecoratedDescriptor(_class.prototype, '_switchNavBar', [_coreDecorators.autobind], Object.getOwnPropertyDescriptor(_class.prototype, '_switchNavBar'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, '_renderCrumbs', [_coreDecorators.autobind], Object.getOwnPropertyDescriptor(_class.prototype, '_renderCrumbs'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, '_goBack', [_coreDecorators.autobind], Object.getOwnPropertyDescriptor(_class.prototype, '_goBack'), _class.prototype)), _class);
exports.default = coreLayout;