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('hrmWorkHour')
@observer
export default class Header extends React.Component {
	constructor(props) {
		super(props);
	}

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

		getButtonsMenu();
	}

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

		let btns = [];
		const len = tableStore.selectedRowKeys.length;
		toJS(menu.topMenu).map((item, index) => {
			if (item.menuFun !== 'batchDel') {
				btns.push(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@22i3ef@${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@dj2fq8@${index}`} type='primary' disabled={len > 0 ? false : true} onClick={() => this.handleClick(item)}>{item.menuName}</Button>);
			}
		});
		return btns;
	}

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

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

	getDropMenuDatas = () => {
		const {
			hrmWorkHour
		} = this.props, {
			menu,
			mainTable,
		} = hrmWorkHour, {
			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') {
				obj.disabled = true;
			}

			if (item.menuFun == 'batchDel') {
				obj.disabled = len > 0 ? false : true;
			}
			menus.push(obj);
		})

		return menus;
	}

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

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

	addnew = () => {
		const {
			hrmWorkHour
		} = this.props, {
			openDialog,
		} = hrmWorkHour;

		openDialog('main', 'new');
	}

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

	batchDel() {
		const {
			hrmWorkHour
		} = this.props, {
			mainTable,
			setSelectedRowsKeys,
		} = hrmWorkHour, {
			tableStore,
		} = mainTable, {
			selectedRowKeys
		} = tableStore;

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

		if (!keys) {
			message.warning(i18n.message.pleaseConfirmDelRows());
			return;
		}

		setSelectedRowsKeys(keys, 'main');

		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 {
			hrmWorkHour
		} = this.props, {
			deleteTableInfo,
		} = hrmWorkHour;

		deleteTableInfo('main');
	}

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

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

	}
}