import {
	observable,
	action,
	computed,
} from 'mobx';
import {
	WeaLocaleProvider,
} from 'ecCom';
import {
	WeaForm,
} from 'comsMobx';
import {
	Button,
	message,
} from 'antd';
import * as api from '../apis/orgFieldDisplaySetting';
const getLabel = WeaLocaleProvider.getLabel;

class OrgFieldDisplaySetting {
	@observable permission = {
		hasRight: false,
		loading: true,
	}

	@action callPermissionApi = (callback) => {
		api.getHasRight().then(datas => {
			const {
				hasRight
			} = datas;

			this.permission.hasRight = hasRight;
			this.permission.loading = false;
			if (hasRight) {
				callback && callback();
			}
		}).catch(error => {});
	}

	TOP = {
		showDropIcon: true,
		iconBgcolor: '#217346',
		icon: <i className='icon-coms-hrm'/>,
	}

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

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

	@action callRightMenuApi = (isEdit) => {
		api.getRightMenu().then(datas => {
			const {
				btnMenu = []
			} = datas;


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

			this.tConf = tConf;
			this.rConf = rConf;
		}, error => {})
	}

	filter = (datas) => {
		let tConf, rConf;
		tConf = datas.filter(item => {
			var bool = false;
			['isTop', 'isBatch'].forEach(prop => {
				const b = parseInt(item[prop]);
				if (b) {
					bool = true
				}
			});

			return bool
		})

		rConf = datas;

		return {
			tConf,
			rConf
		}
	}

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

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

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

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

	save = () => {
		this.form.validateForm().then(f => {
			if (f.isValid) {
				const params = this.form.getFormParams();
				api.saveOrganizationShowSet(params).then(datas => {
					const {
						sign
					} = datas;
					const msg = datas.message;
					if (sign === '1') {
						message.success(msg);
					} else {
						message.warning(msg);
					}
				}, error => {})
			} else {
				f.showErrors();
			}
		});
	}

	@observable form = new WeaForm();

	@observable loading = false;

	@action callFormApi = () => {
		this.loading = true;
		api.getOrganizationShowSetForm().then(datas => {
			const {
				condition
			} = datas;

			this.form.setCondition(condition);
			this.loading = false;
		}, error => {})
	}

}

export const orgFieldDisplaySetting = new OrgFieldDisplaySetting();