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

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

		getFormInfo('search');
	}

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

		resetSearchInfo();
	}

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

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

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

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

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

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

	handleSearchChange = (val) => {
		const {
			hrmTrainArrange
		} = this.props, {
			searchPanel,
		} = hrmTrainArrange, {
			form
		} = searchPanel;

		form.updateFields({
			trainPlanName: { //复用时，需要修改参数名。
				value: val
			}
		});
	}

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

		return (
			<WeaTab ecId={`${this && this.props && this.props.ecId || ''}_WeaTab@pwzicg`}
				searchType={['base','advanced']}
				showSearchAd={isPanelShow}
				hasMask={false}
				advanceHeight={Math.ceil(conditionCount / 2)*52+20}
				buttonsAd={this.getSearchPanelButtons()}
				searchsBaseValue={form.getFormParams().trainPlanName}
				setShowSearchAd={  b => changeSearchPanelStatus(b)}
				hideSearchAd={ () => changeSearchPanelStatus(false)}
				searchsAd= {isPanelShow ? this.getPanelComponents():<div></div>}
				onSearch={() => this.handleSearch()}
				onSearchChange={val => this.handleSearchChange(val)}
			/>
		)

	}
}