import React, {
	Component
} from 'react';
import {
	inject,
	observer
} from 'mobx-react';
import {
	WeaTop,
	WeaAlertPage,
	WeaLeftRightLayout
} from 'ecCom';
import {
	WeaTableNew
} from 'comsMobx';
import Tabs from '../../coms/Tabs';
import OrganizationTree from '../../coms/OrganizationTree';
import RoleList from './RoleList';
import RoleEditDialog from './RoleEditDialog';
import {
	i18n
} from '../../public/i18n';

const {
	WeaTable
} = WeaTableNew;

@inject('hrmRoleSetting')

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

	componentDidMount() {
		this.init();
	}

	componentWillReceiveProps(nextProps) {
		if (this.props.location.key !== nextProps.location.key) {
			this.init();
			this.refs.orgTree && this.refs.orgTree.refs.tree.fetchData();
		}
	}

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

	render() {
		const {
			hrmRoleSetting: store
		} = this.props;
		//state
		const {
			detachable,
			containerInitFinished,
			topBtnAndMenu,
			orgTreeProps,
			tabConfig,
			formTarget,
			roleEditDialogParams,
			refreshDialog,
			refreshTopBtn
		} = store;
		//data
		const {
			btns,
			menus
		} = topBtnAndMenu();
		const {
			conditionForm: form,
			conditionFormFields: fields
		} = formTarget;

		const tabProps = {
			tabConfig: tabConfig,
			activeTabInfo: {
				activeTabKey: tabConfig.activeTabKey,
				activeTabIndex: 0
			},
			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@9la25h`}
						title={i18n.module.roleSetting()}
						icon={<i className='icon-coms-hrm'/>}
						iconBgcolor='#217346'
						showDropIcon={true}
						buttons={btns}
						dropMenuDatas={menus}
					>
						<WeaLeftRightLayout ecId={`${this && this.props && this.props.ecId || ''}_WeaLeftRightLayout@25l6rw`}
							showLeft={detachable}
							showBtn={detachable}
							leftCom={(<OrganizationTree ecId={`${this && this.props && this.props.ecId || ''}_OrganizationTree@3e8fql`} {...orgTreeProps} ref='orgTree'/>)}>
							<Tabs ecId={`${this && this.props && this.props.ecId || ''}_Tabs@ctldzg`} {...tabProps}>
								<RoleList ecId={`${this && this.props && this.props.ecId || ''}_RoleList@z5d15t`} />
							</Tabs>
						</WeaLeftRightLayout>
						<RoleEditDialog ecId={`${this && this.props && this.props.ecId || ''}_RoleEditDialog@8hedj9`} opt={roleEditDialogParams}/>
					</WeaTop>
				}
				{
					containerInitFinished.init && !containerInitFinished.authorized && 
	              	<WeaAlertPage ecId={`${this && this.props && this.props.ecId || ''}_WeaAlertPage@pl4gbj`}>
	                  	<div style={{ color: '#000' }}>{i18n.message.authFailed()}</div>
	              	</WeaAlertPage>
				}
			</div>
		)
	}
}