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('hrmTrainResource')
@observer
export default class Editor extends React.Component {
	constructor(props) {
		super(props);
		this.state = {
			width: 700,
			height: 305,
		}
	}

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

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

		return buttons;
	}

	getForm() {
		const {
			hrmTrainResource
		} = this.props, {
			editor,
		} = hrmTrainResource, {
			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@2bis7m@${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@8ixetj@${index}`} fieldConfig={field} form={form} formParams={form.getFormParams()} />
                        </WeaFormItem>)
			})
		})
		return <div className='wea-form-item-group'>{arr}</div>
	}

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

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

		return menu
	}

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

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

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

		return (
			<WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@452wyd`}
                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@3ewhal`} spinning={loading}>
                	<WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@klmkz4`} datas={this.getDropMenuDatas()} onClick={ key => this.handleMenuClick(key)}>
	                    <WeaNewScroll ecId={`${this && this.props && this.props.ecId || ''}_WeaNewScroll@zpry5d`} height={height}>
	                        <div className='hrm-dialog-form'>
	                            {this.getForm()}
	                        </div>
	                    </WeaNewScroll>
                    </WeaRightMenu>
                </Spin>
            </WeaDialog>
		)

	}
}