import {
    inject,
    observer
} from 'mobx-react'
import {
    WeaFormItem,
    WeaSearchGroup
} from 'ecCom'
import {
    Spin
} from 'antd'
import {
    WeaSwitch
} from 'comsMobx'
import {addContentPath,AttachToNumberField} from '../../util/index.js'

const verificationCode = '/weaver/weaver.file.MakeValidateCode?isView=1&validatetype=0&validatenum=4&seriesnum_=';

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

    componentDidMount() {
        const {
            hrmRecruitLibrary
        } = this.props, {
            mainTable,
            subTab,
            getFormConditions,
        } = hrmRecruitLibrary;

        if (!(mainTable.index == '1' && subTab.curKey !== '0')) {
            getFormConditions();
        }
    }

    componentWillUnmount() {
        const {
            hrmRecruitLibrary
        } = this.props, {
            mainTable,
            subTab,
            resetFormInfo,
        } = hrmRecruitLibrary;

        if (!(mainTable.index == '1' && subTab.curKey !== '0')) {
            resetFormInfo();
        }
    }

    getForm = () => {
        const {
            hrmRecruitLibrary,
            needTitle
        } = this.props, {
            formInfo,
            time,
            refreshVerificationCode,
        } = hrmRecruitLibrary, {
            form,
            conditions,
            loading,
            accounttype,
        } = 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];
                if (accounttype === '0' && key == 'belongto') {
                    return
                }
                innerArray.push({
                    com: (
                        <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@vpkeyn@${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@fepl1d@${index}`} fieldConfig={field} form={form} formParams={form.getFormParams()} onChange={cb => this.handleChange(cb)}/>}
                            { (key == "validatecode") && (
                                <img border="0" style={{position:"absolute", marginLeft: 10}} align='absmiddle' src={addContentPath(`${verificationCode}${time}`)} onClick={() =>refreshVerificationCode()} />
                            ) }
                            {field.domkey[0] == 'workcode' && <AttachToNumberField field={field} form={form}/>}
                        </WeaFormItem>
                    ),
                    colSpan: 1
                })
            })
            outterArray.push(<WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@3mrw9v@${i}`} needTigger={true} title={ (len > 1 || needTitle) ? c.title : ''} showGroup={c.defaultshow} items={innerArray} col={1} />)
        })

        return outterArray;
    }

    handleChange = (cb) => {
        const {
            accounttype,
        } = cb;
        const {
            hrmRecruitLibrary
        } = this.props, {
            exportSelectedValue
        } = hrmRecruitLibrary;

        if (accounttype) {
            exportSelectedValue('accounttype', accounttype.value)
        }
    }

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

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

    }
}