import '../../style/workflowSetting.less';
import React, {
	Component
} from 'react';
import {
	inject,
	observer
} from 'mobx-react';
import {
	WeaTop,
	WeaAlertPage,
	WeaRightMenu,
	WeaLeftRightLayout
} from 'ecCom';
import DataList from './DataList';
import {
	i18n
} from '../../public/i18n';
// import {FormInfo, Tabs, OrganizationTree} from 'weaHrmComsPublic';
import {FormInfo, Tabs, OrganizationTree} from '../../../hrmComsPublic/index';


@inject('hrmWorkflowSetting')

@observer
export default class WorkflowSetting extends Component {
	constructor(props) {
		super(props);
		this.state = {
			moduleName: ''
		}
	}

	componentDidMount() {
		const {
			params
		} = this.props;
		this.setState({
			moduleName: params.type
		})
		this.init(params.type);
	}

	componentWillReceiveProps(nextProps) {
		const {
			params
		} = nextProps;
		this.setState({
			moduleName: params.type
		})
		this.init(params.type);
		this.refs.orgTree && this.refs.orgTree.refs.tree.fetchData();
	}

	init = (moduleType) => {
		const {
			hrmWorkflowSetting: store
		} = this.props, {
			checkAuthorized,
			initData
		} = store
		const moduleName = moduleType === 'stateChange' ? 'statechange' : 'stateschedule';
		checkAuthorized(`stateset/${moduleName}`, null, () => initData(moduleName), 'getStateRightMenu', true);
	}

	render() {
		// const {FormInfo, Tabs, OrganizationTree} = window.weaHrmComsPublic;
		const {
			hrmWorkflowSetting: store
		} = this.props;
		const {
			containerInitFinished,
			topBtnAndMenu,
			orgTreeProps,
			tabConfig,
			formTarget,
			refreshMainTab,
			attendanceOrgTreeProps
		} = store;
		const {
			buttons,
			menus
		} = topBtnAndMenu;
		const {
			moduleName
		} = this.state;
		const title = moduleName === 'stateChange' ? i18n.label.stateChangeWfSetting() : i18n.label.timeManagerWfSetting();
		const topProps = {
			title,
			icon: <i className='icon-coms-hrm'/>,
			iconBgcolor: '#217346',
			showDropIcon: true,
			buttons: buttons,
			dropMenuDatas: menus
		}
		const {
			conditionForm: form,
			conditionFormFields: fields
		} = formTarget;
		const tabProps = {
			tabConfig: tabConfig,
			activeTabInfo: {
				activeTabKey: tabConfig.activeTabKey,
				activeTabIndex: 0
			},
			rightMenu: menus,
			store: store,
			conditionForm: form,
			conditionFormFields: fields,
		}
		const orgProps = (moduleName === 'stateChange' ? orgTreeProps : attendanceOrgTreeProps);
		return (
			<div className="hrm_module_container wfSetting">
			{
				containerInitFinished.init && containerInitFinished.authorized &&
				<WeaTop ecId={`${this && this.props && this.props.ecId || ''}_WeaTop@kp50rc`} {...topProps}>
					{
						containerInitFinished.detachable ? 
						<WeaLeftRightLayout ecId={`${this && this.props && this.props.ecId || ''}_WeaLeftRightLayout@s8f2o2`} leftCom={<OrganizationTree ecId={`${this && this.props && this.props.ecId || ''}_OrganizationTree@is0tqh`} {...orgProps} ref='orgTree'/>}>
							<Tabs ecId={`${this && this.props && this.props.ecId || ''}_Tabs@9j4k63`} {...tabProps}>
				            	<DataList ecId={`${this && this.props && this.props.ecId || ''}_DataList@2x7850`} store={store} rightMenu={menus} moduleName={moduleName}/>
				            </Tabs>
						</WeaLeftRightLayout>
						:
						<Tabs ecId={`${this && this.props && this.props.ecId || ''}_Tabs@4rfla4`} {...tabProps}>
			            	<DataList ecId={`${this && this.props && this.props.ecId || ''}_DataList@qancnl`} store={store} rightMenu={menus} moduleName={moduleName}/>
			            </Tabs>
					}
				</WeaTop>
			}
			{
				containerInitFinished.init && !containerInitFinished.authorized && 
              	<WeaAlertPage ecId={`${this && this.props && this.props.ecId || ''}_WeaAlertPage@i4rtzy`}>
                  	<div style={{ color: '#000' }}>{i18n.message.authFailed()}</div>
              	</WeaAlertPage>
			}
			</div>
		)
	}
}