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

export class HrmSalaryBank {
	@observable title = i18n.module.salaryBank();
	@observable btnMenu = [];
	@observable authorized = false;
	@observable loading = true;
	@observable isPanelShow = false;
	@observable table = {
		tableStore: new TableStore(),
		selectedRowsKeys: '',
	}
	@observable searchPanel = {
		condition: [],
		form: new WeaForm(),
		value: '',
		loading: true,
		conditionCount: observable.ref(2),
	}
	@observable editor = {
		visible: false,
		form: new WeaForm(),
		condition: [],
		loading: true,
		date: '',
	}

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

		return {
			topMenu,
			rightMenu
		}
	}

	@action
	getRight = () => {
		this.loading = true;
		Api.getRight().then(rs => {
			const {
				status,
				hasRight
			} = rs;

			if (status == '1') {
				if (hasRight) {
					this.authorized = true;
					this.getButtonsMenu();
					this.getTableInfo();
				}
				this.loading = false;
			} else {
				message.error(rs.message);
			}
		}).catch(error => {
			message.error(error);
		});
	}

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

			if (status == '1') {
				this.btnMenu = btnMenu;
			} else {
				message.error(rs.message);
			}
		}).catch(error => {
			message.error(error);
		});
	}

	@action
	getTableInfo = () => {
		this.table.tableStore = new TableStore(); //清空表格选中状态
		const {
			form,
			value
		} = this.searchPanel;
		let params;
		if (form.isFormInit) {
			params = form.getFormParams();
		} else {
			params = {
				bankname: value
			};
		}

		Api.getTableInfo(params).then(rs => {
			const {
				status,
				sessionkey
			} = rs;

			if (status == '1') {
				sessionkey && this.table.tableStore.getDatas(sessionkey, 1);
			} else {
				message.error(rs.message);
			}
		}).catch(error => {
			message.error(error);
		});
	}

	@action
	getSearchCondition = () => {
		Api.getSearchCondition().then(rs => {
			const {
				formField,
				status = '1',
			} = rs;

			if (status == '1') {
				if (formField) {
					this.searchPanel.condition = formField;
					this.searchPanel.form.initFormFields(formField);
				}
				this.searchPanel.loading = false;
			} else {
				message.error(rs.message);
			}
		}).catch(error => {
			message.error(error);
		});
	}

	@action
	getEditorForm = () => {
		let params;
		if (this.editor.formType == 'new') {
			params = {};
		} else {
			params = {
				id: this.table.selectedRowsKeys,
			};
		}
		Api.getEditorForm(params).then(rs => {
			const {
				status,
				formField,
			} = rs;

			if (status == '1') {
				if (formField) {
					this.editor.condition = formField;
					this.editor.form.initFormFields(formField);
				}
				this.editor.loading = false;
			} else {
				message.error(rs.message);
			}
		}).catch(error => {
			message.error(error);
		});
	}

	@action
	deleteTableRows = () => {
		let params = {
			ids: this.table.selectedRowsKeys
		};
		Api.deleteTableRows(params).then(rs => {
			const {
				status,
				success
			} = rs;

			if (status == '1') {
				if (success) {
					message.success(rs.message);
					this.getTableInfo();
				} else {
					message.warning(rs.message)
				}
			}

		}).catch(error => {
			message.error(error);
		});
	}

	@action
	editorSave = () => {
		this.editor.form.validateForm().then(f => {
			if (f.isValid) {
				const {
					formType
				} = this.editor;
				let func;
				let params = this.editor.form.getFormParams();
				if (formType == 'new') {
					func = 'add';
				} else {
					func = 'edit';
					params = {
						...params,
						id: this.table.selectedRowsKeys
					}
				}
				Api[func](params).then(rs => {
					const {
						status,
						success
					} = rs;

					if (status == '1') {
						if (success) {
							message.success(rs.message);
							this.setEditorDialogVisible(false);
							this.getTableInfo();
						} else {
							message.warning(rs.message)
						}
					}
				}).catch(error => {
					message.error(error);
				});
			} else {
				f.showErrors();
				this.setDate({
					date: new Date()
				});
			}
		});
	}

	@action
	setEditorDialogVisible = (bool, type) => {
		this.editor.visible = bool
		if (bool) {
			extendObservable(this.editor, {
				formType: type,
				title: type == 'new' ? i18n.label.newSalaryBank() : i18n.label.editSalaryBank(),
			});
			this.getEditorForm();
		} else {
			this.editorDialogReset();
		}
	}

	@action
	editorDialogReset = () => {
		this.editor.loading = true;
		this.editor.form = new WeaForm();
	}

	@action
	setPanelStatus = (bool) => {
		const {
			condition
		} = this.searchPanel;

		this.isPanelShow = bool;
		bool && this.isEmpty(toJS(condition)) && this.getSearchCondition();
	}

	@action
	updateFields = (val) => {
		this.searchPanel.form.updateFields({
			bankname: {
				value: val
			}
		});
	}

	@action
	setBaseValueOfSearch = (val) => {
		this.searchPanel.value = val;
	}

	@action
	setSelectedRowsKeys = (id) => {
		this.table.selectedRowsKeys = id;
	}

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

	isEmpty = (c) => {
		if (c.length == 0) return true;
		return false;
	}

	@action
	mobxDatasReset = () => {
		this.authorized = false;
		this.isPanelShow = false;
		this.searchPanel.form = new WeaForm();
		this.searchPanel.condition = [];
		this.searchPanel.loading = true;
		this.searchPanel.value = '';
	}

}