import {
	inject,
	observer,
} from 'mobx-react'
import {
	toJS
} from 'mobx'
import {
	WeaTop,
	WeaRightMenu,
} from 'ecCom'
import {
	Modal,
	Button,
} from 'antd'
import {
	i18n
} from '../../public/i18n';
const confirm = Modal.confirm;

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

	componentDidMount() {
		this.props.hrmEmployDemand.getButtonsMenu();
	}

	getTopMenuBtns = () => {
		const {
			hrmEmployDemand
		} = this.props, {
			menu,
			mainTable,
		} = hrmEmployDemand, {
			tableStore
		} = mainTable;

		let btns = [];
		const len = tableStore.selectedRowKeys.length;
		toJS(menu.topMenu).map((item, index) => {
			if (item.menuFun !== 'batchDelete') {
				btns.push(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@0ryyd3@${index}`} type='primary' disabled={tableStore.loading} onClick={() => this.handleClick(item)}>{item.menuName}</Button>);
			} else {
				btns.push(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@i9m16r@${index}`} type='primary' disabled={len > 0 ? false : true} onClick={() => this.handleClick(item)}>{item.menuName}</Button>);
			}
		});
		return btns;
	}

	handleClick = (item) => {
		const {
			hrmEmployDemand
		} = this.props, {
			mainTab,
			changeSearchPanelStatus
		} = hrmEmployDemand, {
			isPanelShow
		} = mainTab;

		isPanelShow && changeSearchPanelStatus(false);
		this[item.menuFun] && this[item.menuFun]()
	}

	getDropMenuDatas = () => {
		const {
			hrmEmployDemand
		} = this.props, {
			menu,
			mainTable,
		} = hrmEmployDemand, {
			rightMenu
		} = menu, {
			tableStore
		} = mainTable;

		const len = tableStore.selectedRowKeys.length;

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

		return menus;
	}

	handleMenuClick = (key) => {
		const {
			hrmEmployDemand
		} = this.props, {
			mainTab,
			changeSearchPanelStatus
		} = hrmEmployDemand, {
			isPanelShow
		} = mainTab;

		isPanelShow && changeSearchPanelStatus(false);
		this[key] && this[key]();
	}

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

	new = () => {
		const {
			hrmEmployDemand
		} = this.props, {
			openDialog,
		} = hrmEmployDemand

		openDialog();
	}

	custom = () => {
		const {
			hrmEmployDemand
		} = this.props, {
			mainTable,
		} = hrmEmployDemand, {
			tableStore
		} = mainTable;

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

	batchDelete() {
		this.showConfirm();
	}

	showConfirm = () => {
		let _this = this;
		confirm({
			title: i18n.confirm.defaultTitle(),
			content: i18n.confirm.batchDeleteConfirm(),
			okText: i18n.button.ok(),
			cancelText: i18n.button.cancel(),
			onOk() {
				_this.onOk()
			},
			onCancel() {
				return false
			},
		});
	}

	onOk = () => {
		const {
			hrmEmployDemand
		} = this.props, {
			mainTable,
			setSelectedRowKeys,
			deleteTableInfo,
		} = hrmEmployDemand;

		setSelectedRowKeys(mainTable.tableStore.selectedRowKeys);
		deleteTableInfo();
	}

	render() {
		const {
			hrmEmployDemand
		} = this.props, {
			main
		} = hrmEmployDemand, {
			title,
		} = main;

		return (
			<WeaTop ecId={`${this && this.props && this.props.ecId || ''}_WeaTop@rmisyz`} 
				title={title}
				icon={<i className='icon-coms-hrm'/>}
				iconBgcolor='#217346'
				loading={true}
				buttons={this.getTopMenuBtns()}
				showDropIcon={true}
				dropMenuDatas={this.getDropMenuDatas()}
				onDropMenuClick={e => this.handleMenuClick(e)}
			>
				<WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@0t2jxz`} 
					datas={this.getDropMenuDatas()} 
					onClick={ key => this.handleMenuClick(key)}
					collectParams={{favname:title,favouritetype:5}}
				>
					{this.props.children}
				</WeaRightMenu>
			</WeaTop>
		)

	}
}