import {
	inject,
	observer,
} from 'mobx-react'
import {
	WeaSearchGroup,
	WeaFormItem,
	WeaLocaleProvider,
} from 'ecCom'
import {
	WeaSwitch
} from 'comsMobx'
import {
	Spin,
} from 'antd'
const getLabel = WeaLocaleProvider.getLabel

@inject('intelogin')
@observer
export default class AlgorithmTestForm extends React.Component {
	constructor(props) {
		super(props);
	}

	componentDidMount() {
		this.props.intelogin.getAlgorithmTestForm()
	}

	componentWillUnmount() {
		this.props.intelogin.resetAlgorithmTestForm()
	}

	getTestResult = () => {
		const {
			intelogin
		} = this.props, {
			algorithmTestForm,
		} = intelogin, {
			testResult
		} = algorithmTestForm;

		return testResult && Object.entries(testResult).map(arr => {
			return (
				<div style={{marginLeft: 135, position:'relative', padding: '5px 0'}}>
					<span>{arr[0]}</span>
					{
						(arr[1] == '0')
						? <i 
							className='icon-coms-Clear integrated-icon' 
							style={{color: '#FF0000'}}
						  />
						: <i 
							className='icon-coms-Approval integrated-icon' 
							style={{color: '#42BEF6'}}
						  />
					}
				</div>
			)
		});
	}

	getForm = () => {
		const {
			intelogin
		} = this.props, {
			algorithmTestForm,
		} = intelogin, {
			condition,
			form,
		} = algorithmTestForm, {
			isFormInit
		} = form;

		let outterArray = [];
		isFormInit && condition.map((c, i) => {
			let innerArray = [];
			c.items.map((field) => {
				const key = field.domkey[0];
				innerArray.push({
					com: (
						<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@2qrwax@integrationLoginAlgorithmTestForm${key}`}
                            label={`${field.label}`}
                            labelCol={(i == 0) ? {span: 4,offset:3} : {span: 0}}
                            wrapperCol={(i == 0) ? {span:  13 } : {span:  20,offset:3}}
                            colon={`${field.label}` ? true : false}
                            error={form.getError(field)}
                            tipPosition="bottom"
                        >
                            {<WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@b1ajnb@integrationLoginAlgorithmTestForm${key}`} fieldConfig={field} form={form} formParams={form.getFormParams()}/>}
                        </WeaFormItem>
					),
					colSpan: 1
				})
			})
			outterArray.push(<WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@hp62te@integrationLoginAlgorithmTestForm${i}`} needTigger={true} title={c.title} showGroup={c.defaultshow} items={innerArray} col={1} />)
		})

		return outterArray;
	}

	render() {
		const {
			intelogin
		} = this.props, {
			algorithmTestForm,
		} = intelogin, {
			loading,
		} = algorithmTestForm;


		if (loading) {
			return (
				<div className='integration-loading-center-small'>
					<Spin ecId={`${this && this.props && this.props.ecId || ''}_Spin@8c2k7t`} spinning={loading}></Spin>
				</div>
			)
		} else {
			return (
				<div>
					<WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@ebioh5`} 
						title={getLabel(129528, '规范性测试')} 
						children={this.getTestResult()} 
						showGroup={true} 
					/>
					{this.getForm()}
				</div>
			)
		}

	}
}