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

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

	componentDidMount() {
		const {
			hrmSalaryItemsSet,
		} = this.props, {
			getTableInfo,
		} = hrmSalaryItemsSet;

		getTableInfo();
	}

	getDropMenuDatas = () => {
		const {
			hrmSalaryItemsSet,
		} = this.props, {
			menu,
			mainTable,
		} = hrmSalaryItemsSet, {
			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 {
			hrmSalaryItemsSet,
		} = this.props, {
			searchPanel,
			changeSearchPanelStatus,
		} = hrmSalaryItemsSet, {
			isPanelShow
		} = searchPanel;

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

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

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

		openDialog();
	}

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

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

	batchDelete() {
		this.showConfirm('batchDel');
	}

	showConfirm = (type) => {
		const {
			hrmSalaryItemsSet
		} = this.props, {
			resetTableSelectedKey
		} = hrmSalaryItemsSet;

		let _this = this;
		confirm({
			title: i18n.confirm.defaultTitle(),
			content: type == 'del' ? i18n.confirm.delete() : (type == 'batchDel') ? i18n.confirm.batchDeleteConfirm() : i18n.confirm.batchHandleTip(),
			okText: i18n.button.ok(),
			cancelText: i18n.button.cancel(),
			onOk() {
				_this.onOk(type)
			},
			onCancel() {
				resetTableSelectedKey();
				return false
			},
		});
	}

	onOk = (type) => {
		const {
			hrmSalaryItemsSet,
		} = this.props, {
			deleteTableInfo,
			batchHandle,
		} = hrmSalaryItemsSet;

		type == 'batchHandle' ? batchHandle() : deleteTableInfo();
	}

	onOperatesClick = (record, rowIndex, operate) => {
		const {
			hrmSalaryItemsSet,
		} = this.props, {
			setSelectedRowInfo,
			openDialog,
		} = hrmSalaryItemsSet, {
			randomFieldId,
			subcompanyid,
		} = record, {
			index
		} = operate;

		setSelectedRowInfo(randomFieldId, subcompanyid);

		switch (index) {
			case '0':
				openDialog()
				break;
			case '1':
				this.showConfirm('del')
				break;
			case '2':
				this.showConfirm('batchHandle');
				break;
		}
	}

	render() {
		const {
			hrmSalaryItemsSet,
		} = this.props, {
			mainTable,
		} = hrmSalaryItemsSet, {
			tableStore
		} = mainTable;

		return (
			<WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@9vuzm7`} 
				datas={this.getDropMenuDatas()} 
				onClick={ key => this.handleMenuClick(key)}
				collectParams={{favname: i18n.module.wageSetting(),favouritetype:5}}
			>
	            <WeaTable ecId={`${this && this.props && this.props.ecId || ''}_WeaTable@3mw32e`} 
				 	comsWeaTableStore={tableStore}
				 	hasOrder={true}
				 	needScroll={true}
				 	onOperatesClick={(record, rowIndex, operate) => this.onOperatesClick(record, rowIndex, operate)}
				/>
	        </WeaRightMenu>
		)

	}
}