import {
	inject,
	observer,
} from 'mobx-react'
import {
	toJS
} from 'mobx'
import {
	Modal,
	message
} from 'antd'
import {
	WeaTableNew
} from 'comsMobx'
import {
	i18n
} from '../../public/i18n';

const WeaTable = WeaTableNew.WeaTable;
const confirm = Modal.confirm;

@inject('hrmTrainArrange')
@observer
export default class MainTableInfo extends React.Component { //复用这个组件需要修改4处：1、store名 2、showConfirm() 3、onOk() 4、onOperatesClick() 。
	constructor(props) {
		super(props);
	}

	componentDidMount() { //这里不需要调用获取右键菜单接口（已经在Header组件中调用过了）。
		const {
			hrmTrainArrange,
		} = this.props, {
			getTableInfo,
		} = hrmTrainArrange;

		getTableInfo('main');
	}

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

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

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

	batchDelete() {
		const {
			hrmTrainArrange
		} = this.props, {
			mainTable,
			setSelectedRowsKeys,
		} = hrmTrainArrange, {
			tableStore,
		} = mainTable, {
			selectedRowKeys
		} = tableStore;

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

		setSelectedRowsKeys(keys, 'main');

		this.showConfirm('batchDel');
	}

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

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

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

	onOk = (oper) => {
		const {
			hrmTrainArrange,
		} = this.props, {
			deleteTableInfo,
			notify
		} = hrmTrainArrange;

		(oper == 'notify') ? notify(): deleteTableInfo('main');
	}

	onOperatesClick = (record, rowIndex, operate) => {
		const {
			hrmTrainArrange,
		} = this.props, {
			setSelectedRowsKeys,
			openDialog,
			getWorkFlowId,
			setFuncIndex,
		} = hrmTrainArrange, {
			randomFieldId
		} = record, {
			index
		} = operate;

		setSelectedRowsKeys(randomFieldId, 'main');

		setFuncIndex(index);

		switch (index) {
			case '0':
				openDialog('main', 'edit')
				break;
			case '1':
				this.showConfirm('del')
				break;
			case '2':
				openDialog('main', 'publicScope')
				break;
			case '3':
				this.showConfirm('notify')
				break;
			case '4':
				getWorkFlowId()
				break;
			case '5':
				window.setLogViewProps({
					logSmallType: '3036',
					targetId: randomFieldId,
				});
				break;
		}

	}

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

		return (
            <WeaTable ecId={`${this && this.props && this.props.ecId || ''}_WeaTable@c06bse`} 
			 	comsWeaTableStore={tableStore}
			 	hasOrder={true}
			 	needScroll={true}
			 	onOperatesClick={(record, rowIndex, operate) => this.onOperatesClick(record, rowIndex, operate)}
			/>
		)

	}
}