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('sub');
	}

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

		refreshFormInfo();
	}

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

		let arr = [];
		isFormInit && conditions.map(c => {
			c.items.map((field, index) => {
				const key = field.domkey[0];

				if (selectedValue == '4' && (key !== 'trainPlanRangeType' && key !== 'resourceId')) {
					return
				}
				if (selectedValue == '0' && (key !== 'trainPlanRangeType' && key !== 'seclevel')) {
					return
				}
				if (selectedValue == '5' && (key !== 'trainPlanRangeType' && key !== 'subcompanyId' && key !== 'seclevel')) {
					return
				}
				if (selectedValue == '1' && (key !== 'trainPlanRangeType' && key !== 'departmentId' && key !== 'seclevel')) {
					return
				}
				if (selectedValue == '2' && (key !== 'trainPlanRangeType' && key !== 'jobactivitieId' && key !== 'seclevel')) {
					return
				}
				if (selectedValue == '3' && (key !== 'trainPlanRangeType' && key !== 'jobtitleId' && key !== 'seclevel')) {
					return
				}

				arr.push(<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@yu6jh0@${index}`}
                            label={`${field.label}`}
                            labelCol={{span: 6, offset: 2}}
                            error={form.getError(field)}
                            tipPosition="bottom"
                            wrapperCol={{span: 14}}
                        >
                                <WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@2m1qrc@${index}`} fieldConfig={field} form={form} formParams={form.getFormParams()} onChange={callbackInfo => this.handleChange(callbackInfo)}/>
                        </WeaFormItem>)
			})
		})
		return <div className='wea-form-item-group'>{arr}</div>
	}

	handleChange = (callbackInfo) => {
		const {
			hrmTrainArrange
		} = this.props, {
			formInfo,
			exportSelectedValue
		} = hrmTrainArrange, {
			form
		} = formInfo;

		if (Object.keys(callbackInfo)[0] == 'trainPlanRangeType') {
			const {
				trainPlanRangeType
			} = callbackInfo, {
				value
			} = trainPlanRangeType;

			form.resetConditionValue();

			form.updateFields({
				seclevel__seclevelto: {
					value: ['0', '100']
				},
				trainPlanRangeType: {
					value: value
				}
			});

			exportSelectedValue(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('sub');
	}

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

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

	}
}