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

import RouterData from './routerData.js';
import asyncComponent from './AsyncComponent.js';
export default class Routers extends React.Component {

	/**动态加载component */
	dynamicLoadComponent = (component) => {
		if(component !== "" && typeof component === 'string') {
			return asyncComponent(() => import('./../' + component));
		} else if(typeof component === 'function' || typeof component === 'object') {
			return component;
		}
	}

	render() {
		// let windowUrl = "http://xyswechat.e24health.com/PubAccount/?params=route:/0003/00040006^startDate:昨天的日期&code=001UZAr50uWqlE13kDo50P1Er50UZArW&state=NewElements";
		let url = window.location.href.indexOf("?") !== -1 ? String(window.location.href).split("?")[1] : "";
		let path = Util.interceptTheValueOfTheURL(Util.interceptTheValueOfTheURL(url, "params", "&", "="), "route", "^", ":");
		return (
			<Switch>
				{RouterData&&RouterData.map((item, index) => {
					const DynamicComponent = this.dynamicLoadComponent(item.component);
					return <Route exact key={index} path={item.path} render={(props) => {
						document.title = item.path === "/0002/00040001" ? "选择院区" : (item.path === "/0002/00040002" ? "预约挂号" : item.title);
						return <DynamicComponent history={props.history} location={props.location} match={props.match}></DynamicComponent>
						// return <item.component history={props.history} location={props.location} match={props.match}></item.component>
					}} />
				})}
				{/*<Route path='/0001/0006' component={Home}/>*/}
				{/* 重定向 */}
				<Route path='/' exact render={() => (
					<Redirect to={path ? path : '/0001/00000001'} />
				)} />
			</Switch>
		)
	}
};
