import React, {
	Component
} from 'react';

import {
	WeaFormItem,
	WeaSearchGroup,
	WeaError
} from 'ecCom';
import {
	WeaSwitch
} from 'comsMobx'
import {
	Row,
	Col
} from 'antd';

import {
	observer
} from 'mobx-react';
import CustomerFormItem from './CustomerFormItem';
import isEmpty from 'lodash/isEmpty';
import remove from 'lodash/remove';
import {
	i18n
} from '../../public/i18n';

@observer
export default class PasswordStrategyForm extends Component {
	constructor(props) {
		super(props);
	}
	row = [{
		key: 'minpasslen',
		className: 'oneColInput',
		children: []
	}, {
		key: 'passwordComplexity',
		className: 'oneColInput',
		children: []
	}, {
		key: 'valid',
		className: 'switch',
		children: [{
			key: 'remindperiod',
			className: '',
			label: i18n.label.remindingCycle,
			info: i18n.label.day
		}]
	}, {
		key: 'PasswordChangeReminder',
		className: 'switch',
		children: [{
			key: 'ChangePasswordDays',
			className: '',
			label: i18n.label.useSamePassword,
			info: i18n.label.forcedRevision
		}, {
			key: 'DaysToRemind',
			className: '',
			label: i18n.label.preCompulsoryRevision,
			info: i18n.label.remindingUser
		}]
	}, {
		key: 'loginMustUpPswd',
		className: 'switch',
		children: []
	}, {
		key: 'openPasswordLock',
		className: 'switch',
		children: [{
			key: 'sumPasswordLock',
			className: '',
			label: i18n.label.errAccumulative,
			info: i18n.label.lockAccount
		}, {
			key: 'needPasswordLockMin',
			info: i18n.label.locking
		}, {
			key: 'passwordLockMin',
			className: '',
			info: i18n.label.unlockingInMinutes
		}]
	}];

	renderItem = () => {
		window.e9HideFormFieldKeys = [];
		const {
			group,
			store
		} = this.props;
		const {
			basicSettingForm: form,
		} = store;
		let formParams = form.getFormParams();
		const items = [],
			children = [];

		const map = new Map();
		group.items.map(fields => {
			map.set(fields.domkey[0].toLowerCase(), fields);
		});

		this.row.map(r => {
			const children = [],
				keys = [],
				fields = map.get(r.key.toLowerCase());
			if (fields.conditionType === 'SWITCH' && formParams[r.key] === '1') {
				r.children.map(child => {
					remove(window.e9HideFormFieldKeys, child.key);
					const fields = map.get(child.key.toLowerCase());
					if (!_.isEmpty(fields)) {
						let disabled = false;
						if (child.key === 'PasswordLockMin' && formParams['needPasswordLockMin'] === '0') {
							disabled = true;
						}
						children.push(
							<div className="multi">
								<div>{child.label && child.label()}</div>
								<div className={child.className}>
									<WeaSwitch fieldConfig={{...fields, viewAttr: disabled ? 1 : fields.viewAttr}} form={form} />
								</div>
								<div>{child.info && child.info()}</div>
							</div>
						);
						keys.push(fields);
					}
				})
			} else {
				r.children.map(child => {
					window.e9HideFormFieldKeys.push(child.key);
				});
			}

			items.push({
				com: (
					<CustomerFormItem fields={fields} form={form} keys={keys}>
                    	<div className="formItem">
                    		<div className={r.className}>
                				<WeaSwitch fieldConfig={fields} form={form} />
                    		</div>
                    		{children}
                    	</div>
					</CustomerFormItem>
				),
				colSpan: 1,
			});

		});

		return <WeaSearchGroup needTigger={true} title={group.title} col={1} showGroup={group.defaultshow} items={items} />
	}

	render() {
		const {
			store
		} = this.props;
		const {
			rDate,
		} = store;
		return this.renderItem();
	}
}