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

@inject('hrmPaidLeavePeriod')

@observer
export default class PaidLeavePeriod 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 {
			hrmPaidLeavePeriod: store
		} = this.props;
		const {
			checkAuthorized,
			initData
		} = store;
		checkAuthorized('plsmanager', {
			type: 'PLSPeriod'
		}, initData);
	}

	render() {
		const {
			hrmPaidLeavePeriod: store
		} = this.props;
		const {
			containerInitFinished,
			topBtnAndMenu,
			tabConfig,
			getTabIndex,
			orgTreeProps,
			setActiveTab,
			refreshTab,
			selectedSubCompany
		} = store;
		const {
			btns,
			menus
		} = topBtnAndMenu();
		let title = i18n.module.paidLeavePeriod();
		if (selectedSubCompany != null && selectedSubCompany.name)
			title = `${title} - ${selectedSubCompany.name}`;
		const topProps = {
			title,
			icon: <i className='icon-coms-hrm'/>,
			iconBgcolor: '#217346',
			showDropIcon: true,
			buttons: btns,
			dropMenuDatas: menus
		}
		const tabProps = {
			tabConfig,
			tabChangeHandle: (key) => setActiveTab(key),
			activeTabInfo: {
				activeTabKey: tabConfig.activeTabKey,
				activeTabIndex: getTabIndex(tabConfig.tabs, tabConfig.activeTabKey)
			},
			rightMenu: menus,
			store
		}
		return (
			<div className="hrm_module_container">
			{
				containerInitFinished.init && containerInitFinished.authorized &&
				<WeaTop ecId={`${this && this.props && this.props.ecId || ''}_WeaTop@v60r3f`} {...topProps}>
					<WeaLeftRightLayout ecId={`${this && this.props && this.props.ecId || ''}_WeaLeftRightLayout@js192c`} leftCom={<OrganizationTree ecId={`${this && this.props && this.props.ecId || ''}_OrganizationTree@9ty9sq`} {...orgTreeProps} ref='orgTree'/>}>
						<Tabs ecId={`${this && this.props && this.props.ecId || ''}_Tabs@yherg9`} {...tabProps}>
							<DataList ecId={`${this && this.props && this.props.ecId || ''}_DataList@vaunu5`} />
						</Tabs>
					</WeaLeftRightLayout>
				</WeaTop>
			}
			{
				containerInitFinished.init && !containerInitFinished.authorized && 
              	<WeaAlertPage ecId={`${this && this.props && this.props.ecId || ''}_WeaAlertPage@riy5og`}>
                  	<div style={{ color: '#000' }}>{i18n.message.authFailed()}</div>
              	</WeaAlertPage>
			}
			</div>
		)
	}
}