import * as Api from '../apis/salaryIndexSetting';
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 HrmSalaryIndexSetting {
	@observable title = i18n.module.salaryIndexSetting();
	@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: '',
		selectedValue: '',
	}
	@observable leftTree = {
		subId: '',
		triggerRefresh: false
	}

	@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 = () => {
		const params = {
			subCompanyId: this.leftTree.subId
		}
		Api.getButtonsMenu(params).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(); //清空表格选中状态
		let params;
		if (this.leftTree.subId) {
			params = {
				subCompanyId: this.leftTree.subId
			};
		}
		if (this.searchPanel.form.isFormInit) {
			params = {
				...params,
				...this.searchPanel.form.getFormParams()
			};
		} else {
			params = {
				...params,
				targetname: this.searchPanel.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') {
			const {
				subId
			} = this.leftTree;

			if (subId) params = {
				subCompanyId: subId
			};
		} else {
			params = {
				id: this.table.selectedRowsKeys,
			};
		}
		Api.getEditorForm(params).then(rs => {
			const {
				status,
				formField,
			} = rs;

			if (status == '1') {
				if (formField) {
					this.operateCondition(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 = {
			Targetid: 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 = () => {
		const {
			formType,
			selectedValue
		} = this.editor;

		if (selectedValue == '3') {
			let index = window.e9HideFormFieldKeys.findIndex(str => str == 'Areaids1');
			window.e9HideFormFieldKeys.splice(index, 1);
			window.e9HideFormFieldKeys.push('Areaids2');
		} else if (selectedValue == '4') {
			let index = window.e9HideFormFieldKeys.findIndex(str => str == 'Areaids2');
			window.e9HideFormFieldKeys.splice(index, 1);
			window.e9HideFormFieldKeys.push('Areaids1');
		} else {
			window.e9HideFormFieldKeys = ['Areaids1', 'Areaids2'];
		}

		this.editor.form.validateForm().then(f => {
			if (f.isValid) {
				let func;
				if (formType == 'new') {
					func = 'add'
				} else {
					func = 'edit'
				}
				const {
					form
				} = this.editor;
				if (!form.getFormParams().showOrder) {
					form.updateFields({
						showOrder: {
							value: 0
						}
					});
				}
				const params = {
					...form.getFormParams(),
					Targetid: 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
	operateCondition = (conditions) => {
		conditions.map(c => {
			c.items.map(item => {
				let key = item.domkey[0];
				if (key == 'AreaType') {
					if (!item.value) {
						item.value = '0'
					} else {
						this.setSelectedValue(item.value);
					}
				}
				if (key == 'TargetName' || key == 'subcompanyid' || key == 'Areaids1' || key == 'Areaids2') {
					item.rules = 'required|string'
				}
				if (key == 'showOrder') {
					if (item.rules) delete item.rules;
					item.precision = 0;
					item.viewAttr = 2;
					item.conditionType = 'INPUTNUMBER';
					item.min = -9999;
					item.value = (item.value || 0).toString();
				}
				if (key == 'memo') {
					item.otherParams = {
						maxRows: 4
					}
				}
			})
		});
	}

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

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

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

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

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

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

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

	@action
	handleLeftTreeClick = (id) => {
		this.leftTree.subId = id;
		this.isPanelShow && this.setPanelStatus(false);
		this.searchPanel.form.resetForm();
		this.searchPanel.value = '';
		this.getButtonsMenu();
		this.getTableInfo();
	}

	@action
	forceLeftTreeRefresh = () => {
		this.leftTree.triggerRefresh = !this.leftTree.triggerRefresh;
	}

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

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

	isNumber = (param) => {
		if (!Number.isNaN(parseInt(param))) {
			return true
		} else {
			return false
		}
	}

	@action
	setSelectedValue = (value) => {
		this.editor.selectedValue = value;
	}

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

}