import {
	observer,
	inject
} from 'mobx-react';
import {
	WeaCheckbox,
	WeaLocaleProvider,
} from 'ecCom';
import {
	WeaTableNew
} from 'comsMobx';
import {
	confirmation,
} from '../../util/save-time-util.js';
const WeaTable = WeaTableNew.WeaTable;
const getLabel = WeaLocaleProvider.getLabel;

@inject('commonStore')
@inject('hrmHolidayRule')
@observer
class Table extends React.Component {
	constructor(props) {
		super(props);
		this.editParams = {
			title: getLabel('388998', "编辑假期"),
			type: 1,
		}
	}

	componentDidMount() {
		this.init();
	}

	init = () => {
		this.props.commonStore.getSearchList();
	}

	componentWillUnmount() {
		this.props.commonStore.resetTable();
	}

	reRenderColumns = (columns) => {
		const {
			handleCkChange,
			collectCheckedId,
		} = this.props.hrmHolidayRule;

		columns.map(c => {
			if (c.dataIndex === 'leaveName') {
				c.render = (t, r) => {
					const {
						leaveNamespan,
						randomFieldId
					} = r;

					return <a onClick={() => ['setSelectedRowId','setSelectedRowIndex' ,'openDialog'].map( (f,i) => {
							let params;
							if (i === 0) {
								params = randomFieldId;
							} else if(i === 1){
								params = '0';
							}else{
								params = this.editParams;
							};
							this.props.commonStore[f](params)
					}) }>{leaveNamespan}</a>
				}
			}
			if (c.dataIndex === 'isEnable') {
				c.render = (t, r) => {
					const {
						isEnablespan,
						randomFieldId
					} = r;

					if (isEnablespan === '1') collectCheckedId(randomFieldId)

					return <WeaCheckbox ecId={`${this && this.props && this.props.ecId || ''}_WeaCheckbox@5p582y@${c.dataIndex}`} value={isEnablespan} onChange={val => handleCkChange(val,randomFieldId)}/>
				}
			}
		});
	}

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

		const {
			setSelectedRowId,
			setSelectedRowIndex,
			openDialog,
			delTableRows,
		} = this.props.commonStore;

		//设置选中行的id
		setSelectedRowId(randomFieldId);
		//设置选中行的索引值
		setSelectedRowIndex(index);
		//根据索引值，执行相应的函数
		switch (index) {
			case '0': //编辑
				openDialog(this.editParams);
				break;
			case '1': //删除
				confirmation.showDelConfirm({
					type: 0, //单个删除
					logic: (type) => delTableRows(type),
				});
				break;
			case '3': //同步
				openDialog({
					title: getLabel('21952', "同步设置"),
					type: 0, //同步单个假期
				});
				break;

		}
	}

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

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

export default Table