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

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

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

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

					return <a onClick={() => onTableTdClick(randomFieldId)}>{text}</a>
				}
			}
			if (c.dataIndex === 'isEnable') {
				c.render = function(text, record) {
					const {
						randomFieldId
					} = record;

					(text === '1') && collectChecked(randomFieldId)

					return <WeaCheckbox ecId={`${this && this.props && this.props.ecId || ''}_WeaCheckbox@75w5pn@${c.dataIndex}`} value={text} onChange={val => onCheckBoxChange(val,randomFieldId) }/>
				}
			}
		});
	}

	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@bsi0ui`} 
				{...TABLE}
				{...table}
				getColumns={columns => this.reRenderColumns(columns,store)}
				onOperatesClick={(record, index, operate) => this.onOperatesClick(record,index, operate,store)}
			/>
		);
	}
}