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

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

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

	componentWillUnmount() {
		this.props.hrmEmployDemand.refreshFormInfo();
	}

	getForm = () => {
		const {
			hrmEmployDemand
		} = this.props, {
			formInfo,
		} = hrmEmployDemand, {
			form,
			conditions,
		} = formInfo, {
			isFormInit,
		} = form;

		let arr = [];
		isFormInit && conditions.map(c => {
			c.items.map((field, index) => {
				arr.push(<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@785zbb@${index}`}
                            label={`${field.label}`}
                            labelCol={{span: 6, offset: 2}}
                            wrapperCol={{span: 14}}
                            error={form.getError(field)}
                            tipPosition="bottom"
                        >
                                <WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@k1iamo@${index}`} fieldConfig={field} form={form} formParams={form.getFormParams()} onChange={cb => this.handleChange(cb)}/>
                        </WeaFormItem>)
			})
		})
		return <div className='wea-form-item-group'>{arr}</div>
	}

	handleChange = (cb) => {}


	getDropMenuDatas = () => {
		const {
			hrmEmployDemand
		} = this.props, {
			formInfo
		} = hrmEmployDemand, {
			loading
		} = formInfo;

		const menu = [{
			key: '0',
			icon: <i className='icon-coms-Preservation' />,
			content: i18n.button.save(),
			disabled: loading,
		}];

		return menu
	}

	handleMenuClick = (key) => {
		const {
			hrmEmployDemand,
		} = this.props, {
			save,
		} = hrmEmployDemand;

		key == '0' && save();
	}

	render() {
		const {
			hrmEmployDemand,
		} = this.props, {
			formInfo,
		} = hrmEmployDemand, {
			date,
			loading,
		} = formInfo;

		if (loading) {
			return (
				<div className='hrm-loading-center-small'>
					<Spin ecId={`${this && this.props && this.props.ecId || ''}_Spin@m8fs92`} spinning={loading}></Spin>
				</div>
			)
		} else {
			return (
				<WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@hf6xdc`} datas={this.getDropMenuDatas()} onClick={ key => this.handleMenuClick(key)}>
				    <div className='hrm-dialog-form hrm-form-date'>
				        {this.getForm()}
				    </div>
                </WeaRightMenu>
			)
		}

	}
}