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

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

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

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

		return buttons;
	}

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

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

		return (
			<WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@ei3z7e`}
                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@9ureha`} spinning={loading}>
                    <WeaNewScroll ecId={`${this && this.props && this.props.ecId || ''}_WeaNewScroll@fqc9hx`} height={height+10}>
                        <div style={{padding: '27px 8% 25px'}}>
                            {this.getForm()}
                        </div>
                    </WeaNewScroll>
                </Spin>
            </WeaDialog>
		)

	}
}