import React from 'react';
import {
	observer,
	inject
} from 'mobx-react';
import {
	AlertPage,
	Loading,
} from '../../public/learn';
import '../../style/init.less'

import Top from './Top';

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

	componentWillMount() {
		this.props.hrmInit.refresh((domain) => domain.newForm());
	}

	componentDidMount() {
		this.props.hrmInit.getPermissionInfo();
	}

	componentWillReceiveProps(nextProps) {
		if (this.props.location.key !== nextProps.location.key) {
			this.props.hrmInit.refresh((domain) => domain.resetForm());
		}
	}

	render() {
		const {
			hrmInit,
		} = this.props, {
			pageAccessPermission
		} = hrmInit, {
			hasRight,
			loading
		} = pageAccessPermission;

		if (!hasRight) {
			return loading ? <Loading ecId={`${this && this.props && this.props.ecId || ''}_Loading@t4gws4`} size='lg'/> : <AlertPage ecId={`${this && this.props && this.props.ecId || ''}_AlertPage@87vbbn`} />
		} else {
			return (
				<Top ecId={`${this && this.props && this.props.ecId || ''}_Top@p6rcho`} />
			)
		}
	}
}