import {
	inject,
	observer,
} from 'mobx-react'
import {
	toJS
} from 'mobx'
import {
	WeaTab,
	WeaFormItem,
	WeaRightMenu,
} from 'ecCom'
import {
	WeaSwitch
} from 'comsMobx'
import {
	Row,
	Col,
	Spin,
	Modal,
	Button,
} from 'antd'
import {
	i18n
} from '../../public/i18n';
const confirm = Modal.confirm;

import MainTable from './MainTable';

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

	componentWillUnmount() {
		this.props.hrmContractTemplate.refreshSearch();
	}

	getSearchPanelButtons = () => {
		const {
			hrmContractTemplate
		} = this.props, {
			mainTab,
			changeSearchPanelStatus,
		} = hrmContractTemplate, {
			form
		} = mainTab;

		const btn = [
			(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@hl7i9b`} type="primary" onClick={() => this.handleSearch()}>{i18n.button.search()}</Button>),
			(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@hl4m58`} onClick={() => form.reset()}>{i18n.button.reset()}</Button>),
			(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@q09xbp`} onClick={() => changeSearchPanelStatus(false)}>{i18n.button.cancel()}</Button>),
		];

		return btn;
	}

	handleSearch = () => {
		this.props.hrmContractTemplate.getTableInfo();
	}

	getDropMenuDatas = () => {
		const {
			hrmContractTemplate,
		} = this.props, {
			menu,
			mainTable,
		} = hrmContractTemplate, {
			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 == 'log' || item.menuFun == 'collection' || item.menuFun == 'help' || item.menuFun == 'pageAddress') {
				obj.disabled = true;
			}
			if (item.menuFun == 'batchDelete') {
				obj.disabled = len > 0 ? false : true;
			}
			menus.push(obj);
		})

		return menus;
	}

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

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

	new = () => {
		const {
			hrmContractTemplate
		} = this.props, {
			openDialog
		} = hrmContractTemplate;

		openDialog();
	}

	batchDelete = () => {
		const {
			hrmContractTemplate
		} = this.props, {
			mainTable,
			setSelectedRowKeys,
		} = hrmContractTemplate;

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

		setSelectedRowKeys(keys);

		this.showConfirm();
	}

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

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

	showConfirm = () => {
		let _this = this;
		confirm({
			title: i18n.confirm.defaultTitle(),
			content: i18n.confirm.batchDeleteConfirm(),
			okText: i18n.button.ok(),
			cancelText: i18n.button.cancel(),
			onOk() {
				_this.onOk()
			},
			onCancel() {
				_this.props.hrmContractTemplate.resetSelectedRowKeys();
				return false
			},
		});
	}

	onOk = () => {
		this.props.hrmContractTemplate.deleteTableInfo();
	}

	render() {
		const {
			hrmContractTemplate
		} = this.props, {
			mainTab,
		} = hrmContractTemplate, {
			baseSearchValue,
		} = mainTab;

		return (
			<WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@zf9q5a`} 
				datas={this.getDropMenuDatas()} 
				onClick={ key => this.handleMenuClick(key)}
				collectParams={{favname:i18n.module.contractTemplate(),favouritetype:5}}
			>
		        <WeaTab ecId={`${this && this.props && this.props.ecId || ''}_WeaTab@x7u2q1`}
		            searchType={['base']}
					hasMask={false}
					searchsBaseValue={baseSearchValue}
					onSearch={() => this.handleSearch()}
					onSearchChange={val => setSearchBaseValue(val)}
		         />
		        <MainTable ecId={`${this && this.props && this.props.ecId || ''}_MainTable@35l5fo`} />
	        </WeaRightMenu>
		)

	}
}