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

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

	getButtons = () => {
		const {
			hrmTrainPlan
		} = this.props, {
			testForm,
			testFormSave,
		} = hrmTrainPlan, {
			loading
		} = testForm;

		let buttons = [
			(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@qxt5o0`} type="primary" disabled={loading} onClick={() => testFormSave()}>{i18n.button.save()}</Button>),
			(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@np48g1`} disabled>{i18n.button.more()}</Button>)
		];

		return buttons;
	}

	getForm() {
		const {
			hrmTrainPlan
		} = this.props, {
			testForm,
		} = hrmTrainPlan, {
			form,
			condition,
		} = testForm, {
			isFormInit,
		} = form;

		let arr = [];
		isFormInit && condition.map(c => {
			c.items.map((field, index) => {
				arr.push(<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@5k9r6g@${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@cx3nm9@${index}`} fieldConfig={field} form={form} formParams={form.getFormParams()} />
                        </WeaFormItem>)
			})
		})
		return <div className='wea-form-item-group'>{arr}</div>
	}

	getDropMenuDatas = () => {
		const {
			hrmTrainPlan
		} = this.props, {
			testForm,
		} = hrmTrainPlan, {
			loading
		} = testForm;

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

		return menu
	}

	handleMenuClick = (key) => {
		const {
			hrmTrainPlan
		} = this.props, {
			testFormSave,
		} = hrmTrainPlan;

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

	render() {
		const {
			hrmTrainPlan
		} = this.props, {
			testForm,
			setTestFormDialogVisible,
			date,
		} = hrmTrainPlan, {
			title,
			visible,
			loading,
		} = testForm, {
			width,
			height,
		} = this.state;

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

	}
}