import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
import _createClass from 'babel-runtime/helpers/createClass';
import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
import _inherits from 'babel-runtime/helpers/inherits';
var _jsxFileName = 'src/slideMenu/SlideMenu.jsx';
/**
 * menu.jsx
 *
 * @Author: jruif/zhuwei
 * @Date: 2017/7/20 下午6:13
 */

import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
import { Icon } from '../index';
// import { NavLink } from 'react-router-dom';

var SlideMenu = function (_PureComponent) {
    _inherits(SlideMenu, _PureComponent);

    function SlideMenu(props, context) {
        var _this2 = this;

        _classCallCheck(this, SlideMenu);

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

        _this.handleClick = function (item, url) {
            var onClick = _this.props.onClick;

            if (url) {
                onClick(item);
                _this.setState({
                    currentUrl: url
                });
            }
        };

        _this.renderMenu = function () {
            var menu = _this.props.menu;

            var menuArray = [];
            // 子菜单模板
            var menuItemElement = function menuItemElement(item, className) {
                var reactElement = void 0;
                if (item.target) {
                    reactElement = React.createElement(
                        'a',
                        { href: item.url, target: item.target, __source: {
                                fileName: _jsxFileName,
                                lineNumber: 62
                            },
                            __self: _this2
                        },
                        React.createElement(Icon, { type: item.icon, __source: {
                                fileName: _jsxFileName,
                                lineNumber: 63
                            },
                            __self: _this2
                        }),
                        ' ',
                        item.name
                    );
                } else if (item.url) {
                    reactElement = React.createElement(
                        'a',
                        { className: item.new ? 'red-dot ' + className : '' + className, __source: {
                                fileName: _jsxFileName,
                                lineNumber: 68
                            },
                            __self: _this2
                        },
                        React.createElement(Icon, { type: item.icon, __source: {
                                fileName: _jsxFileName,
                                lineNumber: 69
                            },
                            __self: _this2
                        }),
                        ' ',
                        item.name
                    );
                } else {
                    reactElement = React.createElement(
                        'span',
                        { className: 'menu-sub-title', __source: {
                                fileName: _jsxFileName,
                                lineNumber: 74
                            },
                            __self: _this2
                        },
                        React.createElement(Icon, { type: item.icon, __source: {
                                fileName: _jsxFileName,
                                lineNumber: 75
                            },
                            __self: _this2
                        }),
                        item.name
                    );
                }
                return reactElement;
            };

            // 开始组装子菜单成数组
            var packageMenu = function packageMenu(menu) {
                var currentUrlArray = _this.state.currentUrl.split('/');

                menu.forEach(function (item) {
                    var className = '';
                    if (item.url) {
                        var urlArray = item.url.split('/');
                        if (currentUrlArray[1] === urlArray[1]) {
                            className = 'isActive';
                        }
                    }
                    // const className = item.url === this.state.currentUrl ? 'isActive' : ''
                    if (item.subMenu && item.subMenu.length !== 0) {
                        // 存在子菜单
                        var parentMenu = React.createElement(
                            'li',
                            { key: item.id, className: 'menu-item', onClick: function onClick() {
                                    return _this.handleClick(item, item.url);
                                }, __source: {
                                    fileName: _jsxFileName,
                                    lineNumber: 99
                                },
                                __self: _this2
                            },
                            menuItemElement(item, className)
                        );
                        menuArray.push(parentMenu);
                        packageMenu(item.subMenu);
                    } else if (!item.isHide) {
                        // 不存在子菜单
                        var menuLi = React.createElement(
                            'li',
                            { key: item.id, className: 'menu-item', onClick: function onClick() {
                                    return _this.handleClick(item, item.url);
                                }, __source: {
                                    fileName: _jsxFileName,
                                    lineNumber: 108
                                },
                                __self: _this2
                            },
                            menuItemElement(item, className)
                        );
                        menuArray.push(menuLi);
                    }
                });
            };
            packageMenu(menu);
            return menuArray;
        };

        _this.state = {
            currentUrl: _this.props.location.pathname || '/index'
        };
        return _this;
    }

    _createClass(SlideMenu, [{
        key: 'componentWillReceiveProps',
        value: function componentWillReceiveProps(nextProps) {
            if (nextProps.location !== this.props.location) {
                console.log(nextProps.location.pathname);
                this.setState({
                    currentUrl: nextProps.location.pathname
                });
            }
        }

        // componentDidMount() {
        //     const { location } = this.props
        //     this.setState({
        //         currentUrl: location.pathname,
        //     });
        // }

    }, {
        key: 'render',
        value: function render() {
            var menu = this.props.menu;

            return React.createElement(
                'div',
                { className: 'menu', __source: {
                        fileName: _jsxFileName,
                        lineNumber: 123
                    },
                    __self: this
                },
                React.createElement(
                    'ul',
                    {
                        __source: {
                            fileName: _jsxFileName,
                            lineNumber: 124
                        },
                        __self: this
                    },
                    this.renderMenu(menu).map(function (item) {
                        return item;
                    })
                )
            );
        }
    }]);

    return SlideMenu;
}(PureComponent);

SlideMenu.propTypes = {
    menu: PropTypes.array,
    size: PropTypes.string
};
SlideMenu.defaultProps = {};


export default SlideMenu;