import React,{Component} from 'react'
import { Switch, Route, Redirect } from 'react-router-dom';

import { inject, observer } from 'mobx-react'

import {Layout,Card} from 'antd' 

import HeaderCom from './Header'
import SideMenuCom from './SideMenu'
import ContainerCom from './Container'

import './index.less'

import Home from '../Home'

import BusinessRouter from '../Business'

import EntryRouter from '../BusEntry'
import Bar from './Bars'

const {
  Header, Footer, Sider, Content,
} = Layout;

@inject('UserContext')
@inject('i18n')
@observer
export default class LayoutCom extends Component {
    constructor(props)
    {
        super(props)
    }
    
    isPathAuthed()
    {
        const bus_str = 'business'
        let path = this.props.location.pathname
        let index = path.indexOf(bus_str)
        if(index < 0)
            return false
        index += bus_str.length+1
        if(index >= path.length)
            return false
        let trnnam = path.substring(index)
        if(trnnam == 'home')
            return true
        return this.walkPath(this.props.UserContext.menu,trnnam)
    }
    walkPath(menuList,trnnam)
    {
        if(!menuList.length)
            return false
        for(let item of menuList)
        {
            if(item.value == trnnam)
                return true
            if(item.subMenu && item.subMenu.length)
            {
                if(this.walkPath(item.subMenu,trnnam))
                    return true
            }
        }
        return false
    }

    render()
    {
        const prefix = this.props.i18n.prefix
        //let isAuthed = this.isPathAuthed();
        // if(!isAuthed)
        // {
        //     return (
        //     <Redirect to={`${prefix}business/home`} repace={true}  />
        //     )
        // }
        if(!this.props.UserContext.token || !this.props.UserContext.userId)
        {
            return <Redirect to={`${this.props.i18n.lang?'/'+this.props.i18n.lang:''}/login`} />
        }
        return (
            <Layout className="x_layout">
                <Header  className="header_bg">
                   <HeaderCom/> 
                </Header>
                <Layout className="xx_layout">
                    <Sider className="sideMenu">
                        <SideMenuCom />
                    </Sider>
                    <Content className="xxx_content">
                        <ContainerCom style={{height:'100%'}}>
                            {/*模块路由配置*/}
                            <div style={{padding:'5px',backgroundColor:'#dce3ee',height:'100%'}}>
                            <React.Fragment>
                                <Route path={`${prefix}business/home`}  component={Home} />
                                
                                <Route path={`${prefix}business`}  render={()=><BusinessRouter prefix={`${prefix}business/`} />} />
                                <Route path={`${prefix}business`} component={EntryRouter} />
                            </React.Fragment>
                            </div>
                        </ContainerCom>
                    </Content>
                    {/* <Bar/> */}
                </Layout>
            </Layout>
        )
    }
}