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('hrmRpManage')
@observer
export default class Tab extends React.Component {
	constructor(props) {
		super(props);
	}

	componentDidMount() {
		this.props.hrmRpManage.getFormInfo();
	}

	componentWillUnmount() {
		this.props.hrmRpManage.resetSearch();
	}


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

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

		return btn;
	}

	handleSearch = () => {
		const {
			hrmRpManage
		} = this.props, {
			changeSearchPanelStatus,
			getTableInfo,
			mainTab,
		} = hrmRpManage, {
			isPanelShow
		} = mainTab

		getTableInfo();
		isPanelShow && changeSearchPanelStatus(false);
	}

	getPanelComponents = () => {
		const {
			hrmRpManage
		} = this.props, {
			mainTab,
		} = hrmRpManage, {
			conditions,
			form,
			loading
		} = mainTab, {
			isFormInit
		} = form;

		let arr = [];
		isFormInit && conditions.map(c => {
			c.items.map((field, index) => {
				arr.push(<Col ecId={`${this && this.props && this.props.ecId || ''}_Col@q6joo3@${index}`} span={10} offset={1}>
                              <div style={{marginTop: 20}}>
                                  <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@jpuadn@${index}`}
                                      label={`${field.label}`}
                                      labelCol={{span: 6}}
                                      wrapperCol={{span: 18}}>
                                          {<WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@qf93dt@${index}`} fieldConfig={field} form={form} formParams={form.getFormParams()} />}
                                  </WeaFormItem>
                              </div>
                  	     </Col>)
			})
		})

		const com = (<Spin ecId={`${this && this.props && this.props.ecId || ''}_Spin@b4iocd`} spinning={loading}>
						<Row ecId={`${this && this.props && this.props.ecId || ''}_Row@drkpig`}
							onKeyDown={(e) =>( e.keyCode == 13 && e.target.tagName === "INPUT")  && this.handleSearch()}
						>{arr}</Row>
					</Spin>)
		return com
	}

	getDropMenuDatas = () => {
		const {
			hrmRpManage,
		} = this.props, {
			menu,
			mainTable,
		} = hrmRpManage, {
			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' || 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 {
			hrmRpManage,
		} = this.props, {
			mainTab,
			changeSearchPanelStatus,
		} = hrmRpManage, {
			isPanelShow
		} = mainTab;

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

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

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

		openDialog();
	}

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

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

		setSelectedRowKeys(keys);

		this.showConfirm();
	}

	custom = () => {
		const {
			hrmRpManage
		} = this.props, {
			mainTable,
		} = hrmRpManage, {
			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.hrmRpManage.resetSelectedRowKeys();
				return false
			},
		});
	}

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

	render() {
		const {
			hrmRpManage
		} = this.props, {
			mainTab,
			changeSearchPanelStatus,
			setBaseSearchValue
		} = hrmRpManage, {
			isPanelShow,
			conditionCount,
			baseSearchValue,
			form,
		} = mainTab;

		return (
			<WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@0wqga4`} 
				datas={this.getDropMenuDatas()} 
				onClick={ key => this.handleMenuClick(key)}
				collectParams={{favname: i18n.module.rpManage(),favouritetype:5}}
			>
		        <WeaTab ecId={`${this && this.props && this.props.ecId || ''}_WeaTab@83du75`}
		            searchType={['base','advanced']}
					showSearchAd={isPanelShow}
					hasMask={false}
					advanceHeight={Math.ceil(conditionCount / 2)*52+20}
					buttonsAd={this.getSearchPanelButtons()}
					searchsBaseValue={baseSearchValue}
					setShowSearchAd={  b => changeSearchPanelStatus(b)}
					hideSearchAd={ () => changeSearchPanelStatus(false)}
					searchsAd= {isPanelShow ? this.getPanelComponents():<div></div>}
					onSearch={() => this.handleSearch()}
					onSearchChange={val => setBaseSearchValue(val)}
		        />
		        {form.isFormInit && <MainTable ecId={`${this && this.props && this.props.ecId || ''}_MainTable@3zi972`} />}
		    </WeaRightMenu>
		)

	}
}