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

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

	showConfirm = () => {
		const {
			hrmEmployDemand
		} = this.props, {
			resetSelectedRowKeys
		} = hrmEmployDemand;

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

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

		deleteTableInfo();
	}

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

		setSelectedRowKeys(randomFieldId);

		switch (index) {
			case '0':
				openDialog()
				break;
			case '1':
				this.showConfirm();
				break;
			case '2':
				window.setLogViewProps({
					logSmallType: '3044',
					targetId: randomFieldId,
				});
				break;

		}

	}
	reRenderColumns = (columns) => {
		const {
			hrmEmployDemand
		} = this.props, {
			openDialog,
			setSelectedRowKeys
		} = hrmEmployDemand;

		columns.map(c => {
			if (c.dataIndex == 'jobtitlename') { //TODO
				c.render = (t, r) => {
					return <a onClick={() => {setSelectedRowKeys(r.randomFieldId);openDialog()}}>{r.jobtitlenamespan}</a>
				}
			}
		})
	}

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

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

	}
}