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

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

		if (this.props && this.props.location && this.props.location.query.id) {
			setSelectedRowKeys(this.props.location.query.id);
			getFormInfo({
				isView: true
			});
		} else {
			getFormInfo();
		}

	}

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

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

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

				arr.push(<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@lvkfu7@${index}`}
                            label={`${field.label}`}
                            labelCol={{span: 6, offset: 2}}
                            wrapperCol={{span: 15}}
                            error={form.getError(field)}
                            tipPosition="bottom"
                        >
                                <WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@ymovg1@${index}`} fieldConfig={field} form={form} formParams={form.getFormParams()} onChange={cb => this.handleChange(cb)}/>
                                {key == 'remindAheadDate' && <span style={{position: 'absolute',padding: 5}}>{i18n.label.day()}</span>}
                        </WeaFormItem>)
			})
		})
		return <div className='wea-form-item-group'>{arr}</div>
	}

	handleChange = (cb) => {}

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

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

		return menu
	}

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

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

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

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