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

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

		getFormInfo('main');
	}

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

		refreshFormInfo();
	}

	getForm = () => {
		const {
			hrmTrainArrange
		} = this.props, {
			formInfo,
		} = hrmTrainArrange, {
			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@hymj7l@${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@pmhwvy@${index}`} fieldConfig={field} form={form} formParams={form.getFormParams()} onChange={cb => this.handleChange(cb)}/>
                        </WeaFormItem>)
			})
		})
		return <div className='wea-form-item-group hrm-train-arrange-form'>{arr}</div>
	}

	handleChange = (cb) => {
		const {
			hrmTrainArrange
		} = this.props, {
			getTrainPlanContent,
			addLimitParam,
		} = hrmTrainArrange;

		const key = Object.keys(cb)[0];

		if (key == 'trainLayoutId') {
			getTrainPlanContent(cb.trainLayoutId.value)
		}

		if (key == 'planStartDate') {
			addLimitParam(cb.planStartDate.value)
		}
	}

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

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

		return menu
	}

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

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

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

		return (
			<Spin ecId={`${this && this.props && this.props.ecId || ''}_Spin@zv0ukg`} spinning={loading}>
            	<WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@8hlm72`} datas={this.getDropMenuDatas()} onClick={ key => this.handleMenuClick(key)}>
                	<div className='hrm-dialog-form'>
                	    {this.getForm()}
                	</div>
                </WeaRightMenu>
            </Spin>
		)

	}
}