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

	@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);
		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 = () => {
		this.mainTable.tableStore = new TableStore(); //主要为了清除选中项

		const params = this.mainTab.form.getFormParams();

		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 = (type) => {
		let func;
		let params;

		if (!this.mainDialog.visible) {
			func = 'getSearchItems';
			params = {};
		} else {
			func = 'getForm';
			params = {};
			if (this.mainTable.selectedRowKeys) {
				Object.assign(params, {
					awardTypeId: this.mainTable.selectedRowKeys
				});
			}
		}

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

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

	@action
	deleteTableInfo = () => {
		const params = {
			awardTypeIds: 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
	save = () => {
		this.formInfo.form.validateForm().then(f => {
			if (f.isValid) {
				let func;
				let params = this.formInfo.form.getFormParams();

				if (this.mainTable.selectedRowKeys) {
					func = 'edit';
					Object.assign(params, {
						awardTypeId: this.mainTable.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
	openDialog = () => {
		this.mainDialog.visible = true;
		this.mainDialog.title = (!this.mainTable.selectedRowKeys) ? i18n.label.newRpType() : i18n.label.editRpType();
	}

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

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

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

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

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

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

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

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

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

}