import * as Api from '../apis/trainTypes';
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 HrmTrainTypes {
	@observable title = i18n.module.trainTypes();
	@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: '',
	}

	@computed get menu() {
		let topMenu = this.btnMenu.filter(item => item.isTop == '1' || item.isBatch == '1');
		// let rightMenu = this.btnMenu.filter(item => !item.isBatch);
		let rightMenu = this.btnMenu.filter(item => item);

		return {
			topMenu,
			rightMenu
		}
	}

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

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


	@action
	getTableInfo = () => {
		this.table.tableStore = new TableStore(); //清空表格选中状态
		const {
			form,
			value
		} = this.searchPanel;
		let params;
		if (form.isFormInit) {
			params = form.getFormParams();
		} else {
			params = {
				trainTypeName: value
			};
		}

		Api.getTableInfo(params).then(rs => {
			const {
				api_status,
				sessionkey
			} = rs;

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

	@action
	getButtonsMenu = () => {
		Api.getButtonsMenu().then(rs => {
			const {
				api_status,
				btnMenu,
			} = rs;

			if (api_status) {
				this.btnMenu = btnMenu;
			} else {
				message.error(rs.message);
			}
		}).catch(error => {
			message.error(error);
		});
	}

	@action
	getSearchCondition = () => {
		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 = {
				trainTypeId: this.table.selectedRowsKeys,
			};
		}
		Api.getEditorForm(params).then(rs => {
			const {
				api_status,
				conditions,
			} = rs;

			if (api_status) {
				if (conditions) {
					this.editor.condition = conditions;
					this.editor.form.initFormFields(conditions);
				}
				this.editor.loading = false;
			} else {
				message.error(rs.message);
			}
		}).catch(error => {
			message.error(error);
		});
	}

	@action
	deleteTableRows = () => {
		let params = {
			trainTypeIds: 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)
				}
			}
		}).catch(error => {
			message.error(error);
		});
	}

	@action
	editorSave = () => {
		this.editor.form.validateForm().then(f => {
			if (f.isValid) {
				const {
					formType
				} = this.editor;
				let func;
				let params = this.editor.form.getFormParams();
				if (formType == 'new') {
					func = 'add';
				} else {
					func = 'edit';
					params = {
						...params,
						trainTypeId: this.table.selectedRowsKeys
					}
				}
				Api[func](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)
						}
					}
				}).catch(error => {
					message.error(error);
				});
			} else {
				f.showErrors();
				this.setDate({
					date: new Date()
				});
			}
		});
	}

	@action
	setEditorDialogVisible = (bool, type) => {
		const {
			form
		} = this.editor;

		this.editor.visible = bool;

		if (bool) {
			extendObservable(this.editor, {
				formType: type,
				title: type == 'new' ? i18n.label.newTrainTypes() : i18n.label.editTrainTypes(),
			});
			this.getEditorForm();
		} else {
			this.editorDialogReset();
		}
	}

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

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

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

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

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

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

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

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

	@action
	mobxDatasReset = () => {
		const {
			form
		} = this.searchPanel;
		this.loading = true;
		this.authorized = false;
		this.isPanelShow = false;
		form.isFormInit && form.reset();
		this.searchPanel.value = '';
	}

}