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

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

	showConfirm = () => {
		let _this = this;
		confirm({
			title: i18n.confirm.defaultTitle(),
			content: i18n.confirm.delete(),
			okText: i18n.button.ok(),
			cancelText: i18n.button.cancel(),
			onOk() {
				_this.onOk()
			},
			onCancel() {
				_this.props.hrmContractTemplate.resetSelectedRowKeys();
				return false
			},
		});
	}

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

	reRenderColumns = (cs) => {
		const {
			hrmContractTemplate
		} = this.props, {
			openDialog
		} = hrmContractTemplate;

		cs.map(c => {
			if (c.dataIndex) {
				if (c.dataIndex == 'typename') {
					c.render = (t, r) => {
						return <a onClick={() => openDialog(r.randomFieldId)} >{r.typename}</a>
					}
				}
			}


		});
	}

	onOperatesClick = (record, rowIndex, operate) => {
		const {
			hrmContractTemplate
		} = this.props, {
			setSelectedRowKeys,
			openDialog,
		} = hrmContractTemplate, {
			randomFieldId
		} = record, {
			index
		} = operate;

		if (index == '0' || index == '1') setSelectedRowKeys(randomFieldId);

		switch (index) {
			case '0':
				openDialog();
				break;
			case '1':
				this.showConfirm();
				break;
			case '2':
				message.info(i18n.message.implementation());
				break;
		}

	}

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

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

	}
}