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

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

	getTab = () => {
		const tabs = [{
			key: '0',
			title: i18n.label.curAss(),
		}, {
			key: '1',
			title: i18n.label.completedAss(),
		}]

		return tabs
	}

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

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

		return btn;
	}

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

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

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

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

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

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

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

		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;
			}
			menus.push(obj);
		})

		return menus;
	}

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

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

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

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

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

		return (
			<WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@1386q4`} 
				datas={this.getDropMenuDatas()} 
				onClick={ key => this.handleMenuClick(key)}
				collectParams={{favname: i18n.module.assImple(),favouritetype:5}}
			>
            	<WeaTab ecId={`${this && this.props && this.props.ecId || ''}_WeaTab@jb671v`}
            		datas={this.getTab()}
            		keyParam='key'
            		selectedKey={curKey}
            		onChange={key => clickTab(key,'main')}
            		searchType={['base','advanced']}
					showSearchAd={isPanelShow}
					hasMask={false}
					advanceHeight={Math.ceil(conditionCount / 2)*52+20}
					buttonsAd={this.getSearchPanelButtons()}
					searchsBaseValue={form.getFormParams().checkInfoName}
					setShowSearchAd={  b => changeSearchPanelStatus(b)}
					hideSearchAd={ () => changeSearchPanelStatus(false)}
					searchsAd= {isPanelShow ? this.getPanelComponents():<div></div>}
					onSearch={() => this.handleSearch()}
					onSearchChange={val => this.handleSearchChange(val)}
         		/>
         		<MainTable ecId={`${this && this.props && this.props.ecId || ''}_MainTable@jh3zmx`} />
         	</WeaRightMenu>
		)

	}
}