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

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

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

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

    getForm = () => {
        const {
            hrmRecruitPlan
        } = this.props, {
            formInfo,
            handleChange,
        } = hrmRecruitPlan, {
            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@vb6aic@${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@ff0bbc@${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@3i11cr@${i}`} needTigger={true} title={len > 1 ? c.title : ''} showGroup={c.defaultshow} items={innerArray} col={1} />)
        })

        return outterArray;
    }

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

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

    }
}