import {
	observable,
	action,
	computed,
} from 'mobx';
import {
	WeaForm
} from 'comsMobx';
import {
	WeaLocaleProvider,
} from 'ecCom';
import {
	Button,
	message,
} from 'antd';
import {
   isArray
} from 'lodash';
const getLabel = WeaLocaleProvider.getLabel;

export default class SearchStore {
	constructor(root) {
		this.root = root;
	}
	
	advanceHeight = 130;
	hasMask = false;
	keyParam = 'key';

	otherProps = {};

	@observable datas = [];
	@observable selectedKey = '0';
	@observable form = new WeaForm();
	@observable conditions = [];
	@observable showSearchAd = false;
	@observable searchType = ['base', 'advanced'];
	@observable mark = '';
	@observable tabButtons = [];
	value = '';

	@action init = (config) => {
		Object.keys(config).forEach(key => {
			this[key] = config[key];
		})
		return this
	}

	@computed get searchsBaseValue() {
		return this.form.getFormParams()[this.linkageFieldName]
	}

	@computed get searchsAd() {
		return <div 
			onKeyDown={(e) => {
                if ( e.keyCode == 13 && e.target.tagName === "INPUT") {
                  this.handleSearch();
                }
            }}
		>{this.form.render()}</div>;
	}

	get buttons() {
		const mark = (this.mark !== undefined) ? this.mark : '';
		return this.root[`${mark}SearchButtons`] || [];
	}

	get buttonsAdNames() {
		return [getLabel(82529, '搜索'), getLabel(27088, '重置'), getLabel(32694, '取消')];
	}

	get buttonsAd() {
		return this.buttonsAdNames.map((name, index) => {
			const type = (index === 0) && "primary";

			let callback;
			if (index === 0) {
				callback = () => this.handleSearch();
			} else if (index === 1) {
				callback = () => {
					if (this.isFormIncludeSelect) {
						this.form.resetConditionValue();
					} else {
						this.form.reset();
					}
				}
			} else {
				callback = () => this.setShowSearchAd(false)
			}

			return <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@z2qizn@${index}`} type={type} onClick={callback}>{name}</Button>
		})
	}

	get isFormIncludeSelect() {
		return this.conditions.some(condition => {
			return condition.items.some(item => {
				const {
					conditionType
				} = item;

				return conditionType === 'SELECT'
			})
		})
	}

	@action fetchSearchForm = (params = {},hasTitle) => {
		this.form = new WeaForm();

		return new Promise((resolve, reject) => {
			if (this.api) {
				this.api(params).then(datas => {
					const conditions = this.getCondition(datas);
					if (conditions.length && !hasTitle) {
						conditions[0].title = ''
					}
					this.conditions = conditions;
					this.form.setCondition(conditions);

					resolve();
				});
			}else{
				resolve();
			}
		})
	}

	@action fetchTabs = (params = {}) => {
		this.datas = [];
		if (this.tabApi) {
			return new Promise((resolve, reject) => {
				this.tabApi(params).then(datas => {
					const {
						status,
					} = datas;

					if (status === '1') {
						this.setDatas(this.getTabs(datas));

						resolve();
					} else {
						message.error(datas.message);
					}
				})
			})
		}else{
			return Promise.resolve()
		}
		
	}

	@action setDatas = (datas) => {
		this.datas = datas;
	}

	@action setSelectedKey = (selectedKey) => {
		this.selectedKey = selectedKey;
	}

	@action setShowSearchAd = (showSearchAd) => {
		this.showSearchAd = showSearchAd;
	}

	handleTabChange = (selectedKey) => {
		this.setSelectedKey(selectedKey);

		this._handleTabChange && this._handleTabChange();
	}

	handleSearch = () => {
		this.setShowSearchAd(false);
		this._handleSearch && this._handleSearch();
	}

	handleSearchChange = (val) => {
		if (this.linkageFieldName) {
			this.form.updateFields({
				[this.linkageFieldName]: val
			});
		}else{
			this.value = val;
		}
	}

	getCondition = (datas) => {
		let condition = [];
		Object.keys(datas).forEach(key => {
			if (key.includes('condition')) {
				condition = datas[key];
			}
		})
		return condition;
	}

	getTabs = (datas) => {
		let tabs;

		Object.values(datas).forEach(value => {
			if (isArray(value)) {
				tabs = value;
			}
		});

		return tabs;
	}
 

}