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

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

		getFormInfo();
	}

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

		refreshFormInfo();
	}

	getForm = () => {
		const {
			hrmOtherSetting
		} = this.props, {
			formInfo,
		} = hrmOtherSetting, {
			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@snbavx@${index}`}
                            label={`${field.label}`}
                            labelCol={{span: 6, offset: 8}}
                            wrapperCol={{span: 10}}
                            error={form.getError(field)}
                            tipPosition="bottom"
                        >
                                <WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@hhqs1q@${index}`} fieldConfig={field} form={form} formParams={form.getFormParams()} />
                        </WeaFormItem>)
			})
		})
		return <div className='wea-form-item-group'>{arr}</div>
	}

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

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

		return menu
	}

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

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

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

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