import {
	observer
} from 'mobx-react';
import {
	WeaTableNew
} from 'comsMobx';
const WeaTable = WeaTableNew.WeaTable;

@observer
export default class Table extends React.Component {
	constructor(props) {
		super(props);
	}

	componentDidMount() {
		this.props.store.getTable();
	}

	reRenderColumns = (columns, store) => {
		const {
			onTableTdClick,
			orgTree,
		} = store, {
			type
		} = orgTree;

		columns.map(c => {
			let bool;
			if (type === 'all') {
				bool = ['jobgroupremark', 'result'].includes(c.dataIndex) ? true : false;
			}
			if (type === 'JobGroup') {
				bool = ['jobactivitymark'].includes(c.dataIndex) ? true : false;
			}
			if (type === 'JobActivity') {
				bool = ['jobtitlemark'].includes(c.dataIndex) ? true : false;
			}
			if (bool) {
				c.render = function(text, record) {
					const {
						randomFieldId,
						jobtitlemarkspan
					} = record;

					if (type === 'JobActivity') {
						return <span onClick={() => onTableTdClick(randomFieldId)} dangerouslySetInnerHTML={{__html:jobtitlemarkspan}}></span>
					}else{
						return <a onClick={() => onTableTdClick(randomFieldId)}>{text}</a>
					}

				}
			}
		});
	}

	onOperatesClick = (record, rIndex, operate, store) => {
		const {
			randomFieldId
		} = record, {
			index
		} = operate;

		const {
			orgTree,
			edit,
			del,
			doUndo,
			showConfirm,
		} = store, {
			type
		} = orgTree;

		if (index === '0') {
			edit(randomFieldId);
		} else if (index === '1') {
			del(randomFieldId);
		} else if(index == "3" ){
			showConfirm().then(() => {
				doUndo(randomFieldId,index);
			})
		} else if( index == "4"){
			doUndo(randomFieldId,index);
		} else {
			window.setLogViewProps({
				logSmallType: (type == 'all') ? '3012' : (type == 'JobGroup') ? '3013' : '3014',
				targetId: randomFieldId,
			});
		}

	}

	render() {
		const {
			store
		} = this.props, {
			tableStore,
			onTableTdClick,
		} = store;

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