import * as Api from '../apis/employDemand';
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 HrmEmployDemand {
	@observable main = {
		btnMenu: [],
		authorized: false,
		loading: true,
		title: i18n.module.employDemand(),
	}
	@observable mainTable = {
		tableStore: new TableStore(),
		selectedRowKeys: '',
	}
	@observable mainDialog = {
		title: i18n.label.assSituation(),
		visible: false,
	}
	@observable mainTab = {
		curKey: '1',
		isPanelShow: false,
		form: new WeaForm(),
		conditions: [],
		loading: true,
		conditionCount: observable.ref(2),
		qname: '',
	}
	@observable formInfo = {
		conditions: [],
		form: new WeaForm(),
		loading: true,
		date: '',
	}

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

		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 = () => {
		const {
			form,
			curKey,
			qname
		} = this.mainTab;

		const params = {
			qname,
			status: curKey - 1,
			...form.getFormParams(),
		}

		this.mainTable.tableStore = new TableStore();

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

			if (api_status) {
				sessionkey && this.mainTable.tableStore.getDatas(sessionkey, 1);
			} else {
				message.error(cb.message);
			}
		}).catch(error => {
			message.error(error);
		});
	}

	@action
	getFormInfo = () => {
		let func;
		let params = {};

		if (this.mainDialog.visible) {
			func = 'getFormInfo';
			if (this.mainTable.selectedRowKeys)
				params = Object.assign(params, {
					useDemandId: this.mainTable.selectedRowKeys
				})
		} else {
			func = 'getSearchItems';
		}
		Api[func](params).then(cb => {
			const {
				api_status,
				conditions,
			} = cb;

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

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

		form.validateForm().then(f => {
			if (f.isValid) {
				let func;
				let params = form.getFormParams();

				this.formInfo.loading = true;

				if (selectedRowKeys) {
					func = 'edit';
					Object.assign(params, {
						useDemandId: selectedRowKeys
					})
				} else {
					func = 'add';
				}

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

					if (api_status) {
						if (sign == '1') {
							message.success(cb.message);
							this.closeDialog();
							this.getTableInfo();
						} else {
							message.warning(cb.message)
						}
						this.formInfo.loading = false;
					}
				}).catch(error => {
					message.error(error);
				});
			} else {
				f.showErrors();
				this.setDate({
					date: new Date()
				});
			}
		});
	}

	@action
	deleteTableInfo = () => {
		const params = {
			useDemandIds: this.mainTable.selectedRowKeys
		}
		Api.deleteTableInfo(params).then(cb => {
			const {
				api_status,
				sign
			} = cb;

			if (api_status) {
				if (sign == '1') {
					message.success(cb.message);
					this.getTableInfo();
					this.resetSelectedRowKeys();
				} else {
					message.warning(cb.message)
				}
			}
		}).catch(error => {
			message.error(error);
		});
	}

	@action
	openDialog = () => {
		this.mainDialog.visible = true;
		this.mainDialog.title = (this.mainTable.selectedRowKeys) ? i18n.label.editEmployDemand() : i18n.label.newEmployDemand();
	}

	@action
	closeDialog = () => {
		this.mainDialog.visible = false;
		this.resetSelectedRowKeys();
	}

	@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('1');
	}

	@action
	resetSearch = () => {
		const {
			form
		} = this.mainTab;

		this.mainTab.isPanelShow = false;
		this.mainTab.qname = '';
		form.isFormInit && form.reset();
	}

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

	@action
	resetSelectedRowKeys = () => {
		this.mainTable.selectedRowKeys = '';
	}

	@action
	setSelectedRowKeys = (key) => {
		this.mainTable.selectedRowKeys = key;
	}

	@action
	setBaseSearchValue = (val) => {
		this.mainTab.qname = val;
	}

	@action
	setDate = (date) => {
		this.formInfo.date = date;
	}

}