import * as Api from '../apis/personalSalaryAdjustment';
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 HrmPersonalSalaryAdjustment {
	@observable title = i18n.module.personalSalaryAdjustment();
	@observable btnMenu = [];
	@observable authorized = false;
	@observable hasAddSalaryIndexRight = false;
	@observable loading = true;
	@observable isPanelShow = false;
	@observable key = '0';
	@observable table = {
		tableStore: new TableStore(),
	}
	@observable searchPanel = {
		condition: [],
		form: new WeaForm(),
		value: '',
		loading: true,
		conditionCount: observable.ref(4),
	}
	@observable editor = {
		form: new WeaForm(),
		condition: [],
		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 = () => {
		Api.getRight().then(rs => {
			const {
				status,
				hasRight,
				hasTabRight = false,
			} = rs;
			if (status == '1') {
				if (hasRight) {
					this.authorized = true;
					this.hasAddSalaryIndexRight = hasTabRight;
					if (hasTabRight) {
						this.getForm();
					} else {
						this.getTableInfo();
					}
				} else {
					this.loading = false;
				}
			} else {
				message.error(rs.message);
			}
		}).catch(error => {
			message.error(error);
		});
	}

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

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

	@action
	getButtonsMenu = () => {
		let params;
		if (this.hasAddSalaryIndexRight) {
			params = {
				islist: this.key
			};
		} else {
			params = {
				islist: '1'
			}
		}

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

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

	@action
	getTableInfo = () => {
		const {
			value,
			form
		} = this.searchPanel;

		let params = {
			lastname: value
		};
		if (form.isFormInit) {
			params = {
				...params,
				...form.getFormParams()
			}
		}
		Api.getTableInfo(params).then(rs => {
			const {
				status,
				sessionkey
			} = rs;

			if (status == '1') {
				sessionkey && this.table.tableStore.getDatas(sessionkey, 1);
				this.getButtonsMenu();
			} 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
	editorSave = () => {
		this.editor.form.validateForm().then(f => {
			if (f.isValid) {
				const params = this.editor.form.getFormParams();
				Api.save(params).then(rs => {
					const {
						status,
						success
					} = rs;

					if (status == '1') {
						success ? message.success(rs.message) && this.editor.form.resetForm() : message.warning(rs.message);
					} else {
						message.error(rs.message);
					}
				}).catch(error => {
					message.error(error);
				});
			} else {
				f.showErrors();
				this.setDate({
					date: new Date()
				});
			}
		});
	}

	@action
	operateCondition = (formField) => {
		const item = formField[0].items.splice(3, 1);
		formField[0].items.unshift(item[0]);
		formField[0].items.map(item => {
			if (item.domkey[0] == 'changeresion') {
				item.otherParams = {
					maxRows: 4
				}
			}
		})
	}

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

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

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

	@action
	switchKey = (key) => {
		this.key = key;
		if (key == '0') {
			this.resetSearchParams();
			this.getButtonsMenu();
		} else {
			this.resetSalaryAdjustmentForm();
			this.getTableInfo();
		}
	}

	@action
	resetSalaryAdjustmentForm = () => {
		const {
			form
		} = this.editor;

		form.resetForm();
		form.resetConditionValue();
	}

	@action
	resetSearchParams = () => {
		const {
			form
		} = this.searchPanel;

		this.isPanelShow = false;
		this.searchPanel.value = '';
		if (form.isFormInit) {
			form.resetForm();
			form.resetConditionValue();
		}
	}

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

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

	@action
	mobxDatasReset = () => {
		const {
			form
		} = this.searchPanel;
		this.authorized = false;
		this.loading = true;
		this.isPanelShow = false;
		this.key = '0';
		this.editor.form = new WeaForm();
		if (form.isFormInit) {
			form.resetForm();
			form.resetConditionValue()
		}
	}

}