import '../../style/batchMaintenance.less';
import React, {
	Component
} from 'react';
import {
	WeaTop,
	WeaAlertPage
} from 'ecCom';
import {
	inject,
	observer
} from 'mobx-react';
import {Spin} from 'antd';
import {
	i18n
} from '../../public/i18n';
import Dashboard from './Dashboard';
import {
	indexOf,
	has
} from 'lodash';

@inject("hrmBatchMaintenance")

@observer
export default class BatchMaintenance extends Component {
	constructor(props) {
		super(props);
	}

	componentDidMount() {
		this.init(this.setLocationInfo(this.props.location, this.props.params));
	}

	componentWillReceiveProps(nextProps) {
		if (this.props.location.key !== nextProps.location.key) {
			this.init(this.setLocationInfo(nextProps.location, nextProps.params));
		}
	}

	setLocationInfo = (location, params) => {
		const {
			hrmBatchMaintenance: store,
		} = this.props;
		const {
			onRouteChange
		} = store;
		const {
			pathname
		} = location;

		const getPath = () => {
			const arr = pathname.split('/');
			if(arr.indexOf('adjust') >= 0)
				return 'adjust';
			else if(arr.indexOf('edit') >= 0)
				return 'edit';
			else if(arr.indexOf('uploadPhoto') >= 0)
				return 'uploadPhoto';
			else
				return 'batchMaintenance';
		}
		
		const routeObject = {
			location: getPath(),
			params: has(params, 'type') ? params.type : ''
		}
		onRouteChange(routeObject);
		return routeObject;
	}

	init = (routeObject) => {
		const {
			hrmBatchMaintenance: store,
		} = this.props;
		const {
			checkAuthorized,
			initData
		} = store;
		let authKey = '';
		switch(routeObject.location){
			case 'batchMaintenance':
				authKey = 'batchMaintenance';
				break;
			case 'adjust':
				authKey = 'batchMaintenanceAdjust';
				break;
			case 'edit':
				authKey = 'batchMaintenanceAdjustEdit';
				break;
			case 'uploadPhoto':
				authKey = 'batchResourcePhoto';
				break;
		}
		checkAuthorized(authKey, null, initData, null, true);
	}

	render() {
		const {
			hrmBatchMaintenance: store
		} = this.props;
		const {
			containerInitFinished,
			groupDef,
			topMenuDef,
			routeProps,
			routeChanged,
			translateLocation,
			spinning
		} = store;
		let topButtons = {}
		const topProps = {
			icon: <i className='icon-coms-hrm'/>,
			iconBgcolor: '#217346',
			showDropIcon: true
		}
		if (routeProps.location != null) {
			topButtons = topMenuDef()[routeProps.location];
			routeProps.location === 'batchMaintenance' && Object.assign(topProps, {
				dropMenuDatas: []
			})
			routeProps.location === 'adjust' && has(topButtons, 'buttons') && Object.assign(topProps, {
				buttons: topButtons.buttons,
				dropMenuDatas: topButtons.menus
			})
			routeProps.location === 'edit' && has(topButtons, routeProps.params) && Object.assign(topProps, {
				buttons: topButtons[routeProps.params].buttons,
				dropMenuDatas: topButtons[routeProps.params].menus
			})
			routeProps.location === 'uploadPhoto' && Object.assign(topProps, {
				buttons: topButtons.buttons,
				dropMenuDatas: topButtons.menus
			})
		}
		if (indexOf(['batchMaintenance', 'dashboard'], routeProps.location) >= 0) {
			Object.assign(topProps, {
				title: i18n.module.batchMaintenance()
			})
		} else {
			Object.assign(topProps, {
				breadcrumb: [{
					name: i18n.module.batchMaintenance(),
					onClick: () => {
						this.props.router.push('hrmengine/batchMaintenance')
					}
				}, {
					name: translateLocation()
				}]
			})
		}

		return (
			<div className="hrm_module_container batchMaintenance">
				<Spin ecId={`${this && this.props && this.props.ecId || ''}_Spin@qd7nt5`} spinning={spinning}>
				{
					containerInitFinished.init && containerInitFinished.authorized && 
  	              	<WeaTop ecId={`${this && this.props && this.props.ecId || ''}_WeaTop@13ifoo`} {...topProps}>
	              	{
	              		this.props.children && React.cloneElement(this.props.children, {
			                routeProps,
			                rootStore: store,
			                route: this.props.router
			            })
	              	}
	              	{
	              		this.props.children == null && <Dashboard ecId={`${this && this.props && this.props.ecId || ''}_Dashboard@n8fm24`} store={store} />
	              	}
	              	</WeaTop>
				}
				</Spin>
				{
					containerInitFinished.init && !containerInitFinished.authorized && 
	              	<WeaAlertPage ecId={`${this && this.props && this.props.ecId || ''}_WeaAlertPage@aghkaz`}>
	                  	<div style={{ color: '#000' }}>{i18n.message.authFailed()}</div>
	              	</WeaAlertPage>
				}
			</div>
		)
	}
}