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

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

	componentDidMount() {
		this.props.hrmRpManage.getTableInfo();
	}

	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.hrmRpManage.resetSelectedRowKeys();
				return false
			},
		});
	}

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

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

		cs.map(c => {
			if (c.dataIndex) {
				if (c.dataIndex == 'rptitle') {
					c.render = (t, r) => {
						return <a onClick={() => openDialog(r.randomFieldId)} >{r.rptitle}</a>
					}
				}
				if (c.dataIndex == 'resourseid') {
					c.render = (t, r) => {
						return <a href={`javaScript:openhrm(${r.resourseid})`} onClick={e => window.pointerXY(e)} >{r.resourseidspan}</a>
					}
				}
			}


		});
	}

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

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

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

	}

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

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

	}
}