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);
	}

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

		columns.map(c => {
			if (c.dataIndex === 'ruleName') {
				c.render = function(text, record) {
					const {
						randomFieldId
					} = record;

					return <a onClick={() => onTableTdClick(randomFieldId)}>{text}</a>
				}
			}
		});
	}

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

		const {
			onTableTdClick,
			del,
			log,
		} = store;

		if (index === '0') {
			onTableTdClick(randomFieldId);
		}
		if (index === '1') {
			del(randomFieldId);
		}
		if (index === '2') {
			log(randomFieldId, 'table')
		}
	}

	render() {
		const {
			store
		} = this.props, {
			TABLE,
			table
		} = store;

		return (
			<WeaTable ecId={`${this && this.props && this.props.ecId || ''}_WeaTable@nzhzbh`} 
				{...TABLE}
				{...table}
				getColumns={columns => this.reRenderColumns(columns,store)}
				onOperatesClick={(record, index, operate) => this.onOperatesClick(record,index, operate,store)}
			/>
		);
	}
}