import React from 'react'
import {
	toJS
} from 'mobx'
import {
	inject,
	observer,
} from 'mobx-react'
import {
	WeaAlertPage,
	WeaLeftRightLayout
} from 'ecCom'
import {
	Spin,
} from 'antd'
import {
	i18n
} from '../../public/i18n';

import '../../style/funcDecent.css'

import Header from './Header';
import Tab from './Tab';
import MainDialog from './MainDialog';
import OrganizationTree from '../../coms/OrganizationTree';

@inject('hrmFuncManageDecent')
@observer
export default class FuncManageDecent extends React.Component {
	constructor(props) {
		super(props);
	}

	componentWillMount() {
		let bool = window.location.href.indexOf('/spa/hrm/engine.html') > -1;
		if (bool) {
			document.title = i18n.module.funcManageDecent();
		}
	}

	componentWillReceiveProps(nextProps) {
		if (this.props.location.key !== nextProps.location.key) {
			this.props.hrmFuncManageDecent.refreshPage();
		}
	}

	componentDidMount() {
		this.init();
	}

	init() {
		const {
			hrmFuncManageDecent
		} = this.props, {
			getAuth,
			refreshPage,
		} = hrmFuncManageDecent;

		getAuth();
		refreshPage();
	}

	render() {
		const {
			hrmFuncManageDecent
		} = this.props, {
			main,
			detachable,
			orgTreeProps
		} = hrmFuncManageDecent, {
			authorized,
			loading
		} = main;

		if (!authorized) {
			return (
				<div style={{height: '100%'}}>
					{loading ?  <div className='hrm-loading-center'>
									<Spin ecId={`${this && this.props && this.props.ecId || ''}_Spin@621g09`} spinning={loading} size='large'></Spin>
								</div>
						     :	<div className="hrm-wea-alert-page">
		                			<WeaAlertPage ecId={`${this && this.props && this.props.ecId || ''}_WeaAlertPage@ltyg4y`}>
						                <div style={{ color: '#000' }}>
						                    {i18n.message.authFailed()}
						                </div>
		                      		</WeaAlertPage>
	                        	</div>
	                }	
                </div>
			)
		} else {
			const coms = [
				<Tab ecId={`${this && this.props && this.props.ecId || ''}_Tab@ftinmp`} />
			];
			let children;
			if(detachable){
				children = (
					<WeaLeftRightLayout ecId={`${this && this.props && this.props.ecId || ''}_WeaLeftRightLayout@jby2rf`} leftCom={<OrganizationTree ecId={`${this && this.props && this.props.ecId || ''}_OrganizationTree@m3r89a`} {...orgTreeProps} ref='orgTree'/>}>
						{coms}
					</WeaLeftRightLayout>
				)
			}else{
				children = coms;
			}

			return (
				<div style={{height: '100%'}}>
					<Header ecId={`${this && this.props && this.props.ecId || ''}_Header@57wghz`}>
						{children}
					</Header>
					<MainDialog ecId={`${this && this.props && this.props.ecId || ''}_MainDialog@plhtdw`} />
				</div>
			)
		}
	}


}