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/assType.css'

import Header from './Header';
import Tab from './Tab';
import MainDialog from './MainDialog';
import LeftTree from './LeftTree';

@inject('hrmContractMaintain')
@observer
export default class ContractMaintain 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.contractMaintain();
		}
	}

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

	componentDidMount() {
		this.init();
	}

	init = () => {
		const {
			hrmContractMaintain
		} = this.props, {
			getAuth,
			forceLeftTreeRefresh,
		} = hrmContractMaintain;

		getAuth();
		forceLeftTreeRefresh();
	}

	render() {
		const {
			hrmContractMaintain
		} = this.props, {
			main,
			leftTree,
		} = hrmContractMaintain, {
			authorized,
			loading,
			hasTree
		} = main, {
			triggerRefresh
		} = leftTree;

		if (!authorized) {
			return (
				<div style={{height: '100%'}}>
					{loading ?  <div className='hrm-loading-center'>
									<Spin ecId={`${this && this.props && this.props.ecId || ''}_Spin@s9rbk2`} spinning={loading} size='large'></Spin>
								</div>
						     :	<div className="hrm-wea-alert-page">
		                			<WeaAlertPage ecId={`${this && this.props && this.props.ecId || ''}_WeaAlertPage@96dr6e`}>
						                <div style={{ color: '#000' }}>
						                    {i18n.message.authFailed()}
						                </div>
		                      		</WeaAlertPage>
	                        	</div>
	                }	
                </div>
			)
		} else {
			return (
				<div style={{height: '100%'}}>
					<Header ecId={`${this && this.props && this.props.ecId || ''}_Header@yvzm7b`}>
						{hasTree ? (<WeaLeftRightLayout ecId={`${this && this.props && this.props.ecId || ''}_WeaLeftRightLayout@1qzilz`} 
								    	isNew={true}
								    	leftCom={<LeftTree ecId={`${this && this.props && this.props.ecId || ''}_LeftTree@mk0flu`} triggerRefresh={triggerRefresh}/>}>
										<Tab ecId={`${this && this.props && this.props.ecId || ''}_Tab@jlczh5`}/>
									</WeaLeftRightLayout>)
			 					  : <Tab ecId={`${this && this.props && this.props.ecId || ''}_Tab@iy2fxl`} />
			 			}
					</Header>
					<MainDialog ecId={`${this && this.props && this.props.ecId || ''}_MainDialog@j5ebm7`} />
				</div>
			)
		}
	}


}