import {
	inject,
	observer,
} from 'mobx-react'
import {
	WeaDialog,
	WeaFormItem,
	WeaNewScroll,
	WeaRightMenu,
	WeaMoreButton,
} from 'ecCom'
import {
	Spin,
	Button,
	message
} from 'antd'
import {
	WeaSwitch
} from 'comsMobx'
import {
	i18n
} from '../../public/i18n';

@inject('hrmTrainTypes')
@observer
export default class Editor extends React.Component {
	constructor(props) {
		super(props);
		this.state = {
			width: 700,
			height: 355,
		}
	}

	getButtons = () => {
		const {
			hrmTrainTypes
		} = this.props, {
			editor,
			editorSave,
		} = hrmTrainTypes, {
			loading
		} = editor;

		let buttons = [
			(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@3cov8a`} type="primary" disabled={loading} onClick={() => editorSave()}>{i18n.button.save()}</Button>),
			(<WeaMoreButton ecId={`${this && this.props && this.props.ecId || ''}_WeaMoreButton@d69pgb`} />)
		];

		return buttons;
	}

	getForm() {
		const {
			hrmTrainTypes
		} = this.props, {
			editor,
		} = hrmTrainTypes, {
			form,
			condition,
			selectedValue,
		} = editor, {
			isFormInit,
		} = form;

		let arr = [];
		isFormInit && condition.map(c => {
			c.items.map((field, index) => {
				arr.push(<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@it2e1s@${index}`}
                            label={`${field.label}`}
                            labelCol={{span: 6}}
                            error={form.getError(field)}
                            tipPosition="bottom"
                            wrapperCol={{span: 18}}
                        >
                                <WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@dbali4@${index}`} fieldConfig={field} form={form} formParams={form.getFormParams()} />
                        </WeaFormItem>)
			})
		})
		return <div className='wea-form-item-group'>{arr}</div>
	}

	getDropMenuDatas = () => {
		const {
			hrmTrainTypes
		} = this.props, {
			editor
		} = hrmTrainTypes, {
			loading
		} = editor;

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

		return menu
	}

	handleMenuClick = (key) => {
		const {
			hrmTrainTypes
		} = this.props, {
			editorSave,
		} = hrmTrainTypes;

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

	render() {
		const {
			hrmTrainTypes
		} = this.props, {
			editor,
			setEditorDialogVisible
		} = hrmTrainTypes, {
			title,
			visible,
			loading,
			date,
		} = editor, {
			width,
			height,
		} = this.state;

		return (
			<WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@e2kx16`}
                title={title}
                icon="icon-coms-hrm"
                iconBgcolor="#217346"
                visible={visible}
                closable={true}
                onCancel={() => setEditorDialogVisible(false)}
                buttons={this.getButtons()}
                style={{width: width, height: height}}
                onChangeHeight={(h) => this.setState({height: h})}
            >
                <Spin ecId={`${this && this.props && this.props.ecId || ''}_Spin@ekfs24`} spinning={loading}>
                	<WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@vhca7j`} datas={this.getDropMenuDatas()} onClick={ key => this.handleMenuClick(key)}>
	                    <WeaNewScroll ecId={`${this && this.props && this.props.ecId || ''}_WeaNewScroll@ww9vr1`} height={height}>
	                        <div className='hrm-dialog-form'>
	                            {this.getForm()}
	                        </div>
	                    </WeaNewScroll>
                    </WeaRightMenu>
                </Spin>
            </WeaDialog>
		)

	}
}