import React from 'react'
import * as mobx from 'mobx'

import {
	inject,
	observer,
} from 'mobx-react'

import {
	WeaTop,
	WeaTab,
	WeaRightMenu,
	WeaAlertPage,
	WeaPopoverHrm,
} from 'ecCom'

import {
	Spin,
	Button
} from 'antd'

import {
	WeaSwitch
} from 'comsMobx'

import {
	i18n
} from '../../public/i18n';
import {addContentPath} from '../../util/index.js'

const toJS = mobx.toJS;

import Info from './Info'
import {
	HocFactory
} from './HocFactory'

const WrappedForm = HocFactory(Info);

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

	componentWillMount() {
		let bool = window.location.href.indexOf('/spa/hrm/engine.html') > -1;
		if (bool) {
			document.title = i18n.module.rehire();
		}
	}

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

	componentDidMount() {
		this.init();
	}

	init() {
		const {
			hrmRehireSet
		} = this.props, {
			mobxDataReset,
			getRight
		} = hrmRehireSet;

		mobxDataReset();
		getRight();
	}

	getTopMenuBtns() {
		const {
			hrmRehireSet
		} = this.props, {
			menu,
			saveLoading,
		} = hrmRehireSet, {
			topMenu
		} = menu;

		let btns = [];
		toJS(topMenu).map((item, index) => {
			btns.push(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@gnp1tr@${index}`} type='primary' disabled={saveLoading} onClick={() => this.handleClick(item)} >{item.menuName}</Button>);
		});
		return btns;
	}

	handleClick(item) {
		this[item.menuFun] && this[item.menuFun]();
	}

	getDropMenuDatas() {
		const {
			hrmRehireSet
		} = this.props, {
			menu,
			saveLoading,
		} = hrmRehireSet, {
			rightMenu
		} = menu;

		let menus = [];
		toJS(rightMenu).map((item, index) => {
			let obj = {
				key: item.menuFun,
				icon: <i className={`${item.menuIcon}`}/>,
				content: item.menuName,
			}
			if (item.menuFun == 'custom' || item.menuFun == 'collection' || item.menuFun == 'help' || item.menuFun == 'pageAddress') {
				obj.disabled = true;
			}
			if (item.menuFun == 'save') {
				obj.disabled = saveLoading;
			}
			menus.push(obj);
		})

		return menus;
	}

	handleMenuClick(key) {
		this[key] && this[key]();
	}


	log = () => {
		window.setLogViewProps({
			logSmallType: '20048',
		});
	}

	save() {
		const {
			hrmRehireSet
		} = this.props, {
			save
		} = hrmRehireSet;

		save();
	}

	getTabName() {
		const tabName = [{
				key: '0',
				title: i18n.module.rehire()
			},
			// {
			// 	key: '1',
			// 	title: i18n.label.untreatedMatters()
			// }
		]

		return tabName;
	}

	handleFormChange = (cb) => {
		if (Object.keys(cb)[0] === 'tempresourceid') {
			this.props.hrmRehireSet.getReHireSource(cb.tempresourceid.value);
		}
	}

	render() {
		const {
			hrmRehireSet
		} = this.props, {
			date,
			weaTopTitle,
			selectedKey,
			authorized,
			setSelectedKey,
			tabOne,
			loading,
		} = hrmRehireSet;

		if (!authorized) {
			return (
				<div style={{height: '100%'}}>
                    {loading ?  <div className='hrm-loading-center'>
                                    <Spin ecId={`${this && this.props && this.props.ecId || ''}_Spin@5b24cg`} spinning={loading} size='large'></Spin>
                                </div>
                             :  <div className="hrm-wea-alert-page">
                                    <WeaAlertPage ecId={`${this && this.props && this.props.ecId || ''}_WeaAlertPage@h976gg`}>
                                        <div style={{ color: '#000' }}>
                                            {i18n.message.authFailed()}
                                        </div>
                                    </WeaAlertPage>
                                </div>
                    }   
                </div>
			)
		} else {
			return (
				<WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@o0y2b3`} 
					datas={(selectedKey == '0') ? this.getDropMenuDatas() : []} 
					onClick={ key => this.handleMenuClick(key)}
					collectParams={{favname:weaTopTitle,favouritetype:5}}
				>
			        <WeaTop ecId={`${this && this.props && this.props.ecId || ''}_WeaTop@973lw3`} 
			            title={weaTopTitle}
			            icon={<i className='icon-coms-hrm'/>}
			            iconBgcolor='#217346'
			            loading={true}
			            buttons={this.getTopMenuBtns()}
			            showDropIcon={true}
			            dropMenuDatas={(selectedKey == '0') ? this.getDropMenuDatas() : []}
			            onDropMenuClick={(e) => this.handleMenuClick(e)}
			        >
			            <WeaTab ecId={`${this && this.props && this.props.ecId || ''}_WeaTab@77z9zm`}
					        selectedKey={selectedKey}
					        datas={this.getTabName()}
					        keyParam='key'
					        onChange={(key) => setSelectedKey(key)}
			     	/>
			     		<WeaPopoverHrm ecId={`${this && this.props && this.props.ecId || ''}_WeaPopoverHrm@nuba6x`} />
			     		{selectedKey == '0' && <WrappedForm ecId={`${this && this.props && this.props.ecId || ''}_WrappedForm@9p77ly`} {...tabOne} isFormInit={tabOne.form.isFormInit} onFormChange={cb => this.handleFormChange(cb)}/>}	
			            {selectedKey == '1' && <div><iframe src={addContentPath("/hrm/resign/Resign.jsp?resourceid=")} onload="update();" id="tabcontentframe" name="tabcontentframe" class="flowFrame" frameborder="0" scrolling="auto" style={{width: '100%',height: 248}} ></iframe></div>}
			        </WeaTop>
			    </WeaRightMenu>
			)
		}
	}

}