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

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

	getForm = () => {
		const {
			hrmTrainActivity
		} = this.props, {
			formInfo,
		} = hrmTrainActivity, {
			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@osjgao@${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@o7rjnp@${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) => {
		const {
			hrmTrainActivity
		} = this.props, {
			getTrainArrangeContent
		} = hrmTrainActivity;

		if (Object.keys(cb)[0] == 'trainPlanId') {
			getTrainArrangeContent(cb.trainPlanId.value)
		}
	}


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

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

		return menu
	}

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

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

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

		return (<div style={{height: '100%'}}>
					{loading ? 	<div className='hrm-loading-center-small'>
									<Spin ecId={`${this && this.props && this.props.ecId || ''}_Spin@nwlvil`} spinning={loading}></Spin>
							   	</div>
							 : 	<WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@lhgd3p`} datas={this.getDropMenuDatas()} onClick={ key => this.handleMenuClick(key)}>
				                	<div className='hrm-dialog-form hrm-train-activity-date'>
				                	    {this.getForm()}
				                	</div>
                				</WeaRightMenu>
					}
				</div>)
	}
}