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

import MainTable from './MainTable';

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

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

		clickTab('1');
		getFormInfo();
	}


	getTab = () => {
		const tabs = [{
			key: '0',
			title: i18n.label.all(),
		}, {
			key: '1',
			title: i18n.label.untreated(),
		}, {
			key: '2',
			title: i18n.label.recruiting(),
		}, {
			key: '3',
			title: getLabel('391173', "已满足"),
		}, {
			key: '4',
			title: getLabel('391174', "无用"),
		}]

		return tabs
	}

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

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

		return btn;
	}

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

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

	getPanelComponents = () => {
		const {
			hrmEmployDemand
		} = this.props, {
			mainTab,
		} = hrmEmployDemand, {
			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@bszp82@${index}`} span={10} offset={1}>
                              <div style={{marginTop: 20}}>
                                  <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@ne77pl@${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@n4sjkw@${index}`} fieldConfig={field} form={form} formParams={form.getFormParams()} />}
                                  </WeaFormItem>
                              </div>
                  	     </Col>)
			})
		})

		if (loading) {
			return (
				<div className='hrm-loading-center-small' style={{top:'25%'}}>
                    <Spin ecId={`${this && this.props && this.props.ecId || ''}_Spin@aoznev`} spinning={loading}></Spin>
                </div>
			)
		} else {
			return <Row ecId={`${this && this.props && this.props.ecId || ''}_Row@gdpfih`} onKeyDown={(e) =>( e.keyCode == 13 && e.target.tagName === "INPUT")  && this.handleSearch()}>{arr}</Row>
		}

	}

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

		return (
			<div style={{height: '100%'}}>
            	<WeaTab ecId={`${this && this.props && this.props.ecId || ''}_WeaTab@ihdcjc`}
            		datas={this.getTab()}
            		keyParam='key'
            		selectedKey={curKey}
            		onChange={key => clickTab(key)}
            		searchType={['base','advanced']}
					showSearchAd={isPanelShow}
					hasMask={false}
					advanceHeight={Math.ceil(conditionCount / 2)*52+20}
					buttonsAd={this.getSearchPanelButtons()}
					searchsBaseValue={qname}
					setShowSearchAd={  b => changeSearchPanelStatus(b)}
					hideSearchAd={ () => changeSearchPanelStatus(false)}
					searchsAd= {isPanelShow ? this.getPanelComponents():<div></div>}
					onSearch={() => this.handleSearch()}
					onSearchChange={val => setBaseSearchValue(val)}
         		/>
         		<MainTable ecId={`${this && this.props && this.props.ecId || ''}_MainTable@dxsu7n`} />
         	</div>
		)

	}
}