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

import Top from './Top';

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

	componentWillMount() {
		const {
			hrmLevelProtection: store
		} = this.props;

		store.reset();
	}

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

	componentWillReceiveProps(nextProps) {
		if (this.props.location.key !== nextProps.location.key) {
			const {
				hrmLevelProtection: store
			} = this.props;

			store.refresh();
		}
	}

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

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