import * as Api from '../apis/assImple';
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 HrmAssImple {
	@observable main = {
		btnMenu: [],
		authorized: false,
		loading: true,
		title: i18n.module.assImple(),
	}
	@observable mainTable = {
		tableStore: new TableStore(),
		selectedRowKeys: '',
	}
	@observable subTable = {
		tableStore: new TableStore(),
		selectedRowKeys: '',
	}
	@observable thirdTable = {
		tableStore: new TableStore(),
	}
	@observable mainDialog = {
		title: i18n.label.assSituation(),
		visible: false,
	}
	@observable subDialog = {
		title: i18n.label.assDetail(),
		visible: false,
	}
	@observable mainTab = {
		curKey: '0',
		isPanelShow: false,
		form: new WeaForm(),
		conditions: [],
		loading: true,
		conditionCount: observable.ref(3),
	}

	@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(cb => {
			const {
				api_status,
				btnMenu,
			} = cb;

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

	@action
	getTableInfo = () => {
		let func;
		let params;
		if (!this.mainDialog.visible) {
			func = 'getMainTableInfo';
			params = {
				...this.mainTab.form.getFormParams(),
				isFinish: (this.mainTab.curKey == '0') ? false : true,
			}
		} else {
			params = {
				checkInfoId: this.mainTable.selectedRowKeys
			}
			if (!this.subDialog.visible) {
				func = 'getSubTableInfo';
			} else {
				func = 'getThirdTableInfo';
				params = {
					...params,
					resourceId: this.subTable.selectedRowKeys
				}
			}
		}

		Api[func](params).then(cb => {
			const {
				api_status,
				sessionkey,
			} = cb;

			if (api_status) {
				if (!this.mainDialog.visible) {
					sessionkey && this.mainTable.tableStore.getDatas(sessionkey, 1);
				} else {
					if (!this.subDialog.visible) {
						sessionkey && this.subTable.tableStore.getDatas(sessionkey, 1);
					} else {
						sessionkey && this.thirdTable.tableStore.getDatas(sessionkey, 1);
					}
				}
			} else {
				message.error(cb.message);
			}
		}).catch(error => {
			message.error(error);
		});
	}

	@action
	getFormInfo = (type) => {
		Api.getSearchItems().then(cb => {
			const {
				api_status,
				conditions,
			} = cb;

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

	@action
	openDialog = () => {
		if (!this.mainDialog.visible) {
			this.mainDialog.visible = true;
		} else {
			this.subDialog.visible = true;
		}
	}

	@action
	closeDialog = () => {
		if (this.subDialog.visible) {
			this.subDialog.visible = false;
		} else {
			this.mainDialog.visible = false;
		}
	}

	@action
	clickTab = (key) => {
		this.mainTab.curKey = key;
		this.resetSearch();
		this.getTableInfo();
	}

	@action
	changeSearchPanelStatus = (bool) => {
		this.mainTab.isPanelShow = bool;
	}

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

	@action
	refreshPage = () => {
		this.clickTab('0');
	}

	@action
	resetSearch = () => {
		this.mainTab.isPanelShow = false;
		this.mainTab.form.reset();
	}

	@action
	setSelectRowKeys = (key) => {
		if (!this.mainDialog.visible) {
			this.mainTable.selectedRowKeys = key;
		} else {
			this.subTable.selectedRowKeys = key;
		}
	}

}