import {
	observer
} from 'mobx-react';
import {toJS} from 'mobx';
import {
	WeaFormItem,
	WeaSearchGroup,
} from 'ecCom';
import {
	WeaSwitch
} from 'comsMobx';
import {
	isEmpty
} from 'lodash';
import '../style/magicForm.less';

@observer
export default class MagicForm extends React.Component {
	//表单是否需要分组
	isNeedSearchGroup = (conditions) => {
		return conditions.length > 1
	}

	//表单元素是否显示
	isItemShow = (item) => {
		const {
			store
		} = this.props, {
			form,
			customRenderKeyList
		} = store;

		const {
			domkey
		} = item;

		//自定义渲染
		if (customRenderKeyList && customRenderKeyList.includes(domkey[0])) {
			return false;
		}

		const linkageConfig = this.findLinkageConfig(domkey[0]);

		if (linkageConfig) {
			const {
				activeKey,
				showValue
			} = toJS(linkageConfig);

			const checkFlag = (key,index) => {
				if(Array.isArray(showValue)){
					const str = showValue[index];
					return str.split(",").includes(form.getFormParams()[key])
				}else{
					return showValue.split(",").includes(form.getFormParams()[key])
				}
				
			}

			//active表单元素当前的value
			if(Array.isArray(activeKey)){
				let flag = true;
				activeKey.map((k,i) => {
					flag = flag && checkFlag(k,i)
				})
				return flag;
			}
			return checkFlag(activeKey);
		}

		return true;

	}

	//查找联动当前表单元素的
	findLinkageConfig = (passiveKey) => {
		const {
			store
		} = this.props, {
			linkageConfigs
		} = store;

		return linkageConfigs && linkageConfigs.find(config => config.passiveKey === passiveKey);
	}

	renderForm = (items) => {
		const {
			store
		} = this.props, {
			form
		} = store;

		return items.map( (item,index) => {
			if (!this.isItemShow(item)) {
				return null;
			}

			const {
				label,
				labelcol,
				fieldcol,
				domkey,
				conditionType,
				custom,
				colon = true,
			} = item;
			
			if(conditionType == 'CUSTOM' && typeof custom == 'object'){
				return custom
			}

			return (
				<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@frakio@${index}`}
					label={this.getLabel(item, index) }
					colon={colon}
					labelCol={{span: labelcol}}
					wrapperCol={{span:fieldcol}}
					error={form.getError(item)} 
					tipPosition='bottom'
					className={domkey[0]}
				>
					{this.renderFormItemChildren(item)}
				</WeaFormItem>
			)
		})
	}

	renderFormItemChildren = (item) => {
		const {
			store
		} = this.props, {
			form,
			handleFormChange,
		} = store;

		return (
			<div>
				<WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@e1z9l4`} 
					fieldConfig={item} 
					form={form} 
					formParams={form.getFormParams()} 
					onChange={handleFormChange}
				/>
				{this.renderOtherComponents(item)}
			</div>
		)
	}

	renderOtherComponents = (item) => {
		const {
			store
		} = this.props, {
			form,
			handleFormChange,
			otherComponents
		} = store;

		if (!otherComponents) {
			return null;
		}

		const {
			domkey
		} = item;

		const fn = otherComponents[domkey[0]];

		if (fn) {
			return fn(item, form)
		} else {
			return null;
		}
	}

	//表单元素控制组
	getFormGroup = (index,condition) => {
		const {
			store
		} = this.props, {
			groupShowConfigs,
			form,
		} = store;

		const {
			title,
			items,
			defaultshow = true,
		} = condition;

		if (groupShowConfigs) {
			return groupShowConfigs.map((config, index) => {
				const {
					activeKey,
					passiveIndex,
					showValue
				} = config;

				const activeVal = form.getFormParams()[activeKey];

				if (
					(index === passiveIndex && showValue.includes(activeVal)) ||
					(index !== passiveIndex)
				) {
					return (
						<WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@ljxtbj@${index}`} 
							center
						    showGroup={defaultshow}
						    title={title} 
						    children={this.renderForm(items)}
						/>
					)
				}

			})
		}else{
			return (
				<WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@jyg6vd`}
					center 
				    showGroup={defaultshow}
				    title={title} 
				    children={this.renderForm(items)}
				/>
			)
		}
	}

	getLabel = (item, index) => {
		const {
			store
		} = this.props, {
			hasOrderNumber,
			labelPres
		} = store;

		const {label, domkey} = item;

		if (!hasOrderNumber && isEmpty(labelPres)) {
			return label
		}

		if (hasOrderNumber) {
			return (
				<span style={{display:'inline-block',width: '100%'}} className='text-elli'>
					<div className='circle'>
						<span className='number-icon'>{index+1}</span>
					</div>
					<span style={{marginLeft:10}}>{label}</span>
				</span>
			)
		}

		if (!isEmpty(labelPres)) {
			return (
				<span>
					{
						labelPres[domkey[0]] && <span>{labelPres[domkey[0]]}</span>
					}
					<span style={{marginLeft:10}}>{label}</span>
				</span>
			)
		}
	}

	render() {
		const {
			store
		} = this.props, {
			conditions,
			form,
			className
		} = store, {
			isFormInit
		} = form;

		const hasSearchGroup = this.isNeedSearchGroup(conditions);

		return (
			<div className={`magicForm ${className}`}>
				{
					isFormInit && conditions.map( (condition,index) => {
						const {
							items,
						} = condition;

						if (hasSearchGroup) {
							return this.getFormGroup(index,condition);
						} else {
							return this.renderForm(items)
						}
					})
				}
			</div>
		);
	}
}