import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { browserHistory } from 'amos-react-router';
import { sessionConsts } from '../../consts/storageConsts';
import { Store } from 'amos-tool';
import  BizNav from './../../view/main/header/BizNav';

const lsTool = Store.lsTool;
/**
 * main
 *业务屏主屏
 * @class MainView
 * @extends {Component}
 */
class RootBizView extends Component {
  constructor(props) {
    super(props);
    this.state = {
      submenu: []
    };
  }

  componentWillMount() {

  }
  componentDidMount() {
    window.addEventListener('storage', this.onStorage);
    this.initSubmenu();
  }
  componentWillUnmount() {
    window.localStorage.removeItem('submenu');
    window.localStorage.removeItem('sub');
    window.removeEventListener('storage', this.onStorage);
  }
  initSubmenu(){
    let systemMenu = JSON.parse(lsTool.read(sessionConsts.systemMenu));
    if (systemMenu.length > 0) {
      window.localStorage.setItem('submenu',JSON.stringify());
      console.info(systemMenu[0].children);
      if ( systemMenu[0].children && systemMenu[0].children.length > 0 ){
        this.setState({ submenu: systemMenu[0].children });
        this.setfirstpage(systemMenu[0].children);
      }
    }
  }
  onStorage = e => {
    if (e.key === 'submenu') {
      console.info('submenu'+e);
      let menuItem = JSON.parse(e.newValue);
      this.setState({ submenu: menuItem });
      this.setfirstpage(menuItem);
    }}
  setfirstpage = data => {
    browserHistory.push(data[0].path);
  };
  render() {
    const { submenu } = this.state;
    const { children } = this.props;
    return (
      <div className="biz-view-root">
        <BizNav submenu={submenu} />
        <div className="content-body">
          {children}
        </div>
      </div>
    );
  }
}

RootBizView.propTypes = {
  children: PropTypes.node
};

export default RootBizView;
