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

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

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

		getFormInfo('search');
	}

	componentWillUnmount() {
		const {
			hrmWorkHour
		} = this.props, {
			resetSearchInfo
		} = hrmWorkHour;

		resetSearchInfo();
	}

	getSearchPanelButtons = () => {
		const {
			hrmWorkHour
		} = this.props, {
			searchPanel,
			changeSearchPanelStatus,
		} = hrmWorkHour, {
			form
		} = searchPanel;

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

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

		let arr = [];
		isFormInit && conditions.map(c => {
			c.items.map((field, index) => {
				arr.push(<Col ecId={`${this && this.props && this.props.ecId || ''}_Col@lh15x3@${index}`} span={11} offset={1}>
                              <div style={{marginTop: 20}}>
                                  <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@j3s9ps@${index}`}
                                      label={`${field.label}`}
                                      labelCol={{span: (index % 2 == 0) ? 6 : 8}}
                                      wrapperCol={{span: (index % 2 == 0) ? 17 : 15}}>
                                          {<WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@fbjwpl@${index}`} fieldConfig={field} form={form} formParams={form.getFormParams()} />}
                                  </WeaFormItem>
                              </div>
                  	     </Col>)
			})
		})

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

	handleSearch = () => {
		const {
			hrmWorkHour
		} = this.props, {
			changeSearchPanelStatus,
			getTableInfo,
			searchPanel,
		} = hrmWorkHour, {
			isPanelShow
		} = searchPanel

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

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

		return (
			<WeaTab ecId={`${this && this.props && this.props.ecId || ''}_WeaTab@4g9yic`}
				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)}
			/>
		)

	}
}