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

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

	componentDidMount() {
		this.props.intelogin.getAlgorithmForm();
	}

	componentWillUnmount() {
		this.props.intelogin.resetAlgorithmForm();
	}

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

		let outterArray = [];
		isFormInit && condition.forEach((c, i) => {
			let innerArray = [],
			  eleStyle = (i == 1 ? {margin: "0 -20px"} : {});

			c && c.items && c.items.forEach(field => {
				const key = field.domkey[0];

				if (i == 0 && this.filter({
						isSysAlgorithm,
						key
					})) {
					return
				}

				innerArray.push({
					com: (<div style={eleStyle}>
						<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@yjkmfc@integrationLoginAlgorithmForm${key}`}
                            label={(i == 0) ? `${field.label}`: ''}
                            labelCol={{span: (i == 0) ? 6 : 0}}
                			wrapperCol={{span: (i == 0) ? 18 : 24}}
                			colon={(i == 0) ? true : false }
                            error={form.getError(field)}
                            tipPosition="bottom"
                        >
							<WeaSwitch 
							  ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@myiqkm@integrationLoginAlgorithmForm${key}`} 
							  fieldConfig={field} 
							  form={form} 
							  onChange={this.handleChange}
							/>
                        </WeaFormItem>
					</div>),
					colSpan: 1
				})

				if (i == 0) {
					innerArray = [...innerArray, {
						com: (
							this.add({
								isSysAlgorithm,
								key,
								form,
							})
						),
						colSpan: 1
					}]
				}
			})
			outterArray.push(
			  <WeaSearchGroup 
				ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@m8aw33@integrationLoginAlgorithmForm${i}`} 
				center={(i == 0) ? true : false} 
				needTigger={true} 
				title={c.title} 
				showGroup={c.defaultshow} 
				items={innerArray} 
				col={1} 
			  />
			)
		})
		return outterArray;
	}

	filter = (params) => {
		const {
			isSysAlgorithm,
			key
		} = params;

		if (!isSysAlgorithm && (key == 'isneedpwd' || key == 'password' || key == 'isneediv' || key == 'ivparam')) {
			return true
		}
	}

	add = (params) => {
		const {
			isSysAlgorithm,
			key,
			form,
		} = params;

		let temp = [];
		if (!isSysAlgorithm && key == 'decryptmethod') {
			['getPwd', 'getIv'].map(f => {
				temp.push(this[f]({
					form,
				}))
			});
		}

		return temp;
	}

	getPwd = (params) => {
		const {
			intelogin
		} = this.props, {
			algorithmForm
		} = intelogin, {
			pwd,
			isneedpwd,
		} = algorithmForm, {
			form
		} = params;

		return (
			<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@0pvs13`}
                label={getLabel(129397,'是否需要密钥')}
                labelCol={{span: 6}}
                wrapperCol={{span: 18}}
            >
				<div className='integrated-form-item'>
					{pwd.map((field, index) => {
						if (isneedpwd !== '1' && index == 1) return

						return (<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@p3odw3@integrationLoginAlgorithmForm${field.domkey[0]}`}
					                label={(index == 1) ? getLabel(32348,'加密密钥'):''}
					                colon={(index == 1) ? true : false}
					                labelCol={{span: (index==1) ? 6 : 0}}
					                error={form.getError(field)}
	                            	tipPosition="bottom"
					                wrapperCol={{span: (index == 1) ? 18 : 1}}
					                style={{float:'left', width:(index == 0) ? 50 : 325}}
	            				>
									<WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@ri329r@integrationLoginAlgorithmForm${field.domkey[0]}`} fieldConfig={field} form={form} formParams={form.getFormParams()} onChange={cb => this.handleChange(cb)}/>
								</WeaFormItem>)
					})}
				</div>
			</WeaFormItem>
		)
	}

	getIv = (params) => {
		const {
			intelogin
		} = this.props, {
			algorithmForm
		} = intelogin, {
			iv,
			isneediv
		} = algorithmForm, {
			form
		} = params;

		return (
			<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@uytd3j`}
                label={getLabel(129398,'是否需要向量')}
                labelCol={{span: 6}}
                wrapperCol={{span: 18}}
            >	
            	<div className='integrated-form-item'>
					{iv.map((field, index) => {
						if (isneediv !== '1' && index == 1) return

						return (<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@uaj1kc@integrationLoginAlgorithmForm${field.domkey[0]}`}
					                label={(index==1) ? getLabel(129315,'加密向量'):''}
					                colon={(index==1) ? true : false}
					                labelCol={{span:(index==1) ? 6 : 0}}
					                error={form.getError(field)}
	                            	tipPosition="bottom"
					                wrapperCol={{span: (index == 1) ? 18 : 1}}
					                style={{float:'left', width:(index == 0) ? 50 : 325}}
	            				>
									<WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@b2n5x7@integrationLoginAlgorithmForm${field.domkey[0]}`} fieldConfig={field} form={form} formParams={form.getFormParams()} onChange={cb => this.handleChange(cb)}/>
								</WeaFormItem>)
					})}
				</div>
			</WeaFormItem>
		)
	}

	handleChange = (cb) => {
		const {
			intelogin
		} = this.props, {
			exportafsSelectedValue,
			checkTableFieldValueUnique,
			mainTable,
			algorithmForm,
		} = intelogin, {
			id
		} = mainTable;

		Object.entries(cb).map(arr => {
			const key = arr[0],
				t = arr[1],
				val = Array.isArray(t.value) ? t.value[0] : t.value;

			exportafsSelectedValue({
				val,
				key
			});

			(key == 'encryptname') && ['outter_encryptclass_sys', 'outter_encryptclass'].map((v, i) => {
				// 加密算法有两个入口，集成设置新建弹窗自定义加密算法入口和加密tab页新建入口，因此校验的时候需要注意
				checkTableFieldValueUnique({
					tableName: v,
					fieldName: 'encryptname',
					fieldValue: val.replace(/^(\s*)|(\s*)$/g, ''),
					idName: 'id',
					idValue: algorithmForm.from ? "" : id
				}, i, algorithmForm.from);
			})

		});
	}

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

		if (loading) {
			return (
				<div className='integration-loading-center-small'>
                    <Spin ecId={`${this && this.props && this.props.ecId || ''}_Spin@9ddda4`} spinning={loading}></Spin>
                </div>
			)
		} else {
			return (
				<div className='integrated-login-form'>
				    {this.getForm()}
				</div>
			)
		}
	}
}