import * as Api from '../apis/trainPlan';
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 HrmTrainPlan {
	@observable title = i18n.module.trainPlan();
	@observable btnMenu = [];
	@observable authorized = false;
	@observable loading = true;
	@observable isPanelShow = false;
	@observable date = '';
	@observable table = {
		tableStore: new TableStore(),
		selectedRowsKeys: '',
		canView: false,
	}
	@observable searchPanel = {
		condition: [],
		form: new WeaForm(),
		value: '',
		loading: true,
		conditionCount: observable.ref(2),
	}
	@observable editor = {
		visible: false,
		form: new WeaForm(),
		condition: [],
		loading: true,
	}

	@observable test = {
		visible: false,
		tableStore: new TableStore(),
		hasAddRight: false,
		title: observable.ref(i18n.label.test()),
	}

	@observable testForm = {
		visible: false,
		form: new WeaForm(),
		condition: [],
		loading: true,
		title: observable.ref(i18n.label.test()),
	}

	@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
	getTestDialogRight = () => {
		const params = {
			trainLayoutId: this.table.selectedRowsKeys
		}
		Api.getTestDialogRight(params).then(rs => {
			const {
				status,
				hasRight, //是否有“考评”权限，已在table上控制。
				hasBtnRight = false,
			} = rs;

			if (status == '1') {
				if (hasRight) {
					this.test.hasAddRight = hasBtnRight;
				} 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 = {
				trainLayoutName: value
			};
		}

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

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

	@action
	getTestDialogTable = () => {
		const params = {
			trainLayoutId: this.table.selectedRowsKeys
		}
		Api.getTestDialogTable(params).then(rs => {
			const {
				api_status,
				sessionkey
			} = rs;

			if (api_status) {
				sessionkey && this.test.tableStore.getDatas(sessionkey, 1);
			} 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 = {
				trainLayoutId: 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
	getTestForm = () => {
		const params = {
			trainLayoutId: this.table.selectedRowsKeys
		};

		Api.getTestForm(params).then(rs => {
			const {
				api_status,
				conditions,
			} = rs;

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

	@action
	deleteTableRows = () => {
		const params = {
			trainLayoutIds: 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,
						trainLayoutId: 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
	testFormSave = () => {
		this.testForm.form.validateForm().then(f => {
			if (f.isValid) {
				const params = {
					trainLayoutId: this.table.selectedRowsKeys,
					...this.testForm.form.getFormParams(),
				}
				Api.saveTestEditorForm(params).then(rs => {
					const {
						api_status,
						sign
					} = rs;

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

	@action
	inform = () => {
		const params = {
			trainLayoutId: this.table.selectedRowsKeys
		};
		Api.inform(params).then(rs => {
			const {
				api_status,
				sign = '1'
			} = rs;

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

	@action
	getTrainTypeContent = (id) => {
		const params = {
			trainTypeId: id
		};

		Api.getTrainTypeContent(params).then(rs => {
			const {
				api_status,
				trainTypeContent = '',
				trainTypeAim = '',
			} = rs;

			if (api_status) {
				this.updateTrainPlanForm(trainTypeContent, trainTypeAim)
			} else {
				message.warning(rs.api_errormsg)
			}
		}).catch(error => {
			message.error(error);
		});
	}

	@action
	changeConditions = (date) => {
		this.editor.condition.map(c => {
			c.items.map(item => {
				const key = item.domkey[0];
				if (key == 'layoutEndDate') {
					item.startValue = date;
				}
			});
		})
	}

	@action
	updateTrainPlanForm = (trainTypeContent, trainTypeAim) => {
		this.editor.form.updateFields({
			trainLayoutContent: {
				value: trainTypeContent
			},
			trainLayoutAim: {
				value: trainTypeAim
			}
		})
	}

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

		this.editor.visible = bool;

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

	@action
	setTestDialogVisible = (bool) => {
		this.test.visible = bool;
		if (bool) {
			this.getTestDialogRight();
			this.getTestDialogTable();
		}
	}

	@action
	setTestFormDialogVisible = (bool) => {
		this.testForm.visible = bool;
		if (bool) {
			this.getTestForm();
		} else {
			this.testForm.form = new WeaForm();
		}
	}

	@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({
			trainLayoutName: {
				value: val
			}
		});
	}

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

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

	@action
	setDate = (date) => {
		this.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 = '';
	}

}