import {
	observable,
	action,
	computed,
} from 'mobx';
import {
	Button,
} from 'antd';

export default class TopStore {
	constructor(api, rootStore) {
		this.api = api;
		this.rootStore = rootStore;
	}

	get topProps() {
		return {
			showDropIcon: true,
			iconBgcolor: '#217346',
			icon: <i className='icon-coms-hrm'/>,
		}
	}

	@observable tConf = [];
	@observable rConf = [];

	@computed get loading() {
		return this.rootStore.topLoading;
	}

	@action fetchRightMenu = (params = {}) => {
		this.api(params).then(datas => {
			const {
				btnMenu = []
			} = datas;

			const {
				tConf,
				rConf
			} = this.convertDatasToTopRightConf(btnMenu);

			this.tConf = tConf;
			this.rConf = rConf;
		})
	}

	@computed get buttons() {
		const buttons = this.tConf.map((item, index) => {
			const {
				menuFun,
				menuName
			} = item;

			const state = this.getRightMenuState(index);

			return (
				<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@x9y20v@${index}`} 
					type="primary" 
					disabled={state} 
					onClick={() => this.rootStore[menuFun]()}
				>
					{menuName}
				</Button>
			)
		});

		if (this.rootStore.otherTopButtons) {
			return [...this.rootStore.otherTopButtons, ...buttons];
		} else {
			return buttons;
		}
	}

	@computed get dropMenuDatas() {
		return this.rConf.map((item, index) => {
			const {
				menuFun,
				menuName,
				menuIcon
			} = item;

			const state = this.getRightMenuState(index);

			return {
				key: menuFun,
				disabled: state,
				icon: <i className={`${menuIcon}`} />,
				content: menuName,
				onClick: () => this.rootStore[menuFun]()
			}
		});
	}

	getRightMenuState = (index) => {
		const {
			rightMenuStates
		} = this.rootStore;

		if (rightMenuStates) {
			return rightMenuStates[index];
		} else {
			return false
		}
	}

	convertDatasToTopRightConf = (datas) => {
		let tConf, rConf;
		tConf = datas && datas.filter(data => {
			const {
				isTop,
				isBatch
			} = data;
			if (isTop === '1') {
				return true;
			}
			if (isBatch === '1') {
				return true;
			}
		})

		rConf = datas;

		return {
			tConf,
			rConf
		}
	}

	//config包括：title。
	setTopConfig = (config) => {
		Object.keys(config).forEach(key => {
			this[key] = config[key];
		})
	}

}