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

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

	reRenderColumns = (columns) => {
		const dataIndexs = ['baseAmount', 'baseAmount2', 'usedAmount', 'usedAmount2', 'extraAmount', 'extraAmount2'];
		columns.map(c => {
			if (dataIndexs.includes(c.dataIndex)) {
				c.render = (text, record, index) => this.renderCom({
					record,
					index, //行索引
					colIndex: dataIndexs.findIndex(dataIndex => dataIndex === c.dataIndex)
				});
			}
		});
		return columns;
	}


	renderCom = (params) => {
		const {
			record,
			index,
			colIndex
		} = params, {
			baseAmountspan,
			baseAmount2span,
			usedAmountspan,
			usedAmount2span,
			extraAmountspan,
			extraAmount2span,
			randomFieldId,
		} = record;

		//基础假期、已修假期和额外假期数据集合
		//数据集合为DATAINDEXS的映射
		const values = [baseAmountspan, baseAmount2span, usedAmountspan, usedAmount2span, extraAmountspan, extraAmount2span];

		return <WeaInputNumber ecId={`${this && this.props && this.props.ecId || ''}_WeaInputNumber@oosivb`} 
					defaultValue={values[colIndex]} 
					min={0} 
					precision={2} 
					onChange={value => this.handleInputNumberChange({
						index,//行的索引值
						values: this.getValues(colIndex,values,value),
						id:randomFieldId,
					})}
				/>
	}
	//修改后，行可编辑列表域的值
	getValues = (colIndex, values, value) => {
		const clone = [...values];

		clone[colIndex] = value;

		return clone;
	}

	handleInputNumberChange = (params) => {
		const {
			values,
			id,
			index
		} = params, {
			hrmVacationBalance
		} = this.props;

		this.changeTableDatas(index, values);

		hrmVacationBalance.sortChangedTableDatasOut(values, id)
	}

	//修改tableStore中datas的值
	changeTableDatas = (index, values) => {
		const {
			hrmVacationBalance
		} = this.props, {
			table
		} = hrmVacationBalance;


		['baseAmountspan', 'baseAmount2span', 'usedAmountspan', 'usedAmount2span', 'extraAmountspan', 'extraAmount2span'].map((v, i) => {
			if (values[i] !== undefined) table.tableStore.datas[index][v] = values[i]
		});
	}


	render() {
		const {
			hrmVacationBalance,
		} = this.props, {
			table
		} = hrmVacationBalance, {
			tableStore,
		} = table;

		return (
			<WeaTable ecId={`${this && this.props && this.props.ecId || ''}_WeaTable@ogu1k4`} 
				comsWeaTableStore={tableStore}
				hasOrder
				needScroll
				getColumns={columns => this.reRenderColumns(columns)}
			/>
		)
	}
}