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

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

	getDropMenuDatas = () => {
		const {
			hrmTrainActivity,
		} = this.props, {
			mainDialogMenu,
			subTable,
		} = hrmTrainActivity, {
			rightMenu
		} = mainDialogMenu;

		const len = subTable.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 == 'log' || 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) => {
		this[key] && this[key]();
	}

	new = () => {
		const {
			hrmTrainActivity
		} = this.props, {
			openSubDialog
		} = hrmTrainActivity;

		openSubDialog();
	}

	batchDelete = () => {
		this.showConfirm();
	}

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

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

	showConfirm = () => {
		const {
			hrmTrainActivity
		} = this.props, {
			refreshTable
		} = hrmTrainActivity;

		let _this = this;
		confirm({
			title: i18n.confirm.defaultTitle(),
			content: i18n.confirm.delete(),
			okText: i18n.button.ok(),
			cancelText: i18n.button.cancel(),
			onOk() {
				_this.onOk()
			},
			onCancel() {
				refreshTable('sub');
				return false
			},
		});
	}

	onOk = () => {
		const {
			hrmTrainActivity
		} = this.props, {
			deleteTableInfo
		} = hrmTrainActivity;

		deleteTableInfo();
	}

	onOperatesClick = (record, rowIndex, operate) => {
		const {
			hrmTrainActivity
		} = this.props, {
			setSelectRowKey,
			openSubDialog,
		} = hrmTrainActivity, {
			randomFieldId
		} = record, {
			index
		} = operate;

		setSelectRowKey(randomFieldId, 'sub');

		switch (index) {
			case '0':
				openSubDialog();
				break;
			case '1':
				this.showConfirm();
				break;
		}

	}

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

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

	}
}