import {
    inject,
    observer
} from 'mobx-react'
import {
    WeaFormItem,
    WeaSearchGroup
} from 'ecCom'
import {
    Spin
} from 'antd'
import {
    WeaSwitch
} from 'comsMobx'

@inject('hrmRecruitInfo') //todo
@observer
export default class FormInfo extends React.Component {
    constructor(props) {
        super(props);
    }

    componentDidMount() {
        //需条件判断
        this.props.hrmRecruitInfo.getFormConditions();
    }

    componentWillUnmount() {
        //需条件判断
        this.props.hrmRecruitInfo.resetFormInfo();
    }

    getForm = () => {
        const {
            hrmRecruitInfo
        } = this.props, {
            formInfo,
            handleChange,
        } = hrmRecruitInfo, {
            form,
            conditions,
            loading,
        } = formInfo, {
            isFormInit,
        } = form;

        let outterArray = [];
        const len = conditions.length;
        isFormInit && conditions.map((c, i) => {
            let innerArray = [];
            c.items.map((field, index) => {
                const key = field.domkey[0];
                innerArray.push({
                    com: (
                        <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@725q97@${index}`}
                            label={`${field.label}`}
                            labelCol={{span: 5, offset:3}}
                            wrapperCol={{span: 12}}
                            error={form.getError(field)}
                            tipPosition="bottom"
                        >
                            {<WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@yqq5dr@${index}`} fieldConfig={field} form={form} formParams={form.getFormParams()} onChange={cb => handleChange(cb)}/>}
                        </WeaFormItem>
                    ),
                    colSpan: 1
                })
            })
            outterArray.push(<WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@7xq9uv@${i}`} needTigger={true} title={len > 1 ? c.title : ''} showGroup={c.defaultshow} items={innerArray} col={1} />)
        })

        return outterArray;
    }



    render() {
        const {
            hrmRecruitInfo,
        } = this.props, {
            formInfo,
        } = hrmRecruitInfo, {
            loading,
            date,
        } = formInfo;

        if (loading) {
            return (
                <div className='hrm-loading-center-small'>
                    <Spin ecId={`${this && this.props && this.props.ecId || ''}_Spin@snkkoi`} spinning={loading}></Spin>
                </div>
            )
        } else {
            return this.getForm()
        }

    }
}