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

import StaticRouterData from './StaticRouterData';
import AddDoctorRouter from './AddDoctor';
import AddOutPatientPageRouter from './OutPatientPage';
import CMOrderSaveRouter from './CMOrderSave';
import DiagnosisRouter from './Diagnosis';
import DiagRouterData from './DiagOperation';
import CommonComponentRouter from './CommonComponent';
import PersonalInformationRoute from './Information'; 

// 按需加载组件
import asyncComponent from './AsyncComponent';

import { $http } from '../containers/config/https';

// 错误地址可以跳转到一个错误页面（引入错误组件然后写到最后面）
// import ErrorView from '../components/errorView/Index.jsx'


export default class Routers extends React.Component {
    constructor() {
        super();
        this.state = {
            RouterData: [],
            bedMenuList: [],
        }
    }
    componentDidMount() {
        let that = this;
        let data = {
            params: [{
                type: 'BedMenu'
            }]
        }
        $http.post('urlDeault', this, {
            code: "01010013",
            data: data,
            success: (res) => {
                if (+res.errorCode === 0) {
                    that.setState({
                        bedMenuList: res.data
                    })
                } else {

                }
            }
        })
    }
    componentWillReceiveProps(props) {
        if (props.routerData !== this.state.RouterData) {
            this.setState({ RouterData: props.routerData });
        }
    }

    componentWillUnmount() {
        // 组件销毁前将静止让setState修改state的状态
        this.setState = (state, callback) => { return; }
    }

    render() {
        return (
            <div style={{ minHeight: 280, width: '100%', height: '100%' }}>
                <Switch>
                    {this.state.RouterData && this.state.RouterData.map((item, index) => {
                        return <Route exact key={index} path={item.path} component={asyncComponent(() => import('../' + item.component))}></Route>
                    })}
                    {StaticRouterData.map((item, index) => {
                        return <Route exact key={index} path={item.path} component={asyncComponent(() => import('../' + item.component))}></Route>
                    })}
                    {AddDoctorRouter.map((item, index) => {
                        return <Route exact key={index} path={item.path} component={asyncComponent(() => import('../' + item.component))}></Route>
                    })}
                    {AddOutPatientPageRouter.map((item, index) => {
                        return <Route exact key={index} path={item.path} component={asyncComponent(() => import('../' + item.component))}></Route>
                    })}
                    {CMOrderSaveRouter.map((item, index) => {
                        return <Route exact key={index} path={item.path} component={asyncComponent(() => import('../' + item.component))}></Route>
                    })}
                    {DiagnosisRouter.map((item, index) => {
                        return <Route exact key={index} path={item.path} component={item.component}></Route>
                    })}
                    {DiagRouterData.map((item, index) => {
                        return <Route exact key={index} path={item.path} component={asyncComponent(() => import('../' + item.component))}></Route>
                    })}
                    {PersonalInformationRoute.map((item, index) => {
                        return <Route exact key={index} path={item.path} component={item.component}></Route>
                    })}
                    {CommonComponentRouter.map((item, index) => {
                        return <Route exact key={index} path={item.path} component={item.component}></Route>
                    })}
                    {this.state.bedMenuList.map((item, index) => {
                        return <Route exact key={index} path={item.code} component={asyncComponent(() => import('../' + item.value))}></Route>
                    })}
                    {/* 重定向 */}
                    <Route path='/' exact render={() => (
                        <Redirect to='/login' />
                    )} />
                    {/* <Route component={ErrorView} /> */}
                </Switch>
            </div>
        )
    }
};