import * as mobx from 'mobx';
import * as Api from '../apis/unitSet';

import {
	observable,
	action,
	computed,
	extendObservable
} from 'mobx';

import {
	WeaForm,
	WeaTableNew
} from 'comsMobx';

import {
	message
} from 'antd';

import {
	i18n
} from '../public/i18n';

const {
	TableStore
} = WeaTableNew;

const toJS = mobx.toJS;

export class HrmUnitSet {
	@observable title = i18n.module.unitSetting();
	@observable btnMenu = [];
	@observable authorized = false;
	@observable loading = true;
	@observable date = '';
	@observable tableStore = new TableStore();
	@observable unitName = '';
	@observable editorDialog = {
		title: i18n.label.newUnit(),
		visible: false,
		condition: [],
		form: new WeaForm(),
		loading: true,
		formType: '',
		icon: 'icon-coms-currency',
	}

	@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 = false
			} = 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);
		});
	}

	@action
	getTableInfo = () => {
		let params = {
			unitname: this.unitName
		}
		this.tableStore = new TableStore();
		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);
		});
	}

	@action
	getEditorForm = () => {
		this.editorDialogReset();
		let type = this.editorDialog.formType;
		let params = (type == 'new') ? {} : {
			id: this.selectedRowsKeys
		}
		this.editorDialog.title = (type == 'new') ? i18n.label.newUnit() : i18n.label.editUnit();

		Api.getEditorForm(params).then(rs => {
			const {
				api_status,
				condition,
			} = rs;

			if (api_status) {
				if (condition) {
					condition.map(c => {
						c.items.map(item => {
							if (item.domkey[0] == 'unitdesc') {
								item.otherParams = {
									maxRows: 4
								}
							}
						});
					});
					this.editorDialog.condition = condition;
					this.editorDialog.form.initFormFields(condition);
				}
				this.editorDialog.loading = false;
			} else {
				message.error(rs.message);
			}
		}).catch(error => {
			message.error(error);
		});
	}

	@action
	addEditorInfo = () => {
		this.editorDialog.form.validateForm().then(f => {
			if (f.isValid) {
				let params = {
					...this.editorDialog.form.getFormParams()
				};
				Api.addEditorInfo(params).then(rs => {
					const {
						api_status,
						sign
					} = rs;

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

	@action
	editEditorInfo = () => {
		this.editorDialog.form.validateForm().then(f => {
			if (f.isValid) {
				let params = {
					...this.editorDialog.form.getFormParams(),
					id: this.selectedRowsKeys
				}
				Api.editEditorInfo(params).then(rs => {
					const {
						api_status,
						sign
					} = rs;

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

	@action
	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);
		});
	}

	@action
	onSearchChange = (val) => {
		this.unitName = val;
	}

	@action
	setEditorDialogVisible = (bool, formType) => {
		this.editorDialog.visible = bool;
		this.editorDialog.formType = formType;
		bool && this.getEditorForm();
	}

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

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

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

	@action
	mobxDatasReset = () => {
		this.unitName = '';
	}

}