import React from 'react';
import { Menu, Icon } from 'antd';
import '../leftTab.less';
const SubMenu = Menu.SubMenu;
const MenuItemGroup = Menu.ItemGroup;
import {getMenu} from '../publicMenu.jsx'

const LeftTab = React.createClass({
    getInitialState() {
        return {
            current: '',
            subKey: '',
        };
    },
    current(e){
    	let menuArr=this.state.menu;
    	let curr;
    	menuArr.map(function(item){
        		if(e==item.url){
        			curr=item.subMenus[0].url.split('#') 
        		}
        })
    	return curr;
    },
    handleClick(e) {
    	let menuArr=this.state.menu;
    	let path=e.keyPath[1];
        if (this.state.subKey !== e.keyPath[1]) {
        	let urls= this.current(path);
        	location.href=globalConfig.context +urls[0]+'?rid='+this.state.idtt+'#' + e.key;
        };
        this.props.handlekey(e.key);
        this.setState({
	        	subKey:path,
	            current: e.key,
	        });
    },
   componentWillMount() {
    	let me_=this;
        var ids=window.location.href.indexOf('rid=')
        var idk=window.location.href.substr(ids+4)
        var rid=idk.split('#')
        var menu=getMenu(rid);
        let sub;
        let curr=rid[1];
        for(var i=0;i<menu.length;i++){
        	menu[i].subMenus.map(function(atem){
        		if(atem.url.indexOf(curr)>0){
        			sub=menu[i].url 
        		}
        	})
        }
        let cur,subs;
        if(menu[0].subMenus.length){
        	 cur=menu[0].subMenus[0].url.split('#');
        	 subs=menu[0].url;
        }else{
        	 cur=menu[0].url.split('#');
        	 subs=''
        };
        if(sub!=undefined){
        	this.setState({
	        	menu:menu,
	        	current:rid[1],
	        	subKey:sub,
	        	idtt:rid[0]
        	});
        }else{
        	this.setState({
	        	menu:menu,
	        	current:cur[1],
        		subKey:subs,
	        	idtt:rid[0]
        	});
        }
        
        var htmlMenu=[];
        for(var j=0;j<menu.length;j++){
        	if(menu[j].subMenus.length){
        		htmlMenu.push(
				<SubMenu key={menu[j].url} title={<span>{menu[j].name}</span>}>
					{
						menu[j].subMenus.map(function(item){
							let urls=item.url.split('#');
							return <Menu.Item key={urls[1]}>{item.name}</Menu.Item>
						})
					}
				</SubMenu>
				);
        	}else{
        		let urlt=menu[j].url.split('#');
        		htmlMenu.push(
        			<Menu.Item key={urlt[1]}>{menu[j].name}</Menu.Item>
        		)
        	}
        };
        this.setState({
        	htmlMenus:htmlMenu
	    })
        const _me = this;
        if (window.location.hash&&menu[0].subMenus.length) {
            let theKey = window.location.hash.substr(1);
            menu.map(function (item1) {
	                item1.subMenus.map(function (item2) {
	                	let urls=item2.url.split('#')
	                    if (theKey == urls[1]) {
	                        _me.state.subKey = item1.url
	                    };
	                });
            });
            this.state.current = theKey;
        }else{
        	this.state.current = window.location.hash.substr(1);
        };
    },
    render() {
        return (
            <Menu onClick={this.handleClick}
                selectedKeys={[this.state.current]}
                defaultOpenKeys={[this.state.subKey]}
                mode="vertical"
                className="account-left"
                mode="inline" >
               {this.state.htmlMenus}
            </Menu>
        );
    }
});

export default LeftTab;