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

import MainTable from './MainTable';

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

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

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


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

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

		return btn;
	}

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

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

	getPanelComponents = () => {
		const {
			hrmAssType
		} = this.props, {
			mainTab,
		} = hrmAssType, {
			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@lrxi8u@${index}`} span={10} offset={1}>
                              <div style={{marginTop: 20}}>
                                  <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@9qxsw1@${index}`}
                                      label={`${field.label}`}
                                      labelCol={{span: 6}}
                                      wrapperCol={{span: 18}}>
                                          {<WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@nsrh1p@${index}`} fieldConfig={field} form={form} formParams={form.getFormParams()} />}
                                  </WeaFormItem>
                              </div>
                  	     </Col>)
			})
		})

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

	handleSearchChange = (val) => {
		const {
			hrmAssType
		} = this.props, {
			mainTab,
		} = hrmAssType, {
			form
		} = mainTab;

		form.updateFields({
			checkTypeName: {
				value: val
			}
		});
	}

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

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

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

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

		openDialog();
	}

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

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

		setSelectedRowKeys(keys);

		this.showConfirm();
	}

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

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

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

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

	}
}