import {
	inject,
	observer,
} from 'mobx-react'
import {
	toJS
} from 'mobx'
import {
	WeaRightMenu,
} from 'ecCom'
import {
	Modal,
	message
} from 'antd'
import {
	WeaTableNew
} from 'comsMobx'
import {
	i18n
} from '../../public/i18n';
const WeaTable = WeaTableNew.WeaTable;
const confirm = Modal.confirm;

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

	componentDidMount() {
		const {
			hrmWorkHour,
		} = this.props, {
			getTableInfo,
		} = hrmWorkHour;

		getTableInfo();
	}

	getDropMenuDatas = () => {
		const {
			hrmWorkHour,
		} = this.props, {
			menu,
			mainTable,
		} = hrmWorkHour, {
			rightMenu
		} = menu;

		const len = mainTable.tableStore.selectedRowKeys.length;

		let menus = [];
		toJS(rightMenu).map((item, index) => {
			let obj = {
				key: item.menuFun,
				icon: <i className={`${item.menuIcon}`}/>,
				content: item.menuName,
			}
			if (item.menuFun == 'collection') {
				obj.disabled = true;
			}
			if (item.menuFun == 'batchDel') {
				obj.disabled = len > 0 ? false : true;
			}
			menus.push(obj);
		})

		return menus;
	}

	handleMenuClick = (key) => {
		const {
			hrmWorkHour,
		} = this.props, {
			searchPanel,
			changeSearchPanelStatus,
		} = hrmWorkHour, {
			isPanelShow
		} = searchPanel;

		isPanelShow && changeSearchPanelStatus(false);
		this[key] && this[key]();
	}

	addnew = () => {
		const {
			hrmWorkHour
		} = this.props, {
			openDialog,
		} = hrmWorkHour;

		openDialog('main', 'new');
	}

	log = () => {
		window.setLogViewProps({
			logSmallType: '20038',
		})
	}

	batchDel() {
		const {
			hrmWorkHour
		} = this.props, {
			mainTable,
			setSelectedRowsKeys,
		} = hrmWorkHour, {
			tableStore,
		} = mainTable, {
			selectedRowKeys
		} = tableStore;

		const keys = toJS(selectedRowKeys).toString();

		setSelectedRowsKeys(keys, 'main');

		this.showConfirm('batchDel');
	}

	custom = () => {
		const {
			hrmWorkHour
		} = this.props, {
			mainTable,
		} = hrmWorkHour, {
			tableStore
		} = mainTable;

		tableStore.setColSetVisible(true);
		tableStore.tableColSet(true);
	}

	showConfirm = (type) => {
		let _this = this;
		confirm({
			title: i18n.confirm.defaultTitle(),
			content: type == 'singleDel' ? i18n.confirm.delete() : i18n.confirm.batchDeleteConfirm(),
			okText: i18n.button.ok(),
			cancelText: i18n.button.cancel(),
			onOk() {
				_this.onOk()
			},
			onCancel() {
				return false
			},
		});
	}

	onOk = () => {
		const {
			hrmWorkHour,
		} = this.props, {
			deleteTableInfo,
		} = hrmWorkHour;

		deleteTableInfo();
	}

	onOperatesClick = (record, rowIndex, operate) => {
		const {
			hrmWorkHour,
		} = this.props, {
			setSelectedRowsKeys,
			setCurScope,
			openDialog,
			openLogDialog,
			clickTab,
		} = hrmWorkHour, {
			randomFieldId,
			scheduleType
		} = record, {
			index
		} = operate;

		setSelectedRowsKeys(randomFieldId, 'main');

		setCurScope(scheduleType);

		switch (index) {
			case '0':
				openDialog('main', 'edit')
				break;
			case '3':
				openDialog('main', 'edit');
				clickTab('1');
				break;
			case '1':
				this.showConfirm('singleDel')
				break;
			case '2':
				window.setLogViewProps({
					logSmallType: '20038',
					targetId: randomFieldId
				})
				break;
		}

	}

	render() {
		const {
			hrmWorkHour,
		} = this.props, {
			mainTable,
		} = hrmWorkHour, {
			tableStore
		} = mainTable;

		return (
			<WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@1d00a7`} 
				datas={this.getDropMenuDatas()} 
				onClick={ key => this.handleMenuClick(key)}
				collectParams={{favname:i18n.module.generalWorkingHours(),favouritetype:5}}
			>
	            <WeaTable ecId={`${this && this.props && this.props.ecId || ''}_WeaTable@z728lr`} 
				 	comsWeaTableStore={tableStore}
				 	hasOrder={true}
				 	needScroll={true}
				 	onOperatesClick={(record, rowIndex, operate) => this.onOperatesClick(record, rowIndex, operate)}
				/>
	        </WeaRightMenu>
		)

	}
}