import '../../style/orgChart.less';
import React, {
	Component
} from 'react';
import {
	inject,
	observer
} from 'mobx-react';
import {
	WeaTop,
	WeaAlertPage,
	WeaRightMenu,
	WeaNewScroll,
} from 'ecCom';
import {
	Spin
} from 'antd';
import {
	i18n
} from '../../public/i18n';
import Chart from './Chart';

@inject('hrmOrgChart')

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

	componentDidMount() {
		this.init();
		window.addEventListener('message', this.messageHandle, false);
	}

	componentWillUnmount() {
		window.removeEventListener('message', this.messageHandle, false);
	}

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

	messageHandle = (e) => {
		const {
			hrmOrgChart: store
		} = this.props;
		if (e.data.act == 'initOrgChart') {
			store.onInitOrgChart(e.data.status);
		} else if (e.data.act == 'callChild') {
			store.showSpin(e.data.status);
		}
	}

	init = (refresh = false) => {
		const {
			hrmOrgChart: store
		} = this.props;
		const {
			initData
		} = store;
		initData(refresh);
	}

	render() {
		const {
			hrmOrgChart: store
		} = this.props;
		const {
			containerInitFinished,
			topBtnAndMenu,
			spinning
		} = store;
		const {
			btns,
			menus
		} = topBtnAndMenu();
		const topProps = {
			title: i18n.module.orgChart(),
			icon: <i className='icon-coms-hrm'/>,
			iconBgcolor: '#217346',
			showDropIcon: true,
			buttons: btns,
			dropMenuDatas: menus
		}
		return (
			<div className="hrm_module_container orgChart">
			{
				containerInitFinished.init && containerInitFinished.authorized &&
				<Spin ecId={`${this && this.props && this.props.ecId || ''}_Spin@lmus09`} spinning={spinning}>
					<WeaTop ecId={`${this && this.props && this.props.ecId || ''}_WeaTop@hj68uv`} {...topProps}>
						<Chart ecId={`${this && this.props && this.props.ecId || ''}_Chart@8ksaj4`} store={store}/>
					</WeaTop>
				</Spin>
			}
			{
				containerInitFinished.init && !containerInitFinished.authorized && 
              	<WeaAlertPage ecId={`${this && this.props && this.props.ecId || ''}_WeaAlertPage@yioiei`}>
                  	<div style={{ color: '#000' }}>{i18n.message.authFailed()}</div>
              	</WeaAlertPage>
			}
			</div>
		)
	}
}