import * as Api from '../apis/otherSetting';
import {
	toJS
} from 'mobx';
import {
	observable,
	action,
	computed,
} from 'mobx';
import {
	WeaForm,
	WeaTableNew
} from 'comsMobx';
import {
	message
} from 'antd';
import {
	i18n
} from '../public/i18n';
const {
	TableStore
} = WeaTableNew;

export class HrmOtherSetting {
	@observable main = {
		btnMenu: [],
		authorized: false,
		loading: true,
	}
	@observable formInfo = {
		conditions: [],
		form: new WeaForm(),
		loading: true,
		date: '',
		id: '',
	}

	@computed get menu() {
		let topMenu = this.main.btnMenu.filter(item => item.isTop == '1' || item.isBatch == '1');
		let rightMenu = this.main.btnMenu.filter(item => !item.isBatch);

		return {
			topMenu,
			rightMenu
		}
	}

	@action
	getAuth = () => {
		Api.getAuth().then(cb => {
			const {
				status,
				hasRight
			} = cb;

			if (status == '1') {
				hasRight && this.grantAuth();
				this.main.loading = false;
			} else {
				message.error(cb.message);
			}
		}).catch(error => {
			message.error(error);
		});
	}

	@action
	getButtonsMenu = () => {
		Api.getButtonsMenu().then(rs => {
			const {
				api_status,
				btnMenu,
			} = rs;

			if (api_status) {
				this.main.btnMenu = btnMenu;
			} else {
				message.error(rs.message);
			}
		}).catch(error => {
			message.error(error);
		});
	}

	@action
	getFormInfo = () => {
		Api.getFormInfo().then(rs => {
			const {
				api_status,
				conditions,
				id,
			} = rs;

			if (api_status) {
				this.formInfo.conditions = conditions;
				this.formInfo.form.initFormFields(conditions);
				this.formInfo.id = id;
				this.formInfo.loading = false;
			} else {
				message.error(rs.message);
			}
		}).catch(error => {
			message.error(error);
		});
	}

	@action
	save = () => {
		const {
			form,
			id
		} = this.formInfo;

		const params = {
			id: id,
			...form.getFormParams()
		}

		Api.save(params).then(rs => {
			const {
				api_status,
				sign,
			} = rs;

			if (api_status) {
				sign == '1' ? message.success(rs.message) : message.warning(rs.message);
			} else {
				message.error(rs.message);
			}
		}).catch(error => {
			message.error(error);
		});
	}

	@action
	grantAuth = () => {
		this.main.authorized = true;
	}

	@action
	closeAuth = () => {
		this.main.authorized = false;
	}

	@action
	refreshFormInfo = () => {
		this.formInfo.form = new WeaForm();
		this.formInfo.conditions = [];
		this.formInfo.loading = true;
	}

}