import {
    WeaDialog,
    WeaFormItem,
    WeaMoreButton,
} from 'ecCom'

import {
    Spin,
    Button,
} from 'antd'

import {
    WeaSwitch
} from 'comsMobx'

import {
    i18n
} from '../../public/i18n';

export default class NewAndEditDialog extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            scrollHeight: this.props.height,
        }
    }

    getButtons() {
        const {
            loading,
            save
        } = this.props;

        return [
            (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@6g7mhg`} type="primary" onClick={() => save()} disabled={loading}>{i18n.button.save()}</Button>),
            (<WeaMoreButton ecId={`${this && this.props && this.props.ecId || ''}_WeaMoreButton@51bnw6`} />)
        ];
    }

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

        let arr = [];
        isFormInit && condition.map(c => {
            c.items.map((field, index) => {
                if (field.domkey[0] == 'jobgroupid') {
                    field.addOnClick = () => addOnClick('jobGroup');
                } else if (field.domkey[0] == 'jobactivityid') {
                    field.addOnClick = () => addOnClick('job');
                }
                arr.push(<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@nysywx@${index}`}
                            label={`${field.label}`}
                            labelCol={{span: `${field.labelcol}`}}
                            error={form.getError(field)}
                            tipPosition="bottom"
                            wrapperCol={{span: `${field.fieldcol}`}}>
                                {<WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@r7euhp@${index}`} fieldConfig={field} form={form} formParams={form.getFormParams()} />}
                        </WeaFormItem>)
            })
        })
        return <div className="wea-form-item-group">{arr}</div>
    }

    render() {
        const {
            width,
            height,
            title,
            visible,
            loading,
            onCancel,
        } = this.props, {
            scrollHeight
        } = this.state;

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