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

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

	componentDidMount() {
		const {
			hrmContractMaintain
		} = this.props, {
			getButtonsMenu,
			getFormInfo
		} = hrmContractMaintain;

		getButtonsMenu();
		getFormInfo();
	}

	getTopMenuBtns = () => {
		const {
			hrmContractMaintain
		} = this.props, {
			menu,
			mainTable,
		} = hrmContractMaintain, {
			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@j2m4pi@${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@ftnl2a@${index}`} type='primary' disabled={len > 0 ? false : true} onClick={() => this.handleClick(item)}>{item.menuName}</Button>);
			}
		});
		return btns;
	}

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

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

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

		const len = mainTable.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 {
			hrmContractMaintain
		} = this.props, {
			mainTab,
			changeSearchPanelStatus
		} = hrmContractMaintain, {
			isPanelShow
		} = mainTab;

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

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

		openDialog();
	}

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

	batchDelete() {
		const {
			hrmContractMaintain
		} = this.props, {
			mainTable,
			setSelectedRowKeys,
		} = hrmContractMaintain;

		const keys = toJS(mainTable.tableStore.selectedRowKeys).toString();

		setSelectedRowKeys(keys);

		this.showConfirm();
	}

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

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


	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() {
				_this.props.hrmContractMaintain.resetSelectedRowKeys();
				return false
			},
		});
	}

	onOk = () => {
		this.props.hrmContractMaintain.deleteTableInfo();
	}

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

		return (
			<WeaTop ecId={`${this && this.props && this.props.ecId || ''}_WeaTop@f335yw`} 
				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)}
			>
				{this.props.children}
			</WeaTop>
		)

	}
}