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

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

	componentDidMount() {
		this.init();
	}

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

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

	reRenderColumns = (columns) => {
		const {
			openDialog,
			setCheckedRowId,
			setSelectedRowId,
		} = this.props.hrmOvertimeRule;

		columns.map(c => {
			//加班规则名称
			if (c.dataIndex === 'name') {
				c.render = (text, record, index) => {
					const {
						namespan,
						randomFieldId
					} = record;

					return <a onClick={() => {setSelectedRowId(randomFieldId);openDialog({
						title: getLabel('505605', "编辑加班规则"),
						type: 1
					})} }>{namespan}</a>
				}
			}
			//是否默认
			if (c.dataIndex === 'isDefault') {
				c.render = (text, record, index) => {
					const {
						isDefault,
						randomFieldId
					} = record;

					//当前行是否选中
					const isChecked = (isDefault === '1') ? true : false;
					//设置默认选中行的id
					(isDefault === '1') && setCheckedRowId(randomFieldId);

					return <input type='radio' name='overtimeRule' defaultChecked={isChecked} onChange={this.handleRadioChange.bind(this,randomFieldId)}/>
				}
			}
		});
	}

	handleRadioChange = (id) => {
		this.props.hrmOvertimeRule.setCheckedRowId(id);
	}

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

		const {
			setSelectedRowId,
			openDialog,
			delOverTimeRule,
			log,
		} = this.props.hrmOvertimeRule;

		//设置选中行的id
		setSelectedRowId(randomFieldId);

		if (index === '0') {
			openDialog({
				title: getLabel('505605', "编辑加班规则"),
				type: 1
			});
		};
		if (index === '1') {
			confirmation.showDelConfirm({
				type: 0,
				logic: (type) => delOverTimeRule(type)
			});
		}
		if (index === '2') {
			log(randomFieldId, 'table');
		}
	}

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

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

export default Table