import * as Api from '../apis/moduleManage';
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 HrmModuleManageSet {
	@observable title = i18n.module.moduleManagementDecent();
	@observable btnMenu = [];
	@observable authorized = false;
	@observable loading = true;
	@observable date = '';
	@observable tableStore = new TableStore();
	@observable isPanelShow = false;
	@observable searchPanel = {
		condition: [],
		form: new WeaForm(),
		value: '',
		loading: true,
		conditionCount: observable.ref(2),
	}
	@observable editor = {
		visible: false,
		form: new WeaForm(),
		checkboxForm: new WeaForm(),
		condition: [],
		loading: true,
		selectedValue: '',
		checkboxCondition: [],
		isSaveButtonShow: 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 {
				api_status,
				btnMenu = [],
				hasRight
			} = rs;

			if (api_status) {
				if (hasRight) {
					this.authorized = true;
					this.btnMenu = btnMenu;
					this.getTableInfo();
				}
				this.loading = false;
			} else {
				message.error(rs.message);
			}
		}).catch(error => {
			message.error(error);
		});
	}

	getTableInfo = () => {
		let params = this.searchPanel.form.isFormInit ? this.searchPanel.form.getFormParams() : {
			name: this.searchPanel.value
		};
		Api.getTableInfo(params).then(rs => {
			const {
				api_status,
				sessionkey
			} = rs;

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

	getEditorForm = () => {
		this.editor.loading = true;
		this.editor.isSaveButtonShow = false;
		this.editorFormReset();
		this.editorConditionReset();

		let type = this.editor.formType;
		let params = type == 'new' ? {} : {
			id: this.selectedRowsKeys
		}

		Api.getEditorForm(params).then(rs => {
			const {
				api_status,
				conditions,
				roleIdType = '',
				roleid = '',
				hrmid = '',
			} = rs;

			if (api_status) {
				if (conditions) {
					conditions[0].items.map(item => {
						let key = item.domkey[0];
						if (key == 'roleIdType') {
							this.setSelectedValue(item.value, true, conditions);
						}
						if (type == 'edit' && key == 'role') {
							item.hasBorder = true;
						}
						if (key == 'subcomids') {
							item.otherParams = {
								maxBrowerHeight: 50
							}
						}
					});

					roleIdType && extendObservable(this.editor, {
						roleIdType: roleIdType
					})
					roleid && extendObservable(this.editor, {
						roleid: roleid,
						hrmid: '',
					})
					hrmid && extendObservable(this.editor, {
						hrmid: hrmid,
						roleid: '',
					})

					let checkboxConditions = this.spliceCheckboxConditions(conditions);
					type == 'new' && this.adjustConditionIndex(conditions);

					this.editor.form.initFormFields(conditions);
					this.editor.condition = conditions;

					if (checkboxConditions.length > 0) {
						this.editor.checkboxCondition = checkboxConditions;
						this.editor.checkboxForm.initFormFields(checkboxConditions);

						let params = this.editor.checkboxForm.getFormParams();
						if (this.isCheckboxSelected(params)) {
							this.setDialogSaveButtonStatus(true);
						} else {
							this.setDialogSaveButtonStatus(false);
						}
					}
				}
				this.editor.loading = false;
			} else {
				message.error(rs.message);
			}
		}).catch(error => {
			message.error(error);
		});
	}

	isCheckboxSelected = (params) => {
		let values = Object.values(params);
		let index = values.findIndex(value => value == '1');
		if (index > -1) {
			return true;
		} else {
			return false;
		}
	}

	spliceCheckboxConditions = (conditions) => {
		let arr = [];
		let firstCheckboxIndex = conditions[0].items.findIndex(item => item.conditionType == 'CHECKBOX');
		if (firstCheckboxIndex > -1) {
			let len = conditions[0].items.length;
			let checkboxItems = conditions[0].items.splice(firstCheckboxIndex, len - firstCheckboxIndex);
			arr.push({
				items: checkboxItems,
			});
		}

		return arr;
	}

	adjustConditionIndex = (conditions) => {
		let spliceItems = conditions[0].items.splice(1, 2);
		spliceItems.map(item => {
			conditions[0].items.unshift(item);
		});
	}

	deleteTableRows = () => {
		let params = {
			ids: this.selectedRowsKeys
		};
		Api.deleteTableRows(params).then(rs => {
			const {
				api_status,
				sign
			} = rs;

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

	addModuleManageInfo = () => {
		this.editor.form.validateForm().then(f => {
			if (f.isValid) {
				let type = this.editor.formType;

				let params = {
					...this.editor.form.getFormParams(),
					...this.editor.checkboxForm.getFormParams()
				}

				if (type == 'edit') {
					params = { ...params,
						id: this.selectedRowsKeys,
						roleIdType: this.editor.roleIdType,
					}
					if (this.editor.roleid) {
						params.roleid = this.editor.roleid;
					}
					if (this.editor.hrmid) {
						params.hrmid = this.editor.hrmid;
					}
				}

				this.editor.loading = true;

				Api.addModuleManageInfo(params).then(rs => {
					const {
						api_status,
						sign
					} = rs;

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

	validateCheckboxForm = () => {
		let values = Object.values(this.editor.checkboxForm.getFormParams());
		let index = values.findIndex(value => value == '1');
		if (index == -1) {
			message.info(i18n.message.pleaseAtLeastSelectOne());
			return false;
		}
		return true;
	}

	setEditorDialogVisible = (bool, type) => {
		this.editor.visible = bool
		if (bool) {
			extendObservable(this.editor, {
				formType: type,
				title: type == 'new' ? i18n.label.newModuleDecent() : i18n.label.editModuleDecent(),
			});
			this.getEditorForm();
		} else {
			this.editor.form = new WeaForm();
		}
	}

	getSearchCondition = () => {
		this.searchPanel.loading = true;
		Api.getSearchCondition().then(rs => {
			const {
				api_status,
				conditions,
			} = rs;

			if (api_status) {
				if (conditions) {
					const {form} = this.searchPanel;
					conditions[0].title = "";
					form.setCondition(toJS(conditions))
				}
				this.searchPanel.loading = false;
			} else {
				message.error(rs.message);
			}
		}).catch(error => {
			message.error(error);
		});
	}

	setSelectedValue = (val, isInit, conditions) => {
		this.editor.selectedValue = val;
		this.operateEditorCondition(val, conditions);
		if (!isInit) {
			this.editor.form = new WeaForm();
			this.editor.form.initFormFields(toJS(this.editor.condition));
		}
	}

	operateEditorCondition = (val, conditions) => {
		let condition = conditions ? conditions : this.editor.condition;
		condition[0].items.map(item => {
			if (val == '1') {
				if (item.domkey[0] == 'roleid') {
					if (!item.rules) item.rules = 'required|string';
				}
				if (item.domkey[0] == 'hrmid') {
					if (item.rules) delete item.rules;
					item.browserConditionParam.replaceDatas = [];
				}
			}
			if (val == '2') {
				if (item.domkey[0] == 'hrmid') {
					if (!item.rules) item.rules = 'required|string';
				}
				if (item.domkey[0] == 'roleid') {
					if (item.rules) delete item.rules;
					item.browserConditionParam.replaceDatas = [];
				}
			}
			if (item.domkey[0] == 'roleIdType') {
				item.value = val;
			}
		});
	}

	setSelectedRowsKeys = (id) => {
		extendObservable(this, {
			selectedRowsKeys: id
		});
	}

	updateFields = (val) => {
		this.searchPanel.form.updateFields({
			name: {
				value: val
			}
		});
	}

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

	setLoginId = (id) => {
		this.securitySet.loginId = id;
	}

	setDate = (date) => {
		this.date = date;
	}

	setPanelStatus = (bool) => {
		this.isPanelShow = bool;
	}

	setDialogSaveButtonStatus = (bool) => {
		this.editor.isSaveButtonShow = bool;
	}

	editorFormReset = () => {
		this.editor.form = new WeaForm();
		this.editor.checkboxForm = new WeaForm();
	}

	editorConditionReset = () => {
		this.editor.condition = [];
		this.editor.checkboxCondition = [];
	}

	mobxDatasReset = () => {
		this.searchPanel.form = new WeaForm();
		if (this.searchPanel.value) {
			this.searchPanel.value = '';
		}
		if (this.searchPanel.condition.length > 0) {
			this.searchPanel.condition = [];
		}
		if (this.isPanelShow) {
			this.isPanelShow = false;
		}
		this.authorized = false;
	}


}