import * as Api from '../apis/signatureManagement';
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 HrmSignatureManagement {
	title = i18n.module.signatureManagement;
	@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(4),
	}
	@observable editor = {
		visible: false,
		form: new WeaForm(),
		condition: [],
		loading: true,
		list: [],
		date: '',
		selectedRadioValue: '',
		imgId: '',
	}
	@observable leftTree = {
		subId: '',
		triggerRefresh: false
	}
	@observable canAdd = true;

	@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 = (params = {}) => {
		this.loading = true;
		Api.getRight(params).then(rs => {
			const {
				api_status,
				btnMenu = [],
				hasRight
			} = rs;

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

	@action
	getTableInfo = () => {
		this.table.tableStore = new TableStore(); //清空表格选中状态
		const params={};
		if (this.nodeType === '1') {
			Object.assign(params,{
				subid: this.leftTree.subId
			});
		}
		if (this.nodeType === '2') {
			Object.assign(params,{
				departmentid: this.departmentid
			});
		}
		
		if (this.searchPanel.form.isFormInit) {
			Object.assign(params,this.searchPanel.form.getFormParams());
		} else {
			Object.assign(params,{
				name: this.searchPanel.value
			});
		}
		Api.getTableInfo(params).then(rs => {
			const {
				api_status,
				sessionkey
			} = rs;

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

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

			if (api_status) {
				if (conditions) {
					this.searchPanel.condition = conditions;
					this.searchPanel.form.initFormFields(conditions);
				}
				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 = {
				markId: this.table.selectedRowsKeys,
			};
		}
		Api.getEditorForm(params).then(rs => {
			const {
				api_status,
				condition,
				canAdd,
				markId = ''
			} = rs;

			if (api_status) {
				if (condition) {
					this.modifyCondition(condition);
					this.editor.condition = condition;
					this.editor.form.initFormFields(condition);
					this.editor.imgId = markId;
					this.editor.canAdd = canAdd;
				}
				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 {
				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);
		});
	}

	@action
	editorSave = () => {
		this.editor.form.validateForm().then(f => {
			if (f.isValid) {
				const {
					formType,
					list,
					form,
				} = this.editor;

				if (list.length > 0) {
					this.uploadRef.doUpload();
				} else {
					let params = { ...form.getFormParams(),
						hasPic: 'no'
					};
					let funcName;
					if (formType == 'new') {
						params = {
							...params,
							opera: 'add'
						};
						funcName = 'add';
					} else {
						params = {
							...params,
							opera: 'edit',
							markId: this.table.selectedRowsKeys
						}
						funcName = 'edit'
					}

					Api[funcName](params).then(rs => {
						const {
							api_status,
							sign
						} = rs;
						if (api_status) {
							if (sign == '1') {
								message.success(rs.message);
								this.setEditorDialogVisible(false);
								this.getTableInfo();
							} else {
								message.warning(rs.message)
							}
						} else {
							message.error(rs.message);
						}
					}).catch(error => {
						message.error(error);
					});
				}
			} else {
				f.showErrors();
				this.setDate({
					date: new Date()
				});
			}
		});
	}

	@action
	modifyCondition = (condition) => {
		condition.map(c => {
			c.items.map(item => {
				if (item.domkey[0] == 'sealType') {
					if (!item.value) {
						item.value = '1'
					} else {
						this.setSelectedRadioValue(item.value);
					}
				}
			})
		})
	}

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

	@action
	editorDialogReset = () => {
		this.editor.loading = true;
		this.editor.form = new WeaForm();
		this.editor.condition = [];
		this.editor.list = '';
		this.editor.imgId = '';
		this.editor.selectedRadioValue = '';
	}

	@action
	setPanelStatus = (bool) => {
		this.isPanelShow = bool;
		bool && this.isEmpty(this.searchPanel.condition) && this.getSearchCondition();
	}

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

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

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

	nodeType;
	departmentid;
	@action
	handleLeftTreeClick = (id,type,nodeids,pid) => {
		this.nodeType = type;
		if (type === '1') {
			this.leftTree.subId = id;
		}
		if (type === '2') {
			this.departmentid=id;;
		}
		this.isPanelShow && this.setPanelStatus(false);
		this.getRight({subid: (type == '1') ? id : (type == '2') ? pid : '' })
		this.getTableInfo();
	}

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

	@action
	setWaitingListDatas = (list) => {
		this.editor.list = list;
	}

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

	@action
	setSelectedRadioValue = (val) => {
		this.editor.selectedRadioValue = val;
	}

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

	setRef = (ref) => {
		this.uploadRef = ref
	}

	@action
	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;
		if (this.leftTree.subId) this.leftTree.subId = '';
		this.authorized = false;
	}

}