import {
	inject,
	observer,
} from 'mobx-react'
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('hrmContractMaintain')
@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.hrmContractMaintain.resetSelectedRowKeys();
				return false
			},
		});
	}

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

	reRenderColumns = (cs) => {
		const {
			hrmContractMaintain
		} = this.props, {
			openDialog,
			setSelectedRowKeys,
			openContractType,
		} = hrmContractMaintain;

		cs.map(c => {
			if (c.dataIndex) {
				if (c.dataIndex == 'contractname') {
					c.render = (t, r) => {
						return <a onClick={() => {setSelectedRowKeys(r.randomFieldId);openDialog()}} >{r.contractnamespan}</a>
					}
				}
				if (c.dataIndex == 'contractman') {
					c.render = (t, r) => {
						return (<div>
							<WeaPopoverHrm ecId={`${this && this.props && this.props.ecId || ''}_WeaPopoverHrm@u7wz5z`} />
							<a href={`javaScript:openhrm(${r.contractman})`} onClick={e => window.pointerXY(e)}>{r.contractmanspan}</a>
						</div>)
					}
				}
				if (c.dataIndex == 'contracttypeid') {
					c.render = (t, r) => {
						return <a onClick={() => openContractType(r.contracttypeid)} >{r.contracttypeidspan}</a>
					}
				}
			}


		});
	}

	onOperatesClick = (record, rowIndex, operate) => {
		const {
			hrmContractMaintain
		} = this.props, {
			setSelectedRowKeys,
			openDialog,
		} = hrmContractMaintain, {
			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: '3043',
					targetId: randomFieldId,
				});
				break;
		}

	}

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

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

	}
}