import React, {
	Component
} from 'react';
import {
	WeaTop,
	WeaAlertPage
} from 'ecCom';
import Tabs from '../../coms/Tabs';
import Group from './Group';
import AuthorizationList from './AuthorizationList';
import {
	inject,
	observer
} from 'mobx-react';
import {
	i18n
} from '../../public/i18n';

@inject("hrmAuthorizationSetting")

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

	componentDidMount() {
		this.init();
	}

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

	init = () => {
		const {
			hrmAuthorizationSetting: store
		} = this.props;
		const {
			checkAuthorized,
			initData
		} = store;
		checkAuthorized('systemrightgroup', null, initData);
	}

	tabChangeHandle = (key) => {
		const {
			hrmAuthorizationSetting: store
		} = this.props, {
			tabConfig
		} = store;
		store.setActiveTab(tabConfig, key);
	}

	render() {
		const {
			hrmAuthorizationSetting: store
		} = this.props;
		//state
		const {
			containerInitFinished,
			refreshMainTabComponent
		} = store;
		//data
		const {
			tabConfig,
			btnsAndMenus,
			formTarget,
			getTabIndex
		} = store;
		const {
			btns,
			menus,
			tabBtnDef
		} = btnsAndMenus(tabConfig);
		const {
			conditionForm: form,
			conditionFormFields: fields
		} = formTarget;

		const tabProps = {
			tabConfig: tabConfig,
			tabChangeHandle: this.tabChangeHandle,
			activeTabInfo: {
				activeTabKey: tabConfig.activeTabKey,
				activeTabIndex: getTabIndex(tabConfig.tabs, tabConfig.activeTabKey)
			},
			rightMenu: menus,
			store: store,
			conditionForm: form,
			conditionFormFields: fields,
		}

		return (
			<div className="hrm_module_container">
				{
					containerInitFinished.init && containerInitFinished.authorized && 
  	              	<WeaTop ecId={`${this && this.props && this.props.ecId || ''}_WeaTop@9pol4h`}
	                  	title={i18n.module.authorizationSetting()}
	                  	icon={<i className='icon-coms-hrm'/>}
	                  	iconBgcolor='#217346'
	                  	showDropIcon={true}
	                  	buttons={btns}
	                  	dropMenuDatas={menus}
	              	>
                  		<Tabs ecId={`${this && this.props && this.props.ecId || ''}_Tabs@gxz32x`} {...tabProps}>
                      		<Group ecId={`${this && this.props && this.props.ecId || ''}_Group@vqr8rt`}/>
                      		<AuthorizationList ecId={`${this && this.props && this.props.ecId || ''}_AuthorizationList@ixkz42`}/>
	                  	</Tabs>
	              	</WeaTop>
				}
				{
					containerInitFinished.init && !containerInitFinished.authorized && 
	              	<WeaAlertPage ecId={`${this && this.props && this.props.ecId || ''}_WeaAlertPage@nogwn2`}>
	                  	<div style={{ color: '#000' }}>{i18n.message.authFailed()}</div>
	              	</WeaAlertPage>
				}
			</div>
		)
	}
}