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

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

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

		return buttons;
	}

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

		let arr = [];
		isFormInit && condition.map(c => {
			c.items.map((field, index) => {
				const key = field.domkey[0];
				if (selectedValue == '3') {
					if (key == 'Areaids2') return
				} else if (selectedValue == '4') {
					if (key == 'Areaids1') return
				} else {
					if (key == 'Areaids1' || key == 'Areaids2') return
				}

				arr.push(<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@jmk9n1@${index}`}
                            label={`${field.label}`}
                            labelCol={{span: 8}}
                            error={form.getError(field)}
                            tipPosition="bottom"
                            wrapperCol={{span: 16}}
                        >
                                <WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@pc3k8h@${index}`} fieldConfig={field} form={form} formParams={form.getFormParams()} onChange={e => this.handleChange(e)}/>
                        </WeaFormItem>)
			})
		})
		return <div className='wea-form-item-group'>{arr}</div>
	}

	handleChange = (e) => {
		const {
			hrmSalaryIndexSetting
		} = this.props, {
			setSelectedValue
		} = hrmSalaryIndexSetting;

		if (Object.keys(e)[0] == 'AreaType') setSelectedValue(e.AreaType.value);
	}

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

		return (
			<WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@0mi8qu`}
                title={title}
                icon="icon-coms-hrm"
                iconBgcolor="#217346"
                visible={visible}
                closable={true}
                hasScroll
                onCancel={() => setEditorDialogVisible(false)}
                buttons={this.getButtons()}
                style={{width: width, height: height}}
                onChangeHeight={(h) => this.setState({height: h})}
            >
                {
                    loading ?   <div className='hrm-loading-center-small'>
                                   <Spin ecId={`${this && this.props && this.props.ecId || ''}_Spin@ub3ukz`} spinning={loading}></Spin>
                                </div>
                            :   <div className='hrm-dialog-form'>
                                    {this.getForm()}
                                </div>
                }
            </WeaDialog>
		)

	}
}