import {
	inject,
	observer,
} from 'mobx-react'
import {
	toJS
} from 'mobx'
import {
	WeaRightMenu,
	WeaPopoverHrm,
} from 'ecCom'
import {
	WeaTableNew
} from 'comsMobx'
import {
	i18n
} from '../../public/i18n';
import {
	jumpToHrmDept,
	jumpToHrmPost,
} from '../../public/pulic-func';
const WeaTable = WeaTableNew.WeaTable;

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

	componentDidMount() {
		this.props.hrmAssImple.getTableInfo();
	}

	getDropMenuDatas = () => {
		const {
			hrmAssImple,
		} = this.props, {
			menu,
		} = hrmAssImple, {
			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 == 'log' || item.menuFun == 'collection' || item.menuFun == 'help' || item.menuFun == 'pageAddress') {
				obj.disabled = true;
			}
			menus.push(obj);
		})

		return menus;
	}

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

	custom = () => {
		const {
			hrmAssImple
		} = this.props, {
			subTable,
		} = hrmAssImple, {
			tableStore
		} = subTable;

		tableStore.setColSetVisible(true);
		tableStore.tableColSet(true);
	}

	onOperatesClick = (record, rowIndex, operate) => {
		const {
			hrmAssImple
		} = this.props, {
			setSelectRowKeys,
			openDialog,
		} = hrmAssImple, {
			randomFieldId
		} = record, {
			index
		} = operate;

		setSelectRowKeys(randomFieldId);

		switch (index) {
			case '0':
				openDialog();
				break;
		}
	}

	reRenderColumns = (c) => {
		const {
			hrmAssImple
		} = this.props, {
			openDialog,
			setSelectRowKeys,
		} = hrmAssImple;

		c.map(cs => {
			if (cs.dataIndex == 'departmentid') {
				cs.render = (t, r) => {
					return <a onClick={() => jumpToHrmDept(r.departmentid)}>{r.departmentidspan}</a>
				}
			}
			if (cs.dataIndex == 'jobtitle') {
				cs.render = (t, r) => {
					return <a onClick={() => jumpToHrmPost(r.jobtitle)}>{r.jobtitlespan}</a>
				}
			}
			if (cs.dataIndex == 'resourceid') {
				cs.render = (t, r) => {
					return (<div>
								<WeaPopoverHrm ecId={`${this && this.props && this.props.ecId || ''}_WeaPopoverHrm@ngft6s@${cs.dataIndex}`} />
								<a href={`javaScript:openhrm(${r.resourceid})`} onClick={e => window.pointerXY(e)}>{r.resourceidspan}</a>
							</div>)
				}
			}
			if (cs.dataIndex == 'aCount') {
				cs.render = (t, r) => {
					return <a onClick={() => {setSelectRowKeys(r.randomFieldId);openDialog()}}>{r.aCountspan}</a>
				}
			}
			if (cs.dataIndex == 'bCount') {
				cs.render = (t, r) => {
					return <a onClick={() => {setSelectRowKeys(r.randomFieldId);openDialog()}}>{r.bCountspan}</a>
				}
			}
		})
	}

	render() {
		const {
			hrmAssImple,
		} = this.props, {
			subTable,
		} = hrmAssImple, {
			tableStore
		} = subTable;

		return (
			<WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@pwadet`} 
				datas={this.getDropMenuDatas()} 
				onClick={ key => this.handleMenuClick(key)}
			>
				<WeaTable ecId={`${this && this.props && this.props.ecId || ''}_WeaTable@xx2tgz`} 
				 	comsWeaTableStore={tableStore}
				 	hasOrder={true}
				 	needScroll={true}
				 	getColumns={c => this.reRenderColumns(c)}
				 	onOperatesClick={(record, rowIndex, operate) => this.onOperatesClick(record, rowIndex, operate)}
				/>
			</WeaRightMenu>
		)

	}
}